{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cookie-consent","type":"registry:ui","title":"Cookie consent","description":"Floating consent card with Accept all, Reject and customizable Analytics and Marketing toggles.","dependencies":["lucide-react","motion"],"registryDependencies":["utils"],"files":[{"path":"registry/items/cookie-consent/cookie-consent.tsx","type":"registry:ui","target":"components/facade/cookie-consent.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { ChevronDown } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface ConsentState {\n  necessary: true;\n  analytics: boolean;\n  marketing: boolean;\n}\n\nexport type ConsentAction = \"accept-all\" | \"reject\" | \"save\";\n\nexport interface CookieConsentProps {\n  /** Called whenever the visitor makes a choice. */\n  onChange?: (consent: ConsentState, action: ConsentAction) => void;\n  /** Show the card. Uncontrolled default: shown after `delay` unless a stored choice exists. */\n  open?: boolean;\n  /** Delay before the card appears, in ms. */\n  delay?: number;\n  /** localStorage key to remember the choice. Set to null to disable persistence. */\n  storageKey?: string | null;\n  title?: string;\n  description?: string;\n  policyHref?: string;\n  /** Position absolute inside the nearest positioned parent instead of fixed to the viewport. */\n  contained?: boolean;\n  className?: string;\n}\n\nconst CATEGORIES = [\n  {\n    key: \"necessary\",\n    label: \"Strictly necessary\",\n    description: \"Required for booking, forms and security. Always on.\",\n    locked: true,\n  },\n  {\n    key: \"analytics\",\n    label: \"Analytics\",\n    description: \"Helps us understand which pages are useful so we can improve them.\",\n    locked: false,\n  },\n  {\n    key: \"marketing\",\n    label: \"Marketing\",\n    description: \"Lets us measure ads and show you relevant offers on other sites.\",\n    locked: false,\n  },\n] as const;\n\nconst EASE = [0.16, 1, 0.3, 1] as const;\n\nfunction Toggle({\n  checked,\n  onChange,\n  disabled,\n  labelledBy,\n  describedBy,\n}: {\n  checked: boolean;\n  onChange?: (v: boolean) => void;\n  disabled?: boolean;\n  labelledBy: string;\n  describedBy: string;\n}) {\n  return (\n    <button\n      type=\"button\"\n      role=\"switch\"\n      aria-checked={checked}\n      aria-labelledby={labelledBy}\n      aria-describedby={describedBy}\n      disabled={disabled}\n      onClick={() => onChange?.(!checked)}\n      className={cn(\n        \"relative inline-flex h-[22px] w-9 shrink-0 items-center rounded-full p-0.5 transition-colors duration-200\",\n        \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n        checked ? \"bg-brand\" : \"bg-foreground/15\",\n        disabled && \"cursor-not-allowed opacity-60\",\n      )}\n    >\n      <motion.span\n        className=\"size-[18px] rounded-full bg-brand-foreground\"\n        animate={{ x: checked ? 14 : 0 }}\n        transition={{ type: \"spring\", stiffness: 400, damping: 34 }}\n      />\n    </button>\n  );\n}\n\nexport function CookieConsent({\n  onChange,\n  open: openProp,\n  delay = 600,\n  storageKey = \"facade-cookie-consent\",\n  title = \"We value your privacy\",\n  description = \"We use cookies to run this site, understand traffic and personalize offers. You can change your choice anytime.\",\n  policyHref = \"#privacy\",\n  contained = false,\n  className,\n}: CookieConsentProps) {\n  const reduce = useReducedMotion();\n  const [shown, setShown] = React.useState(false);\n  const [expanded, setExpanded] = React.useState(false);\n  const [prefs, setPrefs] = React.useState({ analytics: false, marketing: false });\n  const uid = React.useId();\n  const open = openProp ?? shown;\n\n  React.useEffect(() => {\n    if (openProp !== undefined) return;\n    let stored = false;\n    if (storageKey) {\n      try {\n        stored = window.localStorage.getItem(storageKey) !== null;\n      } catch {\n        stored = false;\n      }\n    }\n    if (stored) return;\n    const id = window.setTimeout(() => setShown(true), delay);\n    return () => window.clearTimeout(id);\n  }, [openProp, delay, storageKey]);\n\n  const decide = (consent: ConsentState, action: ConsentAction) => {\n    if (storageKey) {\n      try {\n        window.localStorage.setItem(storageKey, JSON.stringify(consent));\n      } catch {\n        /* storage unavailable, keep going */\n      }\n    }\n    onChange?.(consent, action);\n    setShown(false);\n  };\n\n  const acceptAll = () => decide({ necessary: true, analytics: true, marketing: true }, \"accept-all\");\n  const reject = () => decide({ necessary: true, analytics: false, marketing: false }, \"reject\");\n  const save = () => decide({ necessary: true, ...prefs }, \"save\");\n\n  const btnBase =\n    \"h-10 rounded-full text-[13px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\";\n  const btnSecondary = cn(btnBase, \"bg-muted text-foreground hover:bg-foreground/10\");\n  const btnPrimary = cn(btnBase, \"bg-foreground text-background hover:opacity-90\");\n\n  return (\n    <AnimatePresence>\n      {open && (\n        <motion.section\n          role=\"dialog\"\n          aria-modal=\"false\"\n          aria-labelledby={`${uid}-title`}\n          aria-describedby={`${uid}-desc`}\n          initial={reduce ? { opacity: 0 } : { opacity: 0, y: 20 }}\n          animate={{ opacity: 1, y: 0 }}\n          exit={reduce ? { opacity: 0 } : { opacity: 0, y: 12, transition: { duration: 0.2 } }}\n          transition={{ duration: 0.6, ease: EASE }}\n          className={cn(\n            contained ? \"absolute\" : \"fixed\",\n            \"bottom-4 left-4 right-4 z-50 sm:right-auto sm:w-[380px]\",\n            \"rounded-[24px] bg-card p-6 text-card-foreground shadow-float dark:bg-secondary dark:ring-1 dark:ring-border\",\n            className,\n          )}\n        >\n          <h2 id={`${uid}-title`} className=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </h2>\n          <p id={`${uid}-desc`} className=\"mt-1.5 text-[13px] leading-relaxed text-muted-foreground\">\n            {description}{\" \"}\n            <a\n              href={policyHref}\n              className=\"rounded font-medium text-brand hover:underline hover:underline-offset-4 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n            >\n              Privacy policy\n            </a>\n          </p>\n\n          <button\n            type=\"button\"\n            onClick={() => setExpanded((v) => !v)}\n            aria-expanded={expanded}\n            aria-controls={`${uid}-prefs`}\n            className=\"mt-4 flex w-full items-center justify-between rounded-lg py-1 text-[13px] font-medium text-foreground/80 transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n          >\n            Customize preferences\n            <motion.span animate={{ rotate: expanded ? 180 : 0 }} transition={{ duration: 0.25, ease: EASE }}>\n              <ChevronDown className=\"size-4 text-muted-foreground\" aria-hidden />\n            </motion.span>\n          </button>\n\n          <AnimatePresence initial={false}>\n            {expanded && (\n              <motion.div\n                id={`${uid}-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.4, ease: EASE }}\n                className=\"overflow-hidden\"\n              >\n                <ul className=\"mt-2 grid gap-1.5\">\n                  {CATEGORIES.map((c) => {\n                    const checked = c.key === \"necessary\" ? true : prefs[c.key];\n                    return (\n                      <li key={c.key} className=\"flex items-start justify-between gap-4 rounded-2xl bg-muted p-3.5\">\n                        <div className=\"min-w-0\">\n                          <p id={`${uid}-${c.key}-l`} className=\"text-[13px] font-medium\">\n                            {c.label}\n                          </p>\n                          <p id={`${uid}-${c.key}-d`} className=\"mt-0.5 text-xs leading-relaxed text-muted-foreground\">\n                            {c.description}\n                          </p>\n                        </div>\n                        <Toggle\n                          checked={checked}\n                          disabled={c.locked}\n                          labelledBy={`${uid}-${c.key}-l`}\n                          describedBy={`${uid}-${c.key}-d`}\n                          onChange={\n                            c.key === \"necessary\"\n                              ? undefined\n                              : (v) => setPrefs((p) => ({ ...p, [c.key]: v }))\n                          }\n                        />\n                      </li>\n                    );\n                  })}\n                </ul>\n              </motion.div>\n            )}\n          </AnimatePresence>\n\n          <div className=\"mt-4 grid grid-cols-2 gap-2\">\n            {expanded ? (\n              <>\n                <button type=\"button\" onClick={save} className={btnSecondary}>\n                  Save choices\n                </button>\n                <button type=\"button\" onClick={acceptAll} className={btnPrimary}>\n                  Accept all\n                </button>\n              </>\n            ) : (\n              <>\n                <button type=\"button\" onClick={reject} className={btnSecondary}>\n                  Reject\n                </button>\n                <button type=\"button\" onClick={acceptAll} className={btnPrimary}>\n                  Accept all\n                </button>\n              </>\n            )}\n          </div>\n        </motion.section>\n      )}\n    </AnimatePresence>\n  );\n}\n\nexport default CookieConsent;\n"}],"categories":["feedback"],"meta":{"tier":"free","demo":"\"use client\";\n\nimport * as React from \"react\";\nimport { CircleCheck } from \"lucide-react\";\nimport { CookieConsent, type ConsentState } from \"./cookie-consent\";\n\nconst listings = [\n  { price: \"$685,000\", meta: \"3 bd, 2 ba, Mount Pleasant\" },\n  { price: \"$1,240,000\", meta: \"4 bd, 3.5 ba, South of Broad\" },\n  { price: \"$449,900\", meta: \"2 bd, 2 ba, Daniel Island\" },\n];\n\nexport default function Demo() {\n  const [run, setRun] = React.useState(0);\n  const [result, setResult] = React.useState<ConsentState | null>(null);\n\n  return (\n    <div className=\"mx-auto w-full max-w-3xl\">\n      <div className=\"relative h-[500px] overflow-hidden rounded-[28px] bg-muted\">\n        {/* Page behind the card */}\n        <header className=\"flex h-14 items-center justify-between px-6\">\n          <span className=\"text-sm font-semibold tracking-[-0.01em] text-foreground\">Harbor & Pine Realty</span>\n          <span className=\"hidden text-xs text-muted-foreground sm:block\">Buy, sell and rent in Charleston, SC</span>\n        </header>\n        <div className=\"grid gap-3 px-6 pt-1 sm:grid-cols-3\">\n          {listings.map((l) => (\n            <div key={l.price} className=\"rounded-2xl bg-background p-3 dark:bg-secondary\">\n              <div className=\"aspect-[4/3] rounded-xl bg-foreground/[0.06]\" />\n              <p className=\"mt-3 text-sm font-semibold tracking-[-0.01em] text-foreground\">{l.price}</p>\n              <p className=\"mt-0.5 text-xs text-muted-foreground\">{l.meta}</p>\n            </div>\n          ))}\n        </div>\n\n        {result && (\n          <div className=\"absolute right-5 top-16 flex items-center gap-2 rounded-full bg-background px-3 py-1.5 text-xs text-foreground shadow-float dark:bg-secondary\">\n            <CircleCheck className=\"size-3.5 text-success\" aria-hidden />\n            Analytics {result.analytics ? \"on\" : \"off\"}, marketing {result.marketing ? \"on\" : \"off\"}\n            <button\n              type=\"button\"\n              onClick={() => {\n                setResult(null);\n                setRun((r) => r + 1);\n              }}\n              className=\"ml-1 rounded font-medium text-brand focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n            >\n              Reset\n            </button>\n          </div>\n        )}\n\n        <CookieConsent key={run} contained storageKey={null} delay={400} onChange={(c) => setResult(c)} />\n      </div>\n    </div>\n  );\n}\n"}}