{
  "name": "poll",
  "type": "registry:ui",
  "framework": "react",
  "title": "Poll",
  "description": "A question, its answers, and the share of the vote behind each one.",
  "files": [
    {
      "path": "ui/poll.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/*\n * A poll in a message: a question, the answers, and what everyone picked.\n *\n * The bar behind each answer is the result, so it only exists once the results\n * are showing — before that the row is a button and nothing more. Percentages\n * are computed here from the counts rather than passed in, because two numbers\n * that must agree are one number too many.\n */\ninterface PollContextValue {\n  readonly total: number\n  readonly showResults: boolean\n  readonly selected: readonly string[]\n  readonly choose: (value: string) => void\n  readonly multiple: boolean\n}\n\nconst PollContext = React.createContext<PollContextValue | null>(null)\n\nfunction usePoll(component: string): PollContextValue {\n  const context = React.useContext(PollContext)\n  if (context === null) {\n    throw new Error(`<${component}> must be used inside a <Poll>.`)\n  }\n  return context\n}\n\nexport interface PollProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"onChange\"\n> {\n  /** Every vote cast, which is what the percentages are taken out of. */\n  total?: number\n  /** Shows the bars and the counts. A closed poll always shows them. */\n  showResults?: boolean\n  /** Whether more than one answer can be picked. */\n  multiple?: boolean\n  /** Controlled selection. */\n  value?: readonly string[]\n  defaultValue?: readonly string[]\n  onValueChange?: (value: readonly string[]) => void\n}\n\nconst Poll = React.forwardRef<HTMLDivElement, PollProps>(\n  (\n    {\n      className,\n      total = 0,\n      showResults = false,\n      multiple = false,\n      value,\n      defaultValue = [],\n      onValueChange,\n      children,\n      ...props\n    },\n    ref\n  ) => {\n    const [internal, setInternal] =\n      React.useState<readonly string[]>(defaultValue)\n    const isControlled = value !== undefined\n    const selected = isControlled ? value : internal\n\n    const choose = React.useCallback(\n      (answer: string) => {\n        const next = multiple\n          ? selected.includes(answer)\n            ? selected.filter((each) => each !== answer)\n            : [...selected, answer]\n          : [answer]\n        if (!isControlled) setInternal(next)\n        onValueChange?.(next)\n      },\n      [multiple, selected, isControlled, onValueChange]\n    )\n\n    const context = React.useMemo<PollContextValue>(\n      () => ({ total, showResults, selected, choose, multiple }),\n      [total, showResults, selected, choose, multiple]\n    )\n\n    return (\n      <PollContext.Provider value={context}>\n        <div\n          ref={ref}\n          className={cn(\n            \"flex w-full max-w-[440px] flex-col gap-3 rounded-[var(--newt-radius-lg)] border border-newt-border bg-newt-bg-surface p-4\",\n            className\n          )}\n          {...props}\n        >\n          {children}\n        </div>\n      </PollContext.Provider>\n    )\n  }\n)\nPoll.displayName = \"Poll\"\n\nconst PollQuestion = React.forwardRef<\n  HTMLParagraphElement,\n  React.HTMLAttributes<HTMLParagraphElement>\n>(({ className, ...props }, ref) => (\n  <p\n    ref={ref}\n    className={cn(\n      \"font-display text-base font-bold leading-snug text-newt-text-primary\",\n      className\n    )}\n    {...props}\n  />\n))\nPollQuestion.displayName = \"PollQuestion\"\n\nconst PollAnswers = React.forwardRef<\n  HTMLDivElement,\n  React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => (\n  <div ref={ref} className={cn(\"flex flex-col gap-2\", className)} {...props} />\n))\nPollAnswers.displayName = \"PollAnswers\"\n\nexport interface PollAnswerProps extends Omit<\n  React.ButtonHTMLAttributes<HTMLButtonElement>,\n  \"value\"\n> {\n  /** What this answer is called in the poll's value. */\n  value: string\n  /** How many people picked it. */\n  votes?: number\n  /** The emoji beside the answer, if it has one. */\n  emoji?: React.ReactNode\n}\n\nconst PollAnswer = React.forwardRef<HTMLButtonElement, PollAnswerProps>(\n  (\n    {\n      className,\n      value,\n      votes = 0,\n      emoji,\n      disabled,\n      children,\n      onClick,\n      ...props\n    },\n    ref\n  ) => {\n    const poll = usePoll(\"PollAnswer\")\n    const chosen = poll.selected.includes(value)\n    const share = poll.total === 0 ? 0 : Math.round((votes / poll.total) * 100)\n\n    return (\n      <button\n        ref={ref}\n        type=\"button\"\n        aria-pressed={chosen}\n        disabled={disabled}\n        onClick={(event) => {\n          onClick?.(event)\n          if (event.defaultPrevented || disabled) return\n          poll.choose(value)\n        }}\n        data-chosen={chosen || undefined}\n        className={cn(\n          \"relative flex items-center gap-3 overflow-hidden rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-base px-3 py-2.5 text-start text-sm text-newt-text-secondary\",\n          \"transition-colors duration-fast ease-newt hover:border-newt-text-muted\",\n          \"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link\",\n          \"data-[chosen]:border-newt-brand data-[chosen]:text-newt-text-primary\",\n          disabled && \"cursor-not-allowed opacity-60\",\n          className\n        )}\n        {...props}\n      >\n        {/*\n         * The bar sits behind the label rather than beside it: the row keeps\n         * its height whatever the result is, so the list does not shuffle when\n         * the votes land.\n         */}\n        {poll.showResults ? (\n          <span\n            aria-hidden=\"true\"\n            style={{ inlineSize: `${share}%` }}\n            className=\"absolute inset-y-0 start-0 bg-newt-brand/25\"\n          />\n        ) : null}\n\n        {emoji === undefined ? null : (\n          <span aria-hidden=\"true\" className=\"relative shrink-0 text-base\">\n            {emoji}\n          </span>\n        )}\n        <span className=\"relative min-w-0 flex-1 truncate\">{children}</span>\n        {poll.showResults ? (\n          <span className=\"relative shrink-0 text-xs tabular-nums text-newt-text-muted\">\n            {share}%\n          </span>\n        ) : null}\n      </button>\n    )\n  }\n)\nPollAnswer.displayName = \"PollAnswer\"\n\nconst PollFooter = React.forwardRef<\n  HTMLDivElement,\n  React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => (\n  <div\n    ref={ref}\n    className={cn(\n      \"flex items-center gap-2 text-xs text-newt-text-muted\",\n      className\n    )}\n    {...props}\n  />\n))\nPollFooter.displayName = \"PollFooter\"\n\nexport { Poll, PollAnswer, PollAnswers, PollFooter, PollQuestion }\n",
      "type": "registry:ui"
    }
  ]
}