ナビゲーション

v2.1.0
フォーム

アンケート

質問と複数の回答を表示し、回答を選ぶと得票結果を表示。

このページの内容

アンケートは、質問と複数の回答、得票結果を表示するコンポーネントです。各回答はネイティブのラジオボタンを <label> で囲む構造なので、矢印キーで選択でき、フォームでも選択値を扱えます。投票前の回答は、1 本の輪郭で囲んだ平らな面として表示されます。回答を選ぶと、各回答の先頭から得票率に応じた幅で色が付き、末尾に数値が表示されます。この表示の切り替えは入力要素の選択状態だけで制御でき、スクリプトは不要です。

投票

<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which theme should be the default?</legend>    <label class="poll-option" style="--value: 58">      <input class="poll-input" type="radio" name="theme" value="moonstone"><span class="poll-label">Moonstone</span><span class="poll-value">58%</span>    </label>    <label class="poll-option" style="--value: 31">      <input class="poll-input" type="radio" name="theme" value="obsidian"><span class="poll-label">Obsidian</span><span class="poll-value">31%</span>    </label>    <label class="poll-option" style="--value: 11">      <input class="poll-input" type="radio" name="theme" value="system"><span class="poll-label">Follow the system</span><span class="poll-value">11%</span>    </label>    <p class="poll-meta">214 votes · 2 days left</p>  </fieldset></div>
<div className="card w-full max-w(360)">  <fieldset className="poll">    <legend className="poll-title">Which theme should be the default?</legend>    <label className="poll-option" style={{ '--value': '58' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme" value="moonstone" /><span className="poll-label">Moonstone</span><span className="poll-value">58%</span>    </label>    <label className="poll-option" style={{ '--value': '31' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme" value="obsidian" /><span className="poll-label">Obsidian</span><span className="poll-value">31%</span>    </label>    <label className="poll-option" style={{ '--value': '11' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme" value="system" /><span className="poll-label">Follow the system</span><span className="poll-value">11%</span>    </label>    <p className="poll-meta">214 votes · 2 days left</p>  </fieldset></div>
<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which theme should be the default?</legend>    <label class="poll-option" style="--value: 58">      <input class="poll-input" type="radio" name="theme" value="moonstone"><span class="poll-label">Moonstone</span><span class="poll-value">58%</span>    </label>    <label class="poll-option" style="--value: 31">      <input class="poll-input" type="radio" name="theme" value="obsidian"><span class="poll-label">Obsidian</span><span class="poll-value">31%</span>    </label>    <label class="poll-option" style="--value: 11">      <input class="poll-input" type="radio" name="theme" value="system"><span class="poll-label">Follow the system</span><span class="poll-value">11%</span>    </label>    <p class="poll-meta">214 votes · 2 days left</p>  </fieldset></div>
<template>  <div class="card w-full max-w(360)">    <fieldset class="poll">      <legend class="poll-title">Which theme should be the default?</legend>      <label class="poll-option" style="--value: 58">        <input class="poll-input" type="radio" name="theme" value="moonstone"><span class="poll-label">Moonstone</span><span class="poll-value">58%</span>      </label>      <label class="poll-option" style="--value: 31">        <input class="poll-input" type="radio" name="theme" value="obsidian"><span class="poll-label">Obsidian</span><span class="poll-value">31%</span>      </label>      <label class="poll-option" style="--value: 11">        <input class="poll-input" type="radio" name="theme" value="system"><span class="poll-label">Follow the system</span><span class="poll-value">11%</span>      </label>      <p class="poll-meta">214 votes · 2 days left</p>    </fieldset>  </div></template>
回答を選ぶと、結果が表示されます。

#結果の表示

各 poll-option では、得票率を 0 から 100 の値として --value に指定し、同じ数値を poll-value にテキストで入れます。どの入力要素も選択されていない間は、得票率を示す色の幅は 0 で、数値は画面にも支援技術にも公開されません。回答が選択されると、:has() がグループ内の選択状態を検出し、色が各得票率に応じた幅まで広がり、数値がフェードインします。

選択した回答には文字色の輪郭と濃い背景色が付き、回答文の後ろにチェックマークが表示されます。別の回答を選ぶと、この 3 つの表示が新しい回答に移ります。得票率はそのままです。

得票率の数値はページ側で指定します。今の投票を結果に反映する場合は、投票を記録した時点で --value と表示テキストを更新してください。色の幅が新しい得票率まで滑らかに変化します。

#締め切り後の投票

投票を締め切ったら、入力要素を無効にしてください。結果は最初から表示され、回答は選べなくなります。結果を確認できるように、回答の読みやすさは維持されます。ユーザーが選んだ回答には checked を残してください。

締め切り済み

<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which theme should be the default?</legend>    <label class="poll-option" style="--value: 61">      <input class="poll-input" type="radio" name="theme-closed" value="moonstone" disabled><span class="poll-label">Moonstone</span><span class="poll-value">61%</span>    </label>    <label class="poll-option" style="--value: 29">      <input class="poll-input" type="radio" name="theme-closed" value="obsidian" disabled checked><span class="poll-label">Obsidian</span><span class="poll-value">29%</span>    </label>    <label class="poll-option" style="--value: 10">      <input class="poll-input" type="radio" name="theme-closed" value="system" disabled><span class="poll-label">Follow the system</span><span class="poll-value">10%</span>    </label>    <p class="poll-meta">380 votes · Final result</p>  </fieldset></div>
<div className="card w-full max-w(360)">  <fieldset className="poll">    <legend className="poll-title">Which theme should be the default?</legend>    <label className="poll-option" style={{ '--value': '61' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme-closed" value="moonstone" disabled /><span className="poll-label">Moonstone</span><span className="poll-value">61%</span>    </label>    <label className="poll-option" style={{ '--value': '29' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme-closed" value="obsidian" disabled defaultChecked /><span className="poll-label">Obsidian</span><span className="poll-value">29%</span>    </label>    <label className="poll-option" style={{ '--value': '10' } as React.CSSProperties}>      <input className="poll-input" type="radio" name="theme-closed" value="system" disabled /><span className="poll-label">Follow the system</span><span className="poll-value">10%</span>    </label>    <p className="poll-meta">380 votes · Final result</p>  </fieldset></div>
<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which theme should be the default?</legend>    <label class="poll-option" style="--value: 61">      <input class="poll-input" type="radio" name="theme-closed" value="moonstone" disabled><span class="poll-label">Moonstone</span><span class="poll-value">61%</span>    </label>    <label class="poll-option" style="--value: 29">      <input class="poll-input" type="radio" name="theme-closed" value="obsidian" disabled checked><span class="poll-label">Obsidian</span><span class="poll-value">29%</span>    </label>    <label class="poll-option" style="--value: 10">      <input class="poll-input" type="radio" name="theme-closed" value="system" disabled><span class="poll-label">Follow the system</span><span class="poll-value">10%</span>    </label>    <p class="poll-meta">380 votes · Final result</p>  </fieldset></div>
<template>  <div class="card w-full max-w(360)">    <fieldset class="poll">      <legend class="poll-title">Which theme should be the default?</legend>      <label class="poll-option" style="--value: 61">        <input class="poll-input" type="radio" name="theme-closed" value="moonstone" disabled><span class="poll-label">Moonstone</span><span class="poll-value">61%</span>      </label>      <label class="poll-option" style="--value: 29">        <input class="poll-input" type="radio" name="theme-closed" value="obsidian" disabled checked><span class="poll-label">Obsidian</span><span class="poll-value">29%</span>      </label>      <label class="poll-option" style="--value: 10">        <input class="poll-input" type="radio" name="theme-closed" value="system" disabled><span class="poll-label">Follow the system</span><span class="poll-value">10%</span>      </label>      <p class="poll-meta">380 votes · Final result</p>    </fieldset>  </div></template>

#複数回答

複数の回答を選べる場合は、チェックボックスを使います。1 つでも選ぶと結果が表示され、選択した各回答に輪郭とチェックマークが付きます。得票率の合計は 100% になるとは限りません。

複数回答

<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which frameworks do you ship with?</legend>    <label class="poll-option" style="--value: 72">      <input class="poll-input" type="checkbox" name="framework" value="react" checked><span class="poll-label">React</span><span class="poll-value">72%</span>    </label>    <label class="poll-option" style="--value: 34">      <input class="poll-input" type="checkbox" name="framework" value="svelte" checked><span class="poll-label">Svelte</span><span class="poll-value">34%</span>    </label>    <label class="poll-option" style="--value: 41">      <input class="poll-input" type="checkbox" name="framework" value="vue"><span class="poll-label">Vue</span><span class="poll-value">41%</span>    </label>    <label class="poll-option" style="--value: 18">      <input class="poll-input" type="checkbox" name="framework" value="none"><span class="poll-label">None: plain HTML</span><span class="poll-value">18%</span>    </label>    <p class="poll-meta">Choose all that apply</p>  </fieldset></div>
<div className="card w-full max-w(360)">  <fieldset className="poll">    <legend className="poll-title">Which frameworks do you ship with?</legend>    <label className="poll-option" style={{ '--value': '72' } as React.CSSProperties}>      <input className="poll-input" type="checkbox" name="framework" value="react" defaultChecked /><span className="poll-label">React</span><span className="poll-value">72%</span>    </label>    <label className="poll-option" style={{ '--value': '34' } as React.CSSProperties}>      <input className="poll-input" type="checkbox" name="framework" value="svelte" defaultChecked /><span className="poll-label">Svelte</span><span className="poll-value">34%</span>    </label>    <label className="poll-option" style={{ '--value': '41' } as React.CSSProperties}>      <input className="poll-input" type="checkbox" name="framework" value="vue" /><span className="poll-label">Vue</span><span className="poll-value">41%</span>    </label>    <label className="poll-option" style={{ '--value': '18' } as React.CSSProperties}>      <input className="poll-input" type="checkbox" name="framework" value="none" /><span className="poll-label">None: plain HTML</span><span className="poll-value">18%</span>    </label>    <p className="poll-meta">Choose all that apply</p>  </fieldset></div>
<div class="card w-full max-w(360)">  <fieldset class="poll">    <legend class="poll-title">Which frameworks do you ship with?</legend>    <label class="poll-option" style="--value: 72">      <input class="poll-input" type="checkbox" name="framework" value="react" checked><span class="poll-label">React</span><span class="poll-value">72%</span>    </label>    <label class="poll-option" style="--value: 34">      <input class="poll-input" type="checkbox" name="framework" value="svelte" checked><span class="poll-label">Svelte</span><span class="poll-value">34%</span>    </label>    <label class="poll-option" style="--value: 41">      <input class="poll-input" type="checkbox" name="framework" value="vue"><span class="poll-label">Vue</span><span class="poll-value">41%</span>    </label>    <label class="poll-option" style="--value: 18">      <input class="poll-input" type="checkbox" name="framework" value="none"><span class="poll-label">None: plain HTML</span><span class="poll-value">18%</span>    </label>    <p class="poll-meta">Choose all that apply</p>  </fieldset></div>
<template>  <div class="card w-full max-w(360)">    <fieldset class="poll">      <legend class="poll-title">Which frameworks do you ship with?</legend>      <label class="poll-option" style="--value: 72">        <input class="poll-input" type="checkbox" name="framework" value="react" checked><span class="poll-label">React</span><span class="poll-value">72%</span>      </label>      <label class="poll-option" style="--value: 34">        <input class="poll-input" type="checkbox" name="framework" value="svelte" checked><span class="poll-label">Svelte</span><span class="poll-value">34%</span>      </label>      <label class="poll-option" style="--value: 41">        <input class="poll-input" type="checkbox" name="framework" value="vue"><span class="poll-label">Vue</span><span class="poll-value">41%</span>      </label>      <label class="poll-option" style="--value: 18">        <input class="poll-input" type="checkbox" name="framework" value="none"><span class="poll-label">None: plain HTML</span><span class="poll-value">18%</span>      </label>      <p class="poll-meta">Choose all that apply</p>    </fieldset>  </div></template>

#投票と選択肢

投票では、回答を選ぶと全体の集計結果が表示されます。選択肢は一文で内容を説明するフォーム項目で、選択後に追加の表示はありません。投票操作のない結果表示には、棒を横向きにしたチャートを使います。

#アクセシビリティ

  • <fieldset>を使い、その<legend>に質問を記述します。これで各回答とともに質問が読み上げられます。グループ内のラジオボタンには同じnameを指定します。
  • 各回答の入力要素を<label>で囲み、回答欄全体を操作できるようにします。操作領域の高さは、ポインティングデバイスの種類にかかわらず38pxです。フォーカスリングは回答欄の周囲に表示されます。
  • 数値はテキストとして記述します。結果を表示するまではvisibilityで隠して先に読み上げられるのを防ぎ、結果の表示後は背景色の違いが見えなくても数値を確認できるようにします。
  • 輪郭の違いが見えなくても、チェックマークで選択した回答がわかります。強制カラーモードでは、選択した回答にシステムのハイライト色を使います。

#クラス

クラス説明
poll回答を 8px 間隔で並べるグループ。fieldset または role="group" を持つ要素に適用。
poll-titlelegend 要素または見出しに置く質問。
poll-option回答ごとの label 要素に適用します。高さ 38px の平らな面を、始端から得票率に応じた長さだけ淡い色で塗りつぶします。選択時はインク色の輪郭とチェックマークを表示します。
poll-input回答内のネイティブなラジオボタンまたはチェックボックス。回答全体を覆い、コントロール自体は描画されません。
poll-label回答のテキスト
poll-value末尾に表示する回答の得票率のテキスト。結果が表示されるまでは非表示
poll-meta回答の下に置く控えめな 1 行。投票者数や残り時間を表示

#カスタムプロパティ

プロパティ既定値説明
--value回答の得票率。0 から 100指定先:poll-option
--poll-colorvar(--color-surface-inverse)得票率を示す塗りつぶしの基になるインク色
--poll-radiusvar(--radius-control)回答の角

#ソース

@master/ui/components/poll.css
/* * Poll: a question, a few answers, and how the votes fell. * Each answer is a <label> around a native radio (or a checkbox, when several may be chosen), so the group * keeps the arrow keys and the form keeps the value. Before a vote the answers are flat faces on one contour. * Once one is chosen, every answer fills from its start with a tint as long as its share, set as --value * in percent, and its figure appears at the end; the chosen answer takes an ink contour and a check. * Nothing but the checked input drives this: :has() reads it, so there is no script. A poll that is over * has its inputs disabled and shows its result from the start. * Geometry: answers are 38px, as tall as a button, 8px apart, with a control's corner. An answer clips its share, * so no target can reach past it: on a touch screen it is its own target, at the size it is drawn. */@layer components.ui {  .poll {    --poll-color: var(--color-surface-inverse);    --poll-radius: var(--radius-control);    display: grid;    gap: .5rem;    min-width: 0;    margin: 0;    padding: 0;    border: 0;    color: var(--color-text-body);    font-size: .875rem;    line-height: 1.45;    /* Voted, or over: the shares and their figures show. */    &:has(.poll-input:is(:checked, :disabled)) {      .poll-option::before {        width: calc(clamp(0, var(--value, 0), 100) * 1%);      }      .poll-value {        visibility: visible;        opacity: 1;      }    }  }  /* The question, on a <legend> or a heading. */  .poll-title {    margin: 0 0 .75rem;    padding: 0;    color: var(--color-text-strong);    font: var(--font-weight-control) .9375rem/1.4 var(--font-family-sans);    font-feature-settings: inherit;    text-wrap: balance;    overflow-wrap: anywhere;  }  /* One answer. */  .poll-option {    position: relative;    display: flex;    align-items: center;    gap: .75rem;    min-width: 0;    min-height: 2.375rem;    padding: .5rem .875rem;    border-radius: calc(var(--poll-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    /* The share is finished by the answer's own corner. */    overflow: clip;    background: var(--color-surface-raised);    color: var(--color-text-strong);    box-shadow: var(--shadow-flat);    font: var(--font-weight-control) .84375rem/1.25rem var(--font-family-sans);    font-feature-settings: inherit;    cursor: pointer;    touch-action: manipulation;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    transition: box-shadow var(--duration-quick) var(--easing-glide);    /* The share: a tint of ink from the start edge, which grows to its width when the result shows. */    &::before {      content: '';      position: absolute;      inset-block: 0;      inset-inline-start: 0;      width: 0;      border-start-end-radius: calc(.1875rem * var(--corner-scale));      border-end-end-radius: calc(.1875rem * var(--corner-scale));      corner-shape: var(--corner-shape);      background: color-mix(in oklab, var(--poll-color) 7%, transparent);      transition: width var(--duration-base) var(--easing-glide);    }    @media (hover: hover) {      &:hover:not(:has(:checked, :disabled)) {        box-shadow: 0 0 0 .5px var(--color-line-mark);      }    }    /* Pressed: the answer sinks into a well before it is chosen. */    &:active:not(:has(:checked, :disabled)) {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      transition-duration: var(--duration-press);    }    /* Chosen: an ink contour, a stronger tint and a check after the words. */    &:has(.poll-input: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);      &::before {        background: color-mix(in oklab, var(--poll-color) 13%, transparent);      }      .poll-label::after {        visibility: visible;        background: currentColor;        mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5'/%3E%3C/svg%3E") center / contain no-repeat;      }      .poll-value {        color: var(--color-text-strong);      }    }    &:has(.poll-input:focus-visible) {      @apply --focus-ring;    }    /* Over: nothing more to choose, and the result stays as legible as before. */    &:has(.poll-input:disabled) {      cursor: default;    }    @media (prefers-reduced-motion: reduce) {      &::before {        transition: none;      }    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &::before {        background: GrayText;        opacity: .3;        forced-color-adjust: none;      }      &:has(.poll-input:checked):not(:has(.poll-input:focus-visible)) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* The chosen answer is the one the keys stand on: its focus ring is not given up to the chosen outline. */      &:has(.poll-input:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }      /* The check is a fill cut by a mask: kept out of forced colors, it takes the answer's system ink. */      &:has(.poll-input:checked) .poll-label::after {        forced-color-adjust: none;        background: currentColor;      }    }  }  /* The native input covers the answer and is not drawn: the answer itself is its face. */  .poll-input {    position: absolute;    inset: 0;    width: 100%;    height: 100%;    margin: 0;    padding: 0;    border: 0;    opacity: 0;    appearance: none;    cursor: inherit;  }  /* The answer's words. */  .poll-label {    position: relative;    flex: 1 1 auto;    min-width: 0;    overflow-wrap: anywhere;    /* An answer that wraps keeps more than a word on its last line, so the check after it has company. */    text-wrap: pretty;    /* The check's room is kept on every answer, so choosing one never wraps its last word and grows the row. */    &::after {      content: '';      display: inline-block;      width: .875rem;      height: .875rem;      margin-inline-start: .375rem;      vertical-align: -.125em;      visibility: hidden;    }  }  /* Its share of the votes, in tabular figures: hidden until the result shows. */  .poll-value {    position: relative;    flex: none;    visibility: hidden;    color: var(--color-text-muted);    font-weight: 400;    font-variant-numeric: tabular-nums;    opacity: 0;    transition: opacity var(--duration-base) var(--easing-glide), visibility var(--duration-base);  }  /* A quiet line under the answers: how many voted, how long is left. */  .poll-meta {    margin: .25rem 0 0;    color: var(--color-text-muted);    font-size: .78125rem;    font-variant-numeric: tabular-nums;  }}