{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tooltip",
  "title": "Tooltip",
  "description": "A label that grows out of its trigger. Tooltips in a group share one that slides from trigger to trigger, carrying its label.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/glide.json",
    "https://kinoui.com/r/kbd.json",
    "https://kinoui.com/r/popup.json",
    "https://kinoui.com/r/refs.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/tooltip.tsx",
      "content": "'use client'\n\nimport { Kbd } from '@/registry/components/kino/kbd'\nimport {\n  GLIDE_GROUP_CLOSE_DELAY,\n  GlideContent,\n  type GlideGroup,\n  GlideGroupMemory,\n  tooltipBoxClassName,\n  tooltipLabelClassName,\n  TooltipSurface,\n  useGlideGroup,\n  useGlideGroupItem,\n} from '@/registry/lib/glide'\nimport { HOVER_DELAY, POPUP_COLLISION_PADDING } from '@/registry/lib/popup'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { ariaKeyshortcuts } from '@ui-sdk/core'\nimport {\n  createTooltipController,\n  Tooltip as TooltipPrimitive,\n  TooltipContent as TooltipContentPrimitive,\n  type TooltipController,\n  TooltipShortcut as TooltipShortcutPrimitive,\n  TooltipTrigger as TooltipTriggerPrimitive,\n} from '@ui-sdk/react/tooltip'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  use,\n  useId,\n  useEffect,\n  useMemo,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype TooltipContentProps = Omit<\n  ComponentProps<typeof TooltipContentPrimitive>,\n  'className' | 'children'\n> & {\n  className?: string\n  children?: ReactNode\n}\n\n/** Tells the tooltip around a trigger whether the trigger's name says what the label does. */\nconst RepeatsContext = createContext<((repeats: boolean) => void) | null>(null)\n\ninterface Group {\n  glide: GlideGroup<TooltipController<string>, TooltipContentProps>\n  repeats: (id: string, repeats: boolean) => void\n  /** Keeps the group's tooltip closed on one tooltip's trigger while it is `disabled`. */\n  disable: (id: string, disabled: boolean) => void\n  /** The `shortcut` of each tooltip, which the group's tooltip shows for its trigger. */\n  shortcut: (id: string, shortcut: string | undefined) => void\n}\n\nconst GroupContext = createContext<Group | null>(null)\nconst ItemContext = createContext<{ id: string; shortcut?: string } | null>(null)\n\n/**\n * Tooltips that sit together, such as a toolbar's, share one tooltip that\n * slides from trigger to trigger. Each `Tooltip` inside keeps its own\n * trigger and content.\n */\nfunction TooltipGroup({ children }: { children: ReactNode }) {\n  const { group, contentOf } = useGlideGroup<\n    TooltipController<string>,\n    TooltipContentProps\n  >(() => createTooltipController<string>())\n  const [shown, setShown] = useState<string | null>(null)\n  const [labelled, setLabelled] = useState(false)\n  const [repeating, setRepeating] = useState<ReadonlySet<string>>(new Set())\n  const [shortcuts, setShortcuts] = useState<ReadonlyMap<string, string>>(new Map())\n  const [off] = useState(() => new Set<string>())\n  const value = useMemo<Group>(\n    () => ({\n      glide: group,\n      disable: (id, disabled) => {\n        if (disabled) off.add(id)\n        else off.delete(id)\n        if (disabled && group.handle.getPayload() === id) group.handle.close()\n      },\n      shortcut: (id, shortcut) =>\n        setShortcuts((previous) => {\n          if (previous.get(id) === shortcut) return previous\n          const next = new Map(previous)\n          if (shortcut) next.set(id, shortcut)\n          else next.delete(id)\n          return next\n        }),\n      repeats: (id, repeats) =>\n        setRepeating((previous) => {\n          if (previous.has(id) === repeats) return previous\n          const next = new Set(previous)\n          if (repeats) next.add(id)\n          else next.delete(id)\n          return next\n        }),\n    }),\n    [group, off],\n  )\n  // The group's one box is placed as the shown trigger's content asks.\n  const content = shown === null ? undefined : contentOf(shown)\n  const { side, align, sideOffset, alignOffset } = content ?? {}\n  return (\n    <GroupContext value={value}>\n      <GlideGroupMemory>\n        {children}\n        <TooltipPrimitive\n          controller={group.handle}\n          delay={HOVER_DELAY}\n          closeDelay={GLIDE_GROUP_CLOSE_DELAY}\n          // The payload is the opening trigger's by now.\n          onOpenChange={(open, details) => {\n            if (open && off.has(group.handle.getPayload() ?? '')) details.cancel()\n          }}\n          onPayloadChange={(payload, details) => {\n            if (typeof payload === 'string' && off.has(payload)) {\n              details.cancel()\n              group.handle.close()\n              return\n            }\n            if (typeof payload === 'string') setLabelled(true)\n            setShown(typeof payload === 'string' ? payload : null)\n          }}\n          type={shown !== null && repeating.has(shown) ? 'label' : undefined}\n          shortcut={shown === null ? undefined : shortcuts.get(shown)}\n        >\n          {/* One popup for many triggers has no one name an agent could find\n              it by, so the agent bridge leaves it out. Its first label mounts a\n              fresh box, so ui-sdk places it with the label in it. */}\n          <TooltipBox<string>\n            key={labelled ? 'label' : 'rest'}\n            side={side}\n            align={align}\n            sideOffset={sideOffset}\n            alignOffset={alignOffset}\n            data-agent-hidden\n          >\n            {({ payload }) => {\n              const shownContent = payload ? contentOf(payload) : undefined\n              return shownContent ? (\n                <TooltipInside className={shownContent.className}>\n                  {shownContent.children}\n                </TooltipInside>\n              ) : null\n            }}\n          </TooltipBox>\n        </TooltipPrimitive>\n      </GlideGroupMemory>\n    </GroupContext>\n  )\n}\n\n/**\n * One tooltip: a trigger and its content. It waits 200ms before it opens,\n * then the next one opens at once while the pointer moves on. Inside a\n * `TooltipGroup`, the group holds whether it is open, and its props are\n * ignored, except `disabled` and `shortcut`. One open as it turns disabled\n * closes.\n */\nfunction Tooltip({\n  children,\n  delay = HOVER_DELAY,\n  type,\n  disabled = false,\n  shortcut,\n  open: openProp,\n  onOpenChange,\n  ...props\n}: ComponentProps<typeof TooltipPrimitive>) {\n  const group = use(GroupContext)\n  const id = useId()\n  const [repeats, setRepeats] = useState(false)\n  // ui-sdk's `disabled` only stops it opening, so one open as it turns\n  // disabled is closed here, as a group closes it.\n  const [ownOpen, setOwnOpen] = useState(props.defaultOpen ?? false)\n  // Closed for good: enabled again, it waits for a pointer or focus.\n  if (disabled && ownOpen) setOwnOpen(false)\n  useEffect(() => {\n    if (!group) return\n    group.disable(id, disabled)\n    return () => group.disable(id, false)\n  }, [group, id, disabled])\n  useEffect(() => {\n    if (!group) return\n    group.shortcut(id, shortcut)\n    return () => group.shortcut(id, undefined)\n  }, [group, id, shortcut])\n  const item = useMemo(() => ({ id, shortcut }), [id, shortcut])\n  if (group) return <ItemContext value={item}>{children}</ItemContext>\n  return (\n    <TooltipPrimitive\n      delay={delay}\n      type={type ?? (repeats ? 'label' : undefined)}\n      disabled={disabled}\n      shortcut={shortcut}\n      {...props}\n      open={(openProp ?? ownOpen) && !disabled}\n      onOpenChange={(open, details) => {\n        onOpenChange?.(open, details)\n        if (!details.canceled) setOwnOpen(open)\n      }}\n    >\n      <RepeatsContext value={setRepeats}>{children}</RepeatsContext>\n    </TooltipPrimitive>\n  )\n}\n\n/*\n * The label names a trigger with no name of its own, and describes one that\n * has a name. A name that already says what the label does, as an icon\n * button's `aria-label` often does, would be read twice: the label then\n * names it instead. Kept outside any component: the React Compiler can drop\n * a side effect written inside one.\n */\nfunction watchRepeats(trigger: HTMLElement, report: (repeats: boolean) => void) {\n  const check = () => {\n    const by =\n      trigger.getAttribute('aria-describedby') ?? trigger.getAttribute('aria-labelledby')\n    const label = by ? trigger.ownerDocument.getElementById(by) : null\n    if (label) report(repeatsName(trigger.getAttribute('aria-label'), label.textContent))\n  }\n  check()\n  const observer = new MutationObserver(check)\n  observer.observe(trigger, {\n    attributes: true,\n    attributeFilter: ['aria-label', 'aria-describedby', 'aria-labelledby'],\n  })\n  return () => observer.disconnect()\n}\n\nfunction TooltipTrigger({\n  ref,\n  ...props\n}: ComponentProps<typeof TooltipTriggerPrimitive>) {\n  const group = use(GroupContext)\n  const item = use(ItemContext)\n  const id = item?.id\n  const setRepeats = use(RepeatsContext)\n  const watch = useMemo(() => {\n    const report =\n      group && id ? (repeats: boolean) => group.repeats(id, repeats) : setRepeats\n    if (!report) return undefined\n    return (trigger: HTMLElement | null) =>\n      trigger ? watchRepeats(trigger, report) : undefined\n  }, [group, id, setRepeats])\n  const own = useMemo(() => mergeRefs(ref, watch), [ref, watch])\n  return (\n    <TooltipTriggerPrimitive\n      ref={own}\n      {...(group && id\n        ? {\n            controller: group.glide.handle,\n            payload: id,\n            // The group's tooltip has one shortcut, its shown trigger's: each trigger says its own.\n            'aria-keyshortcuts': item?.shortcut\n              ? ariaKeyshortcuts(item.shortcut)\n              : undefined,\n          }\n        : {})}\n      {...props}\n    />\n  )\n}\n\n/**\n * The label. It grows out of its trigger, slides to the next trigger of its\n * group, and folds back in. `className` goes to the label inside the box;\n * every other prop goes to ui-sdk's content.\n */\nfunction TooltipContent(props: TooltipContentProps) {\n  const group = use(GroupContext)\n  const id = use(ItemContext)?.id ?? null\n  // Inside a group, the group's one tooltip shows this content.\n  useGlideGroupItem(group?.glide ?? null, id, props)\n  if (group && id) return null\n  return <TooltipLabel {...props} />\n}\n\n/** The box: one for a tooltip, one for a group, placed by ui-sdk. */\nfunction TooltipBox<Payload>({\n  side = 'top',\n  align = 'center',\n  sideOffset = 6,\n  className,\n  ...props\n}: Omit<ComponentProps<typeof TooltipContentPrimitive<Payload>>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <TooltipContentPrimitive<Payload>\n      side={side}\n      align={align}\n      sideOffset={sideOffset}\n      collisionPadding={POPUP_COLLISION_PADDING}\n      arrowPadding={0}\n      className={cn(tooltipBoxClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** The box with the label in it, for one tooltip. */\nfunction TooltipLabel({ className, children, ...props }: TooltipContentProps) {\n  return (\n    <TooltipBox {...props}>\n      <TooltipInside className={className}>{children}</TooltipInside>\n    </TooltipBox>\n  )\n}\n\n/** What a box holds: its surface and the label, for one tooltip or a group's. */\nfunction TooltipInside({\n  className,\n  children,\n}: {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <>\n      <TooltipSurface />\n      <GlideContent>\n        <div className={cn(tooltipLabelClassName, className)}>{children}</div>\n      </GlideContent>\n    </>\n  )\n}\n\n/**\n * A keyboard shortcut after the label, as keys. Give the tooltip its\n * `shortcut`, such as `Mod+K`, and it is written for the platform and set on\n * the trigger for assistive technology; or pass the keys as children.\n */\nfunction TooltipShortcut({\n  className,\n  children = ({ text }) => (text ? <Kbd>{text}</Kbd> : null),\n  ...props\n}: Omit<ComponentProps<typeof TooltipShortcutPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <TooltipShortcutPrimitive\n      className={cn(\n        'text-background/70 ms-2 font-sans tracking-wide forced-colors:text-[CanvasText]',\n        className,\n      )}\n      {...props}\n    >\n      {children}\n    </TooltipShortcutPrimitive>\n  )\n}\n\n/** Whether a trigger's `aria-label` already says what its tooltip's label does, so describing it would read it twice. */\nfunction repeatsName(name: string | null | undefined, label: string | null | undefined) {\n  if (!name || !label) return false\n  const words = (text: string) => text.trim().replace(/\\s+/g, ' ').toLowerCase()\n  return words(name) === words(label)\n}\n\nexport {\n  Tooltip,\n  TooltipContent,\n  TooltipGroup,\n  TooltipShortcut,\n  TooltipTrigger,\n  repeatsName,\n}\nexport type { TooltipContentProps }\n",
      "type": "registry:component",
      "target": "@components/kino/tooltip.tsx"
    }
  ],
  "type": "registry:component"
}