탐색

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에는 0부터 100까지의 득표율을 --value로 지정하고, 같은 수치를 poll-value에 텍스트로 표시합니다. 선택된 입력 요소가 없으면 득표율을 나타내는 영역의 너비는 0이고, 수치도 숨겨져 보조 기술에 노출되지 않습니다. 응답을 선택하면 :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선택지 하나를 높이 38px의 평평한 label로 표시합니다. 시작 가장자리부터 득표율만큼 옅은 색으로 채우고, 선택하면 전경색 윤곽선과 체크 표시가 나타납니다.
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;  }}