导航

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答案内的原生 radio 或 checkbox 覆盖整个答案区域,自身不可见。
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;  }}