ナビゲーション

v2.1.0
フォーム

セレクト

選択肢リストもインターフェースと同じ質感で表示できるネイティブのセレクト。

このページの内容

ネイティブの<select>に.selectを直接指定します。キーボード操作、文字入力による項目検索、フォームでの値の扱いはブラウザーが担います。クラスは、くぼんだ形状と、どちらのテーマでも見分けやすい控えめな山形アイコンを描画します。カスタマイズ可能なセレクトに対応するブラウザーでは、選択肢リストもインターフェース全体と同じ質感で表示されます。

セレクト

<label class="field">  Framework <select class="select">    <option class="select-option">Astro</option>    <option class="select-option">Next.js</option>    <option class="select-option">SvelteKit</option>    <option class="select-option" disabled>Nuxt</option>  </select></label>
<label className="field">  Framework <select className="select">    <option className="select-option">Astro</option>    <option className="select-option">Next.js</option>    <option className="select-option">SvelteKit</option>    <option className="select-option" disabled>Nuxt</option>  </select></label>
<label class="field">  Framework <select class="select">    <option class="select-option">Astro</option>    <option class="select-option">Next.js</option>    <option class="select-option">SvelteKit</option>    <option class="select-option" disabled>Nuxt</option>  </select></label>
<template>  <label class="field">    Framework <select class="select">      <option class="select-option">Astro</option>      <option class="select-option">Next.js</option>      <option class="select-option">SvelteKit</option>      <option class="select-option" disabled>Nuxt</option>    </select>  </label></template>

#選択肢リスト

appearance: base-selectを指定すると、OS標準のリスト表示が::picker(select)に切り替わります。これはブラウザーがコントロールを基準に配置し、ビューポートの端で表示方向を反転させるポップオーバーです。スタイルシートはリストを浮き上がった面として描画し、その角丸の半径は各行の角丸の半径に間隔の4pxを足した値になります。各<option>にselect-optionを指定します。

  • ポインターを重ねた行や、矢印キーで移動した先の行は、チップと同じ色調になります。
  • 選択中の項目の末尾には、::checkmarkでチェックマークを描画します。
  • コントロールの山形アイコンは::picker-iconで描画し、リストが開いている間は上下が反転します。

カスタマイズ可能なセレクトに対応していないブラウザーでは、これらのスタイルが無視され、プラットフォーム標準のリストが表示されます。同じマークアップをそのまま使え、select-optionを指定しても標準の表示には影響しません。

#アイコン付きの選択肢

各選択肢にテキストラベルと値を指定します。テキストの横に装飾用のアイコンを置けます。カスタマイズ可能なセレクトでは、閉じたコントロールに <selectedcontent> が選択中の内容を反映します。ほかのブラウザーでも、ネイティブのピッカーにラベルが表示され、同じ値が送信されます。

通知方法

<form class="flex flex-col items-start gap-md">  <label class="field">    Delivery method <select class="select" name="delivery">      <button type="button"><selectedcontent></selectedcontent></button>      <option class="select-option" value="email">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <rect x="2" y="4" width="12" height="8" rx="1" /><path d="M2.5 5L7.8824 8.9144A0.2 0.2 0 0 0 8.1176 8.9144L13.5 5" />        </svg> Email      </option>      <option class="select-option" value="sms">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M3.5 2.5L12.5 2.5A1 1 0 0 1 13.5 3.5L13.5 10A1 1 0 0 1 12.5 11L7.777 11A1 1 0 0 0 7.2625 11.1425L4.0145 13.0913A1 1 0 0 1 2.5 12.2338L2.5 3.5A1 1 0 0 1 3.5 2.5Z" />        </svg> Text message      </option>    </select>  </label>  <button type="submit" class="btn btn-sm">Save</button>  <output aria-live="polite"></output></form><script>const form = document.querySelector('form')form.addEventListener('submit', event => {  event.preventDefault()  form.querySelector('output').value = new FormData(form).get('delivery')})</script>

#浮き上がったスタイル

ツールバーやフィルター行では、select-raisedを指定すると、ボタンの隣に浮き上がったスタイルのセレクトを配置できます。

浮き上がったスタイル

