{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-morphing-popover",
  "title": "Morphing popover",
  "description": "A popover component that morphs from its trigger with fluid animations.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/morphing-popover.tsx",
      "content": "'use client';\n\nimport {\n  useState,\n  useId,\n  useRef,\n  useEffect,\n  createContext,\n  createElement,\n  useContext,\n  isValidElement,\n} from 'react';\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  Transition,\n  Variants,\n} from 'motion/react';\nimport useClickOutside from '@/hooks/useClickOutside';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nconst TRANSITION = {\n  type: 'spring' as const,\n  bounce: 0.1,\n  duration: 0.4,\n};\n\ntype MorphingPopoverContextValue = {\n  isOpen: boolean;\n  open: () => void;\n  close: () => void;\n  uniqueId: string;\n  variants?: Variants;\n};\n\ntype MorphingChildProps = Record<string, unknown>;\n\nconst motionTriggers = new Map<\n  React.ElementType,\n  React.ElementType<MorphingChildProps>\n>();\n\n// motion.create returns a new component on every call; one per element type keeps the trigger mounted across renders.\nfunction motionTriggerFor(type: React.ElementType) {\n  const cached = motionTriggers.get(type);\n  if (cached) {\n    return cached;\n  }\n  const created = motion.create(\n    type as React.ComponentType<MorphingChildProps>\n  ) as React.ElementType<MorphingChildProps>;\n  motionTriggers.set(type, created);\n  return created;\n}\n\nfunction ReducedMotionConfig({ children }: { children: React.ReactNode }) {\n  const prefersReducedMotion = usePrefersReducedMotion();\n  if (!prefersReducedMotion) {\n    return children;\n  }\n  return <MotionConfig reducedMotion='always'>{children}</MotionConfig>;\n}\n\nconst MorphingPopoverContext =\n  createContext<MorphingPopoverContextValue | null>(null);\n\nfunction usePopoverLogic({\n  defaultOpen = false,\n  open: controlledOpen,\n  onOpenChange,\n}: {\n  defaultOpen?: boolean;\n  open?: boolean;\n  onOpenChange?: (open: boolean) => void;\n} = {}) {\n  const uniqueId = useId();\n  const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);\n\n  const isOpen = controlledOpen ?? uncontrolledOpen;\n\n  const open = () => {\n    if (controlledOpen === undefined) {\n      setUncontrolledOpen(true);\n    }\n    onOpenChange?.(true);\n  };\n\n  const close = () => {\n    if (controlledOpen === undefined) {\n      setUncontrolledOpen(false);\n    }\n    onOpenChange?.(false);\n  };\n\n  return { isOpen, open, close, uniqueId };\n}\n\nexport type MorphingPopoverProps = {\n  children: React.ReactNode;\n  transition?: Transition;\n  defaultOpen?: boolean;\n  open?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  variants?: Variants;\n  className?: string;\n} & React.ComponentProps<'div'>;\n\nfunction MorphingPopover({\n  children,\n  transition = TRANSITION,\n  defaultOpen,\n  open,\n  onOpenChange,\n  variants,\n  className,\n  ...props\n}: MorphingPopoverProps) {\n  const popoverLogic = usePopoverLogic({ defaultOpen, open, onOpenChange });\n\n  return (\n    <MorphingPopoverContext.Provider value={{ ...popoverLogic, variants }}>\n      <ReducedMotionConfig>\n        <MotionConfig transition={transition}>\n          <div\n            className={cn(\n              'relative flex items-center justify-center',\n              className\n            )}\n            key={popoverLogic.uniqueId}\n            {...props}\n          >\n            {children}\n          </div>\n        </MotionConfig>\n      </ReducedMotionConfig>\n    </MorphingPopoverContext.Provider>\n  );\n}\n\nexport type MorphingPopoverTriggerProps = {\n  asChild?: boolean;\n  children: React.ReactNode;\n  className?: string;\n} & React.ComponentProps<typeof motion.button>;\n\nfunction MorphingPopoverTrigger({\n  children,\n  className,\n  asChild = false,\n  ...props\n}: MorphingPopoverTriggerProps) {\n  const context = useContext(MorphingPopoverContext);\n  if (!context) {\n    throw new Error(\n      'MorphingPopoverTrigger must be used within MorphingPopover'\n    );\n  }\n\n  const shouldMergeIntoChild = asChild && isValidElement(children);\n  if (shouldMergeIntoChild) {\n    const childProps = children.props as MorphingChildProps;\n    const childType = children.type as React.ElementType;\n\n    return createElement(motionTriggerFor(childType), {\n      ...childProps,\n      onClick: context.open,\n      layoutId: `popover-trigger-${context.uniqueId}`,\n      className: childProps.className,\n      key: context.uniqueId,\n      'aria-expanded': context.isOpen,\n      'aria-controls': `popover-content-${context.uniqueId}`,\n    });\n  }\n\n  return (\n    <motion.div\n      key={context.uniqueId}\n      layoutId={`popover-trigger-${context.uniqueId}`}\n      onClick={context.open}\n    >\n      <motion.button\n        {...props}\n        layoutId={`popover-label-${context.uniqueId}`}\n        key={context.uniqueId}\n        className={className}\n        aria-expanded={context.isOpen}\n        aria-controls={`popover-content-${context.uniqueId}`}\n      >\n        {children}\n      </motion.button>\n    </motion.div>\n  );\n}\n\nexport type MorphingPopoverContentProps = {\n  children: React.ReactNode;\n  className?: string;\n} & React.ComponentProps<typeof motion.div>;\n\nfunction MorphingPopoverContent({\n  children,\n  className,\n  ...props\n}: MorphingPopoverContentProps) {\n  const context = useContext(MorphingPopoverContext);\n  if (!context)\n    throw new Error(\n      'MorphingPopoverContent must be used within MorphingPopover'\n    );\n\n  const ref = useRef<HTMLDivElement>(null);\n  const { isOpen, close } = context;\n  useClickOutside(ref, close);\n\n  useEffect(() => {\n    if (!isOpen) return;\n\n    const handleKeyDown = (event: KeyboardEvent) => {\n      if (event.key === 'Escape') close();\n    };\n\n    document.addEventListener('keydown', handleKeyDown);\n    return () => document.removeEventListener('keydown', handleKeyDown);\n  }, [isOpen, close]);\n\n  return (\n    <AnimatePresence>\n      {context.isOpen && (\n        <>\n          <motion.div\n            {...props}\n            ref={ref}\n            layoutId={`popover-trigger-${context.uniqueId}`}\n            key={context.uniqueId}\n            id={`popover-content-${context.uniqueId}`}\n            role='dialog'\n            aria-modal='true'\n            className={cn(\n              'surface-raised absolute overflow-hidden rounded-xl p-2',\n              className\n            )}\n            initial='initial'\n            animate='animate'\n            exit='exit'\n            variants={context.variants}\n          >\n            {children}\n          </motion.div>\n        </>\n      )}\n    </AnimatePresence>\n  );\n}\n\nexport { MorphingPopover, MorphingPopoverTrigger, MorphingPopoverContent };\n",
      "type": "registry:component",
      "target": "components/core/morphing-popover.tsx"
    },
    {
      "path": "hooks/useClickOutside.tsx",
      "content": "import { RefObject, useEffect } from 'react';\n\nfunction useClickOutside<T extends HTMLElement>(\n  ref: RefObject<T | null>,\n  handler: (event: MouseEvent | TouchEvent) => void\n): void {\n  useEffect(() => {\n    const handleClickOutside = (event: MouseEvent | TouchEvent) => {\n      if (!ref || !ref.current || ref.current.contains(event.target as Node)) {\n        return;\n      }\n\n      handler(event);\n    };\n\n    document.addEventListener('mousedown', handleClickOutside);\n    document.addEventListener('touchstart', handleClickOutside);\n\n    return () => {\n      document.removeEventListener('mousedown', handleClickOutside);\n      document.removeEventListener('touchstart', handleClickOutside);\n    };\n  }, [ref, handler]);\n}\n\nexport default useClickOutside;\n",
      "type": "registry:hook"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}