{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dropdown-menu","type":"registry:block","title":"Dropdown menu","description":"An invoice actions menu with a highlight that glides to the hovered item, staggered entries, and full keyboard navigation.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/dropdown-menu/dropdown-menu.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useCallback,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  type Variants,\n} from \"motion/react\";\nimport {\n  Archive,\n  Copy,\n  Download,\n  Eye,\n  MoreHorizontal,\n  Send,\n  Trash2,\n  type LucideIcon,\n} from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type DropdownMenuItem = {\n  id: string;\n  label: string;\n  shortcut?: string;\n  icon?: LucideIcon;\n  /** A rule is drawn above this item. */\n  separatorBefore?: boolean;\n  /** Status shown in the card once selected. Defaults to \"<label> · just now\". */\n  feedback?: string;\n  tone?: \"default\" | \"destructive\";\n};\n\nexport type DropdownMenuProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  items?: DropdownMenuItem[];\n};\n\ntype Coords = { top: number; left: number; width: number };\n\n/* ------------------------------------------------------------------ */\n/* demo data                                                           */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_ITEMS: DropdownMenuItem[] = [\n  { id: \"view\", label: \"View invoice\", shortcut: \"↵\", icon: Eye },\n  { id: \"download\", label: \"Download PDF\", shortcut: \"⌘D\", icon: Download },\n  { id: \"remind\", label: \"Send reminder\", icon: Send },\n  { id: \"duplicate\", label: \"Duplicate invoice\", icon: Copy },\n  {\n    id: \"archive\",\n    label: \"Archive\",\n    icon: Archive,\n    separatorBefore: true,\n    feedback: \"Archived · just now\",\n  },\n  {\n    id: \"delete\",\n    label: \"Delete invoice\",\n    icon: Trash2,\n    tone: \"destructive\",\n    feedback: \"Deletion requested\",\n  },\n];\n\nconst panelVariants: Variants = {\n  hidden: {\n    opacity: 0,\n    scale: 0.97,\n    y: -4,\n    transition: { duration: 0.12, ease: EASE },\n  },\n  show: {\n    opacity: 1,\n    scale: 1,\n    y: 0,\n    transition: {\n      type: \"spring\",\n      stiffness: 430,\n      damping: 32,\n      mass: 0.7,\n      staggerChildren: 0.028,\n      delayChildren: 0.015,\n    },\n  },\n};\n\nconst itemVariants: Variants = {\n  hidden: { opacity: 0, y: 5, transition: { duration: 0.1, ease: EASE } },\n  show: { opacity: 1, y: 0, transition: { duration: 0.16, ease: EASE } },\n};\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function DropdownMenu({\n  className,\n  title = \"Invoice INV-2049\",\n  description = \"Sable Systems · €3,840.00\",\n  items = DEFAULT_ITEMS,\n}: DropdownMenuProps) {\n  const cardRef = useRef<HTMLDivElement | null>(null);\n  const triggerRef = useRef<HTMLButtonElement | null>(null);\n  const menuRef = useRef<HTMLDivElement | null>(null);\n  const itemRefs = useRef<Array<HTMLButtonElement | null>>([]);\n\n  const inView = useInView(cardRef, { once: true, margin: \"-10% 0px\" });\n  const uid = useId().replace(/:/g, \"\");\n  const menuId = `${uid}-invoice-menu`;\n\n  const [open, setOpen] = useState(false);\n  const [coords, setCoords] = useState<Coords | null>(null);\n  const [hovered, setHovered] = useState(-1);\n  const [focused, setFocused] = useState(0);\n  const [lastAction, setLastAction] = useState<string | null>(null);\n\n  const safeItems = items.length > 0 ? items : DEFAULT_ITEMS;\n  const activeIndex = hovered >= 0 ? hovered : focused;\n  const menuWidth = 236;\n\n  const close = useCallback((returnFocus: boolean) => {\n    setOpen(false);\n    setHovered(-1);\n    if (returnFocus) triggerRef.current?.focus();\n  }, []);\n\n  const openMenu = () => {\n    const trigger = triggerRef.current;\n    if (!trigger) return;\n    const rect = trigger.getBoundingClientRect();\n    const width = Math.min(menuWidth, window.innerWidth - 24);\n    const left = Math.max(\n      12,\n      Math.min(rect.right - width, window.innerWidth - width - 12)\n    );\n    const top = rect.bottom + 6;\n    setCoords({\n      top,\n      left,\n      width,\n      // keep the panel inside the viewport on short screens\n    });\n    setHovered(-1);\n    setFocused(0);\n    setOpen(true);\n  };\n\n  useEffect(() => {\n    if (!open) return;\n\n    const focusTimer = window.setTimeout(() => itemRefs.current[0]?.focus(), 60);\n\n    const onPointerDown = (event: PointerEvent) => {\n      const target = event.target as Node | null;\n      if (!target) return;\n      if (menuRef.current?.contains(target)) return;\n      if (triggerRef.current?.contains(target)) return;\n      close(false);\n    };\n    const onKeyDown = (event: KeyboardEvent) => {\n      if (event.key !== \"Escape\") return;\n      event.preventDefault();\n      close(true);\n    };\n    const onViewportChange = () => close(false);\n\n    document.addEventListener(\"pointerdown\", onPointerDown);\n    document.addEventListener(\"keydown\", onKeyDown);\n    window.addEventListener(\"resize\", onViewportChange);\n    window.addEventListener(\"scroll\", onViewportChange);\n\n    return () => {\n      window.clearTimeout(focusTimer);\n      document.removeEventListener(\"pointerdown\", onPointerDown);\n      document.removeEventListener(\"keydown\", onKeyDown);\n      window.removeEventListener(\"resize\", onViewportChange);\n      window.removeEventListener(\"scroll\", onViewportChange);\n    };\n  }, [open, close]);\n\n  const moveFocus = (next: number) => {\n    const count = safeItems.length;\n    const index = ((next % count) + count) % count;\n    setHovered(-1);\n    itemRefs.current[index]?.focus();\n    setFocused(index);\n  };\n\n  const select = (item: DropdownMenuItem) => {\n    setLastAction(item.feedback ?? `${item.label} · just now`);\n    close(true);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={cardRef}\n        initial={{ opacity: 0, y: 16 }}\n        animate={inView ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: 0.6, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-5 text-card-foreground sm:p-6\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-start justify-between gap-x-4 gap-y-3\">\n          <div className=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-[-0.01em]\">{title}</h2>\n            <p className=\"mt-0.5 text-[12.5px] text-muted-foreground\">{description}</p>\n          </div>\n\n          <button\n            ref={triggerRef}\n            type=\"button\"\n            aria-label=\"Invoice actions\"\n            aria-haspopup=\"menu\"\n            aria-expanded={open}\n            aria-controls={open ? menuId : undefined}\n            onClick={() => (open ? close(false) : openMenu())}\n            className=\"grid h-8 w-8 flex-none place-items-center rounded-md border border-border text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n          >\n            <MoreHorizontal className=\"h-4 w-4\" aria-hidden=\"true\" />\n          </button>\n        </div>\n\n        <dl className=\"mt-5 space-y-2.5 text-[12.5px]\">\n          <div className=\"flex items-baseline justify-between gap-4 border-b border-border pb-2.5\">\n            <dt className=\"text-muted-foreground\">Issued</dt>\n            <dd className=\"tabular-nums\">4 Nov 2026</dd>\n          </div>\n          <div className=\"flex items-baseline justify-between gap-4 border-b border-border pb-2.5\">\n            <dt className=\"text-muted-foreground\">Due</dt>\n            <dd className=\"tabular-nums\">18 Nov 2026</dd>\n          </div>\n          <div className=\"flex items-baseline justify-between gap-4\">\n            <dt className=\"text-muted-foreground\">Terms</dt>\n            <dd className=\"tabular-nums\">Net 14 · EUR</dd>\n          </div>\n        </dl>\n\n        <div className=\"mt-4 flex flex-wrap items-center justify-between gap-x-4 gap-y-1 border-t border-border pt-3 text-[11.5px]\">\n          <span className=\"inline-flex items-center gap-1.5 text-muted-foreground\">\n            <span className=\"h-1.5 w-1.5 rounded-full bg-success\" aria-hidden=\"true\" />\n            Paid 6 Nov via bank transfer\n          </span>\n          <span\n            className={cn(\n              \"tabular-nums\",\n              lastAction ? \"text-foreground\" : \"text-muted-foreground\"\n            )}\n            aria-live=\"polite\"\n          >\n            {lastAction ?? \"No action yet\"}\n          </span>\n        </div>\n      </motion.div>\n\n      <AnimatePresence>\n        {open && coords ? (\n          <motion.div\n            ref={menuRef}\n            id={menuId}\n            role=\"menu\"\n            aria-label=\"Invoice actions\"\n            initial=\"hidden\"\n            animate=\"show\"\n            exit=\"hidden\"\n            variants={panelVariants}\n            onPointerLeave={() => setHovered(-1)}\n            onKeyDown={(event) => {\n              if (event.key === \"ArrowDown\") {\n                event.preventDefault();\n                moveFocus(focused + 1);\n              } else if (event.key === \"ArrowUp\") {\n                event.preventDefault();\n                moveFocus(focused - 1);\n              } else if (event.key === \"Home\") {\n                event.preventDefault();\n                moveFocus(0);\n              } else if (event.key === \"End\") {\n                event.preventDefault();\n                moveFocus(safeItems.length - 1);\n              } else if (event.key === \"Escape\") {\n                event.preventDefault();\n                close(true);\n              } else if (event.key === \"Tab\") {\n                event.preventDefault();\n                close(true);\n              }\n            }}\n            style={{\n              position: \"fixed\",\n              top: coords.top,\n              left: coords.left,\n              width: coords.width,\n              transformOrigin: \"100% 0%\",\n            }}\n            className=\"z-50 overflow-y-auto rounded-xl border border-border bg-popover p-1.5 text-popover-foreground shadow-xl\"\n          >\n            <div className=\"px-2 pb-1.5 pt-1 text-[11px] text-muted-foreground\">\n              INV-2049 · Sable Systems\n            </div>\n\n            {safeItems.map((item, index) => {\n              const Icon = item.icon;\n              const highlighted = activeIndex === index;\n              const destructive = item.tone === \"destructive\";\n\n              return (\n                <div key={item.id}>\n                  {item.separatorBefore ? (\n                    <div role=\"separator\" className=\"my-1 h-px bg-border\" />\n                  ) : null}\n                  <motion.button\n                    ref={(node) => {\n                      itemRefs.current[index] = node;\n                    }}\n                    type=\"button\"\n                    role=\"menuitem\"\n                    variants={itemVariants}\n                    onPointerEnter={() => setHovered(index)}\n                    onFocus={() => setFocused(index)}\n                    onClick={() => select(item)}\n                    className={cn(\n                      \"relative flex w-full items-center gap-2.5 rounded-md px-2 py-1.5 text-left text-[12.5px] transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                      destructive ? \"text-destructive\" : \"text-popover-foreground\"\n                    )}\n                  >\n                    {highlighted ? (\n                      <motion.span\n                        layoutId={`${uid}-menu-highlight`}\n                        aria-hidden=\"true\"\n                        className=\"absolute inset-0 rounded-md bg-muted\"\n                        transition={{ type: \"spring\", stiffness: 540, damping: 38 }}\n                      />\n                    ) : null}\n                    {Icon ? (\n                      <Icon\n                        className={cn(\n                          \"relative h-3.5 w-3.5 flex-none\",\n                          destructive ? \"text-destructive\" : \"text-muted-foreground\"\n                        )}\n                        aria-hidden=\"true\"\n                      />\n                    ) : null}\n                    <span className=\"relative min-w-0 flex-1 truncate\">{item.label}</span>\n                    {item.shortcut ? (\n                      <span className=\"relative text-[10.5px] tabular-nums text-muted-foreground\">\n                        {item.shortcut}\n                      </span>\n                    ) : null}\n                  </motion.button>\n                </div>\n              );\n            })}\n          </motion.div>\n        ) : null}\n      </AnimatePresence>\n    </MotionConfig>\n  );\n}\n\nexport default DropdownMenu;\n"}]}