{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "notification-matrix",
  "type": "registry:block",
  "title": "Notification Matrix",
  "description": "Events down the side, email, push and in-app across the top: group checkboxes that go half-checked, locked security rows with a reason, edited cells that glow, and a save bar that floats in only when something changed.",
  "categories": [
    "block",
    "Dashboards"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/checkbox.json",
    "https://coss.com/ui/r/switch.json",
    "https://coss.com/ui/r/tooltip.json"
  ],
  "files": [
    {
      "path": "registry/devl/notification-matrix.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport { BellIcon, CheckIcon, InboxIcon, LockIcon, type LucideIcon, MailIcon, MoonIcon } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { useEffect, useId, useMemo, useState } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Checkbox } from \"@/components/ui/checkbox\";\nimport { Switch } from \"@/components/ui/switch\";\nimport { Tooltip, TooltipPopup, TooltipProvider, TooltipTrigger } from \"@/components/ui/tooltip\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface NotificationChannel {\n  id: string;\n  label: string;\n  icon: LucideIcon;\n}\n\nexport interface NotificationEvent {\n  id: string;\n  label: string;\n  description?: string;\n  locked?: Record<string, string>;\n}\n\nexport interface NotificationGroup {\n  id: string;\n  label: string;\n  icon?: LucideIcon;\n  events: NotificationEvent[];\n}\n\nexport type NotificationPrefs = Record<string, Record<string, boolean>>;\n\nexport interface NotificationMatrixProps {\n  title?: string;\n  description?: string;\n  groups: NotificationGroup[];\n  channels?: NotificationChannel[];\n  defaultValue?: NotificationPrefs;\n  quietHours?: string;\n  onSave?: (value: NotificationPrefs) => Promise<void>;\n  className?: string;\n}\n\nconst ease = [0.23, 1, 0.32, 1] as const;\nconst press = \"transition-[box-shadow,scale,background-color] duration-150 active:scale-[0.97]\";\nconst DEFAULT_CHANNELS: NotificationChannel[] = [\n  { id: \"email\", label: \"Email\", icon: MailIcon },\n  { id: \"push\", label: \"Push\", icon: BellIcon },\n  { id: \"inApp\", label: \"In-app\", icon: InboxIcon },\n];\n\nfunction normalise(groups: NotificationGroup[], channels: NotificationChannel[], value: NotificationPrefs = {}) {\n  const out: NotificationPrefs = {};\n  for (const g of groups) {\n    for (const e of g.events) {\n      out[e.id] = {};\n      for (const c of channels) out[e.id][c.id] = e.locked?.[c.id] ? true : Boolean(value[e.id]?.[c.id]);\n    }\n  }\n  return out;\n}\n\nconst COLS = \"grid-cols-[minmax(0,1fr)_repeat(3,3.25rem)] sm:grid-cols-[minmax(0,1fr)_repeat(3,5.5rem)]\";\n\nexport function NotificationMatrix({\n  title = \"Notifications\",\n  description = \"Choose where each kind of update reaches you.\",\n  groups,\n  channels = DEFAULT_CHANNELS,\n  defaultValue,\n  quietHours = \"10 pm – 7 am\",\n  onSave = async () => {},\n  className,\n}: NotificationMatrixProps) {\n  const reduce = useReducedMotion();\n  const uid = useId();\n  const initial = useMemo(() => normalise(groups, channels, defaultValue), [groups, channels, defaultValue]);\n  const [saved, setSaved] = useState(initial);\n  const [value, setValue] = useState(initial);\n  const [quiet, setQuiet] = useState(true);\n  const [savedQuiet, setSavedQuiet] = useState(true);\n  const [phase, setPhase] = useState<\"idle\" | \"saving\" | \"saved\">(\"idle\");\n\n  const changes = useMemo(() => {\n    let n = quiet !== savedQuiet ? 1 : 0;\n    for (const id of Object.keys(value)) for (const c of channels) if (value[id][c.id] !== saved[id]?.[c.id]) n++;\n    return n;\n  }, [value, saved, channels, quiet, savedQuiet]);\n\n  useEffect(() => {\n    if (phase !== \"saved\") return;\n    const t = setTimeout(() => setPhase(\"idle\"), 1400);\n    return () => clearTimeout(t);\n  }, [phase]);\n\n  const set = (eventIds: string[], channel: string, on: boolean) => {\n    setValue((prev) => {\n      const next = { ...prev };\n      for (const id of eventIds) {\n        const ev = groups.flatMap((g) => g.events).find((e) => e.id === id);\n        if (ev?.locked?.[channel]) continue;\n        next[id] = { ...next[id], [channel]: on };\n      }\n      return next;\n    });\n  };\n\n  const save = async () => {\n    setPhase(\"saving\");\n    await onSave(value);\n    setSaved(value);\n    setSavedQuiet(quiet);\n    setPhase(\"saved\");\n  };\n\n  const discard = () => {\n    setValue(saved);\n    setQuiet(savedQuiet);\n  };\n\n  const showBar = changes > 0 || phase !== \"idle\";\n\n  return (\n    <TooltipProvider delay={300}>\n      <section className={cn(\"relative mx-auto w-full max-w-[880px] overflow-clip rounded-2xl border bg-card text-card-foreground shadow-xs/5\", className)}>\n        <header className=\"flex flex-col gap-4 px-4 pt-5 pb-4 sm:flex-row sm:items-start sm:justify-between sm:px-6 sm:pt-6\">\n          <div>\n            <h2 className=\"text-balance font-semibold text-lg tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-pretty text-muted-foreground text-sm\">{description}</p>\n          </div>\n          <label\n            className=\"flex cursor-pointer items-center gap-3 rounded-xl border bg-muted/40 py-2 ps-3 pe-2.5 text-sm sm:max-w-[290px]\"\n            htmlFor={`${uid}-quiet`}\n          >\n            <MoonIcon className=\"size-4 shrink-0 text-muted-foreground\" />\n            <span className=\"min-w-0 flex-1\">\n              <span className=\"block font-medium\">Quiet hours</span>\n              <span className=\"block text-muted-foreground text-xs\">Hold push alerts {quietHours}</span>\n            </span>\n            <Switch checked={quiet} id={`${uid}-quiet`} onCheckedChange={setQuiet} />\n          </label>\n        </header>\n\n        <div className={cn(\"sticky top-0 z-[5] grid items-end border-y bg-card/95 px-4 py-2.5 backdrop-blur-sm sm:px-6\", COLS)}>\n          <span className=\"text-muted-foreground text-xs\">Event</span>\n          {channels.map((c) => (\n            <span className=\"flex flex-col items-center gap-1 text-muted-foreground text-xs\" key={c.id}>\n              <c.icon className=\"size-4\" />\n              {c.label}\n            </span>\n          ))}\n        </div>\n\n        <div className=\"divide-y\">\n          {groups.map((g) => {\n            const ids = g.events.map((e) => e.id);\n            return (\n              <div aria-labelledby={`${uid}-${g.id}`} key={g.id} role=\"group\">\n                <div className={cn(\"grid items-center bg-muted/40 px-4 py-2 sm:px-6\", COLS)}>\n                  <span className=\"flex min-w-0 items-center gap-2 font-medium text-sm\" id={`${uid}-${g.id}`}>\n                    {g.icon && <g.icon className=\"size-4 shrink-0 text-muted-foreground\" />}\n                    <span className=\"truncate\">{g.label}</span>\n                  </span>\n                  {channels.map((c) => {\n                    const editable = g.events.filter((e) => !e.locked?.[c.id]);\n                    const on = editable.filter((e) => value[e.id][c.id]).length;\n                    const all = editable.length > 0 && on === editable.length;\n                    const some = on > 0 && !all;\n                    if (!editable.length) {\n                      return (\n                        <span className=\"flex h-9 items-center justify-center text-muted-foreground/60\" key={c.id}>\n                          <LockIcon aria-label={`${g.label} by ${c.label} is always on`} className=\"size-3.5\" />\n                        </span>\n                      );\n                    }\n                    return (\n                      <label className=\"flex h-9 cursor-pointer items-center justify-center\" key={c.id}>\n                        <Checkbox\n                          aria-label={`All ${g.label} by ${c.label}`}\n                          checked={all}\n                          indeterminate={some}\n                          onCheckedChange={() => set(ids, c.id, !all)}\n                        />\n                      </label>\n                    );\n                  })}\n                </div>\n                <ul className=\"divide-y divide-border/60\">\n                  {g.events.map((e) => (\n                    <li className={cn(\"grid items-center px-4 py-2.5 transition-colors duration-150 hover:bg-accent/30 sm:px-6\", COLS)} key={e.id}>\n                      <div className=\"min-w-0 py-0.5 pe-2\">\n                        <p className=\"text-sm\">{e.label}</p>\n                        {e.description && <p className=\"mt-0.5 text-pretty text-muted-foreground text-xs\">{e.description}</p>}\n                      </div>\n                      {channels.map((c) => {\n                        const lock = e.locked?.[c.id];\n                        const on = value[e.id][c.id];\n                        const dirty = on !== saved[e.id][c.id];\n                        if (lock) {\n                          return (\n                            <div className=\"flex justify-center\" key={c.id}>\n                              <Tooltip>\n                                <TooltipTrigger\n                                  aria-label={`${e.label} by ${c.label} is always on: ${lock}`}\n                                  className=\"flex size-9 cursor-help items-center justify-center rounded-lg text-muted-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                                  render={<button type=\"button\" />}\n                                >\n                                  <LockIcon className=\"size-3.5\" />\n                                </TooltipTrigger>\n                                <TooltipPopup>{lock}</TooltipPopup>\n                              </Tooltip>\n                            </div>\n                          );\n                        }\n                        return (\n                          <label className=\"relative flex h-9 cursor-pointer items-center justify-center\" key={c.id}>\n                            <AnimatePresence initial={false}>\n                              {dirty && (\n                                <motion.span\n                                  animate={{ opacity: 1, scale: 1 }}\n                                  aria-hidden\n                                  className=\"absolute size-8 rounded-lg bg-info/10 dark:bg-info/20\"\n                                  exit={{ opacity: 0, scale: 0.85 }}\n                                  initial={{ opacity: 0, scale: 0.85 }}\n                                  transition={{ duration: 0.15, ease }}\n                                />\n                              )}\n                            </AnimatePresence>\n                            <Checkbox\n                              aria-label={`${e.label} by ${c.label}`}\n                              checked={on}\n                              className=\"relative\"\n                              onCheckedChange={(v) => set([e.id], c.id, Boolean(v))}\n                            />\n                          </label>\n                        );\n                      })}\n                    </li>\n                  ))}\n                </ul>\n              </div>\n            );\n          })}\n        </div>\n\n        <footer className=\"flex h-[72px] items-center border-t px-4 text-muted-foreground text-xs sm:px-6\">\n          Security alerts always reach your email, whatever you choose here.\n        </footer>\n\n        <div className=\"sticky bottom-0 z-10 h-0\">\n          <AnimatePresence>\n            {showBar && (\n              <motion.div\n                animate={{ opacity: 1, y: 0 }}\n                className=\"absolute inset-x-3 bottom-3 flex h-12 items-center gap-2 rounded-xl border bg-popover ps-3.5 pe-2 text-sm shadow-lg/5 sm:inset-x-4\"\n                exit={{ opacity: 0, y: reduce ? 0 : 12, transition: { duration: 0.16 } }}\n                initial={{ opacity: 0, y: reduce ? 0 : 12 }}\n                role=\"status\"\n                transition={{ duration: 0.22, ease }}\n              >\n                <AnimatePresence initial={false} mode=\"popLayout\">\n                  {phase === \"saved\" ? (\n                    <motion.span\n                      animate={{ opacity: 1, y: 0 }}\n                      className=\"flex flex-1 items-center gap-2 font-medium text-success-foreground\"\n                      initial={{ opacity: 0, y: 6 }}\n                      key=\"saved\"\n                      transition={{ duration: 0.18, ease }}\n                    >\n                      <CheckIcon className=\"size-4\" strokeWidth={2.5} />\n                      Preferences saved\n                    </motion.span>\n                  ) : (\n                    <motion.span\n                      animate={{ opacity: 1, y: 0 }}\n                      className=\"flex min-w-0 flex-1 items-center gap-2\"\n                      exit={{ opacity: 0, y: -6 }}\n                      initial={false}\n                      key=\"dirty\"\n                      transition={{ duration: 0.18, ease }}\n                    >\n                      <span className=\"size-1.5 shrink-0 rounded-full bg-info\" />\n                      <span className=\"truncate\">\n                        <span className=\"font-medium tabular-nums\">{changes}</span> <span className=\"hidden sm:inline\">unsaved </span>\n                        {changes === 1 ? \"change\" : \"changes\"}\n                      </span>\n                    </motion.span>\n                  )}\n                </AnimatePresence>\n                {phase !== \"saved\" && (\n                  <>\n                    <Button className={press} disabled={phase === \"saving\"} onClick={discard} size=\"sm\" variant=\"ghost\">\n                      Discard\n                    </Button>\n                    <Button className={press} loading={phase === \"saving\"} onClick={save} size=\"sm\">\n                      Save changes\n                    </Button>\n                  </>\n                )}\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </div>\n      </section>\n    </TooltipProvider>\n  );\n}\n"
    }
  ]
}