<select class="select select-raised select-sm" aria-label="Sort">  <option class="select-option">Newest first</option>  <option class="select-option">Oldest first</option>  <option class="select-option">Name</option></select><button type="button" class="btn btn-sm">Export</button>
<>  <select className="select select-raised select-sm" aria-label="Sort">    <option className="select-option">Newest first</option>    <option className="select-option">Oldest first</option>    <option className="select-option">Name</option>  </select>  <button type="button" className="btn btn-sm">Export</button></>
<select class="select select-raised select-sm" aria-label="Sort">  <option class="select-option">Newest first</option>  <option class="select-option">Oldest first</option>  <option class="select-option">Name</option></select><button type="button" class="btn btn-sm">Export</button>
<template>  <select class="select select-raised select-sm" aria-label="Sort">    <option class="select-option">Newest first</option>    <option class="select-option">Oldest first</option>    <option class="select-option">Name</option>  </select>  <button type="button" class="btn btn-sm">Export</button></template>

#サイズ

セレクトの高さは、くぼんだスタイルでも浮き上がったスタイルでも、ボタンや入力欄と同じ32px、38px、44pxのいずれかです。

サイズ

<select class="select select-sm" aria-label="Small">  <option class="select-option">Small</option></select><select class="select" aria-label="Medium">  <option class="select-option">Medium</option></select><select class="select select-lg" aria-label="Large">  <option class="select-option">Large</option></select>
<>  <select className="select select-sm" aria-label="Small">    <option className="select-option">Small</option>  </select>  <select className="select" aria-label="Medium">    <option className="select-option">Medium</option>  </select>  <select className="select select-lg" aria-label="Large">    <option className="select-option">Large</option>  </select></>
<select class="select select-sm" aria-label="Small">  <option class="select-option">Small</option></select><select class="select" aria-label="Medium">  <option class="select-option">Medium</option></select><select class="select select-lg" aria-label="Large">  <option class="select-option">Large</option></select>
<template>  <select class="select select-sm" aria-label="Small">    <option class="select-option">Small</option>  </select>  <select class="select" aria-label="Medium">    <option class="select-option">Medium</option>  </select>  <select class="select select-lg" aria-label="Large">    <option class="select-option">Large</option>  </select></template>

#無効

無効

<select class="select" aria-label="Region" disabled>  <option class="select-option">Asia Pacific</option></select>
<select className="select" aria-label="Region" disabled>  <option className="select-option">Asia Pacific</option></select>
<select class="select" aria-label="Region" disabled>  <option class="select-option">Asia Pacific</option></select>
<template>  <select class="select" aria-label="Region" disabled>    <option class="select-option">Asia Pacific</option>  </select></template>

#クラス

クラス説明
selectくぼみにシェブロンを添えて描画するネイティブの select
select-optionカスタマイズ可能なリストの選択肢です。ポインターを重ねた行やキーボード操作でフォーカスした行にはチップと同じ色調を適用し、選択中の項目にはチェックマークを表示します。
select-raisedツールバーやフィルター行でボタンの横に置く、浮き上がった外観の select です。サイズを指定しない場合の高さは 38px です。select と併用します
select-sm高さは 32px です。既定の高さはボタンと同じ 38px です。select と併用します
select-lg高さは 44px です。既定の高さはボタンと同じ 38px です。select と併用します

#ソース

