{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"popover","type":"registry:ui","title":"Popover","description":"Click-to-open panel anchored to its trigger that scales from the trigger edge with a spring and flips above or below to stay in the viewport.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/geturui/ui/popover.tsx","type":"registry:ui","target":"components/ui/popover.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface PopoverProps {\n  /** The element that toggles the popover. Must accept onClick and a ref-able focusable element. */\n  trigger: React.ReactElement;\n  children: React.ReactNode | ((api: { close: () => void }) => React.ReactNode);\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** Preferred side; flips when the viewport has more room on the other side. */\n  side?: \"top\" | \"bottom\";\n  align?: \"start\" | \"center\" | \"end\";\n  /** Gap between trigger and panel, px. */\n  sideOffset?: number;\n  /** Accessible name for the dialog when it has no visible heading. */\n  label?: string;\n  className?: string;\n}\n\ntype Placement = { side: \"top\" | \"bottom\"; shift: number; ready: boolean };\n\nconst FOCUSABLE = 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\nconst EDGE = 8;\n\n/**\n * Click-to-open panel anchored to its trigger. It scales from the trigger edge with a spring, flips top/bottom\n * when it would leave the viewport, closes on outside press or Escape and returns focus to the trigger.\n */\nexport function Popover({\n  trigger,\n  children,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  side = \"bottom\",\n  align = \"center\",\n  sideOffset = 8,\n  label,\n  className,\n}: PopoverProps) {\n  const reduce = useReducedMotion();\n  const uid = React.useId();\n  const [inner, setInner] = React.useState(defaultOpen);\n  const isOpen = open ?? inner;\n  const rootRef = React.useRef<HTMLSpanElement>(null);\n  const panelRef = React.useRef<HTMLDivElement>(null);\n  const [placement, setPlacement] = React.useState<Placement>({ side, shift: 0, ready: false });\n\n  // Reset measurement whenever the panel closes.\n  const [wasOpen, setWasOpen] = React.useState(isOpen);\n  if (wasOpen !== isOpen) {\n    setWasOpen(isOpen);\n    if (!isOpen) setPlacement({ side, shift: 0, ready: false });\n  }\n\n  // Bumped whenever focus should go back to the trigger after closing.\n  const [restoreTick, setRestoreTick] = React.useState(0);\n  const setOpen = React.useCallback(\n    (v: boolean, restore = true) => {\n      if (open === undefined) setInner(v);\n      onOpenChange?.(v);\n      if (!v && restore) setRestoreTick((t) => t + 1);\n    },\n    [open, onOpenChange],\n  );\n  const close = React.useCallback(() => setOpen(false), [setOpen]);\n\n  React.useEffect(() => {\n    if (restoreTick) rootRef.current?.querySelector<HTMLElement>(FOCUSABLE)?.focus();\n  }, [restoreTick]);\n\n  // Measure once the panel renders: choose the side with room and nudge it inside the viewport.\n  React.useEffect(() => {\n    const panel = panelRef.current;\n    const root = rootRef.current;\n    if (!isOpen || !panel || !root) return;\n    const ro = new ResizeObserver(() => {\n      const t = root.getBoundingClientRect();\n      const ph = panel.offsetHeight;\n      const pw = panel.offsetWidth;\n      const below = window.innerHeight - t.bottom - sideOffset - EDGE;\n      const above = t.top - sideOffset - EDGE;\n      let s = side;\n      if (side === \"bottom\" && ph > below && above > below) s = \"top\";\n      if (side === \"top\" && ph > above && below > above) s = \"bottom\";\n      const left = align === \"start\" ? t.left : align === \"end\" ? t.right - pw : t.left + t.width / 2 - pw / 2;\n      let shift = 0;\n      if (left < EDGE) shift = EDGE - left;\n      else if (left + pw > window.innerWidth - EDGE) shift = window.innerWidth - EDGE - (left + pw);\n      setPlacement({ side: s, shift, ready: true });\n    });\n    ro.observe(panel);\n    return () => ro.disconnect();\n  }, [isOpen, side, align, sideOffset]);\n\n  // Focus the first field (or the panel) when it opens.\n  React.useEffect(() => {\n    if (!isOpen || !placement.ready) return;\n    const panel = panelRef.current;\n    const first = panel?.querySelector<HTMLElement>(FOCUSABLE);\n    (first ?? panel)?.focus({ preventScroll: true });\n  }, [isOpen, placement.ready]);\n\n  // Outside press closes without stealing focus back.\n  React.useEffect(() => {\n    if (!isOpen) return;\n    const onDown = (e: PointerEvent) => {\n      if (!rootRef.current?.contains(e.target as Node)) setOpen(false, false);\n    };\n    document.addEventListener(\"pointerdown\", onDown);\n    return () => document.removeEventListener(\"pointerdown\", onDown);\n  }, [isOpen, setOpen]);\n\n  const triggerProps = trigger.props as { onClick?: (e: React.MouseEvent) => void };\n  const triggerEl = React.cloneElement(trigger as React.ReactElement<Record<string, unknown>>, {\n    \"aria-haspopup\": \"dialog\",\n    \"aria-expanded\": isOpen,\n    \"aria-controls\": isOpen ? `${uid}-panel` : undefined,\n    onClick: (e: React.MouseEvent) => {\n      triggerProps.onClick?.(e);\n      setOpen(!isOpen, false);\n    },\n  });\n\n  const s = placement.side;\n  const originX = align === \"start\" ? \"0%\" : align === \"end\" ? \"100%\" : \"50%\";\n  const posX = align === \"start\" ? \"left-0\" : align === \"end\" ? \"right-0\" : \"left-1/2 -translate-x-1/2\";\n\n  return (\n    <span ref={rootRef} className=\"relative inline-flex\">\n      {triggerEl}\n      <AnimatePresence>\n        {isOpen && (\n          <motion.div\n            ref={panelRef}\n            id={`${uid}-panel`}\n            role=\"dialog\"\n            aria-label={label}\n            tabIndex={-1}\n            onKeyDown={(e) => {\n              if (e.key === \"Escape\") {\n                e.stopPropagation();\n                close();\n              }\n            }}\n            onBlur={(e) => {\n              const next = e.relatedTarget as Node | null;\n              if (next && !rootRef.current?.contains(next)) setOpen(false, false);\n            }}\n            initial={{ opacity: 0, scale: reduce ? 1 : 0.9, y: reduce ? 0 : s === \"bottom\" ? -6 : 6, filter: reduce ? \"blur(0px)\" : \"blur(4px)\" }}\n            animate={\n              placement.ready\n                ? { opacity: 1, scale: 1, y: 0, filter: \"blur(0px)\" }\n                : { opacity: 0, scale: reduce ? 1 : 0.9, y: reduce ? 0 : s === \"bottom\" ? -6 : 6, filter: reduce ? \"blur(0px)\" : \"blur(4px)\" }\n            }\n            exit={{ opacity: 0, scale: reduce ? 1 : 0.94, filter: reduce ? \"blur(0px)\" : \"blur(2px)\", transition: { duration: 0.14 } }}\n            transition={{ type: \"spring\", stiffness: 420, damping: 30, opacity: { duration: 0.16 } }}\n            style={{\n              transformOrigin: `${originX} ${s === \"bottom\" ? \"0%\" : \"100%\"}`,\n              marginLeft: placement.shift,\n              [s === \"bottom\" ? \"top\" : \"bottom\"]: `calc(100% + ${sideOffset}px)`,\n            }}\n            className={cn(\n              \"absolute z-[80] min-w-[220px] rounded-md border border-border bg-surface-raised p-4 text-ink shadow-lg outline-none\",\n              posX,\n              className,\n            )}\n          >\n            {typeof children === \"function\" ? children({ close }) : children}\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </span>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["components","popover","overlay","dropdown","dialog","floating","collision","form","anchor"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/popover"}}