Skip to content

Poll

A poll's options as buttons, each filled by its share of the votes, the reader's own vote pressed.

<ul class="cap-poll" data-cap="poll" aria-label="Next book" id="pl-open">
  <li><button type="button" class="cap-poll-option" data-votes="3" aria-pressed="true"><span class="cap-poll-label">Field notes</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">3<span class="cap-sr-only"> votes</span></span></button></li>
  <li><button type="button" class="cap-poll-option" data-votes="1" aria-pressed="false"><span class="cap-poll-label">Annual report</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">1<span class="cap-sr-only"> vote</span></span></button></li>
  <li><button type="button" class="cap-poll-option" data-votes="0" aria-pressed="false"><span class="cap-poll-label">Release notes</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">0<span class="cap-sr-only"> votes</span></span></button></li>
</ul>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/poll.css";
import { enhance } from "capsomer/behavior/poll";

enhance();

Usage

<ul class="cap-poll" data-cap="poll" aria-label="Next book">
  <li>
    <button type="button" class="cap-poll-option" data-votes="3" aria-pressed="true">
      <span class="cap-poll-label">Field notes</span>
      <span class="cap-poll-count" aria-live="polite" aria-atomic="true">3<span class="cap-sr-only"> votes</span></span>
    </button>
  </li>
</ul>

Examples

Closed

<ul class="cap-poll" data-cap="poll" aria-label="Meeting day" id="pl-closed">
  <li><button type="button" class="cap-poll-option" data-votes="5" aria-pressed="false" disabled><span class="cap-poll-label">Tuesday</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">5<span class="cap-sr-only"> votes</span></span></button></li>
  <li><button type="button" class="cap-poll-option" data-votes="5" aria-pressed="true" disabled><span class="cap-poll-label">Thursday</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">5<span class="cap-sr-only"> votes</span></span></button></li>
</ul>

No votes yet

<ul class="cap-poll" data-cap="poll" aria-label="Snack" id="pl-none">
  <li><button type="button" class="cap-poll-option" data-votes="0" aria-pressed="false"><span class="cap-poll-label">Tea</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">0<span class="cap-sr-only"> votes</span></span></button></li>
  <li><button type="button" class="cap-poll-option" data-votes="0" aria-pressed="false"><span class="cap-poll-label">Coffee</span><span class="cap-poll-count" aria-live="polite" aria-atomic="true">0<span class="cap-sr-only"> votes</span></span></button></li>
</ul>

API reference

Poll

PropTypeRequiredDoes
optionsPollOption[]Yes
minestring[]The reader's own vote or votes.
onVote(id: string) => void
closedboolean
pendingboolean
labelstring

Functions

FunctionSignatureDoes
shares(votes: number[]): number[]The poll's one piece of behavior: each option's share of the votes as --share, through the CSSOM, so the page keeps style-src 'self'. Server-rendered options carry data-votes; the module reads them and sets the shares from the total.
setShares(poll: ParentNode): void
enhance(root?: ParentNode): () => void

CSS variables

VariableDefault
--share0

Parts and data attributes

ClassIs
.cap-pollthe root
.cap-poll-counta part
.cap-poll-labela part
.cap-poll-optiona part
StateRead from
:disabledthe platform
:hoverthe platform
[aria-pressed="true"]an attribute

Accessibility

KeyDoes
TabMoves from option to option
Enter, SpaceVotes for the option
  • Pressed: an accent edge, bold and a stronger fill, not color alone; forced colors: a Highlight outline.