網站導覽

v2.1.0
表單

選項

有足夠空間說明內容的單選或核取方塊選項。

本頁內容

選項元件有足夠的空間說明選項內容,例如方案、付款方式,或附有一句說明的設定。它以 <label> 包住原生的 單選按鈕 或 核取方塊,讓整個選項表面都能點選,並由輸入元件管理狀態與鍵盤操作。未選取時,表面平坦,只有一道輪廓線。選取後,表面抬高一階,輪廓線改為墨色。

選項

<fieldset class="fieldset max-w(420) mx-auto">  <legend class="fieldset-legend">Plan</legend>  <label class="choice">    <input type="radio" class="radio" name="plan" value="hobby">    <span class="choice-body">      <span class="choice-title">Hobby</span>      <span class="choice-description">For personal projects and experiments.</span>    </span>    <span class="choice-end">Free</span>  </label>  <label class="choice">    <input type="radio" class="radio" name="plan" value="pro" checked>    <span class="choice-body">      <span class="choice-title">Pro</span>      <span class="choice-description">For teams that ship every day.</span>    </span>    <span class="choice-end">$20</span>  </label>  <label class="choice">    <input type="radio" class="radio" name="plan" value="enterprise" disabled>    <span class="choice-body">      <span class="choice-title">Enterprise</span>      <span class="choice-description">Not available in your region yet.</span>    </span>  </label></fieldset>
<fieldset className="fieldset max-w(420) mx-auto">  <legend className="fieldset-legend">Plan</legend>  <label className="choice">    <input type="radio" className="radio" name="plan" value="hobby" />    <span className="choice-body">      <span className="choice-title">Hobby</span>      <span className="choice-description">For personal projects and experiments.</span>    </span>    <span className="choice-end">Free</span>  </label>  <label className="choice">    <input type="radio" className="radio" name="plan" value="pro" defaultChecked />    <span className="choice-body">      <span className="choice-title">Pro</span>      <span className="choice-description">For teams that ship every day.</span>    </span>    <span className="choice-end">$20</span>  </label>  <label className="choice">    <input type="radio" className="radio" name="plan" value="enterprise" disabled />    <span className="choice-body">      <span className="choice-title">Enterprise</span>      <span className="choice-description">Not available in your region yet.</span>    </span>  </label></fieldset>
<fieldset class="fieldset max-w(420) mx-auto">  <legend class="fieldset-legend">Plan</legend>  <label class="choice">    <input type="radio" class="radio" name="plan" value="hobby">    <span class="choice-body">      <span class="choice-title">Hobby</span>      <span class="choice-description">For personal projects and experiments.</span>    </span>    <span class="choice-end">Free</span>  </label>  <label class="choice">    <input type="radio" class="radio" name="plan" value="pro" checked>    <span class="choice-body">      <span class="choice-title">Pro</span>      <span class="choice-description">For teams that ship every day.</span>    </span>    <span class="choice-end">$20</span>  </label>  <label class="choice">    <input type="radio" class="radio" name="plan" value="enterprise" disabled>    <span class="choice-body">      <span class="choice-title">Enterprise</span>      <span class="choice-description">Not available in your region yet.</span>    </span>  </label></fieldset>
<template>  <fieldset class="fieldset max-w(420) mx-auto">    <legend class="fieldset-legend">Plan</legend>    <label class="choice">      <input type="radio" class="radio" name="plan" value="hobby">      <span class="choice-body">        <span class="choice-title">Hobby</span>        <span class="choice-description">For personal projects and experiments.</span>      </span>      <span class="choice-end">Free</span>    </label>    <label class="choice">      <input type="radio" class="radio" name="plan" value="pro" checked>      <span class="choice-body">        <span class="choice-title">Pro</span>        <span class="choice-description">For teams that ship every day.</span>      </span>      <span class="choice-end">$20</span>    </label>    <label class="choice">      <input type="radio" class="radio" name="plan" value="enterprise" disabled>      <span class="choice-body">        <span class="choice-title">Enterprise</span>        <span class="choice-description">Not available in your region yet.</span>      </span>    </label>  </fieldset></template>

#如何讀取狀態

選項元件沒有自己的狀態,而是透過 :has() 讀取內部輸入元件的狀態。:has(:checked) 會抬高表面,:has(:focus-visible) 會在整個選項外圍畫出焦點環,而非只圈住小小的輸入元件,:has(:disabled) 則會讓表面下凹。所有狀態都由表單控制項直接管理,無須透過腳本切換,因此選項外觀會與表單狀態保持一致。

