{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "room-goal",
  "type": "registry:ui",
  "title": "Room Goal",
  "description": "One meter for the whole room: a shared target, named milestones on the way (\"Bonus round\", \"Group photo\"), and, when given, how many rounds in a row most of the room got right. Built for the shared screen; it reads the same on a phone.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "games",
    "game"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/motion-token.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/games/room-goal.tsx",
      "target": "components/ui/games/room-goal.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/room-goal v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/room-goal\n// What changed since: https://ds.interlace.tools/c/room-goal#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — RoomGoal\n *\n * One meter for the whole room: a shared target, named milestones on the\n * way (\"Bonus round\", \"Group photo\"), and, when given, how many rounds in a\n * row most of the room got right. Built for the shared screen; it reads the\n * same on a phone.\n *\n * ## Anatomy\n *\n *   RoomGoal                       (div — data-slot=\"room-goal\", data-state=progress|done)\n *     ├─ div                       (data-slot=\"room-goal-header\": the label, the count, the streak)\n *     ├─ div role=\"meter\"          (data-slot=\"room-goal-meter\": track, fill, milestone ticks)\n *     ├─ ol                        (data-slot=\"room-goal-milestones\": each one's name, reached or not)\n *     └─ span sr-only aria-live    (data-slot=\"room-goal-announce\": \"Milestone reached: Bonus round\")\n *\n * ## Motion\n *\n * The fill moves to each new total (`--duration-slow`, `ease-move`); a\n * milestone the room just passed lifts once (250ms), and the meter's end\n * glows once when the goal is met. Under reduced motion the meter is simply\n * at its value.\n *\n * ## MIN_VIEWPORT — 320\n *\n * | Rule | Concept                   | Where in this file                                  |\n * | ---- | ------------------------- | --------------------------------------------------- |\n * | R4   | Extends native el         | `div` props                                         |\n * | R6   | data-slot per part        | room-goal / -header / -meter / -milestones / -announce |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                   |\n * | R8   | Enums, not booleans       | `size`                                              |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                |\n * | R26  | A11y                      | meter with a value text; milestones as a list; a pass announced once |\n */\n\nimport { Check } from 'lucide-react';\n\nimport { cn } from '@/lib/utils';\nimport { motionEase } from '@/lib/motion-token';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\nimport type { RoomMilestone } from '@/components/ui/games/room-goal-model';\n\nexport const MIN_VIEWPORT = 320 as const;\n\ninterface RoomGoalLabels {\n  /** The meter's value in words. @default (v, t) => `${v} of ${t}` */\n  value?: (value: string, target: string) => string;\n  /** @default (label) => `Milestone reached: ${label}` */\n  reached?: (label: string) => string;\n  /** @default 'Goal reached.' */\n  done?: string;\n  /** @default (n) => `${n} rounds in a row` */\n  streak?: (n: number, formatted: string) => string;\n}\n\ntype RoomGoalProps = Omit<React.ComponentProps<'div'>, 'children'> & {\n  /** What the room is working toward. */\n  label: React.ReactNode;\n  /** The room's total so far, from `tallyRoomGoal`. */\n  value: number;\n  target: number;\n  milestones?: readonly RoomMilestone[];\n  /** Rounds in a row most of the room got right, from `roomStreak`. Hidden below 2. */\n  streak?: number;\n  /** `stage` for a shared screen. @default 'default' */\n  size?: 'default' | 'stage';\n  locale?: string;\n  labels?: RoomGoalLabels;\n};\n\nfunction RoomGoal({\n  label,\n  value,\n  target,\n  milestones = [],\n  streak = 0,\n  size = 'default',\n  locale,\n  labels,\n  className,\n  ...props\n}: RoomGoalProps) {\n  const reduced = useReducedMotion();\n  const nf = React.useMemo(() => new Intl.NumberFormat(locale), [locale]);\n  const stage = size === 'stage';\n  const t = Number.isFinite(target) && target > 0 ? target : 0;\n  const v = Number.isFinite(value) && value > 0 ? value : 0;\n  const share = t > 0 ? Math.min(1, v / t) : 0;\n  const done = t > 0 && v >= t;\n  const ticks = [...milestones]\n    .filter((m) => Number.isFinite(m.at) && m.at > 0 && m.at <= t)\n    .sort((a, b) => a.at - b.at);\n  const labelId = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const prev = React.useRef(v);\n  const [announce, setAnnounce] = React.useState('');\n\n  // When the total passes a milestone (or the goal), say so once and lift it.\n  React.useEffect(() => {\n    const before = prev.current;\n    prev.current = v;\n    if (v <= before) return;\n    const passed = ticks.filter((m) => before < m.at && v >= m.at);\n    const finished = t > 0 && before < t && v >= t;\n    const words = [\n      ...passed.map((m) => (labels?.reached ?? ((l: string) => `Milestone reached: ${l}`))(m.label)),\n      ...(finished && !passed.some((m) => m.at === t) ? [labels?.done ?? 'Goal reached.'] : []),\n    ];\n    if (words.length > 0) setAnnounce(words.join(' '));\n    if (reduced) return;\n    const root = rootRef.current;\n    for (const m of passed) {\n      const el = root?.querySelector<HTMLElement>(`[data-milestone=\"${m.at}\"]`);\n      if (el && typeof el.animate === 'function') {\n        el.animate([{ transform: 'none' }, { transform: 'translateY(-4px) scale(1.04)' }, { transform: 'none' }], {\n          duration: 250,\n          easing: motionEase(el, 'move'),\n        });\n      }\n    }\n    const end = root?.querySelector<HTMLElement>('[data-slot=\"room-goal-fill\"]');\n    if (finished && end && typeof end.animate === 'function') {\n      end.animate([{ opacity: 1 }, { opacity: 0.6 }, { opacity: 1 }], { duration: 400, easing: 'ease-in-out' });\n    }\n    // Runs on each new total.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [v]);\n\n  const valueText = (labels?.value ?? ((a: string, b: string) => `${a} of ${b}`))(nf.format(v), nf.format(t));\n\n  return (\n    <div\n      ref={rootRef}\n      data-slot=\"room-goal\"\n      data-state={done ? 'done' : 'progress'}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn('grid gap-3', className)}\n      {...props}\n    >\n      <div data-slot=\"room-goal-header\" className=\"flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1\">\n        <span\n          id={labelId}\n          className={cn('font-heading font-semibold text-balance', stage ? 'text-section' : 'text-lg')}\n        >\n          {label}\n        </span>\n        <span className={cn('flex items-baseline gap-3 tabular-nums', stage ? 'text-xl' : 'text-sm')}>\n          {streak >= 2 ? (\n            <span\n              data-slot=\"room-goal-streak\"\n              className=\"bg-primary/10 text-primary rounded-full px-2.5 py-0.5 font-medium\"\n            >\n              {(labels?.streak ?? ((n: number, f: string) => `${f} rounds in a row`))(streak, nf.format(streak))}\n            </span>\n          ) : null}\n          <span aria-hidden className=\"font-semibold\">\n            {valueText}\n          </span>\n        </span>\n      </div>\n      <div\n        role=\"meter\"\n        aria-labelledby={labelId}\n        aria-valuemin={0}\n        aria-valuemax={t}\n        aria-valuenow={Math.min(v, t)}\n        aria-valuetext={valueText}\n        data-slot=\"room-goal-meter\"\n        className={cn('bg-muted relative overflow-hidden rounded-full', stage ? 'h-6' : 'h-3')}\n      >\n        <span\n          data-slot=\"room-goal-fill\"\n          className=\"bg-primary absolute inset-0 block origin-left rounded-full motion-safe:transition-transform motion-safe:duration-(--duration-slow) motion-safe:ease-move rtl:origin-right\"\n          style={{ transform: `scaleX(${share})` }}\n        />\n        {ticks\n          .filter((m) => m.at < t)\n          .map((m) => (\n            <span\n              key={m.at}\n              aria-hidden\n              className={cn(\n                'absolute inset-y-0 w-0.5 -translate-x-1/2 rtl:translate-x-1/2',\n                v >= m.at ? 'bg-primary-foreground/70' : 'bg-foreground/25',\n              )}\n              style={{ insetInlineStart: `${(m.at / t) * 100}%` }}\n            />\n          ))}\n      </div>\n      {ticks.length > 0 ? (\n        <ol data-slot=\"room-goal-milestones\" className={cn('flex flex-wrap gap-2', stage ? 'text-base' : 'text-xs')}>\n          {ticks.map((m) => {\n            const hit = v >= m.at;\n            return (\n              <li\n                key={m.at}\n                data-milestone={m.at}\n                data-reached={hit ? '' : undefined}\n                className={cn(\n                  'flex items-center gap-1.5 rounded-full border px-2.5 py-1',\n                  hit ? 'border-primary bg-primary/10 text-foreground' : 'text-muted-foreground',\n                )}\n              >\n                {hit ? <Check aria-hidden className=\"text-primary size-3.5\" strokeWidth={3} /> : null}\n                <span className=\"font-medium\">{m.label}</span>\n                <span className=\"tabular-nums\">{nf.format(m.at)}</span>\n                <span className=\"sr-only\">{hit ? ', reached' : ''}</span>\n              </li>\n            );\n          })}\n        </ol>\n      ) : null}\n      <span data-slot=\"room-goal-announce\" aria-live=\"polite\" className=\"sr-only\">\n        {announce}\n      </span>\n    </div>\n  );\n}\nRoomGoal.displayName = 'RoomGoal';\n\nexport { RoomGoal };\nexport type { RoomGoalProps, RoomGoalLabels };\n"
    },
    {
      "path": "registry/interlace-ui/games/room-goal-model.ts",
      "target": "components/ui/games/room-goal-model.ts",
      "type": "registry:ui",
      "content": "export interface RoomContribution {\n  /** Who added it. */\n  by: string;\n  /** The round it belongs to; one contribution per person per round. */\n  round: string;\n  points: number;\n}\n\nexport interface RoomMilestone {\n  at: number;\n  label: string;\n}\n\nexport interface RoomGoalTally {\n  total: number;\n  /** People who added anything. */\n  people: number;\n  /** Milestones passed, in order. */\n  reached: RoomMilestone[];\n  /** The next milestone, or null when all are passed. */\n  next: RoomMilestone | null;\n  /** The total reached the target. */\n  done: boolean;\n}\n\nexport function tallyRoomGoal(\n  contributions: readonly RoomContribution[],\n  options: { target: number; milestones?: readonly RoomMilestone[]; capPerPerson?: number },\n): RoomGoalTally {\n  const { target, milestones = Array.of<RoomMilestone>(), capPerPerson = Number.POSITIVE_INFINITY } = options;\n  // Per person and round, the highest points: order-free, so every screen agrees.\n  const best = new Map<string, { by: string; points: number }>();\n  for (const c of contributions) {\n    if (!c || typeof c.by !== 'string' || typeof c.round !== 'string') continue;\n    if (!Number.isFinite(c.points) || c.points <= 0) continue;\n    const key = `${c.by}\\uE000${c.round}`;\n    best.set(key, { by: c.by, points: Math.max(best.get(key)?.points ?? 0, c.points) });\n  }\n  const perPerson = new Map<string, number>();\n  for (const { by, points } of best.values()) perPerson.set(by, (perPerson.get(by) ?? 0) + points);\n  let total = 0;\n  for (const n of perPerson.values()) total += Math.min(n, capPerPerson);\n  const sorted = milestones.filter((m) => Number.isFinite(m.at)).sort((a, b) => a.at - b.at);\n  return {\n    total,\n    people: perPerson.size,\n    reached: sorted.filter((m) => total >= m.at),\n    next: sorted.find((m) => total < m.at) ?? null,\n    done: target > 0 && total >= target,\n  };\n}\n\n/** Rounds in a row (ending with the latest) where at least `threshold` of the room was right, and the best run. */\nexport function roomStreak(\n  rounds: readonly { right: number; answered: number }[],\n  options: { threshold?: number } = {},\n): { current: number; best: number } {\n  const { threshold = 0.7 } = options;\n  let current = 0;\n  let best = 0;\n  for (const r of rounds) {\n    const ok = r.answered > 0 && r.right / r.answered >= threshold;\n    current = ok ? current + 1 : 0;\n    best = Math.max(best, current);\n  }\n  return { current, best };\n}\n\n/**\n * @interlace/ui — room-goal-model\n *\n * A goal the whole room works toward together: every right answer (or any\n * contribution the app counts) fills one shared meter, with named\n * milestones on the way. It changes the feel of a quiz from \"who beat me\"\n * to \"we got there\", which suits a workshop.\n *\n * `tallyRoomGoal` adds up contributions, counting each person once per\n * round however many times a message is delivered (the highest one counts,\n * so every screen gets the same total whatever order messages arrive in),\n * optionally capping what one person can add, so the room gets there\n * together. `roomStreak` counts rounds in a row where enough of the room\n * got it right.\n */\n"
    }
  ],
  "meta": {
    "tier": "game",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.0.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/room-goal\n\nInstalled to `components/ui/games/room-goal.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/games/room-goal';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/room-goal\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
