{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "popup",
  "title": "Popup",
  "description": "How every popup anchored to a trigger opens: it grows out from under the trigger and folds back into it. Popover, Menus, Select and Combobox share it.",
  "registryDependencies": [
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/lib/popup.ts",
      "content": "'use client'\n\nimport { reducesMotion, watchReducedMotion } from '@/registry/lib/reduce-motion'\nimport { useState, useSyncExternalStore } from 'react'\n\n/*\n * How every popup anchored to a trigger opens and closes: it grows out from\n * under the trigger, as wide as the trigger at first, and folds back into it.\n * Popover, Menus, Select and Combobox share it, and the glide lib builds on\n * its strip, its sides and its anchor.\n */\n\n/**\n * How long the pointer rests on a trigger before a popup that opens on hover\n * opens, in ms: long enough to skip triggers the pointer only passes over,\n * short enough not to keep anyone waiting. Tooltips, hover cards and\n * popovers opened on hover all wait this long (Greg, 2026-11-14).\n */\nexport const HOVER_DELAY = 200\n\n/** How far from the screen's edge a popup stays, in px. */\nexport const POPUP_COLLISION_PADDING = 8\n\n/** How a ui-sdk popup presents: `auto` is a sheet on a small touch screen. */\nexport type PopupPresentation = 'popover' | 'sheet' | 'auto'\n\n/**\n * How it opens: `auto` stays a popover while motion is reduced, since\n * ui-sdk slides the sheet in on a spring of its own, which reduced motion\n * can't stop. That spring doesn't follow the feel either, so every popup is\n * a popover unless you ask.\n */\nexport function popupPresentation<P extends PopupPresentation>(\n  presentation: P,\n  reduced: boolean,\n): P | 'popover' {\n  return presentation === 'auto' && reduced ? 'popover' : presentation\n}\n\n/**\n * `popupPresentation` for a popup, read where its trigger is, and the ref\n * for that trigger: the motion preference can be set on any element.\n */\nexport function usePopupPresentation<P extends PopupPresentation>(presentation: P) {\n  const [trigger, setTrigger] = useState<HTMLElement | null>(null)\n  return [usePopupPresentationAt(presentation, trigger), setTrigger] as const\n}\n\n/** `popupPresentation` for a popup, read around `element`, such as the field it opens from. */\nexport function usePopupPresentationAt<P extends PopupPresentation>(\n  presentation: P,\n  element: HTMLElement | null,\n) {\n  const reduced = useSyncExternalStore(\n    watchReducedMotion,\n    () => reducesMotion(element),\n    () => false,\n  )\n  return popupPresentation(presentation, reduced)\n}\n\nexport type PhysicalSide = 'top' | 'bottom' | 'left' | 'right'\n/** A side as ui-sdk names it: the inline sides follow the reading direction. */\nexport type PopupSide = PhysicalSide | 'inline-start' | 'inline-end'\nexport type ReadingDirection = 'ltr' | 'rtl'\n\n/** The screen edge a side is on: the inline sides follow the reading direction. */\nexport function physicalSide(\n  side: PopupSide | undefined,\n  direction: ReadingDirection,\n): PhysicalSide {\n  if (side === 'inline-end') return direction === 'rtl' ? 'left' : 'right'\n  if (side === 'inline-start') return direction === 'rtl' ? 'right' : 'left'\n  return side ?? 'top'\n}\n\n/** A box in screen pixels. */\nexport interface ScreenBox {\n  left: number\n  top: number\n  width: number\n  height: number\n}\n\n/*\n * The strip a popup opens from and closes into: the edge of its box that\n * faces the trigger, with no depth, as long as the trigger and centred on it,\n * even where the popup was shifted in from the edge of the screen. Written\n * twice over, for the two engines that draw it: in pixels for the glide lib,\n * which slides between boxes it measures, and as an `inset()` for CSS, which\n * reads the anchor from variables.\n */\n\n/**\n * The strip in pixels, where `anchor` is the trigger's centre along the edge,\n * from the box's left or top.\n */\nexport function anchorStrip(\n  side: PhysicalSide,\n  box: ScreenBox,\n  anchor: number,\n  anchorLength: number,\n): ScreenBox {\n  if (side === 'top' || side === 'bottom') {\n    const left = Math.max(box.left, box.left + anchor - anchorLength / 2)\n    const right = Math.min(box.left + box.width, box.left + anchor + anchorLength / 2)\n    const top = side === 'bottom' ? box.top : box.top + box.height\n    return { left, top, width: Math.max(0, right - left), height: 0 }\n  }\n  const top = Math.max(box.top, box.top + anchor - anchorLength / 2)\n  const bottom = Math.min(box.top + box.height, box.top + anchor + anchorLength / 2)\n  const left = side === 'right' ? box.left : box.left + box.width\n  return { left, top, width: 0, height: Math.max(0, bottom - top) }\n}\n\n/*\n * Where the trigger's centre is along the popup's edge, from its left or top:\n * ui-sdk writes `--arrow-offset` on the popup it places, with no arrow part.\n */\nconst ANCHOR = 'var(--arrow-offset, 50%)'\n\n/** The strip as the `inset()` a popup starts from. */\nexport function popupGrowInset(side: PhysicalSide): string {\n  if (side === 'top' || side === 'bottom') {\n    const left = `max(0px, calc(${ANCHOR} - var(--anchor-width) / 2))`\n    const right = `max(0px, calc(100% - ${ANCHOR} - var(--anchor-width) / 2))`\n    return side === 'bottom' ? `0px ${right} 100% ${left}` : `100% ${right} 0px ${left}`\n  }\n  const top = `max(0px, calc(${ANCHOR} - var(--anchor-height) / 2))`\n  const bottom = `max(0px, calc(100% - ${ANCHOR} - var(--anchor-height) / 2))`\n  return side === 'right' ? `${top} 100% ${bottom} 0px` : `${top} 0px ${bottom} 100%`\n}\n\n/** The shape a popup grows from on `side`, with the corner its strip rounds to. */\nexport function popupGrowFrom(side: PhysicalSide): string {\n  return `inset(${popupGrowInset(side)} round var(--control-radius-capped))`\n}\n\n/** The look of every popup's box. Give it a corner: `--list-radius` for a list, `--surface-radius` for a surface. */\nexport const popupSurfaceClassName =\n  'bg-popover text-popover-foreground shadow-lg ring-1 ring-foreground/10 outline-hidden forced-colors:outline forced-colors:outline-solid'\n\n/**\n * A popup ui-sdk places: the box it grows from, on the side it landed on\n * (`popupGrowFrom`, written out for Tailwind), and `group/popup`, which the\n * box inside it (`popupGrowBoxClassName`) and its content read. It paints\n * nothing itself: ui-sdk clears the browser's own popover styles on some popups only, not\n * the Select's or the lists', so it clears the ones that would show, the\n * background behind the surface's corners and the scroll box that clips its\n * ring and shadow. ui-sdk keeps the room left on screen to the popup itself,\n * so it hands it on as `--popup-available-height` and `--popup-available-width`.\n */\nexport const popupPlacedClassName =\n  'group/popup isolate overflow-visible bg-transparent text-inherit outline-hidden [--popup-available-height:var(--available-height)] [--popup-available-width:var(--available-width)] data-[side=bottom]:[--popup-grow-from:inset(0px_max(0px,calc(100%_-_var(--arrow-offset,50%)_-_var(--anchor-width)_/_2))_100%_max(0px,calc(var(--arrow-offset,50%)_-_var(--anchor-width)_/_2))_round_var(--control-radius-capped))] data-[side=top]:[--popup-grow-from:inset(100%_max(0px,calc(100%_-_var(--arrow-offset,50%)_-_var(--anchor-width)_/_2))_0px_max(0px,calc(var(--arrow-offset,50%)_-_var(--anchor-width)_/_2))_round_var(--control-radius-capped))] data-[side=right]:[--popup-grow-from:inset(max(0px,calc(var(--arrow-offset,50%)_-_var(--anchor-height)_/_2))_100%_max(0px,calc(100%_-_var(--arrow-offset,50%)_-_var(--anchor-height)_/_2))_0px_round_var(--control-radius-capped))] data-[side=left]:[--popup-grow-from:inset(max(0px,calc(var(--arrow-offset,50%)_-_var(--anchor-height)_/_2))_0px_max(0px,calc(100%_-_var(--arrow-offset,50%)_-_var(--anchor-height)_/_2))_100%_round_var(--control-radius-capped))]'\n\n/**\n * The box inside a placed popup: it grows on `morph` and folds back on\n * `exit`, both turning back from where they are. Open, its clip reaches past\n * the box, so the ring and the shadow show. Under reduced motion it only\n * fades.\n */\nexport const popupGrowBoxClassName =\n  '[--popup-grow-to:inset(-2rem_round_calc(var(--surface-radius)+2rem))] [clip-path:var(--popup-grow-to)] transition-[clip-path,opacity] duration-(--motion-morph-duration) ease-(--motion-morph-easing) group-data-starting-style/popup:[clip-path:var(--popup-grow-from)] group-data-ending-style/popup:[clip-path:var(--popup-grow-from)] group-data-ending-style/popup:duration-(--motion-exit-duration) group-data-ending-style/popup:ease-(--motion-exit-easing) motion-reduce:transition-opacity motion-reduce:group-data-starting-style/popup:opacity-0 motion-reduce:group-data-starting-style/popup:[clip-path:var(--popup-grow-to)] motion-reduce:group-data-ending-style/popup:opacity-0 motion-reduce:group-data-ending-style/popup:[clip-path:var(--popup-grow-to)]'\n\n/**\n * The same box for a popup that carries its open state on itself\n * (`data-starting-style`), such as the AI chat popup's window, with its\n * `--popup-grow-from` set by its own code.\n */\nexport const popupGrowClassName =\n  '[--popup-grow-to:inset(-2rem_round_calc(var(--surface-radius)+2rem))] [clip-path:var(--popup-grow-to)] transition-[clip-path,opacity] duration-(--motion-morph-duration) ease-(--motion-morph-easing) data-starting-style:[clip-path:var(--popup-grow-from)] data-ending-style:[clip-path:var(--popup-grow-from)] data-ending-style:duration-(--motion-exit-duration) data-ending-style:ease-(--motion-exit-easing) motion-reduce:transition-opacity motion-reduce:data-starting-style:opacity-0 motion-reduce:data-starting-style:[clip-path:var(--popup-grow-to)] motion-reduce:data-ending-style:opacity-0 motion-reduce:data-ending-style:[clip-path:var(--popup-grow-to)]'\n\n/**\n * What sits inside the box: it fades in once the box has started to grow,\n * and out quickly when it folds back. Needs `group/popup` on the popup.\n */\nexport const popupGrowContentClassName =\n  'transition-opacity duration-(--motion-enter-duration) ease-(--motion-enter-easing) group-data-open/popup:delay-[calc(var(--motion-morph-duration)*0.15)] group-data-starting-style/popup:opacity-0 group-data-ending-style/popup:opacity-0 group-data-ending-style/popup:duration-[calc(var(--motion-exit-duration)*0.4)]'\n",
      "type": "registry:lib",
      "target": "@lib/popup.ts"
    }
  ],
  "type": "registry:lib"
}