导航

v2.1.0
表单

单选按钮

用小圆形凹槽表示选项,供用户从多项中选择一项,基于原生单选按钮。

本页内容

单选按钮是一个小圆形凹槽,选中时填入墨色并显示圆点。圆形表示只能选一项,复选框的方形则表示可以任选多项。将 .radio 加到原生 <input type="radio"> 上,再用 <label class="check"> 将它和标签文字包在一起,让整行都可点击。

单选按钮

<fieldset class="fieldset">  <legend class="fieldset-legend">Visibility</legend>  <label class="check">    <input type="radio" class="radio" name="visibility" value="public" checked> Public  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="team"> Team only  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="private"> Private  </label></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Visibility</legend>  <label className="check">    <input type="radio" className="radio" name="visibility" value="public" defaultChecked /> Public  </label>  <label className="check">    <input type="radio" className="radio" name="visibility" value="team" /> Team only  </label>  <label className="check">    <input type="radio" className="radio" name="visibility" value="private" /> Private  </label></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Visibility</legend>  <label class="check">    <input type="radio" class="radio" name="visibility" value="public" checked> Public  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="team"> Team only  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="private"> Private  </label></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Visibility</legend>    <label class="check">      <input type="radio" class="radio" name="visibility" value="public" checked> Public    </label>    <label class="check">      <input type="radio" class="radio" name="visibility" value="team"> Team only    </label>    <label class="check">      <input type="radio" class="radio" name="visibility" value="private"> Private    </label>  </fieldset></template>

#单选组

具有相同 name 的单选按钮组成一组。选中一项会取消组内其他项的选中状态,方向键可在组内切换选项。将整组放入带有组标题的 fieldset 中,用组标题写明这些选项要回答的问题。

#横向排列

少量简短选项可以并排放置。让选项之间的间距大于圆形按钮与其标签之间的间距,以便看清标签对应哪个按钮。

横向排列

<fieldset class="fieldset">  <legend class="fieldset-legend">Billing period</legend>  <div class="flex flex-wrap gap-lg">    <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>    <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>  </div></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Billing period</legend>  <div className="flex flex-wrap gap-lg">    <label className="check">      <input type="radio" className="radio" name="period" defaultChecked /> Monthly    </label>    <label className="check"><input type="radio" className="radio" name="period" /> Yearly</label>  </div></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Billing period</legend>  <div class="flex flex-wrap gap-lg">    <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>    <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>  </div></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Billing period</legend>    <div class="flex flex-wrap gap-lg">      <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>      <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>    </div>  </fieldset></template>

#禁用

禁用后,单选按钮会淡化,标签改用禁用状态的墨色。可以禁用单个选项,也可以禁用其 fieldset 来禁用整组。

禁用

<fieldset class="fieldset">  <legend class="fieldset-legend">Region</legend>  <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>  <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>  <label class="check">    <input type="radio" class="radio" name="region" disabled> Virginia, at capacity  </label></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Region</legend>  <label className="check">    <input type="radio" className="radio" name="region" defaultChecked /> Tokyo  </label>  <label className="check"><input type="radio" className="radio" name="region" /> Frankfurt</label>  <label className="check">    <input type="radio" className="radio" name="region" disabled /> Virginia, at capacity  </label></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Region</legend>  <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>  <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>  <label class="check">    <input type="radio" class="radio" name="region" disabled> Virginia, at capacity  </label></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Region</legend>    <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>    <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>    <label class="check">      <input type="radio" class="radio" name="region" disabled> Virginia, at capacity    </label>  </fieldset></template>

#如何选择控件

单选按钮会同时显示所有选项,每项占一行。也可以用以下控件实现单选:

  • 用分段控件在一行内放置两到五个简短选项,例如用于切换视图。
  • 选项太多而放不下,或空间有限时,用下拉选择框。
  • 每项都需要一个标题和一句说明时,用选项卡片。
  • 筛选条件一选中就要更新视图时,用胶囊标签。

#无障碍

  • 为组内所有单选按钮设置相同的 name。缺少这一设置时,各选项可以同时选中,也无法用方向键在它们之间切换。
  • 每组在 Tab 导航顺序中只占一个焦点位置。按 Tab 会聚焦到已选中的按钮,未选中任何项时则聚焦到第一个按钮。随后可用方向键在组内切换。
  • 有合适的默认选项时,初始就选中它。如果初始没有选中任何项,用户一旦选中某项,就无法恢复到全未选中的状态。

#类名

类名说明
radio原生 type="radio" input,呈圆形凹槽

#源码

@master/ui/components/radio.css
/* * Radio: a native input drawn as a small round well that fills with ink and shows a dot when chosen. * A circle says "one of these", where a checkbox's square says "any of these". * Wrap it with its text in a .check label, which checkbox.css defines, to make the whole row a target. */@layer components.ui {  .radio {    --check-size: 1.125rem;    position: relative;    display: inline-grid;    flex: none;    place-items: center;    width: var(--check-size);    height: var(--check-size);    /* Centres the control on the first line of a label that wraps. */    margin: max(0px, calc((1lh - var(--check-size)) / 2)) 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    /* A small control needs a firmer edge than a surface to be found: one step, still half a pixel. The well is the checkbox's. */    @apply --check-well;    vertical-align: middle;    appearance: none;    /* Standing alone, as a switch does: a pointer, no double-tap zoom and no grey flash around the well. */    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition-property: background-color, box-shadow;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    /* The dot: a third of the circle, in the ink of the page behind it, at any size the well takes. */    &::after {      content: '';      width: calc(var(--check-size) / 3);      height: calc(var(--check-size) / 3);      border-radius: 999px;      corner-shape: round;      opacity: 0;      scale: .4;      background: var(--color-text-inverse);      /* The dot leaves at once and arrives with a short settle. */      transition: opacity var(--duration-press) linear, scale var(--duration-press) linear;    }    /* Forced colors draw the well with system colors; the hover's tokens, which are more specific, would show through. */    @media (hover: hover) and (forced-colors: none) {      &:hover:not(:checked, :disabled) {        @apply --check-well-hover;      }    }    &:checked {      @apply --check-ink;      &::after {        opacity: 1;        scale: 1;        transition: opacity var(--duration-quick) var(--easing-glide), scale var(--duration-base) var(--easing-glide);      }    }    &:focus-visible {      @apply --focus-ring;    }    /*     * Invalid: a required group left without a choice once the reader has tried to send the form, or aria-invalid.     * Every well of the group takes the danger edge a field takes; the field's error beside them says why.     */    &:is([aria-invalid="true"], :user-invalid):not(:checked, :disabled) {      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);    }    &:disabled {      opacity: .5;      cursor: not-allowed;    }    @media (prefers-reduced-motion: reduce) {      &::after {        scale: 1;      }    }    /* Draw with system colors so the mark survives forced colors. */    @media (forced-colors: active) {      forced-color-adjust: none;      background: Field;      box-shadow: inset 0 0 0 1px FieldText;      &::after {        background: HighlightText;      }      &:checked {        background: Highlight;        box-shadow: none;      }      &:disabled {        opacity: 1;        box-shadow: inset 0 0 0 1px GrayText;      }      /* The danger edge is a theme color: the system's mark color says it here, as a field's outline does. */      &:is([aria-invalid="true"], :user-invalid):not(:checked, :disabled) {        box-shadow: inset 0 0 0 2px Mark;      }      /* Chosen and disabled: the fill takes the disabled system color, as a checkbox's does. */      &:disabled:checked {        background: GrayText;      }      &:focus-visible {        outline-color: Highlight;      }    }  }}