{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "popover",
  "title": "Popover",
  "description": "An accessible popover for interactive content with click or hover triggers, 4 placements and 3 alignments, auto-flip, outside-click and Escape close, an arrow and a spring open animation.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/popover/popover.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type PopoverPlacement = \"top\" | \"bottom\" | \"left\" | \"right\";\nexport type PopoverAlign = \"start\" | \"center\" | \"end\";\n\nexport interface PopoverProps {\n  children: React.ReactNode;\n  content: React.ReactNode;\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  trigger?: \"click\" | \"hover\";\n  placement?: PopoverPlacement;\n  align?: PopoverAlign;\n  offset?: number;\n  arrow?: boolean;\n  width?: number;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0E0E0E\", border: \"rgba(255,255,255,0.1)\", text: \"#F5F4F1\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", text: \"#0A0A0A\", shadow: \"0 20px 50px rgba(0,0,0,0.22), 0 4px 12px rgba(0,0,0,0.1)\" },\n};\n\nconst FOCUSABLE = 'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\n\ninterface Position {\n  left: number;\n  top: number;\n  placement: PopoverPlacement;\n}\n\nexport function Popover({ children, content, open, defaultOpen = false, onOpenChange, trigger = \"click\", placement = \"bottom\", align = \"center\", offset = 10, arrow = true, width = 280, theme = \"dark\", className }: PopoverProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const triggerRef = React.useRef<HTMLSpanElement>(null);\n  const contentRef = React.useRef<HTMLDivElement>(null);\n  const closeTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const [internal, setInternal] = React.useState(defaultOpen);\n  const [mounted, setMounted] = React.useState(false);\n  const [pos, setPos] = React.useState<Position | null>(null);\n  const isControlled = open !== undefined;\n  const isOpen = isControlled ? open : internal;\n\n  const setOpen = React.useCallback(\n    (next: boolean) => {\n      if (!isControlled) setInternal(next);\n      onOpenChange?.(next);\n    },\n    [isControlled, onOpenChange],\n  );\n\n  const measure = React.useCallback(() => {\n    const el = triggerRef.current;\n    if (!el) return;\n    const r = el.getBoundingClientRect();\n    let place = placement;\n    if (place === \"top\" && r.top < 200) place = \"bottom\";\n    else if (place === \"bottom\" && window.innerHeight - r.bottom < 200) place = \"top\";\n    else if (place === \"left\" && r.left < width + 40) place = \"right\";\n    else if (place === \"right\" && window.innerWidth - r.right < width + 40) place = \"left\";\n    const horizontal = place === \"top\" || place === \"bottom\";\n    const along = horizontal ? (align === \"start\" ? r.left : align === \"end\" ? r.right : r.left + r.width / 2) : align === \"start\" ? r.top : align === \"end\" ? r.bottom : r.top + r.height / 2;\n    const map: Record<PopoverPlacement, Position> = {\n      top: { left: along, top: r.top - offset, placement: \"top\" },\n      bottom: { left: along, top: r.bottom + offset, placement: \"bottom\" },\n      left: { left: r.left - offset, top: along, placement: \"left\" },\n      right: { left: r.right + offset, top: along, placement: \"right\" },\n    };\n    setPos(map[place]);\n  }, [placement, align, offset, width]);\n\n  React.useEffect(() => {\n    if (!isOpen) return;\n    const id = setTimeout(() => {\n      measure();\n      setMounted(true);\n    }, 0);\n    const focusId = setTimeout(() => {\n      if (trigger === \"click\") contentRef.current?.focus({ preventScroll: true });\n    }, 60);\n    return () => {\n      clearTimeout(id);\n      clearTimeout(focusId);\n    };\n  }, [isOpen, measure, trigger]);\n\n  React.useEffect(() => {\n    if (!isOpen) return;\n    const onDown = (e: MouseEvent) => {\n      const t = e.target as Node;\n      if (triggerRef.current?.contains(t) || contentRef.current?.contains(t)) return;\n      setOpen(false);\n    };\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        setOpen(false);\n        triggerRef.current?.querySelector<HTMLElement>(FOCUSABLE)?.focus({ preventScroll: true });\n      }\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    document.addEventListener(\"keydown\", onKey);\n    window.addEventListener(\"scroll\", measure, true);\n    window.addEventListener(\"resize\", measure);\n    return () => {\n      document.removeEventListener(\"mousedown\", onDown);\n      document.removeEventListener(\"keydown\", onKey);\n      window.removeEventListener(\"scroll\", measure, true);\n      window.removeEventListener(\"resize\", measure);\n    };\n  }, [isOpen, measure, setOpen]);\n\n  React.useEffect(\n    () => () => {\n      if (closeTimer.current) clearTimeout(closeTimer.current);\n    },\n    [],\n  );\n\n  const place = pos?.placement ?? placement;\n  const horizontal = place === \"top\" || place === \"bottom\";\n  const tx = horizontal ? (align === \"start\" ? \"0%\" : align === \"end\" ? \"-100%\" : \"-50%\") : place === \"left\" ? \"-100%\" : \"0%\";\n  const ty = !horizontal ? (align === \"start\" ? \"0%\" : align === \"end\" ? \"-100%\" : \"-50%\") : place === \"top\" ? \"-100%\" : \"0%\";\n  const origin: Record<PopoverPlacement, string> = { top: \"bottom center\", bottom: \"top center\", left: \"center right\", right: \"center left\" };\n  const arrowPos = horizontal ? (align === \"start\" ? { left: 18 } : align === \"end\" ? { right: 18 } : { left: \"50%\", marginLeft: -5 }) : align === \"start\" ? { top: 18 } : align === \"end\" ? { bottom: 18 } : { top: \"50%\", marginTop: -5 };\n  const arrowSide: Record<PopoverPlacement, React.CSSProperties> = {\n    top: { bottom: -6, borderRight: `1px solid ${p.border}`, borderBottom: `1px solid ${p.border}` },\n    bottom: { top: -6, borderLeft: `1px solid ${p.border}`, borderTop: `1px solid ${p.border}` },\n    left: { right: -6, borderTop: `1px solid ${p.border}`, borderRight: `1px solid ${p.border}` },\n    right: { left: -6, borderBottom: `1px solid ${p.border}`, borderLeft: `1px solid ${p.border}` },\n  };\n\n  const hoverHandlers =\n    trigger === \"hover\"\n      ? {\n          onMouseEnter: () => {\n            if (closeTimer.current) clearTimeout(closeTimer.current);\n            setOpen(true);\n          },\n          onMouseLeave: () => {\n            closeTimer.current = setTimeout(() => setOpen(false), 140);\n          },\n        }\n      : {};\n\n  return (\n    <>\n      <span\n        ref={triggerRef}\n        className={cn(\"inline-flex\", className)}\n        onClick={trigger === \"click\" ? () => setOpen(!isOpen) : undefined}\n        {...hoverHandlers}\n      >\n        {React.isValidElement(children)\n          ? React.cloneElement(children as React.ReactElement<Record<string, unknown>>, {\n              \"aria-haspopup\": \"dialog\",\n              \"aria-expanded\": isOpen,\n              \"aria-controls\": isOpen ? `${uid}-pop` : undefined,\n            })\n          : children}\n      </span>\n      {mounted &&\n        createPortal(\n          <AnimatePresence>\n            {isOpen && pos && (\n              <motion.div\n                key=\"popover\"\n                ref={contentRef}\n                id={`${uid}-pop`}\n                role=\"dialog\"\n                tabIndex={-1}\n                onMouseEnter={trigger === \"hover\" ? () => closeTimer.current && clearTimeout(closeTimer.current) : undefined}\n                onMouseLeave={trigger === \"hover\" ? () => (closeTimer.current = setTimeout(() => setOpen(false), 140)) : undefined}\n                className=\"fixed z-[9999] outline-none\"\n                style={{ left: pos.left, top: pos.top, x: tx, y: ty, width, maxWidth: \"calc(100vw - 24px)\", borderRadius: 16, padding: 16, background: p.bg, border: `1px solid ${p.border}`, boxShadow: p.shadow, color: p.text, fontFamily: \"Inter, sans-serif\", fontSize: 14, lineHeight: 1.55, transformOrigin: origin[place] }}\n                initial={{ opacity: 0, scale: 0.96 }}\n                animate={{ opacity: 1, scale: 1 }}\n                exit={{ opacity: 0, scale: 0.96 }}\n                transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n              >\n                {content}\n                {arrow && <span aria-hidden=\"true\" className=\"absolute block\" style={{ width: 10, height: 10, background: p.bg, transform: \"rotate(45deg)\", ...arrowSide[place], ...arrowPos }} />}\n              </motion.div>\n            )}\n          </AnimatePresence>,\n          document.body,\n        )}\n    </>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}