{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"status-picker","type":"registry:component","title":"Status picker","description":"Pick a status and the badge on your avatar turns into its shape: a dot, a moon, a stop sign or a ring.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/status-picker.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { usePreviewPlay } from \"@/lib/preview-play\";\nimport { cn } from \"@/lib/utils\";\n\nexport type Status = \"online\" | \"away\" | \"dnd\" | \"invisible\";\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\nconst TURN = { type: \"spring\", duration: 0.45, bounce: 0.25 } as const;\n\nconst STATUSES: { id: Status; label: string; hint?: string }[] = [\n  { id: \"online\", label: \"Online\" },\n  { id: \"away\", label: \"Away\" },\n  { id: \"dnd\", label: \"Do not disturb\", hint: \"Mutes notifications\" },\n  { id: \"invisible\", label: \"Invisible\", hint: \"Appear offline\" },\n];\n\n// Each status has its own shape, not just its own colour, so it reads at\n// a glance and for colour-blind eyes too. Presence colours are data, so\n// raw values.\nfunction Badge({ status, className }: { status: Status; className?: string }) {\n  return (\n    <svg viewBox=\"0 0 12 12\" className={cn(\"size-3\", className)} aria-hidden>\n      {status === \"online\" && <circle cx=\"6\" cy=\"6\" r=\"5\" fill=\"oklch(0.7 0.16 150)\" />}\n      {status === \"away\" && (\n        <path d=\"M6.6 1.05A5 5 0 1 0 10.95 5.4 4 4 0 0 1 6.6 1.05Z\" fill=\"oklch(0.78 0.15 75)\" />\n      )}\n      {status === \"dnd\" && (\n        <>\n          <circle cx=\"6\" cy=\"6\" r=\"5\" fill=\"oklch(0.62 0.2 25)\" />\n          <rect x=\"3\" y=\"5.1\" width=\"6\" height=\"1.8\" rx=\"0.9\" fill=\"#fff\" />\n        </>\n      )}\n      {status === \"invisible\" && (\n        <circle cx=\"6\" cy=\"6\" r=\"3.9\" fill=\"none\" stroke=\"oklch(0.65 0 0)\" strokeWidth=\"2.2\" />\n      )}\n    </svg>\n  );\n}\n\nexport function StatusPicker({\n  name,\n  initials,\n  status,\n  onStatusChange,\n  demo,\n  className,\n}: {\n  name: string;\n  initials: string;\n  status: Status;\n  onStatusChange: (next: Status) => void;\n  // Drives the menu from outside (the card preview) without focus.\n  demo?: { open: boolean; active: number } | null;\n  className?: string;\n}) {\n  const reduceMotion = useReducedMotion();\n  const id = useId();\n  const [openState, setOpen] = useState(false);\n  const [activeState, setActive] = useState(0);\n  const root = useRef<HTMLDivElement>(null);\n  const trigger = useRef<HTMLButtonElement>(null);\n  const items = useRef<(HTMLButtonElement | null)[]>([]);\n  const usingKeys = useRef(false);\n\n  const open = demo ? demo.open : openState;\n  const active = demo ? demo.active : activeState;\n  const current = STATUSES.find((s) => s.id === status)!;\n\n  const show = () => {\n    const i = STATUSES.findIndex((s) => s.id === status);\n    setActive(i);\n    setOpen(true);\n    if (usingKeys.current) requestAnimationFrame(() => items.current[i]?.focus());\n  };\n  const hide = () => {\n    setOpen(false);\n    if (usingKeys.current) trigger.current?.focus();\n  };\n  const choose = (next: Status) => {\n    onStatusChange(next);\n    hide();\n  };\n\n  // Closes on a press anywhere else.\n  useEffect(() => {\n    if (!openState) return;\n    const onDown = (e: PointerEvent) => {\n      if (!root.current?.contains(e.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", onDown);\n    return () => document.removeEventListener(\"pointerdown\", onDown);\n  }, [openState]);\n\n  const turn = reduceMotion ? { duration: 0 } : TURN;\n  const fade = reduceMotion ? { duration: 0 } : { duration: 0.2, ease: EASE_OUT };\n\n  return (\n    <div\n      ref={root}\n      className={cn(\"relative\", className)}\n      onPointerDownCapture={() => (usingKeys.current = false)}\n      onKeyDownCapture={() => (usingKeys.current = true)}\n    >\n      <button\n        ref={trigger}\n        type=\"button\"\n        aria-haspopup=\"menu\"\n        aria-expanded={open}\n        aria-controls={`${id}-menu`}\n        aria-label={`${name}, ${current.label}. Change status`}\n        onClick={() => (open ? hide() : show())}\n        onKeyDown={(e) => {\n          if (e.key === \"ArrowDown\" && !open) {\n            e.preventDefault();\n            show();\n          }\n        }}\n        className={cn(\n          \"flex items-center gap-3 rounded-2xl py-2 pr-4 pl-2 text-left outline-hidden transition-[background-color,scale] duration-150 ease-out hover:bg-muted focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.98]\",\n          open && \"bg-muted\",\n        )}\n      >\n        <span className=\"relative shrink-0\">\n          <span className=\"grid size-10 place-items-center rounded-full bg-foreground text-[13px] font-semibold text-background\">\n            {initials}\n          </span>\n          {/* The badge sits in a cut-out ring; its shape turns into the\n              next status, the old one spinning out as the new spins in. */}\n          <span className=\"absolute -right-0.5 -bottom-0.5 grid size-[18px] place-items-center overflow-hidden rounded-full bg-background\">\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.span\n                key={status}\n                className=\"grid place-items-center\"\n                initial={{ scale: 0.3, rotate: 120, opacity: 0 }}\n                animate={{ scale: 1, rotate: 0, opacity: 1 }}\n                exit={{ scale: 0.3, rotate: -120, opacity: 0, transition: { duration: 0.15 } }}\n                transition={turn}\n              >\n                <Badge status={status} />\n              </motion.span>\n            </AnimatePresence>\n          </span>\n        </span>\n        <span className=\"min-w-0\">\n          <span className=\"block text-[14px] font-medium text-foreground\">{name}</span>\n          <span className=\"relative block h-[18px] text-[13px] text-muted-foreground\">\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.span\n                key={status}\n                className=\"block whitespace-nowrap\"\n                initial={{ opacity: 0, y: 4, filter: \"blur(3px)\" }}\n                animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n                exit={{ opacity: 0, y: -4, filter: \"blur(3px)\", transition: { duration: 0.12 } }}\n                transition={fade}\n              >\n                {current.label}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n        </span>\n      </button>\n\n      <AnimatePresence>\n        {open && (\n          <motion.div\n            id={`${id}-menu`}\n            role=\"menu\"\n            aria-label=\"Set status\"\n            initial={{ opacity: 0, scale: 0.96, y: -4, filter: \"blur(4px)\" }}\n            animate={{ opacity: 1, scale: 1, y: 0, filter: \"blur(0px)\" }}\n            exit={{ opacity: 0, scale: 0.98, y: -2, transition: { duration: 0.12 } }}\n            transition={fade}\n            onKeyDown={(e) => {\n              const n = STATUSES.length;\n              if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n                e.preventDefault();\n                const next = (active + (e.key === \"ArrowDown\" ? 1 : -1) + n) % n;\n                setActive(next);\n                items.current[next]?.focus();\n              } else if (e.key === \"Escape\" || e.key === \"Tab\") {\n                e.preventDefault();\n                hide();\n              }\n            }}\n            // Grows out of the avatar it belongs to.\n            style={{ transformOrigin: \"24px 0px\" }}\n            className=\"absolute top-full left-0 z-10 mt-2 w-60 rounded-2xl bg-background p-1.5 shadow-raised\"\n          >\n            {STATUSES.map((s, i) => (\n              <button\n                key={s.id}\n                ref={(el) => {\n                  items.current[i] = el;\n                }}\n                type=\"button\"\n                role=\"menuitemradio\"\n                aria-checked={s.id === status}\n                tabIndex={i === active ? 0 : -1}\n                onClick={() => choose(s.id)}\n                onPointerMove={() => !demo && i !== active && setActive(i)}\n                className={cn(\n                  \"flex w-full items-center gap-3 rounded-xl px-3 py-2 text-left outline-hidden transition-[background-color] duration-100\",\n                  i === active && \"bg-muted\",\n                )}\n              >\n                <span className=\"grid size-4 shrink-0 place-items-center\">\n                  <Badge status={s.id} />\n                </span>\n                <span className=\"min-w-0 flex-1\">\n                  <span className=\"block text-[14px] text-foreground\">{s.label}</span>\n                  {s.hint && <span className=\"block text-[12px] text-muted-foreground\">{s.hint}</span>}\n                </span>\n                {s.id === status && (\n                  <svg\n                    viewBox=\"0 0 16 16\"\n                    className=\"size-4 shrink-0 text-foreground\"\n                    fill=\"none\"\n                    stroke=\"currentColor\"\n                    strokeWidth={1.5}\n                    strokeLinecap=\"round\"\n                    strokeLinejoin=\"round\"\n                    aria-hidden\n                  >\n                    <path d=\"M3.5 8.25l3 3 6-6.5\" />\n                  </svg>\n                )}\n              </button>\n            ))}\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nexport default function StatusPickerDemo() {\n  const play = usePreviewPlay();\n  const [status, setStatus] = useState<Status>(\"online\");\n  const [demo, setDemo] = useState<{ open: boolean; active: number } | null>(null);\n\n  // The card's hover show: step away, then go do-not-disturb, then back.\n  useEffect(() => {\n    if (play !== true) return;\n    const timers: ReturnType<typeof setTimeout>[] = [];\n    const at = (ms: number, fn: () => void) => timers.push(setTimeout(fn, ms));\n    const pick = (base: number, from: number, to: number, next: Status) => {\n      at(base, () => setDemo({ open: true, active: from }));\n      for (let i = from + 1, t = base + 450; i <= to; i++, t += 220) {\n        const a = i;\n        at(t, () => setDemo({ open: true, active: a }));\n      }\n      at(base + 450 + (to - from) * 220 + 250, () => {\n        setStatus(next);\n        setDemo({ open: false, active: to });\n      });\n    };\n    const run = () => {\n      pick(300, 0, 1, \"away\");\n      pick(2100, 1, 2, \"dnd\");\n      at(3900, () => setDemo({ open: true, active: 2 }));\n      at(4350, () => setDemo({ open: true, active: 1 }));\n      at(4570, () => setDemo({ open: true, active: 0 }));\n      at(4900, () => {\n        setStatus(\"online\");\n        setDemo({ open: false, active: 0 });\n      });\n      at(6200, run);\n    };\n    run();\n    return () => {\n      timers.forEach(clearTimeout);\n      setDemo(null);\n      setStatus(\"online\");\n    };\n  }, [play]);\n\n  return (\n    <div className=\"flex h-72 w-[min(260px,100%)] items-start justify-center pt-4\">\n      <StatusPicker\n        name=\"Maya Ruiz\"\n        initials=\"MR\"\n        status={status}\n        onStatusChange={setStatus}\n        demo={play === true ? demo ?? { open: false, active: 0 } : null}\n      />\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"},{"path":"lib/preview-play.ts","type":"registry:lib","target":"lib/preview-play.ts","content":"\"use client\";\n\nimport { createContext, useContext } from \"react\";\n\n// Lets a demo know whether it's showing in an index card, and whether that\n// card is hovered or focused. Demos use it to act themselves out on hover:\n// a star button stars itself, a checkbox scribbles, and so on.\n//\n// null: not in a preview (the component's own page, or real use).\n// false: in a preview, at rest.\n// true: in a preview, being hovered or focused: play the show.\nexport const PreviewPlayContext = createContext<boolean | null>(null);\n\nexport function usePreviewPlay() {\n  return useContext(PreviewPlayContext);\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":["inputs"],"docs":"From ui lab: https://lab.xevrion.dev/lab/status-picker"}