{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"popover","type":"registry:block","title":"Popover","description":"An export popover that grows from its trigger, with a sliding format switch, toggles, and a live confirmation when the file is queued.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/popover/popover.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} from \"motion/react\";\nimport { Check, ChevronDown, Download, X } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\ntype Format = \"csv\" | \"json\" | \"pdf\";\n\nconst FORMATS: { id: Format; label: string }[] = [\n  { id: \"csv\", label: \"CSV\" },\n  { id: \"json\", label: \"JSON\" },\n  { id: \"pdf\", label: \"PDF\" },\n];\n\nconst BASE_ROWS = 12480;\nconst ARCHIVED_ROWS = 622;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type PopoverProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  /** Label on the button that opens the popover. Defaults to \"Export\". */\n  triggerLabel?: string;\n  /** Number of rows in the report, before archived rows. Defaults to 12480. */\n  rows?: number;\n};\n\ntype Coords = { top: number; left: number; width: number };\n\n/* ------------------------------------------------------------------ */\n/* small switch                                                        */\n/* ------------------------------------------------------------------ */\n\nfunction ToggleRow({\n  label,\n  hint,\n  checked,\n  onChange,\n}: {\n  label: string;\n  hint: string;\n  checked: boolean;\n  onChange: (value: boolean) => void;\n}) {\n  return (\n    <label className=\"flex cursor-pointer items-start justify-between gap-3 py-1.5\">\n      <span className=\"min-w-0\">\n        <span className=\"block text-[12.5px] text-popover-foreground\">{label}</span>\n        <span className=\"mt-0.5 block text-[11px] leading-snug text-muted-foreground\">\n          {hint}\n        </span>\n      </span>\n      <span className=\"relative mt-0.5 inline-flex h-4 w-7 flex-none\">\n        <input\n          type=\"checkbox\"\n          checked={checked}\n          onChange={(event) => onChange(event.target.checked)}\n          className=\"peer sr-only\"\n        />\n        <span\n          aria-hidden=\"true\"\n          className={cn(\n            \"absolute inset-0 rounded-full transition-colors duration-200 peer-focus-visible:ring-2 peer-focus-visible:ring-ring peer-focus-visible:ring-offset-2 peer-focus-visible:ring-offset-popover\",\n            checked ? \"bg-foreground\" : \"bg-muted\"\n          )}\n        />\n        <span\n          aria-hidden=\"true\"\n          className={cn(\n            \"absolute top-0.5 h-3 w-3 rounded-full bg-background shadow-sm transition-[left] duration-200\",\n            checked ? \"left-3.5\" : \"left-0.5\"\n          )}\n        />\n      </span>\n    </label>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function Popover({\n  className,\n  title = \"Weekly digest\",\n  description = \"Northwind Labs · Product analytics\",\n  triggerLabel = \"Export\",\n  rows = BASE_ROWS,\n}: PopoverProps) {\n  const cardRef = useRef<HTMLDivElement | null>(null);\n  const triggerRef = useRef<HTMLButtonElement | null>(null);\n  const panelRef = useRef<HTMLDivElement | null>(null);\n  const firstFormatRef = useRef<HTMLButtonElement | null>(null);\n\n  const inView = useInView(cardRef, { once: true, margin: \"-10% 0px\" });\n  const uid = useId().replace(/:/g, \"\");\n  const panelId = `${uid}-export-popover`;\n\n  const [open, setOpen] = useState(false);\n  const [coords, setCoords] = useState<Coords | null>(null);\n  const [format, setFormat] = useState<Format>(\"csv\");\n  const [archived, setArchived] = useState(false);\n  const [emailCopy, setEmailCopy] = useState(true);\n  const [exported, setExported] = useState(false);\n\n  const totalRows = rows + (archived ? ARCHIVED_ROWS : 0);\n\n  const close = useCallback((returnFocus: boolean) => {\n    setOpen(false);\n    if (returnFocus) triggerRef.current?.focus();\n  }, []);\n\n  const openPanel = () => {\n    const trigger = triggerRef.current;\n    if (!trigger) return;\n    const rect = trigger.getBoundingClientRect();\n    const width = Math.min(304, window.innerWidth - 24);\n    const left = Math.max(\n      12,\n      Math.min(rect.right - width, window.innerWidth - width - 12)\n    );\n    setCoords({ top: rect.bottom + 8, left, width });\n    setOpen(true);\n  };\n\n  useEffect(() => {\n    if (!open) return;\n\n    const focusTimer = window.setTimeout(() => firstFormatRef.current?.focus(), 60);\n\n    const onPointerDown = (event: PointerEvent) => {\n      const target = event.target as Node | null;\n      if (!target) return;\n      if (panelRef.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, true);\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, true);\n    };\n  }, [open, close]);\n\n  const runExport = () => {\n    setExported(true);\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-haspopup=\"dialog\"\n            aria-expanded={open}\n            aria-controls={open ? panelId : undefined}\n            onClick={() => (open ? close(false) : openPanel())}\n            className=\"inline-flex flex-none items-center gap-1.5 rounded-md border border-border bg-background px-2.5 py-1.5 text-[12px] font-medium text-foreground transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n          >\n            <Download className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n            {triggerLabel}\n            <ChevronDown className=\"h-3 w-3 text-muted-foreground\" aria-hidden=\"true\" />\n          </button>\n        </div>\n\n        <dl className=\"mt-5 grid grid-cols-3 gap-px overflow-hidden rounded-lg border border-border bg-border\">\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">Users</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              12,480\n            </dd>\n          </div>\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">Sessions</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              38,204\n            </dd>\n          </div>\n          <div className=\"min-w-0 bg-card px-3 py-3\">\n            <dt className=\"truncate text-[11px] text-muted-foreground\">Retention</dt>\n            <dd className=\"mt-0.5 text-[18px] font-semibold leading-none tracking-tight tabular-nums\">\n              61.4%\n            </dd>\n          </div>\n        </dl>\n\n        <div className=\"mt-4 flex flex-wrap items-center justify-between gap-x-4 gap-y-1 text-[11.5px]\">\n          <span\n            className={cn(\n              \"inline-flex items-center gap-1.5\",\n              exported ? \"text-success\" : \"text-muted-foreground\"\n            )}\n            aria-live=\"polite\"\n          >\n            {exported ? (\n              <Check className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n            ) : null}\n            {exported\n              ? `Exported as ${format.toUpperCase()} · just now`\n              : \"Last export 2 days ago\"}\n          </span>\n          <span className=\"tabular-nums text-muted-foreground\">\n            {BASE_ROWS.toLocaleString(\"en-US\")} rows · 4.1 MB\n          </span>\n        </div>\n      </motion.div>\n\n      <AnimatePresence>\n        {open && coords ? (\n          <motion.div\n            ref={panelRef}\n            id={panelId}\n            role=\"dialog\"\n            aria-label=\"Export weekly digest\"\n            onKeyDown={(event) => {\n              if (event.key !== \"Escape\") return;\n              event.preventDefault();\n              close(true);\n            }}\n            initial={{ opacity: 0, scale: 0.95, y: -4 }}\n            animate={{ opacity: 1, scale: 1, y: 0 }}\n            exit={{ opacity: 0, scale: 0.97, y: -3 }}\n            transition={{ type: \"spring\", stiffness: 380, damping: 30, mass: 0.7 }}\n            style={{\n              position: \"fixed\",\n              top: coords.top,\n              left: coords.left,\n              width: coords.width,\n              maxHeight: `calc(100dvh - ${coords.top + 12}px)`,\n              transformOrigin: \"100% 0%\",\n            }}\n            className=\"z-50 overflow-y-auto rounded-xl border border-border bg-popover p-4 text-popover-foreground shadow-xl\"\n          >\n            <div className=\"flex items-start justify-between gap-3\">\n              <div className=\"min-w-0\">\n                <p className=\"text-[13px] font-semibold\">Export weekly digest</p>\n                <p className=\"mt-0.5 text-[11.5px] tabular-nums text-muted-foreground\">\n                  48 columns · {totalRows.toLocaleString(\"en-US\")} rows\n                </p>\n              </div>\n              <button\n                type=\"button\"\n                onClick={() => close(true)}\n                aria-label=\"Close export options\"\n                className=\"grid h-6 w-6 flex-none place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n              >\n                <X className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n              </button>\n            </div>\n\n            <div\n              role=\"radiogroup\"\n              aria-label=\"File format\"\n              className=\"mt-4 inline-flex w-full items-center rounded-lg bg-muted p-1\"\n            >\n              {FORMATS.map((option, index) => {\n                const active = format === option.id;\n                return (\n                  <button\n                    key={option.id}\n                    ref={index === 0 ? firstFormatRef : undefined}\n                    type=\"button\"\n                    role=\"radio\"\n                    aria-checked={active}\n                    onClick={() => setFormat(option.id)}\n                    className={cn(\n                      \"relative flex-1 rounded-md px-2 py-1 text-[12px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                      active ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\"\n                    )}\n                  >\n                    {active ? (\n                      <motion.span\n                        layoutId={`${uid}-format-pill`}\n                        aria-hidden=\"true\"\n                        className=\"absolute inset-0 rounded-md bg-popover shadow-sm ring-1 ring-border\"\n                        transition={{ type: \"spring\", stiffness: 520, damping: 38 }}\n                      />\n                    ) : null}\n                    <span className=\"relative\">{option.label}</span>\n                  </button>\n                );\n              })}\n            </div>\n\n            <div className=\"mt-2 divide-y divide-border\">\n              <ToggleRow\n                label=\"Include archived rows\"\n                hint={`Adds ${ARCHIVED_ROWS} rows from before June.`}\n                checked={archived}\n                onChange={setArchived}\n              />\n              <ToggleRow\n                label=\"Email a copy\"\n                hint=\"Sent to Ama Mensah when the file is ready.\"\n                checked={emailCopy}\n                onChange={setEmailCopy}\n              />\n            </div>\n\n            <div className=\"mt-3 flex items-center justify-between gap-3 border-t border-border pt-3\">\n              <span className=\"text-[11px] text-muted-foreground\">\n                {emailCopy ? \"Delivered by email\" : \"Download only\"}\n              </span>\n              <button\n                type=\"button\"\n                onClick={runExport}\n                className=\"inline-flex items-center gap-1.5 rounded-md bg-primary px-3 py-1.5 text-[12px] font-medium text-primary-foreground transition-colors hover:bg-primary/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-popover\"\n              >\n                <Download className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n                Export {totalRows.toLocaleString(\"en-US\")} rows\n              </button>\n            </div>\n          </motion.div>\n        ) : null}\n      </AnimatePresence>\n    </MotionConfig>\n  );\n}\n\nexport default Popover;\n"}]}