#多選

使用核取方塊時,可選取任意數量的選項。在列尾加上 開關,即可組成一列設定。

多選

<label class="choice">  <input type="checkbox" class="checkbox" checked>  <span class="choice-body">    <span class="choice-title">Deployment succeeded</span>    <span class="choice-description">One email for each production deployment.</span>  </span></label><label class="choice">  <input type="checkbox" class="checkbox">  <span class="choice-body">    <span class="choice-title">Weekly summary</span>    <span class="choice-description">Traffic, errors and build minutes, every Monday.</span>  </span></label>
<>  <label className="choice">    <input type="checkbox" className="checkbox" defaultChecked />    <span className="choice-body">      <span className="choice-title">Deployment succeeded</span>      <span className="choice-description">One email for each production deployment.</span>    </span>  </label>  <label className="choice">    <input type="checkbox" className="checkbox" />    <span className="choice-body">      <span className="choice-title">Weekly summary</span>      <span className="choice-description">Traffic, errors and build minutes, every Monday.</span>    </span>  </label></>
<label class="choice">  <input type="checkbox" class="checkbox" checked>  <span class="choice-body">    <span class="choice-title">Deployment succeeded</span>    <span class="choice-description">One email for each production deployment.</span>  </span></label><label class="choice">  <input type="checkbox" class="checkbox">  <span class="choice-body">    <span class="choice-title">Weekly summary</span>    <span class="choice-description">Traffic, errors and build minutes, every Monday.</span>  </span></label>
<template>  <label class="choice">    <input type="checkbox" class="checkbox" checked>    <span class="choice-body">      <span class="choice-title">Deployment succeeded</span>      <span class="choice-description">One email for each production deployment.</span>    </span>  </label>  <label class="choice">    <input type="checkbox" class="checkbox">    <span class="choice-body">      <span class="choice-title">Weekly summary</span>      <span class="choice-description">Traffic, errors and build minutes, every Monday.</span>    </span>  </label></template>

#格線配置

選項元件是區塊元素,可用格線將它們並排。同一列有多個選項時,請讓標題保持簡短。

格線配置

<label class="choice">  <input type="radio" class="radio" name="region" checked>  <span class="choice-body">    <span class="choice-title">Tokyo</span>    <span class="choice-description">hnd1</span>  </span></label><label class="choice">  <input type="radio" class="radio" name="region">  <span class="choice-body">    <span class="choice-title">Frankfurt</span>    <span class="choice-description">fra1</span>  </span></label><label class="choice">  <input type="radio" class="radio" name="region">  <span class="choice-body">    <span class="choice-title">Virginia</span>    <span class="choice-description">iad1</span>  </span></label>
<>  <label className="choice">    <input type="radio" className="radio" name="region" defaultChecked />    <span className="choice-body">      <span className="choice-title">Tokyo</span>      <span className="choice-description">hnd1</span>    </span>  </label>  <label className="choice">    <input type="radio" className="radio" name="region" />    <span className="choice-body">      <span className="choice-title">Frankfurt</span>      <span className="choice-description">fra1</span>    </span>  </label>  <label className="choice">    <input type="radio" className="radio" name="region" />    <span className="choice-body">      <span className="choice-title">Virginia</span>      <span className="choice-description">iad1</span>    </span>  </label></>
<label class="choice">  <input type="radio" class="radio" name="region" checked>  <span class="choice-body">    <span class="choice-title">Tokyo</span>    <span class="choice-description">hnd1</span>  </span></label><label class="choice">  <input type="radio" class="radio" name="region">  <span class="choice-body">    <span class="choice-title">Frankfurt</span>    <span class="choice-description">fra1</span>  </span></label><label class="choice">  <input type="radio" class="radio" name="region">  <span class="choice-body">    <span class="choice-title">Virginia</span>    <span class="choice-description">iad1</span>  </span></label>
<template>  <label class="choice">    <input type="radio" class="radio" name="region" checked>    <span class="choice-body">      <span class="choice-title">Tokyo</span>      <span class="choice-description">hnd1</span>    </span>  </label>  <label class="choice">    <input type="radio" class="radio" name="region">    <span class="choice-body">      <span class="choice-title">Frankfurt</span>      <span class="choice-description">fra1</span>    </span>  </label>  <label class="choice">    <input type="radio" class="radio" name="region">    <span class="choice-body">      <span class="choice-title">Virginia</span>      <span class="choice-description">iad1</span>    </span>  </label></template>

