# Team poll

A poll card: pick a destination from a `RadioGroup` and Vote, and the card turns into results with a bar, percentage and count for each option, your choice marked with a `Badge`, the total and the time left. Change vote brings the form back with your choice still selected.

**Example: Where should the offsite be?** (`examples/recipes/team-poll.tsx`)

```tsx
'use client'

import { Badge, BodySmall, Button, Card, Heading, Radio, RadioGroup, Stack, Text } from '@blocks/design-system'
import { Check, Clock, Users } from 'lucide-react'
import { type FormEvent, useId, useRef, useState } from 'react'
import styles from './team-poll.module.css'

const options = [
  { value: 'lisbon', label: 'Lisbon', detail: 'Coastal and walkable, an 11 h flight', votes: 14 },
  { value: 'kyoto', label: 'Kyoto', detail: 'Temples and tea, a long flight for most', votes: 17 },
  { value: 'mexico-city', label: 'Mexico City', detail: 'Great food, one direct flight', votes: 9 },
  { value: 'reykjavik', label: 'Reykjavík', detail: 'Hot springs, short winter days', votes: 6 },
]

// A poll that turns into its results. Counts are the team's votes plus yours,
// so changing your vote moves exactly one vote.
export default function TeamPoll() {
  const [choice, setChoice] = useState<string | null>(null)
  const [vote, setVote] = useState<string | null>(null)
  const [error, setError] = useState(false)
  const resultsHeading = useRef<HTMLHeadingElement>(null)
  const id = useId()

  const counts = options.map((option) => ({ ...option, votes: option.votes + (vote === option.value ? 1 : 0) }))
  const total = counts.reduce((sum, option) => sum + option.votes, 0)
  const leader = Math.max(...counts.map((option) => option.votes))

  function submit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    if (!choice) {
      setError(true)
      return
    }
    setVote(choice)
    requestAnimationFrame(() => resultsHeading.current?.focus())
  }

  return (
    <Card as="article" padding="lg" borderRadius="xl" aria-labelledby={`${id}-question`} dangerouslySetClassName={styles.card}>
      <Stack gap="lg">
        <Stack gap="sm">
          <Stack direction="row" gap="sm" align="center" wrap="wrap">
            <Badge tone="accent">Team poll</Badge>
            <Text fontSize="xs" color="text-muted">
              Asked by Priya Natarajan in #offsite
            </Text>
          </Stack>
          <Heading as="h3" id={`${id}-question`} fontSize="xl">
            Where should the offsite be?
          </Heading>
        </Stack>

        {vote ? (
          <Stack gap="md">
            <Heading as="h4" fontSize="sm" color="text-muted" ref={resultsHeading} tabIndex={-1} dangerouslySetClassName={styles.resultsHeading}>
              Results so far
            </Heading>
            <ol className={styles.results}>
              {counts.map((option) => {
                const share = Math.round((option.votes / total) * 100)
                const mine = option.value === vote
                return (
                  <li key={option.value} className={styles.result} data-mine={mine || undefined} data-leader={option.votes === leader || undefined}>
                    <div className={styles.resultLabel}>
                      <Text fontSize="sm" fontWeight={mine ? 'semibold' : 'medium'}>
                        {option.label}
                      </Text>
                      {mine && (
                        <Badge tone="accent">
                          <Check aria-hidden />
                          Your vote
                        </Badge>
                      )}
                      <Text fontSize="sm" fontWeight="semibold" dangerouslySetClassName={styles.share}>
                        {share}%
                      </Text>
                    </div>
                    <div className={styles.track} aria-hidden>
                      <div className={styles.fill} style={{ inlineSize: `${share}%` }} />
                    </div>
                    <Text fontSize="xs" color="text-muted">
                      {option.votes} {option.votes === 1 ? 'vote' : 'votes'}
                    </Text>
                  </li>
                )
              })}
            </ol>
            <Stack direction="row" justify="space-between" align="center" gap="sm" wrap="wrap">
              <Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.meta}>
                <Users aria-hidden size={14} /> {total} votes · <Clock aria-hidden size={14} /> Closes in 2 days
              </Text>
              <Button
                type="button"
                variant="link"
                size="sm"
                onClick={() => {
                  setChoice(vote)
                  setVote(null)
                }}
              >
                Change vote
              </Button>
            </Stack>
          </Stack>
        ) : (
          <Stack as="form" gap="lg" noValidate onSubmit={submit} aria-labelledby={`${id}-question`}>
            <RadioGroup
              name="destination"
              aria-labelledby={`${id}-question`}
              aria-describedby={error ? `${id}-error` : undefined}
              value={choice}
              onValueChange={(value) => {
                setChoice(String(value))
                setError(false)
              }}
              dangerouslySetClassName={styles.options}
            >
              {options.map((option) => (
                <Radio key={option.value} value={option.value}>
                  <span className={styles.optionText}>
                    <Text fontSize="sm" fontWeight="semibold">
                      {option.label}
                    </Text>
                    <Text fontSize="xs" color="text-muted">
                      {option.detail}
                    </Text>
                  </span>
                </Radio>
              ))}
            </RadioGroup>
            {error && (
              <BodySmall id={`${id}-error`} color="danger-text" margin="none" role="alert">
                Pick a destination to vote.
              </BodySmall>
            )}
            <Stack direction="row" justify="space-between" align="center" gap="md" wrap="wrap">
              <Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.meta}>
                <Users aria-hidden size={14} /> {total} votes · <Clock aria-hidden size={14} /> Closes in 2 days
              </Text>
              <Button type="submit" variant="primary">
                Vote
              </Button>
            </Stack>
          </Stack>
        )}
      </Stack>
    </Card>
  )
}
```