@master/ui/components/select.css
/* * Select: a native <select> drawn as a well, or as a raised control in toolbars. * Where the browser supports appearance: base-select, the option list becomes a floating sheet in the same * material: options are rows with a concentric corner and the current one carries a check. Elsewhere the * select keeps the platform's own list. */@layer components.ui {  .select {    --select-height: 2.375rem;    --select-padding-x: .75rem;    --select-font-size: .875rem;    --select-radius: var(--radius-control);    -webkit-tap-highlight-color: transparent;    /* A neutral mid-grey chevron reads on moonstone and obsidian alike. */    --select-indicator: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23888' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5' transform='rotate(90 8 8)'/%3E%3C/svg%3E");    min-width: 0;    min-height: var(--select-height);    max-width: 100%;    margin: 0;    padding-block: 0;    padding-inline: var(--select-padding-x) calc(var(--select-padding-x) + 1.25rem);    border: 0;    border-radius: calc(var(--select-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--select-indicator) no-repeat right calc(var(--select-padding-x) - .125rem) center / 14px, var(--color-surface-inset);    color: var(--color-text-strong);    box-shadow: var(--shadow-pressed);    font: 400 var(--select-font-size)/1.45 var(--font-family-sans);    font-feature-settings: inherit;    text-overflow: ellipsis;    appearance: none;    cursor: pointer;    transition-property: box-shadow, background-color;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    @media (hover: hover) {      &:hover:not(:focus-visible, :disabled) {        background-color: var(--color-surface-inset-hover);      }    }    /*     * Focused, or open. :open stays inside :is(), which forgives a browser that does not know it; a :not(:open)     * on the hover rule would drop that rule there. This rule matches hover's weight and follows it instead,     * so an open select keeps its raised face under the pointer.     */    &:is(:focus-visible, :open):not(:disabled) {      outline: none;      background-color: var(--color-surface-raised);      box-shadow: inset 0 0 0 .5px var(--color-line-mark), 0 0 0 .5px var(--color-line-mark), 0 0 0 3.5px var(--color-focus-halo);    }    /*     * An error keeps its boundary while focused or open, and the halo takes the danger tone, as a field's does.     * :user-invalid reports a required select left on its empty option once the reader has tried, without script.     */    &:is([aria-invalid="true"], :user-invalid) {      box-shadow: var(--shadow-pressed), inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px color-mix(in oklab, var(--color-danger) 45%, transparent);      &:is(:focus-visible, :open):not(:disabled) {        box-shadow: inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px var(--color-danger), 0 0 0 3.5px color-mix(in oklab, var(--color-danger) 15%, transparent);      }    }    &:disabled {      color: var(--color-text-disabled);      cursor: not-allowed;    }    /* Raised: a control on a sheet, for toolbars and filters, matching the button beside it. */    &.select-raised {      background: var(--select-indicator) no-repeat right calc(var(--select-padding-x) - .125rem) center / 14px, linear-gradient(180deg, var(--color-surface-raised), var(--color-surface-raised-low)) var(--color-surface-raised-low);      box-shadow: var(--shadow-raised);      /* Its face is a gradient: swap backgrounds instantly, as a button does. */      transition-property: box-shadow;      font-weight: var(--font-weight-control);      @media (hover: hover) {        &:hover:not(:focus-visible, :disabled) {          box-shadow: var(--shadow-raised-hover);        }      }      &:is(:focus-visible, :open):not(:disabled) {        box-shadow: var(--shadow-raised);      }      &:focus-visible {        @apply --focus-ring;      }      /* Raised and wrong: the face keeps its depth, under the pointer too, and its edge takes the danger color. */      &:is([aria-invalid="true"], :user-invalid):not(:disabled) {        box-shadow: var(--shadow-raised), inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px color-mix(in oklab, var(--color-danger) 45%, transparent);      }      &:disabled {        background-color: var(--color-surface-inset);        background-image: var(--select-indicator);        box-shadow: var(--shadow-flat);      }    }    /*     * The chevron stands at the end of the words, whichever way they run. It follows the raised face, whose     * background shorthand would otherwise put the chevron back on the right.     */    &:dir(rtl) {      background-position: left calc(var(--select-padding-x) - .125rem) center, 0 0;    }    /* A select is as tall as the button and the field beside it: 32, 38 and 44px. */    &.select-sm {      --select-height: 2rem;      --select-padding-x: .625rem;      --select-font-size: .8125rem;      --select-radius: var(--radius-control-sm);    }    &.select-lg {      --select-height: 2.75rem;      --select-padding-x: .875rem;      --select-font-size: 1rem;      --select-radius: var(--radius-control-lg);    }    @media (forced-colors: active) {      outline: 1px solid FieldText;      outline-offset: -1px;      /* As heavy as the focused rule above, whose outline: none would otherwise leave focus unseen. */      &:is(:focus-visible, :open):not(:disabled) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* The danger edge and the halo are shadows, which forced colors drop: the outline says it instead, as a field's does. */      &:is([aria-invalid="true"], :user-invalid):not(:disabled) {        outline-color: Mark;      }      &:disabled {        outline-color: GrayText;      }    }  }  /*   * The customizable select. The control becomes a flex row whose chevron is ::picker-icon, and the list is   * ::picker(select): a popover the browser anchors to the control and flips at the viewport edge.   */  @supports (appearance: base-select) {    .select {      display: inline-flex;      align-items: center;      justify-content: space-between;      gap: .5rem;      padding-block: 0;      padding-inline: var(--select-padding-x) calc(var(--select-padding-x) - .125rem);      background: var(--color-surface-inset);      appearance: base-select;      &::picker-icon {        content: '';        flex: none;        width: 14px;        height: 14px;        background: var(--color-text-muted);        mask: var(--select-indicator) center / contain no-repeat;        transition: rotate var(--duration-base) var(--easing-glide);      }      &:open::picker-icon {        rotate: x 180deg;      }      &:disabled::picker-icon {        background: var(--color-text-disabled);      }      &.select-raised {        @apply --surface-sheet;        &:disabled {          background: var(--color-surface-inset);        }      }      /* The list: a floating sheet 6px from the control, whose corner is a row's corner plus its 4px inset. */      /* The list is as wide as its longest option up to a reading width, and never wider than the viewport. */      &::picker(select) {        max-inline-size: min(24rem, calc(100vw - 1rem));        margin: 6px 0;        padding: 4px;        /* Scrolling to the end of a long list stops there, as a menu's does, and never scrolls the page under it. */        overscroll-behavior: contain;        border: 0;        border-radius: calc(var(--radius-control-sm) * var(--corner-scale) + 4px);        corner-shape: var(--corner-shape);        background: var(--color-surface-floating);        color: var(--color-text-strong);        box-shadow: var(--shadow-floating);        font: 400 var(--select-font-size)/1.2 var(--font-family-sans);        font-feature-settings: inherit;        scrollbar-width: thin;        scrollbar-color: var(--color-line-strong) transparent;        appearance: base-select;        opacity: 0;        scale: .97;        transform-origin: top;        transition-property: opacity, scale, overlay, display;        transition-duration: var(--duration-quick);        transition-timing-function: var(--easing-glide);        transition-behavior: allow-discrete;      }      &:open::picker(select) {        opacity: 1;        scale: 1;        @starting-style {          opacity: 0;          scale: .97;        }      }      @media (prefers-reduced-motion: reduce) {        &::picker-icon {          transition: none;        }        &::picker(select), &:open::picker(select) {          scale: none;          @starting-style {            scale: none;          }        }      }      @media (forced-colors: active) {        &::picker-icon {          background: FieldText;        }        /* Written after the disabled chevron, which is as heavy, so it takes a system color too. */        &:disabled::picker-icon {          background: GrayText;        }        /* An outline, not a border: the list keeps its size and its 4px inset. */        &::picker(select) {          outline: 1px solid CanvasText;          outline-offset: -1px;        }      }    }    /*     * An option in the list. The row under the pointer or the keys takes a chip tone; the current one a check.     * Keyboard focus adds an ink outline inside the row, as in a menu, so focus and hover stay different facts.     */    .select-option {      display: flex;      align-items: center;      gap: .5rem;      min-height: 2rem;      /* A long option wraps inside the list, as a listbox's does; a row of one line keeps its 32px. */      padding: .25rem .5rem;      border-radius: calc(var(--radius-control-sm) * var(--corner-scale));      corner-shape: var(--corner-shape);      outline: none;      background: transparent;      color: var(--color-text-strong);      white-space: normal;      overflow-wrap: anywhere;      cursor: pointer;      &::checkmark {        content: '';        order: 1;        flex: none;        width: 14px;        height: 14px;        margin-inline-start: auto;        background: currentColor;        mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5'/%3E%3C/svg%3E") center / contain no-repeat;      }      &:focus-visible:not(:disabled) {        background: var(--color-surface-chip);        outline: 2px solid var(--color-focus);        outline-offset: -2px;      }      @media (hover: hover) {        &:hover:not(:disabled) {          background: var(--color-surface-chip);        }      }      /* Pressed: a step deeper than the pointer's tone. */      &:active:not(:disabled) {        background: var(--color-surface-inset-hover);      }      &:disabled {        color: var(--color-text-disabled);        cursor: not-allowed;      }      @media (pointer: coarse) {        min-height: 2.75rem;      }      @media (forced-colors: active) {        &::checkmark {          background: CanvasText;        }        /* A highlighted row's check takes the row's text color, as its words do. */        &:focus-visible:not(:disabled) {          background: Highlight;          color: HighlightText;          &::checkmark {            background: HighlightText;          }        }        @media (hover: hover) {          &:hover:not(:disabled) {            background: Highlight;            color: HighlightText;            &::checkmark {              background: HighlightText;            }          }        }        &:disabled {          color: GrayText;        }      }    }  }}