{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comment-thread",
  "title": "Comment Thread",
  "description": "Nested replies, reactions, sorting, moderation states, and a comment composer.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "textarea",
    "toggle",
    "https://uaiblocks.dev/r/uai-theme.json",
    "https://uaiblocks.dev/r/comment.json",
    "https://uaiblocks.dev/r/reaction-bar.json",
    "https://uaiblocks.dev/r/uai-utils.json"
  ],
  "files": [
    {
      "path": "src/registry/uai/blocks/comment-thread.tsx",
      "content": "\"use client\";\n\nimport { ArrowUp } from \"lucide-react\";\nimport {\n  type ComponentProps,\n  createContext,\n  type KeyboardEvent,\n  useContext,\n  useEffect,\n  useId,\n  useLayoutEffect,\n  useRef,\n  useState,\n} from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Textarea } from \"@/components/ui/textarea\";\nimport { toggleVariants } from \"@/components/ui/toggle\";\nimport { Comment, type CommentProps, type CommentVariant } from \"@/components/ui/uai/comment\";\nimport {\n  ReactionBar,\n  type ReactionBarProps,\n  type ReactionBarVariant,\n} from \"@/components/ui/uai/reaction-bar\";\nimport { cn } from \"@/lib/uai-utils\";\n\n/**\n * Single-choice segments are plain radio buttons styled like ToggleGroupItem: ToggleGroup's value\n * is a string on Radix but an array on Base UI, so the group's selection stays in uai state.\n */\nconst segmentClass = \"w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10\";\n\nexport const COMMENT_THREAD_VARIANTS = [\"threaded\", \"cards\", \"compact\"] as const;\nexport type CommentThreadVariant = (typeof COMMENT_THREAD_VARIANTS)[number];\nexport type CommentThreadProps = ComponentProps<\"section\"> & {\n  variant?: CommentThreadVariant;\n  /** The selected sort order. The application sorts the comments it renders. */\n  sort?: string;\n  defaultSort?: string;\n  onSortChange?: (sort: string) => void;\n};\n\ntype ThreadContext = {\n  id: string;\n  variant: CommentThreadVariant;\n  sort: string;\n  setSort: (sort: string) => void;\n};\nconst Context = createContext<ThreadContext | null>(null);\nfunction useThread(part: string) {\n  const context = useContext(Context);\n  if (!context) throw new Error(`${part} must be used within CommentThread`);\n  return context;\n}\n\nconst commentVariants: Record<CommentThreadVariant, CommentVariant> = {\n  threaded: \"thread\",\n  cards: \"card\",\n  compact: \"compact\",\n};\nconst reactionVariants: Record<CommentThreadVariant, ReactionBarVariant> = {\n  threaded: \"pill\",\n  cards: \"outlined\",\n  compact: \"compact\",\n};\n\n/** A discussion with sorting, nested replies, reactions, moderation states, and a composer. */\nexport function CommentThread({\n  variant = \"threaded\",\n  sort,\n  defaultSort = \"\",\n  onSortChange,\n  children,\n  className,\n  ...props\n}: CommentThreadProps) {\n  const id = useId();\n  const [internal, setInternal] = useState(defaultSort);\n  const current = sort ?? internal;\n  return (\n    <Context.Provider\n      value={{\n        id,\n        variant,\n        sort: current,\n        setSort: (next) => {\n          if (next === current) return;\n          if (sort === undefined) setInternal(next);\n          onSortChange?.(next);\n        },\n      }}\n    >\n      <section\n        aria-labelledby={`${id}-title`}\n        data-slot=\"comment-thread\"\n        className={cn(\n          \"grid min-w-0 text-[13px]/[18px] text-foreground\",\n          variant === \"compact\" ? \"gap-3\" : \"gap-5\",\n          className,\n        )}\n        {...props}\n        data-variant={variant}\n      >\n        {children}\n      </section>\n    </Context.Provider>\n  );\n}\n\nexport function CommentThreadHeader({ className, ...props }: ComponentProps<\"header\">) {\n  useThread(\"CommentThreadHeader\");\n  return (\n    <header\n      data-slot=\"comment-thread-header\"\n      className={cn(\"flex min-w-0 flex-wrap items-center justify-between gap-3\", className)}\n      {...props}\n    />\n  );\n}\n\nexport function CommentThreadTitle({ className, ...props }: ComponentProps<\"h2\">) {\n  const { id, variant } = useThread(\"CommentThreadTitle\");\n  return (\n    <h2\n      data-slot=\"comment-thread-title\"\n      className={cn(\n        \"m-0 inline-flex items-baseline gap-2 font-semibold tracking-[-0.01em]\",\n        variant === \"compact\" ? \"text-sm/5\" : \"text-[15px]/[22px]\",\n        className,\n      )}\n      {...props}\n      id={`${id}-title`}\n    />\n  );\n}\n\n/** A muted count beside the title, such as \"14 comments\". */\nexport function CommentThreadCount({ className, ...props }: ComponentProps<\"span\">) {\n  return (\n    <span\n      data-slot=\"comment-thread-count\"\n      className={cn(\n        \"text-[12.5px] font-normal tracking-normal text-subtle-foreground tabular-nums\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport type CommentThreadSortProps = Omit<ComponentProps<\"div\">, \"defaultValue\" | \"dir\">;\n\nconst useIsomorphicLayoutEffect = typeof window === \"undefined\" ? useEffect : useLayoutEffect;\n\n/** A radio group for the sort order. Arrow keys move the selection. */\nexport function CommentThreadSort({\n  \"aria-label\": label = \"Sort comments\",\n  onKeyDown,\n  className,\n  children,\n  ...props\n}: CommentThreadSortProps) {\n  const { variant, sort } = useThread(\"CommentThreadSort\");\n  const ref = useRef<HTMLDivElement>(null);\n  const [thumb, setThumb] = useState<{ x: number; y: number; width: number; height: number }>();\n  useIsomorphicLayoutEffect(() => {\n    const group = ref.current;\n    if (!group) return;\n    const measure = () => {\n      const checked = group.querySelector<HTMLElement>(\"[role=radio][aria-checked=true]\");\n      if (!checked || checked.offsetWidth === 0) return setThumb(undefined);\n      setThumb({\n        x: checked.offsetLeft,\n        y: checked.offsetTop,\n        width: checked.offsetWidth,\n        height: checked.offsetHeight,\n      });\n    };\n    measure();\n    if (typeof ResizeObserver !== \"function\") return;\n    const observer = new ResizeObserver(measure);\n    observer.observe(group);\n    return () => observer.disconnect();\n  }, [sort]);\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-label={label}\n      data-slot=\"comment-thread-sort\"\n      className={cn(\n        \"group/sort relative isolate inline-flex items-center gap-0.5 rounded-full bg-card\",\n        variant === \"compact\" ? \"p-px\" : \"p-0.5\",\n        className,\n      )}\n      {...props}\n      ref={ref}\n      data-ready={thumb ? \"\" : undefined}\n      onKeyDown={(event) => {\n        onKeyDown?.(event);\n        if (!event.defaultPrevented) moveRadio(event);\n      }}\n    >\n      {/* Mounted only once measured, so the thumb never slides in from the corner. */}\n      {thumb ? (\n        <span\n          aria-hidden=\"true\"\n          data-slot=\"comment-thread-sort-thumb\"\n          className=\"pointer-events-none absolute top-0 left-0 -z-1 rounded-full bg-accent transition-[translate,width] duration-240 ease-out-quint motion-reduce:transition-none\"\n          style={{\n            width: thumb.width,\n            height: thumb.height,\n            translate: `${thumb.x}px ${thumb.y}px`,\n          }}\n        />\n      ) : null}\n      {children}\n    </div>\n  );\n}\n\nexport type CommentThreadSortOptionProps = Omit<ComponentProps<\"button\">, \"value\"> & {\n  value: string;\n};\n\nexport function CommentThreadSortOption({\n  value,\n  onClick,\n  className,\n  ...props\n}: CommentThreadSortOptionProps) {\n  const context = useThread(\"CommentThreadSortOption\");\n  const checked = context.sort === value;\n  const compact = context.variant === \"compact\";\n  return (\n    // biome-ignore lint/a11y/useSemanticElements: a segment button in a radio group; ToggleGroupItem renders the same role=\"radio\" button.\n    <button\n      type=\"button\"\n      role=\"radio\"\n      aria-checked={checked}\n      data-slot=\"comment-thread-sort-option\"\n      data-state={checked ? \"on\" : \"off\"}\n      className={cn(\n        toggleVariants(),\n        segmentClass,\n        \"min-w-0 cursor-pointer rounded-full border-0 px-2.5 text-[12px] font-medium [transition:background-color_180ms_cubic-bezier(0.23,1,0.32,1),color_120ms_ease-out,scale_140ms_cubic-bezier(0.23,1,0.32,1)] focus-visible:ring-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring focus-visible:outline-solid active:scale-[0.96] motion-reduce:transition-none motion-reduce:active:scale-100\",\n        compact ? \"h-5.5\" : \"h-6\",\n        checked\n          ? \"bg-accent text-foreground hover:bg-accent hover:text-foreground data-[state=on]:text-foreground group-data-ready/sort:bg-transparent group-data-ready/sort:hover:bg-transparent group-data-ready/sort:data-[state=on]:bg-transparent\"\n          : \"bg-transparent text-subtle-foreground hover:bg-transparent hover:text-foreground\",\n        className,\n      )}\n      {...props}\n      tabIndex={checked ? 0 : -1}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) context.setSort(value);\n      }}\n    />\n  );\n}\n\n/** Top-level comments. Nest replies with CommentReplies inside a CommentThreadComment. */\nexport function CommentThreadList({\n  \"aria-label\": label = \"Comments\",\n  className,\n  ...props\n}: ComponentProps<\"div\">) {\n  const { variant } = useThread(\"CommentThreadList\");\n  return (\n    // biome-ignore lint/a11y/useSemanticElements: comments are articles; a labelled group keeps them together without list semantics.\n    <div\n      role=\"group\"\n      aria-label={label}\n      data-slot=\"comment-thread-list\"\n      className={cn(\"grid min-w-0\", variant === \"threaded\" ? \"gap-5\" : \"gap-3\", className)}\n      {...props}\n    />\n  );\n}\n\n/** A Comment sized for the thread. Compose Comment parts inside it. */\nexport function CommentThreadComment(props: Omit<CommentProps, \"variant\">) {\n  const { variant } = useThread(\"CommentThreadComment\");\n  return <Comment {...props} variant={commentVariants[variant]} />;\n}\n\n/** A Reaction Bar sized for the thread. */\nexport function CommentThreadReactions(props: Omit<ReactionBarProps, \"variant\">) {\n  const { variant } = useThread(\"CommentThreadReactions\");\n  return <ReactionBar {...props} variant={reactionVariants[variant]} />;\n}\n\nexport type CommentThreadComposerProps = Omit<ComponentProps<\"form\">, \"onSubmit\"> & {\n  /** Called with the trimmed text. The field clears after submit. */\n  onSubmit: (value: string) => void;\n  /** Accessible label for the text area. */\n  label?: string;\n  placeholder?: string;\n  disabled?: boolean;\n};\n/** A comment field. Enter with Command or Control submits; the send button is disabled while empty. */\nexport function CommentThreadComposer({\n  onSubmit,\n  label = \"Add a comment\",\n  placeholder = \"Add a comment…\",\n  disabled = false,\n  className,\n  ...props\n}: CommentThreadComposerProps) {\n  const { variant } = useThread(\"CommentThreadComposer\");\n  const [value, setValue] = useState(\"\");\n  const compact = variant === \"compact\";\n  const ready = Boolean(value.trim()) && !disabled;\n  const submit = () => {\n    if (!ready) return;\n    onSubmit(value.trim());\n    setValue(\"\");\n  };\n  return (\n    <form\n      data-slot=\"comment-thread-composer\"\n      className={cn(\n        \"flex min-w-0 items-end gap-2 bg-card shadow-[inset_0_0_0_1px_var(--border)] [transition:box-shadow_120ms_ease-out] focus-within:shadow-[inset_0_0_0_1px_var(--border-strong)] motion-reduce:transition-none\",\n        compact ? \"rounded-xl p-1.5\" : \"rounded-[14px] p-2\",\n        className,\n      )}\n      {...props}\n      onSubmit={(event) => {\n        event.preventDefault();\n        submit();\n      }}\n    >\n      <Textarea\n        aria-label={label}\n        placeholder={placeholder}\n        value={value}\n        disabled={disabled}\n        rows={compact ? 1 : 2}\n        onChange={(event) => setValue(event.target.value)}\n        onKeyDown={(event) => {\n          if (event.key === \"Enter\" && (event.metaKey || event.ctrlKey)) {\n            event.preventDefault();\n            submit();\n          }\n        }}\n        className=\"field-sizing-fixed block min-h-0 w-auto min-w-0 flex-1 resize-none rounded-none border-0 bg-transparent px-1.5 py-1.25 text-[13px]/[18px] text-inherit shadow-none outline-none placeholder:text-subtle-foreground focus-visible:ring-0 md:text-[13px]/[18px] dark:bg-transparent\"\n      />\n      <Button\n        type=\"submit\"\n        size=\"icon\"\n        aria-label=\"Post comment\"\n        disabled={!ready}\n        className={cn(\n          \"grid flex-[0_0_auto] place-items-center rounded-full border-0 [transition:background-color_120ms_ease-out,color_120ms_ease-out,scale_140ms_cubic-bezier(0.23,1,0.32,1)] focus-visible:ring-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring focus-visible:outline-solid not-disabled:active:scale-[0.92] disabled:pointer-events-auto disabled:opacity-100 motion-reduce:transition-none motion-reduce:not-disabled:active:scale-100\",\n          compact ? \"size-6\" : \"size-7\",\n          ready\n            ? \"cursor-pointer bg-foreground text-card hover:bg-foreground\"\n            : \"cursor-not-allowed bg-border-strong text-card/70 hover:bg-border-strong\",\n        )}\n      >\n        <ArrowUp strokeWidth={2} aria-hidden=\"true\" className={compact ? \"size-3.5\" : \"size-4\"} />\n      </Button>\n    </form>\n  );\n}\n\nfunction moveRadio(event: KeyboardEvent<HTMLElement>) {\n  const radios = Array.from(\n    event.currentTarget.querySelectorAll<HTMLButtonElement>('[role=\"radio\"]:not(:disabled)'),\n  );\n  const index = radios.indexOf(document.activeElement as HTMLButtonElement);\n  if (index === -1) return;\n  const next = {\n    ArrowRight: index + 1,\n    ArrowDown: index + 1,\n    ArrowLeft: index - 1,\n    ArrowUp: index - 1,\n    Home: 0,\n    End: radios.length - 1,\n  }[event.key];\n  if (next === undefined) return;\n  event.preventDefault();\n  const target = radios[(next + radios.length) % radios.length];\n  target?.focus();\n  target?.click();\n}\n",
      "type": "registry:component",
      "target": "@components/uai/comment-thread.tsx"
    }
  ],
  "categories": [
    "content",
    "community"
  ],
  "type": "registry:block"
}