{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"confirm-popover","type":"registry:component","title":"Confirm popover","description":"Asks before deleting, from a popover that grows out of the button.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/confirm-popover.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useLayoutEffect, useRef, useState } from \"react\";\nimport { motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst ICON_SWAP = { type: \"spring\", duration: 0.3, bounce: 0 } as const;\n\nexport function ConfirmPopover({\n  title = \"Delete this file?\",\n  description = \"This can't be undone.\",\n  confirmLabel = \"Delete\",\n  doneLabel = \"Deleted\",\n  announcement = \"File deleted\",\n  onConfirm,\n  // Long enough to register the check, then the button is usable again.\n  resetAfter = 2400,\n  className,\n}: {\n  title?: string;\n  description?: string;\n  confirmLabel?: string;\n  doneLabel?: string;\n  announcement?: string;\n  onConfirm?: () => void;\n  resetAfter?: number;\n  className?: string;\n}) {\n  const [open, setOpen] = useState(false);\n  const [done, setDone] = useState(false);\n  const rootRef = useRef<HTMLDivElement>(null);\n  const triggerRef = useRef<HTMLButtonElement>(null);\n  const popoverRef = useRef<HTMLDivElement>(null);\n  const cancelRef = useRef<HTMLButtonElement>(null);\n  const confirmRef = useRef<HTMLButtonElement>(null);\n  const reset = useRef<ReturnType<typeof setTimeout>>(undefined);\n  const id = useId();\n\n  useEffect(() => () => clearTimeout(reset.current), []);\n\n  // The caret and the scale origin both sit over the trigger's center. The\n  // trigger keeps one width (both labels share a cell), so measuring once is\n  // enough.\n  useLayoutEffect(() => {\n    const trigger = triggerRef.current;\n    const popover = popoverRef.current;\n    if (trigger && popover)\n      popover.style.setProperty(\"--anchor\", `${trigger.offsetWidth / 2}px`);\n  }, []);\n\n  useEffect(() => {\n    if (!open) return;\n    // Cancel is the safe default: an Enter pressed out of habit keeps the file.\n    cancelRef.current?.focus();\n    const onPointerDown = (e: PointerEvent) => {\n      if (!rootRef.current?.contains(e.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", onPointerDown);\n    return () => document.removeEventListener(\"pointerdown\", onPointerDown);\n  }, [open]);\n\n  const close = () => {\n    setOpen(false);\n    triggerRef.current?.focus();\n  };\n\n  const confirm = () => {\n    close();\n    setDone(true);\n    onConfirm?.();\n    clearTimeout(reset.current);\n    reset.current = setTimeout(() => setDone(false), resetAfter);\n  };\n\n  return (\n    <div ref={rootRef} className={cn(\"relative inline-flex\", className)}>\n      <button\n        ref={triggerRef}\n        type=\"button\"\n        aria-haspopup=\"dialog\"\n        aria-expanded={open}\n        aria-controls={id}\n        aria-disabled={done}\n        onClick={() => !done && setOpen((o) => !o)}\n        className={cn(\n          \"h-10 touch-manipulation rounded-full bg-background px-4 text-sm font-medium shadow-raised outline-hidden transition-[scale,color] duration-150 ease-out select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[color]\",\n          done ? \"text-foreground active:scale-100\" : \"text-destructive\",\n        )}\n      >\n        {/* Both labels share one cell, so the button keeps the wider width\n            and never shifts when it turns into Deleted. */}\n        <span className=\"grid\">\n          <Variant visible={!done} icon={<TrashIcon />}>\n            {confirmLabel}\n          </Variant>\n          <Variant visible={done} icon={<path d=\"m3.5 8.5 3 3 6-7\" />}>\n            {doneLabel}\n          </Variant>\n        </span>\n      </button>\n\n      <div\n        ref={popoverRef}\n        id={id}\n        role=\"alertdialog\"\n        aria-labelledby={`${id}-title`}\n        aria-describedby={`${id}-desc`}\n        inert={!open}\n        onKeyDown={(e) => {\n          if (e.key === \"Escape\") {\n            e.stopPropagation();\n            close();\n          }\n          // Two buttons, so Tab simply alternates between them while open.\n          if (e.key === \"Tab\") {\n            e.preventDefault();\n            const next =\n              document.activeElement === cancelRef.current ? confirmRef : cancelRef;\n            next.current?.focus();\n          }\n        }}\n        // Grows out of the caret's tip, so it reads as coming from the button.\n        style={{ transformOrigin: \"calc(100% - var(--anchor, 50%)) -6px\" }}\n        className={cn(\n          \"absolute top-full right-0 z-10 mt-3 w-[min(272px,calc(100vw-32px))] rounded-[22px] bg-background p-3 text-foreground shadow-raised\",\n          \"transition-[opacity,scale,visibility] ease-[cubic-bezier(0.23,1,0.32,1)]\",\n          // Opens in 200ms, closes in 150ms: the exit should never hold the eye.\n          open\n            ? \"visible scale-100 opacity-100 duration-200\"\n            : \"invisible scale-[0.96] opacity-0 duration-150 motion-reduce:scale-100\",\n        )}\n      >\n        <span\n          aria-hidden\n          className=\"absolute -top-[5px] size-2.5 rotate-45 rounded-tl-[2px] bg-background\"\n          style={{\n            right: \"calc(var(--anchor, 50%) - 5px)\",\n            // Draws only the two edges that stick out, matching the ring in\n            // shadow-raised so caret and body read as one outline.\n            boxShadow:\n              \"-1px -1px 0 0 color-mix(in oklab, var(--foreground) 8%, transparent)\",\n          }}\n        />\n        <div className=\"px-1.5 pt-1.5 pb-3\">\n          <p id={`${id}-title`} className=\"text-[15px] font-medium\">\n            {title}\n          </p>\n          <p id={`${id}-desc`} className=\"mt-1 text-sm text-muted-foreground\">\n            {description}\n          </p>\n        </div>\n        {/* 10px corners inside 12px of padding keep the radii concentric:\n            22 = 10 + 12. */}\n        <div className=\"flex gap-2\">\n          <button\n            ref={cancelRef}\n            type=\"button\"\n            onClick={close}\n            className=\"h-9 flex-1 touch-manipulation rounded-[10px] bg-muted text-sm font-medium outline-hidden transition-[scale,background-color] duration-150 ease-out select-none hover:bg-border focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[background-color]\"\n          >\n            Cancel\n          </button>\n          <button\n            ref={confirmRef}\n            type=\"button\"\n            onClick={confirm}\n            className=\"h-9 flex-1 touch-manipulation rounded-[10px] bg-destructive text-sm font-medium text-white outline-hidden transition-[scale,opacity] duration-150 ease-out select-none hover:opacity-90 focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[opacity]\"\n          >\n            {confirmLabel}\n          </button>\n        </div>\n      </div>\n\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {done ? announcement : \"\"}\n      </span>\n    </div>\n  );\n}\n\nfunction TrashIcon() {\n  return (\n    <path d=\"M2.75 4.25h10.5M6.25 4.25v-1.5h3.5v1.5M4 4.25l.6 8.1a1 1 0 0 0 1 .9h4.8a1 1 0 0 0 1-.9l.6-8.1M6.75 7v3.75M9.25 7v3.75\" />\n  );\n}\n\nfunction Variant({\n  visible,\n  icon,\n  children,\n}: {\n  visible: boolean;\n  icon: React.ReactNode;\n  children: React.ReactNode;\n}) {\n  const reduceMotion = useReducedMotion();\n  // Reduced motion keeps the cross-fade but drops the scale and blur.\n  const hidden = reduceMotion\n    ? { opacity: 0 }\n    : { scale: 0.25, opacity: 0, filter: \"blur(4px)\" };\n  return (\n    <span\n      aria-hidden={!visible}\n      className=\"col-start-1 row-start-1 flex items-center justify-center gap-2\"\n    >\n      <motion.svg\n        viewBox=\"0 0 16 16\"\n        className=\"size-4 shrink-0\"\n        fill=\"none\"\n        stroke=\"currentColor\"\n        strokeWidth={1.5}\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n        initial={false}\n        animate={visible ? { scale: 1, opacity: 1, filter: \"blur(0px)\" } : hidden}\n        transition={ICON_SWAP}\n      >\n        {icon}\n      </motion.svg>\n      <span\n        className={cn(\n          \"transition-[opacity,filter] duration-200 ease-out\",\n          !visible && \"opacity-0 blur-[4px] motion-reduce:blur-none\",\n        )}\n      >\n        {children}\n      </span>\n    </span>\n  );\n}\n\nexport default function ConfirmPopoverDemo() {\n  const [deleted, setDeleted] = useState(false);\n  const restore = useRef<ReturnType<typeof setTimeout>>(undefined);\n\n  useEffect(() => () => clearTimeout(restore.current), []);\n\n  return (\n    // Leaves room below for the popover, so opening it never changes the\n    // demo's height.\n    <div className=\"w-[min(440px,100%)] pb-[150px]\">\n      {/* 8px around the 40px button: 28 = 20 + 8 keeps the radii concentric. */}\n      <div className=\"flex items-center gap-3 rounded-[28px] bg-muted py-2 pr-2 pl-5\">\n        <div className=\"min-w-0 flex-1\">\n          <p\n            className={cn(\n              \"truncate text-[15px] font-medium transition-[color] duration-200 ease-out\",\n              deleted ? \"text-muted-foreground\" : \"text-foreground\",\n            )}\n          >\n            q3-report.pdf\n          </p>\n          <p className=\"text-[13px] text-muted-foreground\">2.4 MB, edited 3h ago</p>\n        </div>\n        <ConfirmPopover\n          onConfirm={() => {\n            setDeleted(true);\n            clearTimeout(restore.current);\n            // Brings the file back with the button, so the demo can be rerun.\n            restore.current = setTimeout(() => setDeleted(false), 2400);\n          }}\n        />\n      </div>\n    </div>\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["feedback"],"docs":"From ui lab: https://lab.xevrion.dev/lab/confirm-popover"}