{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "navigation-menu",
  "title": "Navigation menu",
  "description": "A site header's menu whose triggers share one panel: it grows out of the trigger, travels and resizes as the pointer moves along, and new content slides in from the side it moved towards. A highlight glides along the triggers and between the links inside, and a panel can show the picture of the link it is on.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/glide.json",
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/popup.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/navigation-menu.tsx",
      "content": "'use client'\n\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport {\n  GLIDE_GROUP_CLOSE_DELAY,\n  GlideClip,\n  GlideContent,\n  GlideGroupMemory,\n  type GlideInstant,\n  glidePopupClassName,\n  GlideSurface,\n} from '@/registry/lib/glide'\nimport {\n  followPick,\n  followPointer,\n  highlightClassName,\n  itemMatching,\n  pickClassName,\n  rowTintClassName,\n} from '@/registry/lib/highlight'\nimport { HOVER_DELAY, POPUP_COLLISION_PADDING } from '@/registry/lib/popup'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { transitionsSettled, withoutTransition } from '@/registry/lib/resize'\nimport { edgeFadeClassName, useEdgeFades } from '@/registry/lib/scroll'\nimport {\n  NavigationMenuContent as NavigationMenuContentPrimitive,\n  NavigationMenuItem as NavigationMenuItemPrimitive,\n  NavigationMenuLink as NavigationMenuLinkPrimitive,\n  NavigationMenuList as NavigationMenuListPrimitive,\n  NavigationMenu as NavigationMenuPrimitive,\n  NavigationMenuTrigger as NavigationMenuTriggerPrimitive,\n  NavigationMenuViewport as NavigationMenuViewportPrimitive,\n} from '@ui-sdk/react/navigation-menu'\nimport {\n  type ComponentProps,\n  createContext,\n  type Dispatch,\n  type ReactNode,\n  type SetStateAction,\n  use,\n  useCallback,\n  useId,\n  useMemo,\n  useState,\n} from 'react'\nimport { createPortal } from 'react-dom'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A site header's menu: its triggers share one panel, which grows out of the\n * trigger, travels to the next one and resizes as the pointer moves along, and\n * folds back in on close. New content slides in from the side the pointer\n * moved towards. A highlight glides between the triggers, and another between\n * the links inside the panel. The link to the page you are on keeps a tint,\n * which travels to the next one you go to. A panel can hold a preview that\n * shows the picture of the link the highlight is on, and a picture settles\n * from a little zoomed in as it comes into view or as its link is pointed at.\n */\n\n/*\n * The highlight's layer, in the list or a panel: `as` is `li` inside the\n * list, so the list holds only list items. It goes to the item under the\n * pointer or with visible focus, else to the `current` one.\n */\nfunction Highlight({\n  items,\n  current,\n  as: Element = 'span',\n}: {\n  items: string\n  current?: string\n  as?: 'span' | 'li'\n}) {\n  const track = useCallback(\n    (layer: HTMLElement | null) => {\n      const area = layer?.parentElement\n      if (!layer || !area) return\n      return followPointer(area, layer, {\n        itemOf: itemMatching(area, items),\n        current,\n        focus: true,\n      })\n    },\n    [items, current],\n  )\n  return (\n    <Element\n      ref={track}\n      aria-hidden\n      data-slot='navigation-menu-highlight'\n      className={cn(\n        highlightClassName,\n        'bg-muted -z-10 list-none forced-colors:bg-[Highlight] forced-colors:forced-color-adjust-none',\n      )}\n    />\n  )\n}\n\n/*\n * The tint under the link to the page you are on, as the Sidebar marks its\n * current page: over the highlight, so the two add up when the pointer is on\n * it, and it moves on `morph`, as the Tabs' pill does. Forced colours\n * underline the link instead, since the highlight takes the system's colour.\n */\nfunction Current({ items }: { items: string }) {\n  const track = useCallback(\n    (layer: HTMLLIElement | null) => {\n      const area = layer?.parentElement\n      if (!layer || !area) return\n      return followPick(area, layer, {\n        picked: () => area.querySelector<HTMLElement>(items),\n        attributes: ['data-current'],\n      })\n    },\n    [items],\n  )\n  return (\n    <li\n      ref={track}\n      aria-hidden\n      data-slot='navigation-menu-current'\n      className={cn(\n        pickClassName,\n        rowTintClassName,\n        '-z-10 list-none forced-colors:hidden',\n      )}\n    />\n  )\n}\n\n/**\n * The menu and its one panel, ui-sdk's viewport. A hover waits 200ms before\n * it opens the panel, as every popup that opens on hover does. Every prop\n * goes to ui-sdk's NavigationMenu. New content slides in from the side the\n * pointer moved towards, by `--navigation-menu-slide` (28px by default).\n */\nfunction NavigationMenu({\n  className,\n  children,\n  delay = HOVER_DELAY,\n  closeDelay = GLIDE_GROUP_CLOSE_DELAY,\n  ...props\n}: Omit<ComponentProps<typeof NavigationMenuPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  // Only a panel open on first paint skips the grow.\n  const [instant] = useState<GlideInstant>(() => {\n    let openOnMount = (props.value ?? props.defaultValue ?? null) !== null\n    const instant: GlideInstant = ({ first }) => {\n      const skip = first && openOnMount\n      if (first) openOnMount = false\n      return skip\n    }\n    return instant\n  })\n  return (\n    <GlideGroupMemory>\n      <NavigationMenuPrimitive\n        delay={delay}\n        closeDelay={closeDelay}\n        className={cn('relative flex max-w-max items-center', className)}\n        {...props}\n      >\n        {children}\n        <NavigationMenuViewportPrimitive\n          side='bottom'\n          align='center'\n          sideOffset={8}\n          collisionPadding={POPUP_COLLISION_PADDING}\n          arrowPadding={0}\n          className={cn(\n            glidePopupClassName,\n            // ui-sdk renders each panel into it and sizes it to the open one.\n            'text-popover-foreground h-(--viewport-height) w-(--viewport-width) max-w-(--available-width) text-sm outline-hidden [--glide-radius:var(--surface-radius)] [--popup-available-height:var(--available-height)] data-ending-style:delay-[calc(var(--motion-exit-duration)*0.35)]',\n          )}\n        >\n          <GlideSurface\n            fill='bg-popover forced-colors:bg-[Canvas]'\n            ring='bg-foreground/10 forced-colors:bg-[CanvasText]'\n            shadow\n          />\n          {/* Opened again soon after it folded, it grows from where it went. */}\n          <GlideContent reopen='grow' instant={instant} />\n        </NavigationMenuViewportPrimitive>\n      </NavigationMenuPrimitive>\n    </GlideGroupMemory>\n  )\n}\n\n/**\n * The row of triggers and links, with one highlight that glides along it, and\n * a tint under the link marked `current`.\n */\nfunction NavigationMenuList({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof NavigationMenuListPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <NavigationMenuListPrimitive\n      className={cn('relative isolate flex list-none items-center gap-0.5', className)}\n      {...props}\n    >\n      <Highlight\n        as='li'\n        items='[data-slot=navigation-menu-trigger],[data-slot=navigation-menu-link]'\n        current='[data-slot=navigation-menu-trigger][data-open]'\n      />\n      <Current items='[data-slot=navigation-menu-link][data-current]' />\n      {children}\n    </NavigationMenuListPrimitive>\n  )\n}\n\nfunction NavigationMenuItem({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof NavigationMenuItemPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return <NavigationMenuItemPrimitive className={cn('relative', className)} {...props} />\n}\n\nconst listItemClassName =\n  'focus-visible:ring-ring/50 relative flex h-(--control-height) items-center gap-1 rounded-(--control-radius) px-(--control-padding) text-sm font-medium whitespace-nowrap select-none focus-visible:ring-3 focus-visible:outline-hidden data-disabled:pointer-events-none data-disabled:opacity-50 forced-colors:data-current:underline forced-colors:data-current:underline-offset-4 forced-colors:data-glided:text-[HighlightText] forced-colors:data-glided:forced-color-adjust-none forced-colors:data-glided:**:text-[HighlightText]'\n\nfunction NavigationMenuTrigger({\n  className,\n  children,\n  ...props\n}: Omit<\n  ComponentProps<typeof NavigationMenuTriggerPrimitive>,\n  'className' | 'children'\n> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <NavigationMenuTriggerPrimitive\n      className={cn('group/navigation-menu-trigger', listItemClassName, className)}\n      {...props}\n    >\n      {children}\n      <span\n        aria-hidden\n        className=\"text-muted-foreground transition-transform duration-(--motion-layout-duration) ease-(--motion-layout-easing) group-data-open/navigation-menu-trigger:rotate-180 motion-reduce:transition-none [&_svg:not([class*='size-'])]:size-4\"\n      >\n        <IconPlaceholder\n          lucide='ChevronDownIcon'\n          tabler='IconChevronDown'\n          hugeicons='ArrowDown01Icon'\n          phosphor='CaretDownIcon'\n          remixicon='RiArrowDownSLine'\n          aria-hidden\n        />\n      </span>\n    </NavigationMenuTriggerPrimitive>\n  )\n}\n\n/** Inside a panel: its preview's frame, where each link's picture is drawn, once there is one. */\nconst PanelContext = createContext<{\n  frame: HTMLElement | null\n  setFrame: Dispatch<SetStateAction<HTMLElement | null>>\n} | null>(null)\n\n/*\n * A panel as ui-sdk renders it into the viewport: at the viewport's corner,\n * at its own size, which ui-sdk measures, inside a layer that clips it to\n * the glide's box.\n */\nfunction ContentFrame({ ref, ...props }: ComponentProps<'div'>) {\n  return (\n    <GlideClip>\n      <div ref={ref} {...props} />\n    </GlideClip>\n  )\n}\n\n/**\n * A trigger's panel. New content slides in from the side the pointer moved\n * towards while the old leaves the other way, faster; on the first open and\n * on close it only fades. A panel taller than the space below scrolls.\n * `className` goes to the panel's layout inside; every other prop goes to\n * ui-sdk's NavigationMenuContent.\n */\nfunction NavigationMenuContent({\n  className,\n  children,\n  ref,\n  ...props\n}: Omit<\n  ComponentProps<typeof NavigationMenuContentPrimitive>,\n  'className' | 'children'\n> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const fades = useEdgeFades<HTMLDivElement>()\n  const refs = useMemo(() => mergeRefs(fades, ref), [fades, ref])\n  const [frame, setFrame] = useState<HTMLElement | null>(null)\n  const panel = useMemo(() => ({ frame, setFrame }), [frame])\n  return (\n    <NavigationMenuContentPrimitive\n      ref={refs}\n      render={<ContentFrame />}\n      className={cn(\n        'scrollbar-thin absolute start-0 top-0 max-h-(--popup-available-height) overflow-y-auto overscroll-contain',\n        edgeFadeClassName,\n        'transition-[opacity,translate] duration-(--motion-layout-duration) ease-(--motion-layout-easing) group-data-[glide=open]/popup:data-open:delay-[calc(var(--motion-morph-duration)*0.15)] motion-reduce:transition-opacity',\n        'data-starting-style:opacity-0 data-ending-style:opacity-0 data-ending-style:duration-[calc(var(--motion-exit-duration)*0.5)] data-ending-style:ease-(--motion-exit-easing)',\n        // ui-sdk marks only the viewport as it closes, so the open panel fades with it.\n        'group-data-ending-style/popup:opacity-0 group-data-ending-style/popup:duration-[calc(var(--motion-exit-duration)*0.5)] group-data-ending-style/popup:ease-(--motion-exit-easing)',\n        // ui-sdk names the way in logical terms: the end is on the right, or on the left in right to left.\n        'data-[motion=from-end]:data-starting-style:translate-x-(--navigation-menu-slide,28px) data-[motion=from-start]:data-starting-style:-translate-x-(--navigation-menu-slide,28px) data-[motion=to-start]:data-ending-style:-translate-x-(--navigation-menu-slide,28px) data-[motion=to-end]:data-ending-style:translate-x-(--navigation-menu-slide,28px)',\n        'rtl:data-[motion=from-end]:data-starting-style:-translate-x-(--navigation-menu-slide,28px) rtl:data-[motion=from-start]:data-starting-style:translate-x-(--navigation-menu-slide,28px) rtl:data-[motion=to-start]:data-ending-style:translate-x-(--navigation-menu-slide,28px) rtl:data-[motion=to-end]:data-ending-style:-translate-x-(--navigation-menu-slide,28px)',\n      )}\n      {...props}\n    >\n      <PanelContext value={panel}>\n        {/* The 8px around the links is the highlight's margin, so it stays the same at every density. */}\n        <div\n          data-slot='navigation-menu-panel'\n          className={cn('relative isolate w-72 max-w-full p-2', className)}\n        >\n          <Highlight items='[data-slot=navigation-menu-link]' />\n          {children}\n        </div>\n      </PanelContext>\n    </NavigationMenuContentPrimitive>\n  )\n}\n\n/**\n * A link: in the list, styled as a trigger; in a panel, a block the highlight\n * glides to, whose icons brighten as it arrives. It closes the menu when\n * followed, and inside a panel its `preview` shows in the panel's\n * `NavigationMenuPreview` while the highlight is on it. `current` marks the\n * page you are on.\n */\nfunction NavigationMenuLink({\n  className,\n  preview,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof NavigationMenuLinkPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n  /** What the panel's preview shows for this link, such as a `NavigationMenuImage`. */\n  preview?: ReactNode\n}) {\n  const panel = use(PanelContext)\n  const id = useId()\n  const frame = preview ? panel?.frame : null\n  return (\n    <>\n      <NavigationMenuLinkPrimitive\n        data-preview-id={preview && panel ? id : undefined}\n        className={cn(\n          panel\n            ? \"focus-visible:ring-ring/50 flex min-w-0 flex-col gap-1 rounded-(--list-radius) p-(--control-padding) focus-visible:ring-3 focus-visible:outline-hidden forced-colors:data-glided:text-[HighlightText] forced-colors:data-glided:forced-color-adjust-none forced-colors:data-glided:**:text-[HighlightText] [&_svg]:transition-colors [&_svg]:duration-(--motion-layout-duration) [&_svg]:ease-(--motion-layout-fade-easing) [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground data-glided:[&_svg:not([class*='text-'])]:text-foreground [&_.text-muted-foreground]:transition-colors [&_.text-muted-foreground]:duration-(--motion-layout-duration) [&_.text-muted-foreground]:ease-(--motion-layout-fade-easing) data-glided:[&_.text-muted-foreground]:text-foreground/80 forced-colors:data-glided:[&_.text-muted-foreground]:text-[HighlightText]\"\n            : listItemClassName,\n          className,\n        )}\n        {...props}\n      >\n        {children}\n      </NavigationMenuLinkPrimitive>\n      {/* Beside the link, not in it: the picture is drawn in the preview, so its events never reach the link. */}\n      {frame\n        ? createPortal(\n            <span\n              data-slot='navigation-menu-preview-item'\n              data-preview-for={id}\n              // Set once: the preview moves it from here, without a render.\n              data-preview='hidden'\n              className={previewItemClassName}\n            >\n              {preview}\n            </span>,\n            frame,\n          )\n        : null}\n    </>\n  )\n}\n\n/*\n * The picture that shows fades in on top of the ones before, which stay whole\n * under it until it has, so the frame never shows through, and then go at\n * once. Going back to one still whole under the others, those fade off it\n * on `exit` (`leaving`).\n */\nconst previewItemClassName =\n  'absolute inset-0 transition-opacity duration-(--motion-layout-duration) ease-(--motion-layout-fade-easing) data-[preview=hidden]:opacity-0 data-[preview=hidden]:transition-none data-[preview=leaving]:opacity-0 data-[preview=leaving]:duration-(--motion-exit-duration)'\n\n/**\n * Shows in `frame` the picture of the link the highlight is on in `area`,\n * and the first link's until then. Kept outside any component: the React\n * Compiler can drop the forced style read it makes.\n */\nfunction followPreview(area: HTMLElement, frame: HTMLElement) {\n  // The links whose pictures are drawn, the latest first.\n  let stack: string[] = []\n  const layers = () =>\n    Array.from(frame.querySelectorAll<HTMLElement>(':scope > [data-preview-for]'))\n  const restack = () => {\n    for (const layer of layers()) {\n      const at = stack.indexOf(layer.dataset.previewFor ?? '')\n      layer.style.zIndex = at < 0 ? '' : String(stack.length - at)\n    }\n  }\n  const show = (id: string | null | undefined, instant = false) => {\n    if (!id || stack[0] === id) return\n    const layer = layers().find((each) => each.dataset.previewFor === id)\n    if (!layer) return\n    const above = previewAbove(stack, id)\n    stack = previewStack(stack, id)\n    if (layer.dataset.preview === 'under') {\n      // Still whole under the ones it gave way to: they fade off it, so the\n      // change turns back from where it is.\n      for (const other of layers()) {\n        if (above.includes(other.dataset.previewFor ?? ''))\n          other.dataset.preview = 'leaving'\n      }\n      layer.dataset.preview = 'shown'\n    } else {\n      restack()\n      for (const other of layers()) {\n        if (other !== layer && other.dataset.preview === 'shown')\n          other.dataset.preview = 'under'\n      }\n      if (instant) {\n        withoutTransition(layer, () => (layer.dataset.preview = 'shown'), {\n          subtree: true,\n        })\n      } else layer.dataset.preview = 'shown'\n    }\n    void transitionsSettled(frame, { subtree: true }).then(() => {\n      if (stack[0] !== id) return\n      for (const other of layers()) if (other !== layer) other.dataset.preview = 'hidden'\n      stack = [id]\n      restack()\n    })\n  }\n  const linkSelector = '[data-slot=navigation-menu-link][data-preview-id]'\n  const glides = new MutationObserver(() => {\n    if (stack.length === 0) {\n      show(area.querySelector(linkSelector)?.getAttribute('data-preview-id'), true)\n    }\n    show(\n      area.querySelector(`${linkSelector}[data-glided]`)?.getAttribute('data-preview-id'),\n    )\n  })\n  // A link's picture arrives a render after the frame, and the highlight marks its link.\n  glides.observe(frame, { childList: true })\n  glides.observe(area, { subtree: true, attributeFilter: ['data-glided'] })\n  return () => glides.disconnect()\n}\n\n/**\n * A frame beside a panel's links that shows the `preview` of the link the\n * highlight is on: the first link's until one is pointed at or focused, and\n * the last one's after the highlight leaves. Each new picture fades in over\n * the last. It is a picture only, so it is hidden from assistive technology\n * and the pointer passes through it. Give it a size in the panel's layout.\n */\nfunction NavigationMenuPreview({ className, ref, ...props }: ComponentProps<'div'>) {\n  const setFrame = use(PanelContext)?.setFrame\n  const own = useCallback(\n    (frame: HTMLDivElement | null) => {\n      const area = frame?.closest<HTMLElement>('[data-slot=navigation-menu-panel]')\n      if (!frame || !area || !setFrame) return\n      setFrame(frame)\n      const stop = followPreview(area, frame)\n      return () => {\n        stop()\n        setFrame(null)\n      }\n    },\n    [setFrame],\n  )\n  const refs = useMemo(() => mergeRefs(own, ref), [own, ref])\n  return (\n    <div\n      ref={refs}\n      aria-hidden\n      data-slot='navigation-menu-preview'\n      className={cn(\n        // A faint line over the pictures keeps a pale photo's edge from melting into the panel.\n        \"bg-muted pointer-events-none relative isolate overflow-hidden rounded-(--list-radius) after:absolute after:inset-0 after:z-10 after:rounded-[inherit] after:ring-1 after:ring-foreground/10 after:ring-inset after:content-['']\",\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/**\n * A picture that sits a little zoomed in, by `--navigation-menu-zoom` (1.06\n * by default), and settles to its size when the highlight reaches its link or\n * when a `NavigationMenuPreview` changes to it. The picture a preview opens on\n * is already settled, since the panel itself is arriving. Its parent clips it. `alt` is\n * empty by default, since the link around it, or the one it previews, names it.\n */\nfunction NavigationMenuImage({ className, alt = '', ...props }: ComponentProps<'img'>) {\n  return (\n    // oxlint-disable-next-line next/no-img-element -- a registry part works in any React project, not only Next.js.\n    <img\n      data-slot='navigation-menu-image'\n      alt={alt}\n      decoding='async'\n      draggable={false}\n      className={cn(\n        // The settle eases on the fade curve, held at its end, because a\n        // spring that overshoots would shrink it past its frame's edges.\n        // Hidden in the preview, it zooms back at once, out of sight.\n        // Kept on its own layer: one dropped as the settle ends is drawn\n        // again snapped to whole pixels, and the picture jumps by one.\n        'size-full object-cover will-change-[scale] transition-[scale] duration-(--motion-layout-duration) ease-(--motion-layout-fade-easing) scale-(--navigation-menu-zoom,1.06) in-data-glided:scale-100 in-data-[preview=shown]:scale-100 in-data-[preview=under]:scale-100 in-data-[preview=leaving]:scale-100 in-data-[preview=hidden]:transition-none motion-reduce:[--navigation-menu-zoom:1]',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** The pictures stacked over `id`'s, which fade off it when it comes back while still under them. */\nfunction previewAbove(stack: readonly string[], id: string): string[] {\n  const at = stack.indexOf(id)\n  return at < 0 ? [] : stack.slice(0, at)\n}\n\n/** The preview's pictures, the latest first, once `id`'s comes in on top. */\nfunction previewStack(stack: readonly string[], id: string): string[] {\n  return [id, ...stack.filter((each) => each !== id)]\n}\n\nexport {\n  NavigationMenu,\n  NavigationMenuContent,\n  NavigationMenuImage,\n  NavigationMenuItem,\n  NavigationMenuLink,\n  NavigationMenuList,\n  NavigationMenuPreview,\n  NavigationMenuTrigger,\n  previewAbove,\n  previewStack,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/navigation-menu.tsx"
    }
  ],
  "type": "registry:component"
}