{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "popover-morph",
  "title": "Popover morph",
  "description": "A small button such as \"Add note\" morphs into a floating form panel, its label sliding into the header, and shrinks back into the button on save, Esc or an outside click.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/overlays/popover-morph.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, MotionConfig, motion, useReducedMotion, type Transition } from \"motion/react\"\nimport { X } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\n\nconst SPRING: Transition = { type: \"spring\", bounce: 0.1, duration: 0.38 }\nconst FOCUSABLE =\n  'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex=\"-1\"])'\n\nfunction useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v: T) => void) {\n  const [inner, setInner] = React.useState(defaultValue)\n  const controlled = value !== undefined\n  const current = controlled ? value : inner\n  const set = React.useCallback(\n    (v: T) => {\n      if (!controlled) setInner(v)\n      onChange?.(v)\n    },\n    [controlled, onChange],\n  )\n  return [current, set] as const\n}\n\nexport type PopoverMorphLabels = { close: string }\nconst DEFAULT_LABELS: PopoverMorphLabels = { close: \"Close\" }\n\nexport type PopoverMorphProps = Omit<React.ComponentProps<\"div\">, \"children\" | \"title\"> & {\n  /** Trigger content; it slides into the panel header when the popover opens. */\n  label: React.ReactNode\n  /** Header text inside the panel. Defaults to the label. */\n  title?: React.ReactNode\n  /** Controlled open state. Pair with onOpenChange. */\n  open?: boolean\n  /** Initial open state when uncontrolled. */\n  defaultOpen?: boolean\n  /** Called with the next state on trigger click, close(), Esc, an outside click or tabbing out. */\n  onOpenChange?: (open: boolean) => void\n  /** The panel grows down from the trigger, or up for \"top\". */\n  side?: \"bottom\" | \"top\"\n  /** Preferred edge (logical: start is the left edge in LTR, the right edge in RTL); it flips when the panel would leave the viewport. */\n  align?: \"start\" | \"end\"\n  /** Panel width in px (capped to the viewport). */\n  width?: number\n  /** Classes for the trigger button. */\n  triggerClassName?: string\n  /** Classes for the floating panel. */\n  panelClassName?: string\n  /** Panel body. Pass a function to get close(), e.g. to close after a form submits. */\n  children: React.ReactNode | ((api: { close: () => void }) => React.ReactNode)\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<PopoverMorphLabels>\n}\n\n/**\n * A button that morphs into a small floating panel (a note, a reminder, a code) and back. Non-modal: outside\n * click, Esc and tabbing away close it. The panel is absolutely positioned inside the wrapper, so give the\n * surrounding container room or `overflow: visible`.\n */\nexport function PopoverMorph({\n  label,\n  title,\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  side = \"bottom\",\n  align = \"start\",\n  width = 320,\n  className,\n  triggerClassName,\n  panelClassName,\n  children,\n  labels,\n  ...props\n}: PopoverMorphProps) {\n  const l = useLabels(\"popover-morph\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const [open, setOpen] = useControllable(openProp, defaultOpen, onOpenChange)\n  const [resolvedAlign, setResolvedAlign] = React.useState(align)\n  const [lifted, setLifted] = React.useState(open)\n  const uid = React.useId()\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const triggerRef = React.useRef<HTMLButtonElement>(null)\n  const panelRef = React.useRef<HTMLDivElement>(null)\n  const restoreFocus = React.useRef(true)\n  const wasOpen = React.useRef(open)\n\n  const close = React.useCallback(\n    (returnFocus = true) => {\n      restoreFocus.current = returnFocus\n      setOpen(false)\n    },\n    [setOpen],\n  )\n\n  const show = () => {\n    const trigger = triggerRef.current\n    const rect = trigger?.getBoundingClientRect()\n    if (trigger && rect) {\n      const w = Math.min(width, window.innerWidth - 32)\n      // \"start\" grows toward the inline end: rightward in LTR, leftward in RTL.\n      const rtl = getComputedStyle(trigger).direction === \"rtl\"\n      const fitsRight = rect.left + w <= window.innerWidth - 8\n      const fitsLeft = rect.right - w >= 8\n      const fitsStart = rtl ? fitsLeft : fitsRight\n      const fitsEnd = rtl ? fitsRight : fitsLeft\n      if (align === \"start\") setResolvedAlign(fitsStart ? \"start\" : \"end\")\n      else setResolvedAlign(fitsEnd ? \"end\" : \"start\")\n    }\n    setOpen(true)\n  }\n\n  React.useEffect(() => {\n    if (open) {\n      setLifted(true)\n      const el = panelRef.current\n      const first = el?.querySelector<HTMLElement>(\"[data-autofocus]\") ?? el?.querySelector<HTMLElement>(`[data-pc-body] :is(${FOCUSABLE})`)\n      first?.focus({ preventScroll: true })\n    } else if (wasOpen.current && restoreFocus.current) {\n      triggerRef.current?.focus({ preventScroll: true })\n    }\n    wasOpen.current = open\n    if (!open) return\n    const onDown = (e: PointerEvent) => {\n      if (!rootRef.current?.contains(e.target as Node)) close(false)\n    }\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        e.preventDefault()\n        close(true)\n      }\n    }\n    document.addEventListener(\"pointerdown\", onDown)\n    document.addEventListener(\"keydown\", onKey)\n    return () => {\n      document.removeEventListener(\"pointerdown\", onDown)\n      document.removeEventListener(\"keydown\", onKey)\n    }\n  }, [open, close])\n\n  return (\n    <MotionConfig transition={reduce ? { duration: 0 } : SPRING}>\n      <GooduiTokens />\n      <div ref={rootRef} className={cn(\"relative inline-block\", className)} {...props}>\n        <motion.button\n          ref={triggerRef}\n          type=\"button\"\n          layoutId={`${uid}-surface`}\n          aria-haspopup=\"dialog\"\n          aria-expanded={open}\n          aria-controls={open ? `${uid}-panel` : undefined}\n          onClick={show}\n          onLayoutAnimationComplete={() => {\n            if (!open) setLifted(false)\n          }}\n          className={cn(\n            \"pc-press relative inline-flex h-9 items-center border bg-background px-3.5 text-sm font-medium shadow-xs transition-colors outline-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0\",\n            triggerClassName,\n          )}\n          style={{ borderRadius: 10, zIndex: lifted ? 40 : undefined }}\n        >\n          <motion.span layoutId={`${uid}-label`} layout=\"position\" className=\"inline-flex items-center gap-2 whitespace-nowrap\">\n            {label}\n          </motion.span>\n        </motion.button>\n\n        <AnimatePresence>\n          {open && (\n            <motion.div\n              ref={panelRef}\n              id={`${uid}-panel`}\n              layoutId={`${uid}-surface`}\n              role=\"dialog\"\n              aria-labelledby={`${uid}-title`}\n              onBlur={(e) => {\n                const next = e.relatedTarget as Node | null\n                if (next && !rootRef.current?.contains(next)) close(false)\n              }}\n              className={cn(\n                \"absolute z-50 overflow-hidden border bg-popover text-popover-foreground shadow-[var(--pc-e3)]\",\n                side === \"bottom\" ? \"top-0\" : \"bottom-0\",\n                resolvedAlign === \"start\" ? \"start-0\" : \"end-0\",\n                panelClassName,\n              )}\n              style={{ borderRadius: 14, width: `min(${width}px, calc(100vw - 2rem))` }}\n            >\n              <div className=\"flex items-center gap-2 py-2 ps-3.5 pe-2\">\n                <motion.span\n                  id={`${uid}-title`}\n                  layoutId={`${uid}-label`}\n                  layout=\"position\"\n                  className=\"inline-flex min-w-0 flex-1 items-center gap-2 text-sm font-medium whitespace-nowrap [&_svg]:size-4 [&_svg]:shrink-0\"\n                >\n                  {title ?? label}\n                </motion.span>\n                <motion.button\n                  type=\"button\"\n                  aria-label={l.close}\n                  onClick={() => close(true)}\n                  initial={{ opacity: 0, scale: 0.6 }}\n                  animate={{ opacity: 1, scale: 1 }}\n                  exit={{ opacity: 0, scale: 0.6, transition: { duration: 0.08 } }}\n                  className=\"pc-press inline-flex size-7 items-center justify-center rounded-md text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n                >\n                  <X className=\"size-4\" />\n                </motion.button>\n              </div>\n              <motion.div\n                data-pc-body=\"\"\n                initial={{ opacity: 0, y: reduce ? 0 : -4 }}\n                animate={{ opacity: 1, y: 0, transition: reduce ? { duration: 0 } : { delay: 0.06, duration: 0.2 } }}\n                exit={{ opacity: 0, transition: { duration: reduce ? 0 : 0.06 } }}\n                className=\"px-3.5 pb-3.5\"\n              >\n                {typeof children === \"function\" ? children({ close: () => close(true) }) : children}\n              </motion.div>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n    </MotionConfig>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/popover-morph.tsx"
    }
  ],
  "categories": [
    "components",
    "overlays"
  ],
  "type": "registry:component"
}