{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "interest-signal",
  "type": "registry:ui",
  "title": "Interest Signal",
  "description": "\"I want more on this\": one tap that tells whoever runs the talk, the lesson or the page what people want to hear more about. The reader presses a toggle on a topic; the host sees every topic ranked by how many asked. It is the cheapest way to learn what an…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "feedback",
    "pattern"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/button.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/patterns/interest-signal.tsx",
      "target": "components/ui/patterns/interest-signal.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/interest-signal v1.1.1 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/interest-signal\n// What changed since: https://ds.interlace.tools/c/interest-signal#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — InterestSignal, InterestBoard\n *\n * \"I want more on this\": one tap that tells whoever runs the talk, the\n * lesson or the page what people want to hear more about. The reader\n * presses a toggle on a topic; the host sees every topic ranked by how\n * many asked. It is the cheapest way to learn what an audience cares\n * about, because asking costs the reader nothing and can be taken back.\n *\n * ## Anatomy\n *\n *   InterestSignal                 (div — data-slot=\"interest-signal\", data-state=idle|pressed)\n *     ├─ Button aria-pressed       (data-slot=\"interest-signal-button\": the label never changes)\n *     │   └─ svg                   (data-slot=\"interest-signal-icon\": plus, then a check)\n *     └─ p aria-live               (data-slot=\"interest-signal-status\": \"Noted\", and how many others asked)\n *\n *   InterestBoard                  (section — data-slot=\"interest-board\")\n *     ├─ h2?                       (data-slot=\"interest-board-title\")\n *     ├─ ol                        (data-slot=\"interest-board-list\")\n *     │   └─ li × n                (data-slot=\"interest-board-row\", data-rank, data-mine)\n *     │       ├─ span              (data-slot=\"interest-board-label\")\n *     │       ├─ span              (data-slot=\"interest-board-count\": the number, in words for a screen reader)\n *     │       └─ span aria-hidden  (data-slot=\"interest-board-bar\": the track)\n *     │           └─ span          (data-slot=\"interest-board-fill\": drawn against the top topic, grows on mount)\n *     └─ p                         (data-slot=\"interest-board-note\": empty, held back, or \"+3 more\")\n *\n * ## Data\n *\n * `InterestSignal` is a toggle: `pressed` / `defaultPressed` /\n * `onPressedChange`. Its accessible name stays the same in both states, as\n * `aria-pressed` requires; the status line says what happened. `count` is\n * how many others asked, from your store.\n *\n * `InterestBoard` ranks topics with `patterns/interest-model`\n * (`rankInterest`). `minCount` holds back topics too small to be anonymous.\n *\n * ## Motion\n *\n * The icon lands with a short scale (200ms) when pressed here, and the\n * board's bars grow from the start edge (400ms) when it first shows. Both\n * are transform only, and both are off under reduced motion.\n *\n * ## MIN_VIEWPORT — 320\n *\n * | Rule | Concept                   | Where in this file                                   |\n * | ---- | ------------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>` / `<'section'>`         |\n * | R6   | data-slot per part        | interest-signal-* / interest-board-*                 |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                    |\n * | R9   | Controlled + uncontrolled | `pressed` / `defaultPressed` / `onPressedChange`     |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                 |\n * | R26  | A11y                      | toggle with a stable name; status announced politely |\n */\n\nimport { Check, Plus } from 'lucide-react';\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\nimport { motionDuration, motionEase } from '@/lib/motion-token';\nimport { Button } from '@/components/ui/button';\nimport { rankInterest } from '@/components/ui/patterns/interest-model';\n\nexport const MIN_VIEWPORT = 320 as const;\n\n\ninterface InterestSignalLabels {\n  /** The button. It stays the same when pressed. @default 'I want more on this' */\n  action?: string;\n  /** After pressing. @default 'Noted.' */\n  noted?: string;\n  /** How many others asked. @default (n, f) => `${f} other(s) asked too.` */\n  others?: (count: number, formatted: string) => string;\n}\n\ntype InterestSignalProps = Omit<React.ComponentProps<'div'>, 'children' | 'onChange'> & {\n  /** Pressed, controlled. */\n  pressed?: boolean;\n  /** Pressed, uncontrolled. @default false */\n  defaultPressed?: boolean;\n  /** Called on every press and un-press. */\n  onPressedChange?: (pressed: boolean) => void;\n  /** How many others asked for more, from your store. Shown once pressed. */\n  count?: number;\n  /** What the topic is, for a screen reader, when the button sits away from its heading. */\n  topic?: string;\n  /** @default 'default' */\n  size?: 'sm' | 'default';\n  disabled?: boolean;\n  locale?: string;\n  labels?: InterestSignalLabels;\n};\n\nconst OTHERS: NonNullable<InterestSignalLabels['others']> = (n, f) =>\n  `${f} ${n === 1 ? 'other person' : 'others'} asked too.`;\n\nfunction InterestSignal({\n  pressed: pressedProp,\n  defaultPressed = false,\n  onPressedChange,\n  count,\n  topic,\n  size = 'default',\n  disabled,\n  locale,\n  labels,\n  className,\n  ...props\n}: InterestSignalProps) {\n  const [uncontrolled, setUncontrolled] = React.useState(defaultPressed);\n  const pressed = pressedProp ?? uncontrolled;\n  const reduced = useReducedMotion();\n  const iconRef = React.useRef<HTMLSpanElement>(null);\n  const pressedHere = React.useRef(false);\n  const topicId = React.useId();\n  const nf = React.useMemo(() => new Intl.NumberFormat(locale), [locale]);\n\n  React.useEffect(() => {\n    if (!pressed || !pressedHere.current) return;\n    pressedHere.current = false;\n    const el = iconRef.current;\n    if (reduced || !el || typeof el.animate !== 'function') return;\n    el.animate([{ transform: 'scale(0.5)' }, { transform: 'scale(1)' }], { duration: 200, easing: motionEase(el, 'enter') });\n  }, [pressed, reduced]);\n\n  const toggle = () => {\n    const next = !pressed;\n    pressedHere.current = next;\n    if (pressedProp === undefined) setUncontrolled(next);\n    onPressedChange?.(next);\n  };\n\n  const others = typeof count === 'number' && Number.isFinite(count) && count > 0 ? Math.floor(count) : 0;\n  const Icon = pressed ? Check : Plus;\n\n  return (\n    <div\n      data-slot=\"interest-signal\"\n      data-state={pressed ? 'pressed' : 'idle'}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn('flex flex-wrap items-center gap-x-3 gap-y-2', className)}\n      {...props}\n    >\n      <Button\n        type=\"button\"\n        data-slot=\"interest-signal-button\"\n        variant={pressed ? 'default' : 'outline'}\n        size={size === 'sm' ? 'sm' : 'default'}\n        aria-pressed={pressed}\n        aria-describedby={topic ? topicId : undefined}\n        disabled={disabled}\n        onClick={toggle}\n        className=\"rounded-full\"\n      >\n        <span ref={iconRef} data-slot=\"interest-signal-icon\" aria-hidden=\"true\" className=\"inline-flex\">\n          <Icon className=\"size-4\" />\n        </span>\n        {labels?.action ?? 'I want more on this'}\n      </Button>\n      {topic ? (\n        <span id={topicId} hidden>\n          {topic}\n        </span>\n      ) : null}\n      <p data-slot=\"interest-signal-status\" aria-live=\"polite\" className=\"text-muted-foreground text-sm empty:hidden\">\n        {pressed\n          ? [labels?.noted ?? 'Noted.', others ? (labels?.others ?? OTHERS)(others, nf.format(others)) : '']\n              .filter(Boolean)\n              .join(' ')\n          : ''}\n      </p>\n    </div>\n  );\n}\nInterestSignal.displayName = 'InterestSignal';\n\ninterface InterestTopic {\n  id: string;\n  label: React.ReactNode;\n}\n\ninterface InterestBoardLabels {\n  /** The count beside a topic. @default (n, f) => `${f} asked` */\n  count?: (count: number, formatted: string) => string;\n  /** Marks the viewer's own asks. @default 'You asked' */\n  mine?: string;\n  /** Nobody asked yet. @default 'No one has asked for more yet.' */\n  empty?: string;\n  /** Topics held back by `minCount`. @default (n, min) => `${n} more topic(s) show once ${min} people ask.` */\n  held?: (topics: number, minCount: number) => string;\n  /** Topics left out by `top`. @default (n) => `And ${n} more.` */\n  more?: (topics: number) => string;\n}\n\ntype InterestBoardProps = Omit<React.ComponentProps<'section'>, 'children' | 'title'> & {\n  topics: readonly InterestTopic[];\n  /** Asks per topic id, from your store. */\n  counts?: Readonly<Record<string, number>>;\n  /** Topic ids the viewer asked about. */\n  mine?: readonly string[];\n  /** `counts` already include the viewer's asks. @default false */\n  countsIncludeMine?: boolean;\n  /** Hold back topics with fewer asks. @default 1 */\n  minCount?: number;\n  /** Show only the most-asked few. */\n  top?: number;\n  /** A heading, rendered as an h2 unless `titleAs` says otherwise. */\n  title?: React.ReactNode;\n  titleAs?: 'h2' | 'h3' | 'h4';\n  /** Grow the bars when the board first shows. @default true */\n  animate?: boolean;\n  locale?: string;\n  labels?: InterestBoardLabels;\n};\n\nconst COUNT: NonNullable<InterestBoardLabels['count']> = (_n, f) => `${f} asked`;\nconst HELD: NonNullable<InterestBoardLabels['held']> = (n, min) =>\n  `${n} more ${n === 1 ? 'topic shows' : 'topics show'} once ${min} people ask.`;\nconst MORE: NonNullable<InterestBoardLabels['more']> = (n) => `And ${n} more.`;\n\nfunction InterestBoard({\n  topics,\n  counts,\n  mine,\n  countsIncludeMine,\n  minCount = 1,\n  top,\n  title,\n  titleAs: Heading = 'h2',\n  animate = true,\n  locale,\n  labels,\n  className,\n  ...props\n}: InterestBoardProps) {\n  const reduced = useReducedMotion();\n  const listRef = React.useRef<HTMLOListElement>(null);\n  const headingId = React.useId();\n  const nf = React.useMemo(() => new Intl.NumberFormat(locale), [locale]);\n  const { rows, held, more } = rankInterest(\n    topics.map((t) => t.id),\n    counts,\n    { mine, countsIncludeMine, minCount, top },\n  );\n  const labelOf = new Map(topics.map((t) => [t.id, t.label]));\n\n  React.useEffect(() => {\n    if (!animate || reduced) return;\n    const bars = listRef.current?.querySelectorAll<HTMLElement>('[data-slot=\"interest-board-fill\"]') ?? [];\n    for (const el of bars) {\n      if (typeof el.animate !== 'function') continue;\n      el.animate([{ transform: 'scaleX(0)' }, { transform: 'scaleX(1)' }], { duration: motionDuration(el, 'slow'), easing: motionEase(el, 'enter') });\n    }\n    // Mount only: a count that moves later should not redraw the whole board.\n    // eslint-disable-next-line react-features/hooks-exhaustive-deps\n  }, []);\n\n  const notes = [\n    rows.length === 0 && held === 0 && more === 0 ? (labels?.empty ?? 'No one has asked for more yet.') : '',\n    held ? (labels?.held ?? HELD)(held, Math.max(1, Math.floor(minCount))) : '',\n    more ? (labels?.more ?? MORE)(more) : '',\n  ].filter(Boolean);\n\n  return (\n    <section\n      data-slot=\"interest-board\"\n      data-min-viewport={String(MIN_VIEWPORT)}\n      aria-labelledby={title ? headingId : undefined}\n      className={cn('@container grid gap-4', className)}\n      {...props}\n    >\n      {title ? (\n        <Heading id={headingId} data-slot=\"interest-board-title\" className=\"font-heading text-lg font-semibold text-balance\">\n          {title}\n        </Heading>\n      ) : null}\n      {rows.length ? (\n        <ol ref={listRef} data-slot=\"interest-board-list\" className=\"grid gap-3\">\n          {rows.map((r) => (\n            <li\n              key={r.id}\n              data-slot=\"interest-board-row\"\n              data-rank={r.rank}\n              data-mine={r.mine ? '' : undefined}\n              className=\"grid grid-cols-[1fr_auto] items-baseline gap-x-3 gap-y-1.5\"\n            >\n              <span data-slot=\"interest-board-label\" className=\"min-w-0 font-medium text-pretty\">\n                {labelOf.get(r.id)}\n                {r.mine ? (\n                  <span className=\"text-muted-foreground ms-2 text-xs font-normal\">{labels?.mine ?? 'You asked'}</span>\n                ) : null}\n              </span>\n              <span data-slot=\"interest-board-count\" className=\"text-muted-foreground text-sm tabular-nums\">\n                {(labels?.count ?? COUNT)(r.count, nf.format(r.count))}\n              </span>\n              <span\n                data-slot=\"interest-board-bar\"\n                aria-hidden=\"true\"\n                className=\"bg-muted col-span-2 block h-2 overflow-hidden rounded-full\"\n              >\n                <span\n                  data-slot=\"interest-board-fill\"\n                  className={cn(\n                    'block h-full origin-left rounded-full rtl:origin-right',\n                    r.rank === 1 ? 'bg-primary' : 'bg-foreground/60',\n                  )}\n                  style={{ inlineSize: `${Math.max(4, Math.round(r.share * 100))}%` }}\n                />\n              </span>\n            </li>\n          ))}\n        </ol>\n      ) : null}\n      {notes.length ? (\n        <p data-slot=\"interest-board-note\" className=\"text-muted-foreground text-sm\">\n          {notes.join(' ')}\n        </p>\n      ) : null}\n    </section>\n  );\n}\nInterestBoard.displayName = 'InterestBoard';\n\nexport { InterestSignal, InterestBoard };\nexport type { InterestSignalProps, InterestSignalLabels, InterestBoardProps, InterestBoardLabels, InterestTopic };\n"
    },
    {
      "path": "registry/interlace-ui/patterns/interest-model.ts",
      "target": "components/ui/patterns/interest-model.ts",
      "type": "registry:ui",
      "content": "export interface InterestRow {\n  id: string;\n  /** People who asked for more on this topic. */\n  count: number;\n  /** The viewer is one of them. */\n  mine: boolean;\n  /** 1 for the most-asked topic; ties share a rank. */\n  rank: number;\n  /** count ÷ the top count, 0 to 1, for a bar. */\n  share: number;\n}\n\nexport interface RankInterestOptions {\n  /** Topic ids the viewer asked about. */\n  mine?: readonly string[];\n  /** `counts` already include the viewer's own asks. @default false */\n  countsIncludeMine?: boolean;\n  /** Hold back topics with fewer asks than this, so a small room gives no one away. @default 1 */\n  minCount?: number;\n  /** Keep only the first `top` rows after ranking. */\n  top?: number;\n}\n\nexport interface InterestRanking {\n  /** Topics at or above `minCount`, most-asked first; equal counts keep the order given. */\n  rows: InterestRow[];\n  /** Topics with at least one ask that `minCount` held back. */\n  held: number;\n  /** Topics left out by `top`. */\n  more: number;\n  /** Every ask, on every topic, after the viewer's are added. */\n  total: number;\n}\n\nconst clean = (n: unknown): number =>\n  typeof n === 'number' && Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;\n\nexport function rankInterest(\n  ids: readonly string[],\n  counts: Readonly<Record<string, number>> = {},\n  { mine = [], countsIncludeMine = false, minCount = 1, top }: RankInterestOptions = {},\n): InterestRanking {\n  const mineSet = new Set(mine);\n  const floor = Math.max(1, Math.floor(minCount));\n  const all = [...new Set(ids)].map((id, order) => {\n    const own = mineSet.has(id);\n    const count = clean(counts[id]) + (own && !countsIncludeMine ? 1 : 0);\n    return { id, count, mine: own, order };\n  });\n  const total = all.reduce((sum, r) => sum + r.count, 0);\n  const shown = all.filter((r) => r.count >= floor).sort((a, b) => b.count - a.count || a.order - b.order);\n  const held = all.filter((r) => r.count > 0 && r.count < floor).length;\n  // Every shown row has at least one ask, so the top count is never 0 here.\n  const max = shown[0]?.count ?? 1;\n  let rank = 0;\n  let last = -1;\n  const ranked = shown.map((r, i) => {\n    if (r.count !== last) {\n      rank = i + 1;\n      last = r.count;\n    }\n    return { id: r.id, count: r.count, mine: r.mine, rank, share: r.count / max };\n  });\n  const keep = top === undefined ? ranked.length : Math.max(0, Math.floor(top));\n  return { rows: ranked.slice(0, keep), held, more: Math.max(0, ranked.length - keep), total };\n}\n\n/**\n * @interlace/ui — interest-model\n *\n * The arithmetic behind \"I want more on this\": how many people asked for\n * more on each topic, ranked, with small counts held back. Pure, so the\n * presenter's screen, a dashboard and a server report rank the same way.\n *\n * A signal is not a vote. One person can ask for more on several topics,\n * so a topic's bar is drawn against the most-asked topic, not against a\n * total: `share` is 1 for the top topic and 0.5 for one with half its asks.\n */\n"
    }
  ],
  "meta": {
    "tier": "pattern",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.1.1",
    "since": "2.0.0"
  },
  "docs": "## @interlace/interest-signal\n\nInstalled to `components/ui/patterns/interest-signal.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/patterns/interest-signal';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/interest-signal\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
