{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "menu",
  "title": "Menu",
  "description": "Docked menu with code cells, shortcut cells, ink cursor, destructive rows, checkbox and radio items, and submenus.",
  "dependencies": [
    "@base-ui/react@1.9.0"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "utils",
    "@keyline-ui/keyline-scope",
    "@keyline-ui/keyline-render"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/menu.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Menu as MenuPrimitive } from \"@base-ui/react/menu\";\n\nimport { cn } from \"@/lib/utils\";\nimport { scopeProps, useKeylineScope } from \"../lib/keyline-scope\";\nimport { resolveClassName, resolveStyle } from \"../lib/keyline-render\";\n\n/*\n * A menu is one grid: a code column (as wide as the widest code, so an ID like SH-20482 never wraps), the\n * labels, and the shortcuts. Every row (inside groups too) is a subgrid of it, so the labels share one edge.\n * Rows are separated by soft rules; the cursor is the inverse row; a destructive row is red text that floods\n * red under the cursor. Parts are named as Base UI names them.\n */\n\n/** Rows carry this attribute: a row that follows another row draws the soft rule between them. */\nconst ROW = { \"data-kl-menu-row\": \"\" } as const;\n\nconst surface = cn(\n  \"kl-attached kl-dock grid max-h-(--available-height) min-w-[max(240px,var(--anchor-width))] content-start overflow-auto\",\n  \"bg-kl-cell text-(length:--kl-text-body) text-kl-ink outline-0 *:col-span-full\",\n  \"grid-cols-[minmax(32px,max-content)_minmax(0,1fr)_auto]\",\n  \"[&_[data-kl-menu-row]+[data-kl-menu-row]]:border-t-(length:--kl-k1) [&_[data-kl-menu-row]+[data-kl-menu-row]]:border-t-kl-line-soft\",\n  // no row carries a code or an indicator: the code column is not drawn (no empty, ruled gutter). The popup reads\n  // its own rows and the code cells read --kl-menu-code-e: a :has() on the popup in the code cell's selector would\n  // restyle the whole document on every DOM change (see item.tsx)\n  \"[--kl-menu-code-e:initial]\",\n  \"not-has-[[data-slot=menu-item-code]:not(:empty)]:grid-cols-[0_minmax(0,1fr)_auto]\",\n  \"not-has-[[data-slot=menu-item-code]:not(:empty)]:[--kl-menu-code-e:0px]\",\n);\n\ntype SurfaceProps = MenuPrimitive.Popup.Props & {\n  /** Keyline weight of the owner's shared edge: 2 for a standalone control, 1 for a cell. */\n  attachWeight?: 1 | 2 | 3;\n};\n\n/**\n * The menu's surface: Base UI's Menu.Popup with Keyline's look, without a portal or a positioner. MenuPopup\n * uses it; so do menus positioned their own way (a context menu at the pointer, a menubar or toolbar menu\n * docked to its row). Give it the rows below.\n */\nfunction MenuSurface({ className, style, attachWeight = 2, children, ...props }: SurfaceProps): React.ReactElement {\n  return (\n    <MenuPrimitive.Popup\n      className={resolveClassName(surface, className)}\n      style={resolveStyle({ \"--kl-attach\": `${attachWeight}px` } as React.CSSProperties, style)}\n      {...props}\n      data-slot=\"menu-popup\"\n    >\n      {/* a submenu written inside this surface is its own submenu again (MenuSubmenuRoot re-provides it) */}\n      <InSubmenu.Provider value={false}>{children}</InSubmenu.Provider>\n    </MenuPrimitive.Popup>\n  );\n}\n\n/** Set by MenuSubmenuRoot: a MenuPopup inside it docks beside the parent row, not under a trigger. */\nconst InSubmenu = React.createContext(false);\n\n/**\n * A menu written inside a ButtonGroup docks to the group's frame, not to its trigger cell: the cell sits\n * inside the frame's k2 border, so docking to the cell would leave the menu's edges a border-width inside the\n * frame's. The anchor is the trigger cell with every edge that touches the frame moved onto the frame's\n * outer edge (in a row: the top and bottom always, the start/end for the first/last cell).\n */\nfunction useFrameDock() {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const [frame, setFrame] = React.useState<HTMLElement | null>(null);\n  // Runs after every render on purpose: the frame can change without any prop changing. The setter bails out\n  // when the frame is the same, so this settles after one pass.\n  // oxlint-disable-next-line react-hooks/exhaustive-deps -- re-reads the DOM each render; see above\n  React.useLayoutEffect(() => {\n    const next = ref.current?.closest<HTMLElement>('[data-slot=\"button-group\"]') ?? null;\n    setFrame((prev) => (prev === next ? prev : next));\n  });\n  const anchor = React.useMemo(() => {\n    if (!frame) return undefined;\n    return {\n      contextElement: frame,\n      getBoundingClientRect() {\n        const f = frame.getBoundingClientRect();\n        const trigger = Array.from(frame.querySelectorAll<HTMLElement>('[aria-expanded=\"true\"]')).find(\n          (el) => el.closest('[data-slot=\"button-group\"]') === frame,\n        );\n        if (!trigger) return f;\n        const t = trigger.getBoundingClientRect();\n        const cs = getComputedStyle(frame);\n        const near = (gap: number, border: string) => gap <= parseFloat(border) + 0.5;\n        const left = near(t.left - f.left, cs.borderLeftWidth) ? f.left : t.left;\n        const right = near(f.right - t.right, cs.borderRightWidth) ? f.right : t.right;\n        const top = near(t.top - f.top, cs.borderTopWidth) ? f.top : t.top;\n        const bottom = near(f.bottom - t.bottom, cs.borderBottomWidth) ? f.bottom : t.bottom;\n        return new DOMRect(left, top, right - left, bottom - top);\n      },\n    };\n  }, [frame]);\n  return { marker: <span ref={ref} hidden data-kl-dock-probe=\"\" />, anchor };\n}\n\ntype PositionerProps = MenuPrimitive.Positioner.Props;\n\ntype MenuPopupProps = SurfaceProps &\n  Pick<PositionerProps, \"anchor\" | \"side\" | \"align\" | \"sideOffset\" | \"alignOffset\" | \"collisionPadding\"> & {\n    /** Where the portal renders (defaults to <body>). */\n    container?: MenuPrimitive.Portal.Props[\"container\"];\n    /** Props for Base UI's Menu.Portal. */\n    portalProps?: Omit<MenuPrimitive.Portal.Props, \"container\" | \"children\">;\n  };\n\n/**\n * The menu: Base UI's Portal, Positioner and Popup in one part, with the positioner's placement props lifted.\n * A docked surface: its shared edge sits on the owner's keyline, it unfolds from that edge, it has no cut. In\n * a submenu it docks beside its row instead: its start border on the parent's end border, its first row level\n * with the trigger row. It carries the configuration (cut, density, motion, theme, dir) of the place it is\n * written, not of <body>.\n */\nfunction MenuPopup({\n  anchor,\n  side,\n  align = \"start\",\n  sideOffset,\n  alignOffset,\n  collisionPadding,\n  attachWeight = 2,\n  container,\n  portalProps,\n  className,\n  ...props\n}: MenuPopupProps): React.ReactElement {\n  const submenu = React.useContext(InSubmenu);\n  const [marker, scope, probe] = useKeylineScope(false, container);\n  const frameDock = useFrameDock();\n  // Base UI resolves `inline-end` from its DirectionProvider, which the page may not set; a submenu reads the\n  // direction its parent menu carries (scope.dir) and picks the physical side itself\n  const place = submenu\n    ? {\n        side: side ?? (scope.dir === \"rtl\" ? \"left\" : \"right\"),\n        align,\n        sideOffset: sideOffset ?? 0,\n        alignOffset: alignOffset ?? -2,\n        collisionPadding: collisionPadding ?? 8,\n      }\n    : {\n        anchor: anchor ?? frameDock.anchor,\n        side: side ?? \"bottom\",\n        align,\n        sideOffset: sideOffset ?? (attachWeight === 1 ? 0 : -attachWeight),\n        alignOffset,\n        collisionPadding,\n      };\n  return (\n    <>\n      {marker}\n      {submenu ? null : frameDock.marker}\n      <MenuPrimitive.Portal container={container} {...portalProps}>\n        {probe}\n        <MenuPrimitive.Positioner {...place} {...scopeProps(scope, { className: \"kl-positioner\" })}>\n          <MenuSurface\n            attachWeight={attachWeight}\n            className={resolveClassName(submenu ? \"min-w-[200px]\" : \"\", className)}\n            {...props}\n          />\n        </MenuPrimitive.Positioner>\n      </MenuPrimitive.Portal>\n    </>\n  );\n}\n\n/* ------------------------------------------------------------------ rows */\n\nconst row = cn(\n  \"group/menu-item grid min-h-(--kl-row) cursor-default grid-cols-subgrid items-stretch outline-0 select-none\",\n  \"data-highlighted:bg-kl-inverse data-highlighted:text-kl-on-inverse\",\n  \"data-disabled:cursor-not-allowed data-disabled:bg-(image:--kl-hatch) data-disabled:text-kl-dis\",\n  \"forced-colors:data-highlighted:outline-3 forced-colors:data-highlighted:outline-offset-[-4px]\",\n  \"forced-colors:data-highlighted:outline-[Highlight]\",\n);\n\n/* the leading cell: an index code, or a checkbox or radio indicator */\nconst codeCell = cn(\n  \"grid place-items-center border-e-[length:var(--kl-menu-code-e,var(--kl-k1))] border-e-kl-seam px-2 whitespace-nowrap empty:px-0\",\n  \"font-(family-name:--kl-font-mono) text-(length:--kl-text-code) text-kl-ink-2\",\n  \"group-data-highlighted/menu-item:border-e-kl-on-inverse group-data-highlighted/menu-item:text-inherit\",\n  \"group-data-popup-open/menu-item:border-e-kl-on-inverse group-data-popup-open/menu-item:text-inherit\",\n);\n\nfunction RowLabel({ children, description }: { children: React.ReactNode; description?: React.ReactNode }) {\n  return (\n    <span data-slot=\"menu-item-label\" className=\"grid content-center gap-px px-3 py-1.5\">\n      <span>{children}</span>\n      {description ? (\n        <small\n          className={cn(\n            \"text-(length:--kl-text-meta) text-kl-ink-2 group-data-disabled/menu-item:text-kl-ink-2\",\n            \"group-data-highlighted/menu-item:text-inherit group-data-popup-open/menu-item:text-inherit\",\n          )}\n        >\n          {description}\n        </small>\n      ) : null}\n    </span>\n  );\n}\n\nfunction RowShortcut({ shortcut }: { shortcut?: React.ReactNode }) {\n  return shortcut ? (\n    <kbd\n      data-slot=\"menu-item-shortcut\"\n      className={cn(\n        \"me-2.5 inline-grid h-[22px] min-w-[22px] place-items-center self-center px-[5px]\",\n        \"border-(length:--kl-k1) border-current font-(family-name:--kl-font-mono) text-(length:--kl-text-code)/none font-medium\",\n      )}\n    >\n      {shortcut}\n    </kbd>\n  ) : (\n    <span />\n  );\n}\n\ntype MenuItemProps = MenuPrimitive.Item.Props & {\n  /** Index code shown in the leading cell; only when a key or a reference uses it. */\n  code?: React.ReactNode;\n  shortcut?: React.ReactNode;\n  description?: React.ReactNode;\n  variant?: \"default\" | \"destructive\";\n};\n\nfunction MenuItem({\n  className,\n  code,\n  shortcut,\n  description,\n  variant = \"default\",\n  children,\n  ...props\n}: MenuItemProps): React.ReactElement {\n  return (\n    <MenuPrimitive.Item\n      data-variant={variant}\n      {...ROW}\n      className={resolveClassName(\n        cn(\n          row,\n          // a disabled destructive row reads disabled: the red is the action's, and it cannot be taken\n          \"data-[variant=destructive]:font-(--kl-weight-strong) data-[variant=destructive]:not-data-disabled:text-kl-red-text\",\n          \"data-[variant=destructive]:data-highlighted:bg-kl-red data-[variant=destructive]:data-highlighted:text-kl-on-red\",\n        ),\n        className,\n      )}\n      {...props}\n      data-slot=\"menu-item\"\n    >\n      <span data-slot=\"menu-item-code\" className={codeCell} aria-hidden={code == null || undefined}>\n        {code}\n      </span>\n      <RowLabel description={description}>{children}</RowLabel>\n      <RowShortcut shortcut={shortcut} />\n    </MenuPrimitive.Item>\n  );\n}\n\n/* ------------------------------------------------------------------ *\n * CHECKBOX AND RADIO ITEMS\n * The leading code cell becomes the indicator cell. A checkbox item's box takes a tick; a radio item's\n * box takes a filled square: two shapes, so the kind reads without colour. The box is drawn in the\n * row's text colour, so it inverts with the cursor. Checking an item keeps the menu open (Base UI's\n * default for these items); pass closeOnClick to close it.\n * ------------------------------------------------------------------ */\nconst box = cn(\n  \"grid size-3.5 place-items-center border-(length:--kl-k2) border-current text-kl-ink\",\n  \"group-data-disabled/menu-item:text-kl-dis group-data-highlighted/menu-item:text-kl-on-inverse\",\n  \"forced-colors:text-[CanvasText] forced-colors:forced-color-adjust-none\",\n  \"forced-colors:group-data-highlighted/menu-item:text-[CanvasText]\",\n);\n\nfunction Indicator({ kind, children }: { kind: \"checkbox\" | \"radio\"; children: React.ReactNode }) {\n  return (\n    <span data-slot=\"menu-item-code\" className={cn(codeCell, \"px-0\")} aria-hidden>\n      <span\n        data-slot=\"menu-item-box\"\n        data-kind={kind}\n        className={cn(box, kind === \"checkbox\" && \"has-[[data-slot=menu-item-indicator]]:bg-current\")}\n      >\n        {children}\n      </span>\n    </span>\n  );\n}\n\ntype ChoiceProps = { shortcut?: React.ReactNode; description?: React.ReactNode };\n\nfunction MenuCheckboxItem({\n  className,\n  shortcut,\n  description,\n  children,\n  ...props\n}: MenuPrimitive.CheckboxItem.Props & ChoiceProps): React.ReactElement {\n  return (\n    <MenuPrimitive.CheckboxItem\n      {...ROW}\n      className={resolveClassName(row, className)}\n      {...props}\n      data-slot=\"menu-checkbox-item\"\n    >\n      <Indicator kind=\"checkbox\">\n        <MenuPrimitive.CheckboxItemIndicator\n          data-slot=\"menu-item-indicator\"\n          className={cn(\n            \"grid size-full place-items-center text-kl-cell group-data-highlighted/menu-item:text-kl-inverse\",\n            \"forced-colors:text-[Canvas] [&_svg]:block\",\n          )}\n        >\n          <svg\n            viewBox=\"0 0 10 10\"\n            width=\"10\"\n            height=\"10\"\n            fill=\"none\"\n            stroke=\"currentColor\"\n            strokeWidth=\"1.75\"\n            strokeLinecap=\"square\"\n          >\n            <path d=\"m2 5.25 2 2 4-4.5\" />\n          </svg>\n        </MenuPrimitive.CheckboxItemIndicator>\n      </Indicator>\n      <RowLabel description={description}>{children}</RowLabel>\n      <RowShortcut shortcut={shortcut} />\n    </MenuPrimitive.CheckboxItem>\n  );\n}\n\n/** One exclusive choice among MenuRadioItems; controlled (value, onValueChange) or uncontrolled (defaultValue). */\nfunction MenuRadioGroup({ className, ...props }: MenuPrimitive.RadioGroup.Props): React.ReactElement {\n  return (\n    <MenuPrimitive.RadioGroup\n      className={resolveClassName(\"grid grid-cols-subgrid *:col-span-full\", className)}\n      {...props}\n      data-slot=\"menu-radio-group\"\n    />\n  );\n}\n\nfunction MenuRadioItem({\n  className,\n  shortcut,\n  description,\n  children,\n  ...props\n}: MenuPrimitive.RadioItem.Props & ChoiceProps): React.ReactElement {\n  return (\n    <MenuPrimitive.RadioItem\n      {...ROW}\n      className={resolveClassName(row, className)}\n      {...props}\n      data-slot=\"menu-radio-item\"\n    >\n      <Indicator kind=\"radio\">\n        <MenuPrimitive.RadioItemIndicator\n          data-slot=\"menu-item-indicator\"\n          className=\"size-1.5 bg-current forced-colors:text-[CanvasText] forced-colors:forced-color-adjust-none\"\n        />\n      </Indicator>\n      <RowLabel description={description}>{children}</RowLabel>\n      <RowShortcut shortcut={shortcut} />\n    </MenuPrimitive.RadioItem>\n  );\n}\n\n/* ------------------------------------------------------------------ *\n * SUBMENUS\n * <MenuSubmenuRoot><MenuSubmenuTrigger>…</MenuSubmenuTrigger><MenuPopup>…</MenuPopup></MenuSubmenuRoot>\n * The trigger is an ordinary row whose trailing cell is a › that mirrors in RTL; while its submenu is open it\n * keeps the cursor (inverse). The submenu's MenuPopup docks beside the parent and unfolds from that shared\n * edge. Hover intent, Arrow Right/Left, Escape and focus return are Base UI's.\n * ------------------------------------------------------------------ */\nfunction MenuSubmenuRoot(props: MenuPrimitive.SubmenuRoot.Props): React.ReactElement {\n  return (\n    <InSubmenu.Provider value={true}>\n      <MenuPrimitive.SubmenuRoot {...props} />\n    </InSubmenu.Provider>\n  );\n}\n\ntype MenuSubmenuTriggerProps = MenuPrimitive.SubmenuTrigger.Props & {\n  code?: React.ReactNode;\n  description?: React.ReactNode;\n};\n\nfunction MenuSubmenuTrigger({\n  className,\n  code,\n  description,\n  children,\n  ...props\n}: MenuSubmenuTriggerProps): React.ReactElement {\n  return (\n    <MenuPrimitive.SubmenuTrigger\n      {...ROW}\n      className={resolveClassName(\n        cn(\n          row,\n          \"data-popup-open:bg-kl-inverse data-popup-open:text-kl-on-inverse\",\n          \"forced-colors:data-popup-open:outline-3 forced-colors:data-popup-open:outline-offset-[-4px]\",\n          \"forced-colors:data-popup-open:outline-[Highlight]\",\n        ),\n        className,\n      )}\n      {...props}\n      data-slot=\"menu-submenu-trigger\"\n    >\n      <span data-slot=\"menu-item-code\" className={codeCell} aria-hidden={code == null || undefined}>\n        {code}\n      </span>\n      <RowLabel description={description}>{children}</RowLabel>\n      <span className=\"grid w-8 place-items-center self-stretch kl-rtl:[&_svg]:-scale-x-100\" aria-hidden>\n        <svg\n          viewBox=\"0 0 16 16\"\n          width=\"14\"\n          height=\"14\"\n          fill=\"none\"\n          stroke=\"currentColor\"\n          strokeWidth=\"1.75\"\n          strokeLinecap=\"square\"\n        >\n          <path d=\"m6 4 4 4-4 4\" />\n        </svg>\n      </span>\n    </MenuPrimitive.SubmenuTrigger>\n  );\n}\n\n/* ------------------------------------------------------------------ structure */\n\nfunction MenuSeparator({ className, ...props }: MenuPrimitive.Separator.Props): React.ReactElement {\n  return (\n    <MenuPrimitive.Separator\n      className={resolveClassName(\"h-0 border-t-(length:--kl-k1) border-t-kl-seam\", className)}\n      {...props}\n      data-slot=\"menu-separator\"\n    />\n  );\n}\n\nfunction MenuGroup({ className, ...props }: MenuPrimitive.Group.Props): React.ReactElement {\n  return (\n    <MenuPrimitive.Group\n      className={resolveClassName(\"grid grid-cols-subgrid *:col-span-full\", className)}\n      {...props}\n      data-slot=\"menu-group\"\n    />\n  );\n}\n\nfunction MenuGroupLabel({ className, ...props }: MenuPrimitive.GroupLabel.Props): React.ReactElement {\n  return (\n    <MenuPrimitive.GroupLabel\n      className={resolveClassName(\n        \"block px-3 pt-2 pb-1 font-(family-name:--kl-font-mono) text-(length:--kl-text-code)/[1.2] font-medium text-kl-ink-2\",\n        className,\n      )}\n      {...props}\n      data-slot=\"menu-group-label\"\n    />\n  );\n}\n\nconst Menu = MenuPrimitive.Root;\n\n/** The element that opens the menu. Rendered as another part (`render={<Button />}`), the element is that part's\n * and carries its data-slot (each part sets its own after the props it is given). */\nfunction MenuTrigger(props: MenuPrimitive.Trigger.Props): React.ReactElement {\n  return <MenuPrimitive.Trigger {...props} data-slot=\"menu-trigger\" />;\n}\n\nexport {\n  Menu,\n  MenuTrigger,\n  MenuPopup,\n  MenuSurface,\n  MenuItem,\n  MenuCheckboxItem,\n  MenuRadioGroup,\n  MenuRadioItem,\n  MenuSeparator,\n  MenuGroup,\n  MenuGroupLabel,\n  MenuSubmenuRoot,\n  MenuSubmenuTrigger,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/menu.tsx"
    }
  ],
  "type": "registry:ui"
}