ナビゲーション

v2.1.0
フォーム

選択肢

説明を添えられる、ラジオボタンやチェックボックスの選択肢。

このページの内容

プランや支払い方法、項目名の下に説明文を添えた設定など、内容を説明できる広さを持つ選択肢です。ネイティブのラジオボタンまたはチェックボックスを<label>で囲むため、面全体が操作対象になり、状態とキー操作は入力要素が管理します。通常時は輪郭が1本の平らな面で、選択時は一段浮き上がり、輪郭がインク色になります。

選択肢

<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>

#アクセシビリティ

  • 選択肢は凡例付きのfieldsetでグループ化し、ラジオボタンには共通のnameを指定します。
  • タイトルと説明はどちらもラベルの一部なので、両方が選択肢の名前として読み上げられます。説明は1文に収めます。
  • 選択状態はインク色と面の高さ、入力要素自体のマークで示します。色相には頼りません。

#クラス

クラス説明
choiceラジオボタン、チェックボックス、またはスイッチを囲む label に適用する選択肢。選択肢全体の周囲にフォーカスリングを描画します。
choice-body片側に配置するタイトルと任意の説明
choice-titlechoice-body内の選択肢名
choice-descriptionタイトルの下に置く控えめな色の1文。choice-bodyの内側に配置
choice-end1 行目の末尾に置く価格、バッジ、数値

#カスタムプロパティ

プロパティ既定値説明
--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;  }}