导航

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选项使用包裹 radio、checkbox 或开关的 label,焦点环绘制在整个选项的外缘。
choice-body沿一侧放置的标题和可选说明。
choice-titlechoice-body 内的选项名称。
choice-descriptionchoice-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;  }}