导航

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 后,选项列表从操作系统绘制的列表变为 ::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>

#尺寸

选择框的凹槽和凸起样式都与按钮和输入框采用相同的高度:32、38 或 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;        }      }    }  }}