{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cookie-consent-banner",
  "title": "Cookie Consent Banner",
  "description": "A cookie consent banner as a corner card or a bottom bar: accept all, reject all with equal weight, or open per-category switches (necessary locked on), and the choice is remembered, with a small button to change it later.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/cookie-consent-banner/cookie-consent-banner.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface CookieCategory {\n  id: string;\n  label: string;\n  description: string;\n  /** Always on and can't be switched off (strictly necessary cookies). */\n  required?: boolean;\n  defaultEnabled?: boolean;\n}\n\nexport type CookieConsent = Record<string, boolean>;\n\nexport interface CookieConsentBannerProps {\n  categories?: CookieCategory[];\n  title?: string;\n  message?: string;\n  policyHref?: string;\n  policyLabel?: string;\n  /** bar: full width along the bottom. card: a floating card in a corner. */\n  variant?: \"bar\" | \"card\";\n  position?: \"left\" | \"right\";\n  /** Called with every category's on/off once the visitor chooses. */\n  onConsentChange?: (consent: CookieConsent) => void;\n  /** Where the choice is remembered. Set to \"\" to not remember it (e.g. previews). */\n  storageKey?: string;\n  /** Show it even if a choice is already stored. */\n  forceOpen?: boolean;\n  /** Seconds to wait before it slides in. */\n  delay?: number;\n  /** A small button that reopens the settings after a choice. */\n  showReopen?: boolean;\n  /** Place it inside the nearest positioned ancestor instead of the viewport. */\n  contained?: boolean;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.58)\", faint: \"rgba(245,244,241,0.12)\", panel: \"#111111\", border: \"rgba(255,255,255,0.12)\", hover: \"rgba(255,255,255,0.06)\", primary: \"#F5F4F1\", primaryText: \"#0A0A0A\", secondary: \"rgba(255,255,255,0.06)\", track: \"rgba(255,255,255,0.16)\", focus: \"rgba(245,244,241,0.85)\", shadow: \"0 20px 60px rgba(0,0,0,0.5)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.58)\", faint: \"rgba(10,10,10,0.1)\", panel: \"#FFFFFF\", border: \"rgba(10,10,10,0.12)\", hover: \"rgba(10,10,10,0.04)\", primary: \"#0A0A0A\", primaryText: \"#FFFFFF\", secondary: \"rgba(10,10,10,0.05)\", track: \"rgba(10,10,10,0.16)\", focus: \"rgba(10,10,10,0.85)\", shadow: \"0 20px 60px rgba(10,10,10,0.14)\" },\n};\n\nconst DEFAULT_CATEGORIES: CookieCategory[] = [\n  { id: \"necessary\", label: \"Strictly necessary\", description: \"Keep you signed in, remember your cart and keep the site secure. These can't be turned off.\", required: true },\n  { id: \"analytics\", label: \"Analytics\", description: \"Help us see which pages are useful and where people get stuck, in aggregate.\" },\n  { id: \"marketing\", label: \"Marketing\", description: \"Let us measure ads and show you relevant offers on other sites.\" },\n  { id: \"preferences\", label: \"Preferences\", description: \"Remember choices like language and region between visits.\" },\n];\n\nfunction Switch({ on, disabled, onChange, label, p, reduce }: { on: boolean; disabled?: boolean; onChange: () => void; label: string; p: (typeof PALETTES)[\"dark\"]; reduce: boolean }) {\n  return (\n    <button\n      type=\"button\"\n      role=\"switch\"\n      aria-checked={on}\n      aria-label={label}\n      disabled={disabled}\n      onClick={onChange}\n      className=\"relative shrink-0 cursor-pointer rounded-full border-none p-0 outline-none focus-visible:ring-2 disabled:cursor-not-allowed\"\n      style={{ width: 36, height: 21, background: on ? p.primary : p.track, opacity: disabled ? 0.55 : 1, transition: \"background 0.18s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n    >\n      <motion.span\n        className=\"absolute rounded-full\"\n        style={{ top: 2.5, left: 2.5, width: 16, height: 16, background: on ? p.primaryText : p.panel, boxShadow: \"0 1px 3px rgba(0,0,0,0.3)\" }}\n        initial={false}\n        animate={{ x: on ? 15 : 0 }}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 600, damping: 34 }}\n      />\n    </button>\n  );\n}\n\nexport function CookieConsentBanner({\n  categories = DEFAULT_CATEGORIES,\n  title = \"We use cookies\",\n  message = \"We use cookies to run the site, understand how it's used and, with your permission, to personalise offers. You can change your mind at any time.\",\n  policyHref = \"/privacy\",\n  policyLabel = \"Cookie policy\",\n  variant = \"card\",\n  position = \"left\",\n  onConsentChange,\n  storageKey = \"cookie-consent\",\n  forceOpen = false,\n  delay = 0.6,\n  showReopen = true,\n  contained = false,\n  theme = \"dark\",\n  className,\n}: CookieConsentBannerProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const [open, setOpen] = React.useState(false);\n  const [decided, setDecided] = React.useState(false);\n  const [customize, setCustomize] = React.useState(false);\n  const [prefs, setPrefs] = React.useState<CookieConsent>(() => Object.fromEntries(categories.map((c) => [c.id, Boolean(c.required || c.defaultEnabled)])));\n\n  // Decide after mount (and after the delay) whether to show it, so the server HTML never differs.\n  React.useEffect(() => {\n    const t = window.setTimeout(() => {\n      let stored: CookieConsent | null = null;\n      try {\n        stored = storageKey ? (JSON.parse(localStorage.getItem(storageKey) ?? \"null\") as CookieConsent | null) : null;\n      } catch {\n        stored = null;\n      }\n      if (stored && !forceOpen) {\n        setPrefs((prev) => ({ ...prev, ...stored }));\n        setDecided(true);\n      } else setOpen(true);\n    }, delay * 1000);\n    return () => window.clearTimeout(t);\n  }, [storageKey, forceOpen, delay]);\n\n  const save = (consent: CookieConsent) => {\n    const full = Object.fromEntries(categories.map((c) => [c.id, c.required ? true : Boolean(consent[c.id])]));\n    setPrefs(full);\n    if (storageKey) {\n      try {\n        localStorage.setItem(storageKey, JSON.stringify(full));\n      } catch {\n        // Storage can be unavailable (private mode); the choice still applies for this visit.\n      }\n    }\n    onConsentChange?.(full);\n    setOpen(false);\n    setCustomize(false);\n    setDecided(true);\n  };\n\n  const all = (value: boolean) => save(Object.fromEntries(categories.map((c) => [c.id, value])));\n\n  const pos = contained ? \"absolute\" : \"fixed\";\n  const bar = variant === \"bar\";\n  const placement: React.CSSProperties = bar ? { left: 12, right: 12, bottom: 12 } : { bottom: 16, [position]: 16, width: 380, maxWidth: \"calc(100% - 32px)\" };\n\n  const button = (kind: \"primary\" | \"secondary\" | \"ghost\", label: string, onClick: () => void) => (\n    <button\n      type=\"button\"\n      onClick={onClick}\n      className=\"cursor-pointer border-none font-semibold whitespace-nowrap outline-none focus-visible:ring-2\"\n      style={{\n        height: 36,\n        padding: kind === \"ghost\" ? \"0 2px\" : \"0 14px\",\n        borderRadius: 10,\n        fontSize: 13,\n        background: kind === \"primary\" ? p.primary : kind === \"secondary\" ? p.secondary : \"transparent\",\n        color: kind === \"primary\" ? p.primaryText : p.text,\n        border: kind === \"secondary\" ? `1px solid ${p.border}` : \"none\",\n        textDecoration: kind === \"ghost\" ? \"underline\" : undefined,\n        textUnderlineOffset: 3,\n        [\"--tw-ring-color\" as string]: p.focus,\n      }}\n    >\n      {label}\n    </button>\n  );\n\n  return (\n    <>\n      <AnimatePresence>\n        {open && (\n          <motion.section\n            key=\"banner\"\n            role=\"dialog\"\n            aria-labelledby={`${uid}-title`}\n            aria-describedby={`${uid}-msg`}\n            initial={reduce ? { opacity: 0 } : { opacity: 0, y: 24 }}\n            animate={{ opacity: 1, y: 0 }}\n            exit={reduce ? { opacity: 0 } : { opacity: 0, y: 24 }}\n            transition={{ type: \"spring\", stiffness: 380, damping: 34 }}\n            className={cn(pos, \"z-[70] flex flex-col overflow-hidden\", className)}\n            style={{ ...placement, maxHeight: \"calc(100% - 24px)\", padding: 16, gap: 12, borderRadius: 16, background: p.panel, border: `1px solid ${p.border}`, boxShadow: p.shadow, fontFamily: \"Inter, sans-serif\" }}\n          >\n            <div className={cn(\"flex shrink-0\", bar ? \"flex-wrap items-center justify-between\" : \"flex-col\")} style={{ gap: bar ? 16 : 12 }}>\n              <div className=\"flex min-w-0 flex-1 items-start\" style={{ gap: 12, minWidth: bar ? 260 : undefined }}>\n                <svg className=\"shrink-0\" width=\"22\" height=\"22\" viewBox=\"0 0 24 24\" fill=\"none\" stroke={p.text} strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" style={{ marginTop: 1 }}>\n                  <path d=\"M12 3a9 9 0 1 0 9 9 3 3 0 0 1-3.5-3A3 3 0 0 1 14 5.5 3 3 0 0 1 12 3Z\" />\n                  <circle cx=\"8.5\" cy=\"10\" r=\"0.9\" fill={p.text} />\n                  <circle cx=\"11\" cy=\"15.5\" r=\"0.9\" fill={p.text} />\n                  <circle cx=\"16\" cy=\"14\" r=\"0.9\" fill={p.text} />\n                </svg>\n                <div className=\"flex min-w-0 flex-col\" style={{ gap: 4 }}>\n                  <h2 id={`${uid}-title`} className=\"m-0 font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: 15 }}>\n                    {title}\n                  </h2>\n                  <p id={`${uid}-msg`} className=\"m-0\" style={{ color: p.muted, fontSize: 13, lineHeight: 1.55 }}>\n                    {message}{\" \"}\n                    {policyHref && (\n                      <a href={policyHref} className=\"font-medium underline underline-offset-2\" style={{ color: p.text }}>\n                        {policyLabel}\n                      </a>\n                    )}\n                  </p>\n                </div>\n              </div>\n              <div className=\"flex flex-wrap items-center\" style={{ gap: 8, justifyContent: bar ? \"flex-end\" : \"flex-start\" }}>\n                {button(\"ghost\", customize ? \"Hide options\" : \"Customize\", () => setCustomize((c) => !c))}\n                <span className=\"flex-1\" />\n                {button(\"secondary\", \"Reject all\", () => all(false))}\n                {button(\"primary\", \"Accept all\", () => all(true))}\n              </div>\n            </div>\n\n            <AnimatePresence initial={false}>\n              {customize && (\n                <motion.div\n                  key=\"prefs\"\n                  initial={{ height: 0, opacity: 0 }}\n                  animate={{ height: \"auto\", opacity: 1 }}\n                  exit={{ height: 0, opacity: 0 }}\n                  transition={{ duration: reduce ? 0 : 0.24, ease: [0.22, 1, 0.36, 1] }}\n                  className=\"flex min-h-0 flex-col overflow-hidden\"\n                >\n                  <ul className={cn(\"m-0 grid min-h-0 list-none overflow-y-auto overscroll-contain p-0\", bar ? \"sm:grid-cols-2\" : \"\")} style={{ gap: 6, paddingTop: 4, scrollbarWidth: \"thin\" }}>\n                    {categories.map((c) => (\n                      <li key={c.id} className=\"flex items-start justify-between\" style={{ gap: 12, padding: 12, borderRadius: 12, border: `1px solid ${p.faint}` }}>\n                        <div className=\"flex min-w-0 flex-col\" style={{ gap: 3 }}>\n                          <span id={`${uid}-${c.id}`} className=\"font-semibold\" style={{ color: p.text, fontSize: 13 }}>\n                            {c.label}\n                            {c.required && <span style={{ color: p.muted, fontWeight: 500 }}> · Always on</span>}\n                          </span>\n                          <span style={{ color: p.muted, fontSize: 12, lineHeight: 1.5 }}>{c.description}</span>\n                        </div>\n                        <Switch on={Boolean(prefs[c.id])} disabled={c.required} onChange={() => setPrefs((prev) => ({ ...prev, [c.id]: !prev[c.id] }))} label={c.label} p={p} reduce={reduce} />\n                      </li>\n                    ))}\n                  </ul>\n                  <div className=\"flex shrink-0 justify-end\" style={{ paddingTop: 10 }}>\n                    {button(\"primary\", \"Save preferences\", () => save(prefs))}\n                  </div>\n                </motion.div>\n              )}\n            </AnimatePresence>\n          </motion.section>\n        )}\n      </AnimatePresence>\n\n      <AnimatePresence>\n        {showReopen && decided && !open && (\n          <motion.button\n            key=\"reopen\"\n            type=\"button\"\n            aria-label=\"Cookie settings\"\n            onClick={() => {\n              setCustomize(true);\n              setOpen(true);\n            }}\n            initial={{ opacity: 0, scale: 0.8 }}\n            animate={{ opacity: 1, scale: 1 }}\n            exit={{ opacity: 0, scale: 0.8 }}\n            className={cn(pos, \"z-[70] flex cursor-pointer items-center justify-center rounded-full border-none outline-none focus-visible:ring-2\")}\n            style={{ bottom: 16, [position]: 16, width: 40, height: 40, background: p.panel, border: `1px solid ${p.border}`, boxShadow: p.shadow, color: p.text, [\"--tw-ring-color\" as string]: p.focus }}\n          >\n            <svg width=\"18\" height=\"18\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n              <path d=\"M12 3a9 9 0 1 0 9 9 3 3 0 0 1-3.5-3A3 3 0 0 1 14 5.5 3 3 0 0 1 12 3Z\" />\n              <circle cx=\"8.5\" cy=\"10\" r=\"0.9\" fill=\"currentColor\" />\n              <circle cx=\"11\" cy=\"15.5\" r=\"0.9\" fill=\"currentColor\" />\n              <circle cx=\"16\" cy=\"14\" r=\"0.9\" fill=\"currentColor\" />\n            </svg>\n          </motion.button>\n        )}\n      </AnimatePresence>\n    </>\n  );\n}\n\nexport default CookieConsentBanner;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}