網站導覽

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 的單選按鈕會形成一組。選取其中一項會取消其他選項,方向鍵可在群組內切換選取項目。請將群組放進含有 legend 元素的 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" 輸入控制項。

#原始碼

@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;      }    }  }}