#無障礙

  • 使用含有 legend 元素的 fieldset 將選項分組,並讓同一組的單選按鈕共用一個 name。
  • 標題與說明都是標籤的一部分,兩者都會作為選項名稱讀出。請將說明控制在一句話內。
  • 選取狀態以墨色、立體層次及輸入元件本身的標記呈現,完全不依賴色相。

#類別

類別說明
choice選項使用 label 包住單選按鈕、核取方塊或開關,焦點外框會環繞整個選項。
choice-body標題和選填說明靠同一側排列
choice-titlechoice-body 內的選項名稱。
choice-description在 choice-body 內的標題下方放置一句低對比的說明文字。
choice-end在第一行末端顯示價格、徽章或數值。

#自訂屬性

屬性預設值說明
--choice-padding.875rem單一選項的內距
--choice-radiusvar(--radius-control-lg)圓角半徑

#原始碼

@master/ui/components/choice.css
/* * Choice: an option large enough to explain itself: a plan, a method, a setting with a sentence under it. * It is a <label> around a native radio or checkbox, so the whole face is the target and the input keeps the * state and the keys. At rest it is a flat face on one contour; chosen, it rises and its contour turns to ink. */@layer components.ui {  .choice {    --choice-padding: .875rem;    --choice-radius: var(--radius-control-lg);    position: relative;    display: flex;    align-items: flex-start;    gap: .75rem;    min-width: 0;    padding: var(--choice-padding);    border-radius: calc(var(--choice-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-raised);    color: var(--color-text-body);    box-shadow: var(--shadow-flat);    font-size: .875rem;    line-height: 1.45;    cursor: pointer;    touch-action: manipulation;    /* The whole face is pressed, as a filter chip is: a quick second click chooses again instead of selecting words. */    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    transition: box-shadow var(--duration-quick) var(--easing-glide);    @media (hover: hover) {      &:hover:not(:has(:checked, :disabled)) {        box-shadow: 0 0 0 .5px var(--color-line-mark);      }    }    /* Pressed: the option sinks into a well before it rises as the chosen one. */    &:active:not(:has(:checked, :disabled)) {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      transition-duration: var(--duration-press);    }    /* Chosen: the face rises one step and its contour turns to ink: still a hairline, now at full strength. */    &:has(:checked) {      box-shadow:        inset 0 1px 0 var(--color-highlight),        0 0 0 .5px var(--color-text-strong), 0 0 0 1px color-mix(in oklab, var(--color-text-strong) 22%, transparent),        0 1px 2px -1px var(--color-shade-1), 0 2px 4px -2px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);    }    /* The ring belongs to the whole option, so the input inside does not draw a second one. */    &:has(:focus-visible) {      @apply --focus-ring;    }    &:has(:disabled) {      background: var(--color-surface-inset);      color: var(--color-text-disabled);      cursor: not-allowed;    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &:has(:checked):not(:has(:focus-visible)) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* A radio's focus stands on the chosen option: its ring is not given up to the chosen outline, as in a poll. */      &:has(:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }      &:has(:disabled) {        outline-color: GrayText;        color: GrayText;      }    }  }  .choice .checkbox:focus-visible, .choice .radio:focus-visible, .choice .switch:focus-visible {    outline: none;  }  /* The words: a title and an optional sentence, on one edge. */  .choice-body {    display: grid;    flex: 1 1 0;    gap: .125rem;    min-width: 0;  }  .choice-title {    margin: 0;    color: var(--color-text-strong);    font-weight: var(--font-weight-control);    text-wrap: balance;    overflow-wrap: anywhere;  }  /* A choice that cannot be made steps back as a whole: its title and what stands at its end take the disabled ink. */  .choice:has(:disabled) .choice-title, .choice:has(:disabled) .choice-end {    color: inherit;  }  .choice-description {    margin: 0;    color: var(--color-text-muted);    font-size: .8125rem;    text-wrap: pretty;    overflow-wrap: anywhere;  }  /* A price, a badge or a figure at the end of the first line. */  .choice-end {    flex: none;    color: var(--color-text-strong);    font-variant-numeric: tabular-nums;  }}