## How it works

- Counts are the team’s votes plus yours, worked out while rendering, so changing your vote moves exactly one vote and nothing can drift.
- Voting with nothing picked shows an inline alert tied to the group with `aria-describedby`. After a vote, focus moves to the results heading so a keyboard or screen reader user lands on what changed.
- Each result is text first (“Kyoto, Your vote, 38%, 18 votes”); the bars are `aria-hidden` decoration. They grow from zero with a staggered CSS animation, which is switched off for `prefers-reduced-motion`.
- The options are full-width tiles. The `Radio` already renders its label, so the module styles those labels and tints the checked one with `:has([data-checked])`.

## App-owned CSS

`examples/recipes/team-poll.module.css`:

```css
.card {
  max-inline-size: 30rem;
  background-color: var(--ds-color-bg);
  box-shadow: var(--ds-shadow-sm);
}

/* Each option is a full-width tile; the whole tile is the radio's label */
.options > label {
  align-items: flex-start;
  padding: var(--ds-space-md) var(--ds-space-lg);
  border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  border-radius: var(--ds-border-radius-md);
  transition: border-color var(--ds-duration-fast) var(--ds-easing-standard);

  &:hover {
    border-color: var(--ds-color-border-strong);
  }

  &:has([data-checked]) {
    background-color: var(--ds-color-accent-subtle);
    border-color: var(--ds-color-accent);
  }

  & > :first-child {
    margin-top: var(--ds-space-xs);
  }
}

.optionText {
  display: grid;
}

.meta {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--ds-space-sm);
}

.resultsHeading:focus {
  outline: none;
}

.results {
  display: grid;
  gap: var(--ds-space-lg);
  margin: 0;
  padding: 0;
  list-style: none;
}

.result {
  display: grid;
  gap: var(--ds-space-sm);
}

.resultLabel {
  display: flex;
  align-items: center;
  gap: var(--ds-space-md);
}

.share {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

.track {
  overflow: hidden;
  block-size: var(--ds-space-md);
  background-color: var(--ds-color-surface);
  border-radius: var(--ds-border-radius-pill);
}

.fill {
  block-size: 100%;
  background-color: var(--ds-color-border-strong);
  border-radius: inherit;
  transform-origin: left;
}

.result[data-leader] .fill {
  background-color: color-mix(in srgb, var(--ds-color-accent) 55%, transparent);
}

.result[data-mine] .fill {
  background-color: var(--ds-color-accent);
}

@media (prefers-reduced-motion: no-preference) {
  .fill {
    animation: grow calc(var(--ds-duration-slow) * 2) var(--ds-easing-emphasized) both;
  }

  .result:nth-child(2) .fill {
    animation-delay: var(--ds-duration-fast);
  }

  .result:nth-child(3) .fill {
    animation-delay: calc(var(--ds-duration-fast) * 2);
  }

  .result:nth-child(4) .fill {
    animation-delay: calc(var(--ds-duration-fast) * 3);
  }
}

@keyframes grow {
  from {
    transform: scaleX(0);
  }
}
```
