網站導覽

v2.1.0
表單

投票調查

提出一個問題,讓使用者從數個答案中選擇,並在選擇後顯示票數分布。

本頁內容

投票調查會顯示一個問題、數個答案和票數分布。每個答案都以 <label> 包住原生單選按鈕,因此仍可用方向鍵在群組中選擇,表單也能取得所選值。投票前,各答案以平坦的面板呈現,並共用同一道輪廓線。選定答案後,每個答案會從起始側填上淡色底,寬度依得票比例而定,末端也會顯示比例數字。整個效果只靠輸入欄位的勾選狀態控制,無須腳本。

投票

<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 都以 --value 保存 0 到 100 的得票比例,並在 poll-value 中以文字放入相同的數值。尚未勾選任何答案時,比例色塊的寬度為零,數字也會隱藏,輔助技術無法讀取。勾選答案後,群組會透過 :has() 偵測到勾選狀態,各色塊便會展開至對應的寬度,數字也會淡入。

選中的答案會顯示深色輪廓、較濃的底色,並在文字後方加上勾號。改選其他答案時,這三種效果會移到新選項,得票比例則維持原值。

得票比例由頁面提供。如果結果要計入剛投下的一票,請在記錄投票時更新 --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>

#複選

允許使用者選擇多個答案時,請使用核取方塊。勾選任一答案後就會顯示結果,每個勾選的答案都有輪廓和勾號,各答案的得票比例加總也不一定等於 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群組使用 fieldset 或設有 role="group" 的元素,選項間距為 8px。
poll-title在 legend 或標題中顯示問題。
poll-option每個選項使用 label,採 38px 高的平面樣式。淡色從起始側填入,填色長度對應得票比例。選取後會加上前景色外框與勾號。
poll-input選項內的原生單選按鈕或核取方塊會覆蓋整個選項區域,外觀隱藏。
poll-label選項文字
poll-value在選項末端以文字顯示得票比例,結果顯示前會隱藏。
poll-meta在選項下方用一行較不醒目的文字顯示投票人數與剩餘時間。

#自訂屬性

屬性預設值說明
--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;  }}