"use client"; import { animate, motion, useMotionValue, useReducedMotion, useTransform } from "motion/react"; import { useEffect, useState } from "react"; import { EASE_IN, EASE_OUT, SPRING_LAYOUT, SPRING_PRESS } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { useSound } from "@/lib/sound"; import { selectSound } from "@/lib/sounds/select"; import { cn } from "@/lib/utils"; export const pollVoteMessages = { poll: "Poll", percent: "{label}, {percent}%", voted: "You voted for {label}", votes: "{count} votes", }; export type PollVoteMessages = typeof pollVoteMessages; export type PollOption = { /** Unique key for the choice, passed to `onValueChange` on a vote. */ id: string; /** Text shown on the choice's row. */ label: string; /** Votes before the viewer's; their vote adds 1 to the share. */ votes: number; }; export interface PollVoteProps { /** The choices, each with an id, label and vote count before the viewer's vote. */ options: PollOption[]; /** The viewer's choice, or null before they vote. */ value?: string | null; /** The initial choice when uncontrolled. Default: null */ defaultValue?: string | null; /** Called with the option id when the viewer votes or changes their vote. */ onValueChange?: (value: string | null) => void; /** Override any built-in string, e.g. for translation. */ labels?: Partial; /** Play a crisp select on a vote. Off by default; a `SoundProvider` can mute it app-wide. */ sound?: boolean; /** Classes merged onto the root fieldset. */ className?: string; } // After a first vote your bar fills first and the rest follow a beat apart. const STAGGER = 0.06; const COUNT = { duration: 0.7, ease: EASE_OUT } as const; const DRAW = { duration: 0.26, ease: EASE_OUT, delay: 0.08 } as const; const FADE = { duration: 0.2, ease: EASE_OUT } as const; // Clearing drains the bars quickly; exits are faster than entrances. const DRAIN = { duration: 0.22, ease: EASE_IN } as const; /** A number that counts up (or down) to its value. */ function Count({ value, delay, reduce, format, }: { value: number; delay: number; reduce: boolean; format: (value: number) => string; }) { const shown = useMotionValue(value); const text = useTransform(shown, (current) => format(Math.round(current))); useEffect(() => { const controls = animate(shown, value, reduce ? { duration: 0 } : { ...COUNT, delay }); return () => controls.stop(); }, [value, delay, reduce, shown]); return {text}; } /** The radio ring: fills with a pop and draws a check when it is your pick. */ function Mark({ on, reduce }: { on: boolean; reduce: boolean }) { return ( ); } /** * A poll you answer with one tap. Your pick's ring fills and draws a check, * your bar fills first and the rest follow in a wave, each percentage slides * in and counts up, and the vote total ticks over. Picking another option * moves every bar at once. */ export function PollVote({ options, value: valueProp, defaultValue = null, onValueChange, labels, sound = false, className, }: PollVoteProps) { const t = useMessages("PollVote", pollVoteMessages, labels); const reduce = Boolean(useReducedMotion()); const [internal, setInternal] = useState(defaultValue); const value = valueProp === undefined ? internal : valueProp; const voted = value !== null; const play = useSound(selectSound, { enabled: sound }); // Remember the vote before this one: a first vote fills in a wave, a // moved vote shifts every bar together. const [last, setLast] = useState(value); const [from, setFrom] = useState(value); if (last !== value) { setFrom(last); setLast(value); } const moved = from !== null && voted; const base = options.reduce((sum, option) => sum + option.votes, 0); const total = base + (voted ? 1 : 0); const share = (option: PollOption) => total === 0 ? 0 : ((option.votes + (option.id === value ? 1 : 0)) / total) * 100; const chosen = options.find((option) => option.id === value); const chosenIndex = options.findIndex((option) => option.id === value); return (
{options.map((option, index) => { const mine = option.id === value; const percent = voted ? share(option) : 0; // Your row leads; the others follow in order. const order = mine ? 0 : index < chosenIndex ? index + 1 : index; const delay = reduce || moved || !voted ? 0 : order * STAGGER; return ( { if (mine) return; play(); if (valueProp === undefined) setInternal(option.id); onValueChange?.(option.id); }} className={cn( "relative isolate flex h-10 items-center justify-between gap-3 overflow-hidden rounded-full border px-3 text-left text-sm transition-colors duration-200", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50", mine ? "border-primary/40 font-medium text-foreground" : "border-border text-foreground", !voted && "hover:bg-muted/60", )} > {/* The bar: a full-width layer scaled from the left to its share. */} ); })} t("votes", { count })} /> {chosen ? t("voted", { label: chosen.label }) : ""}
); }