{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fact-row",
  "type": "registry:ui",
  "title": "Fact Row",
  "description": "One thing the system knows or remembers about a person or a record, shown with where it came from and what the person can do about it: correct it in place, say it is no longer true, or have it removed.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "radix-ui"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/announce",
    "@rivier-ui/button",
    "@rivier-ui/cn",
    "@rivier-ui/input",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/fact-row.tsx",
      "type": "registry:ui",
      "target": "components/ui/fact-row.tsx",
      "content": "\"use client\";\n/**\n * @name fact-row\n * @category ui\n * @description One thing the system knows or remembers about a person or a record, shown with where it came from and what the person can do about it: correct it in place, say it is no longer true, or have it removed.\n * @when Showing people what is held about them and letting them control it: remembered preferences, profile details filled in by an assistant, imported data, settings inferred from use. Every fact gets a source and at least one action. The words of the actions are the project's (the component names none). For plain label and value pairs nobody can change use a description list; for a form use field.\n * @example\n * <FactList aria-label=\"What we remember\">\n *   <FactRow removed={forgotten} removedMessage=\"Diet was forgotten\" restoredMessage=\"Diet was restored\">\n *     <FactLabel>Diet</FactLabel>\n *     <FactValue>{diet}</FactValue>\n *     <FactSource>\n *       <SourceChip icon={<MessageSquare />}>You said this on 3 May</SourceChip>\n *     </FactSource>\n *     <FactActions aria-label=\"Diet\">\n *       <FactEditTrigger>Edit</FactEditTrigger>\n *       <FactAction onClick={endDiet}>Not true any more</FactAction>\n *       <FactAction onClick={() => setForgotten(true)}>Forget this</FactAction>\n *     </FactActions>\n *     <FactEditor aria-label=\"Diet\" defaultValue={diet} onSave={setDiet} savedMessage=\"Diet was changed\" />\n *     <FactRemoved>\n *       Diet was forgotten. <FactUndo onClick={() => setForgotten(false)}>Undo</FactUndo>\n *     </FactRemoved>\n *   </FactRow>\n * </FactList>\n */\nimport { Slot } from \"radix-ui\";\nimport { createContext, use, useEffect, useRef, useState, type ComponentProps, type FormEvent, type KeyboardEvent, type ReactNode } from \"react\";\nimport { buttonVariants } from \"@/components/ui/button\";\nimport { inputVariants } from \"@/components/ui/input\";\nimport { announce } from \"@/lib/announce\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\ntype FactContextValue = {\n  editing: boolean;\n  setEditing: (editing: boolean) => void;\n  removed: boolean;\n  /** Called as the editor closes, so that the Edit button takes focus back as it returns. */\n  askForFocus: () => void;\n  /** True once after askForFocus: the Edit button calls it as it mounts. */\n  takeFocus: () => boolean;\n};\n\nconst FactContext = createContext<FactContextValue | null>(null);\n\nfunction useFact(part: string): FactContextValue {\n  const context = use(FactContext);\n  if (!context) throw new Error(`<${part}> must be used inside <FactRow>.`);\n  return context;\n}\n\n/** A list of facts. Name it with `aria-label` or `aria-labelledby`. It is a container, so rows lay themselves out by its width. */\nexport function FactList({ className, ...props }: ComponentProps<\"ul\">) {\n  return <ul data-slot=\"fact-list\" className={cn(\"@container flex w-full min-w-0 flex-col divide-y divide-border\", className)} {...props} />;\n}\n\nexport type FactRowProps = ComponentProps<\"li\"> & {\n  /** Whether the value is being edited (controlled). */\n  editing?: boolean;\n  defaultEditing?: boolean;\n  onEditingChange?: (editing: boolean) => void;\n  /** The fact has been removed: the row becomes its FactRemoved stub (one line, with a way back) instead of vanishing. */\n  removed?: boolean;\n  /** Said to screen-reader users when the fact becomes removed (\"Diet was forgotten\"). */\n  removedMessage?: string;\n  /** Said when a removed fact comes back (\"Diet was restored\"). */\n  restoredMessage?: string;\n};\n\n/**\n * One fact. Its parts are placed by name: label, value and source at the start, actions at the end (below\n * them when the list is narrow). While `editing`, FactValue and FactActions give way to FactEditor; while\n * `removed`, everything gives way to FactRemoved. Focus is kept: into the field when editing starts, back\n * to the Edit button when it ends, onto the stub's Undo when the row is removed from under the focus.\n */\nexport function FactRow({ className, editing, defaultEditing = false, onEditingChange, removed = false, removedMessage, restoredMessage, children, ...props }: FactRowProps) {\n  const [isEditing, setEditing] = useControllableState({ value: editing, defaultValue: defaultEditing, onChange: onEditingChange });\n  const focusOwed = useRef(false);\n  const askForFocus = (): void => {\n    focusOwed.current = true;\n  };\n  const takeFocus = (): boolean => {\n    const owed = focusOwed.current;\n    focusOwed.current = false;\n    return owed;\n  };\n  const rowRef = useRef<HTMLLIElement>(null);\n  const wasRemoved = useRef(removed);\n\n  useEffect(() => {\n    if (wasRemoved.current === removed) return;\n    wasRemoved.current = removed;\n    const message = removed ? removedMessage : restoredMessage;\n    if (message) announce(message);\n    // The control that had focus has just left the page with its part: give focus to what replaced it.\n    if (document.activeElement === document.body || document.activeElement === null) {\n      const row = rowRef.current;\n      const next = removed\n        ? (row?.querySelector<HTMLElement>(\"[data-slot=fact-undo]\") ?? row?.querySelector<HTMLElement>(\"[data-slot=fact-removed]\"))\n        : row?.querySelector<HTMLElement>(\"[data-slot=fact-actions] button, [data-slot=fact-actions] a\");\n      next?.focus();\n    }\n  }, [removed, removedMessage, restoredMessage]);\n\n  return (\n    <FactContext value={{ editing: isEditing && !removed, setEditing, removed, askForFocus, takeFocus }}>\n      <li\n        ref={rowRef}\n        data-slot=\"fact-row\"\n        data-editing={isEditing && !removed ? \"\" : undefined}\n        data-removed={removed ? \"\" : undefined}\n        className={cn(\n          \"grid min-w-0 grid-cols-1 py-4 text-sm [grid-template-areas:'label''value''source''actions'] data-[removed]:block data-[removed]:py-3 @lg:grid-cols-[minmax(0,1fr)_auto] @lg:gap-x-6 @lg:[grid-template-areas:'label_actions''value_actions''source_actions']\",\n          className,\n        )}\n        {...props}\n      >\n        {children}\n      </li>\n    </FactContext>\n  );\n}\n\n/** What the fact is about (\"Diet\", \"Home airport\"). */\nexport function FactLabel({ className, ...props }: ComponentProps<\"span\">) {\n  const { removed } = useFact(\"FactLabel\");\n  if (removed) return null;\n  return <span data-slot=\"fact-label\" className={cn(\"text-xs font-medium text-muted-foreground [grid-area:label]\", className)} {...props} />;\n}\n\n/** The fact itself. It gives way to FactEditor while editing. */\nexport function FactValue({ className, ...props }: ComponentProps<\"span\">) {\n  const { removed, editing } = useFact(\"FactValue\");\n  if (removed || editing) return null;\n  return <span data-slot=\"fact-value\" className={cn(\"mt-0.5 min-w-0 text-base break-words text-foreground [grid-area:value]\", className)} {...props} />;\n}\n\n/** Where the fact came from: one or more SourceChip. */\nexport function FactSource({ className, ...props }: ComponentProps<\"div\">) {\n  const { removed } = useFact(\"FactSource\");\n  if (removed) return null;\n  return <div data-slot=\"fact-source\" className={cn(\"mt-2 flex min-w-0 flex-wrap items-center gap-1.5 [grid-area:source]\", className)} {...props} />;\n}\n\nexport type SourceChipProps = ComponentProps<\"span\"> & {\n  /** A small icon for the kind of source: a conversation, a form, an import, a guess. Decoration: the words carry the meaning. */\n  icon?: ReactNode;\n  /** Render the child (a button or a link that opens the source) with the chip's look. */\n  asChild?: boolean;\n};\n\n/**\n * Says where a fact came from, in the project's words (\"You said this on 3 May\", \"From your booking in\n * June\", \"Guessed from what you chose\"). To let people open the source, pass a button or a link with\n * `asChild`: it gets a hover ground and a touch target of at least 40 px.\n */\nexport function SourceChip({ icon, asChild = false, className, children, ...props }: SourceChipProps) {\n  const Comp = asChild ? Slot.Root : \"span\";\n  return (\n    <Comp\n      data-slot=\"source-chip\"\n      className={cn(\n        \"relative inline-flex min-h-6 max-w-full items-center gap-1.5 rounded-control bg-muted px-2 py-0.5 text-start text-xs text-foreground [&_svg]:size-3.5 [&_svg]:shrink-0 [&_svg]:text-muted-foreground\",\n        \"[a&]:underline [a&]:underline-offset-2 [a&,button&]:cursor-pointer [a&,button&]:transition-[background-color] [a&,button&]:duration-instant [a&,button&]:ease-out [a&,button&]:after:absolute [a&,button&]:after:-inset-x-1 [a&,button&]:after:-inset-y-2 [a&,button&]:hover:bg-accent [a&,button&]:motion-reduce:transition-none\",\n        className,\n      )}\n      {...props}\n    >\n      {icon ? (\n        <span aria-hidden=\"true\" className=\"contents\">\n          {icon}\n        </span>\n      ) : null}\n      {asChild ? <Slot.Slottable>{children}</Slot.Slottable> : children}\n    </Comp>\n  );\n}\n\n/** The actions on one fact, as a named group: give it an `aria-label` that says which fact (\"Diet\"). */\nexport function FactActions({ className, ...props }: ComponentProps<\"div\">) {\n  const { removed, editing } = useFact(\"FactActions\");\n  if (removed || editing) return null;\n  return (\n    <div\n      role=\"group\"\n      data-slot=\"fact-actions\"\n      className={cn(\"-ms-2.5 mt-2 flex min-w-0 flex-wrap items-center gap-x-1 gap-y-1 [grid-area:actions] @lg:ms-0 @lg:mt-0 @lg:justify-end @lg:self-center\", className)}\n      {...props}\n    />\n  );\n}\n\nconst actionLook = cn(buttonVariants({ variant: \"ghost\", size: \"sm\" }), \"px-2.5 pointer-coarse:h-control\");\n\nexport type FactActionProps = ComponentProps<\"button\"> & {\n  /** \"destructive\" for an action that removes the fact for good. */\n  variant?: \"default\" | \"destructive\";\n};\n\n/** One action. Its words are the project's: \"Not true any more\", \"Forget this\". */\nexport function FactAction({ className, variant = \"default\", type = \"button\", ...props }: FactActionProps) {\n  return <button type={type} data-slot=\"fact-action\" className={cn(actionLook, variant === \"destructive\" && \"text-destructive\", className)} {...props} />;\n}\n\n/** The action that starts editing. It takes focus back when editing ends. */\nexport function FactEditTrigger({ className, onClick, type = \"button\", ...props }: ComponentProps<\"button\">) {\n  const { setEditing, takeFocus } = useFact(\"FactEditTrigger\");\n  const ref = useRef<HTMLButtonElement>(null);\n  useEffect(() => {\n    if (takeFocus()) ref.current?.focus();\n    // Once, as the button mounts: that is when editing has just ended.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n  return (\n    <button\n      ref={ref}\n      type={type}\n      data-slot=\"fact-edit-trigger\"\n      className={cn(actionLook, className)}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) setEditing(true);\n      }}\n      {...props}\n    />\n  );\n}\n\nexport type FactEditorProps = Omit<ComponentProps<\"form\">, \"onSubmit\" | \"defaultValue\"> & {\n  /** What the field starts with: the fact's current value. */\n  defaultValue?: string;\n  /** Called with the new value when it is saved. Not called when nothing changed or the field is empty. */\n  onSave?: (value: string) => void;\n  onCancel?: () => void;\n  /** Said to screen-reader users after saving (\"Diet was changed\"). */\n  savedMessage?: string;\n  saveLabel?: ReactNode;\n  cancelLabel?: ReactNode;\n  /** Props for the field: `placeholder`, `maxLength`, `inputMode`, `autoComplete`. */\n  inputProps?: Omit<ComponentProps<\"input\">, \"value\" | \"defaultValue\" | \"onChange\">;\n};\n\n/**\n * The value as a field, shown only while the row is editing. Enter saves, Escape cancels. Name the field\n * with `aria-label` or `aria-labelledby` (they go to the input). An empty value cannot be saved: removing a\n * fact is a different action, with its own button.\n */\nexport function FactEditor({\n  className,\n  defaultValue = \"\",\n  onSave,\n  onCancel,\n  savedMessage,\n  saveLabel = \"Save\",\n  cancelLabel = \"Cancel\",\n  inputProps,\n  \"aria-label\": ariaLabel,\n  \"aria-labelledby\": ariaLabelledBy,\n  ...props\n}: FactEditorProps) {\n  const { editing } = useFact(\"FactEditor\");\n  if (!editing) return null;\n  return (\n    <FactEditorForm\n      className={className}\n      defaultValue={defaultValue}\n      onSave={onSave}\n      onCancel={onCancel}\n      savedMessage={savedMessage}\n      saveLabel={saveLabel}\n      cancelLabel={cancelLabel}\n      inputProps={inputProps}\n      aria-label={ariaLabel}\n      aria-labelledby={ariaLabelledBy}\n      {...props}\n    />\n  );\n}\n\n/** Mounted afresh each time editing starts, so the field always starts from the current value. */\nfunction FactEditorForm({ className, defaultValue = \"\", onSave, onCancel, savedMessage, saveLabel, cancelLabel, inputProps, \"aria-label\": ariaLabel, \"aria-labelledby\": ariaLabelledBy, ...props }: FactEditorProps) {\n  const { setEditing, askForFocus } = useFact(\"FactEditor\");\n  const [draft, setDraft] = useState(defaultValue);\n  const inputRef = useRef<HTMLInputElement>(null);\n\n  useEffect(() => {\n    inputRef.current?.focus();\n    inputRef.current?.select();\n  }, []);\n\n  const close = (): void => {\n    askForFocus();\n    setEditing(false);\n  };\n  const cancel = (): void => {\n    onCancel?.();\n    close();\n  };\n  const save = (event: FormEvent<HTMLFormElement>): void => {\n    event.preventDefault();\n    const value = draft.trim();\n    if (value === \"\") return;\n    if (value !== defaultValue) {\n      onSave?.(value);\n      if (savedMessage) announce(savedMessage);\n    }\n    close();\n  };\n  const onKeyDown = (event: KeyboardEvent<HTMLFormElement>): void => {\n    if (event.key !== \"Escape\") return;\n    // Escape belongs to the editor: it must not also close a sheet or dialog the list sits in.\n    event.preventDefault();\n    event.stopPropagation();\n    cancel();\n  };\n\n  return (\n    <form data-slot=\"fact-editor\" noValidate onSubmit={save} onKeyDown={onKeyDown} className={cn(\"col-span-full mt-1.5 flex min-w-0 flex-wrap items-center gap-2 [grid-area:value]\", className)} {...props}>\n      <input\n        ref={inputRef}\n        type=\"text\"\n        data-slot=\"fact-editor-input\"\n        aria-label={ariaLabel}\n        aria-labelledby={ariaLabelledBy}\n        autoComplete=\"off\"\n        {...inputProps}\n        value={draft}\n        onChange={(event) => setDraft(event.target.value)}\n        className={cn(inputVariants({ size: \"md\" }), \"min-w-0 flex-1 basis-48\", inputProps?.className)}\n      />\n      <div className=\"flex shrink-0 items-center gap-2\">\n        <button type=\"submit\" data-slot=\"fact-editor-save\" disabled={draft.trim() === \"\"} className={buttonVariants({ variant: \"solid\", size: \"md\" })}>\n          {saveLabel}\n        </button>\n        <button type=\"button\" data-slot=\"fact-editor-cancel\" onClick={cancel} className={buttonVariants({ variant: \"ghost\", size: \"md\" })}>\n          {cancelLabel}\n        </button>\n      </div>\n    </form>\n  );\n}\n\n/**\n * What is left of a removed fact: one line that says what happened, with a way back (FactUndo). Shown only\n * while the row is `removed`. Drop the row from the list for good once the removal can no longer be undone.\n */\nexport function FactRemoved({ className, ...props }: ComponentProps<\"p\">) {\n  const { removed } = useFact(\"FactRemoved\");\n  if (!removed) return null;\n  return <p data-slot=\"fact-removed\" tabIndex={-1} className={cn(\"flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 text-sm text-muted-foreground\", className)} {...props} />;\n}\n\n/** Brings a removed fact back. Its word is the project's (\"Undo\"). */\nexport function FactUndo({ className, type = \"button\", ...props }: ComponentProps<\"button\">) {\n  return <button type={type} data-slot=\"fact-undo\" className={cn(actionLook, \"text-foreground underline underline-offset-4\", className)} {...props} />;\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Showing people what is held about them and letting them control it: remembered preferences, profile details filled in by an assistant, imported data, settings inferred from use. Every fact gets a source and at least one action. The words of the actions are the project's (the component names none). For plain label and value pairs nobody can change use a description list; for a form use field.",
    "example": "<FactList aria-label=\"What we remember\">\n  <FactRow removed={forgotten} removedMessage=\"Diet was forgotten\" restoredMessage=\"Diet was restored\">\n    <FactLabel>Diet</FactLabel>\n    <FactValue>{diet}</FactValue>\n    <FactSource>\n      <SourceChip icon={<MessageSquare />}>You said this on 3 May</SourceChip>\n    </FactSource>\n    <FactActions aria-label=\"Diet\">\n      <FactEditTrigger>Edit</FactEditTrigger>\n      <FactAction onClick={endDiet}>Not true any more</FactAction>\n      <FactAction onClick={() => setForgotten(true)}>Forget this</FactAction>\n    </FactActions>\n    <FactEditor aria-label=\"Diet\" defaultValue={diet} onSave={setDiet} savedMessage=\"Diet was changed\" />\n    <FactRemoved>\n      Diet was forgotten. <FactUndo onClick={() => setForgotten(false)}>Undo</FactUndo>\n    </FactRemoved>\n  </FactRow>\n</FactList>",
    "client": true
  }
}
