Xzero UI
Pro library

Cookie consent

A consent bar that morphs into preferences and folds into a reopen button.

$ pnpm dlx shadcn@4.23.0 add @xzero-ui/cookie-consent
Live · keyboard ready

When to use

When not to use

Installation

Register both Xzero UI namespaces in your project's components.json:

{
  "registries": {
    "@xzero-ui": "https://mcp.xzero.co.uk/r/{name}.json",
    "@xzero-ui-pro": "https://mcp.xzero.co.uk/r/pro/{name}.json"
  }
}
npx shadcn@4.23.0 add @xzero-ui/cookie-consent

Install xzero-foundation once, and import @/components/xzero/foundation.css in your app root. No Xzero token is used by this snapshot. For sample portraits, photos, fonts, or logos, download https://mcp.xzero.co.uk/install-assets.mjs and run node xzero-install-assets.mjs <receiving-project>.

Usage

import { useRef } from "react";
import { CookieConsent, type CookieConsentHandle } from "@/components/xzero/cookie-consent/cookie-consent";

export function Consent() {
  const consent = useRef<CookieConsentHandle>(null);
  return (
    <>
      <CookieConsent
        ref={consent}
        storageKey="acme-consent"
        policy={{ label: "Cookie policy", href: "/cookies" }}
        onChange={value => { if (value?.analytics) loadAnalytics(); }}
      />
      <button type="button" onClick={() => consent.current?.openPreferences()}>Cookie settings</button>
    </>
  );
}

API reference

CookieConsent

A consent bar that morphs into a preferences panel and then folds into a small reopen button, with optional localStorage memory.

PropTypeDefaultDescription
categoriesCookieCategory[]defaultCookieCategoriesGroups: { id, label, description?, required?, defaultEnabled? }. Required ones are always on.
valueCookieConsentValue | null–Controlled saved choice (category id to allowed). null means the visitor has not decided.
defaultValueCookieConsentValue | nullnullStarting choice when uncontrolled.
onChange(value: CookieConsentValue | null, detail: { action: CookieConsentAction; updatedAt: Date }) => void–Called on accept-all, reject-all, save, or reset. Load optional scripts from here.
storageKeystring–Remembers the choice in localStorage under this key. Read it elsewhere with readCookieConsent.
versionstring"1"Bump when categories or policy change; a stored choice for another version is ignored and the bar asks again.
preferencesOpenboolean–Controlled open state of the preferences panel.
defaultPreferencesOpenbooleanfalseOpens the panel on first render when uncontrolled.
onPreferencesOpenChange(open: boolean) => void–Called when the panel opens or closes.
policy{ label: string; href?: string; onClick?: () => void }–Link to the full policy, shown in the bar and panel. Renders a button when href is missing.
position"bottom" | "bottom-left" | "bottom-right""bottom"Where the bar sits along the bottom edge.
strategy"fixed" | "absolute""fixed"fixed pins to the viewport; absolute fills the nearest positioned ancestor, for previews.
showReopenButtonbooleantrueKeeps a 44px cookie button after a choice so visitors can change their mind.
reopenSide"left" | "right"–Corner for the reopen button. Defaults to the bar's side, or left when centered.
labelsPartial<CookieConsentLabels>–Overrides for message, customize, acceptAll, rejectAll, save, preferencesTitle, preferencesDescription, reopen, alwaysOn, and close.
classNamestring–Class on the layer.
refRef<CookieConsentHandle>–Imperative handle: openPreferences(), closePreferences(), reset().

readCookieConsent

Reads a saved choice as { value, version, updatedAt }, or null when there is none or it was saved for another version. Safe on the server.

PropTypeDefaultDescription
storageKey (required)string–The same key passed to CookieConsent.
versionstring"1"The version the choice must match.

defaultCookieCategories

The default categories: Strictly necessary (required), Preferences, Analytics, and Marketing.

No props.

Keyboard interactions

KeysAction
TabMoves through the bar or panel buttons and switches.
SpaceToggles the focused category switch.
EscapeCloses the preferences panel without saving and returns focus to the bar or reopen button.

Accessibility

Motion

Responsive behavior

Performance

Notes for AI

Also in messages

Guidance for AI tools

Cookie consent: A consent bar that morphs into preferences and folds into a reopen button. Follow the declared prop types and do not invent props. Keep keyboard access, reduced motion support, and both light and dark themes intact when adapting it.

Full library index: https://ui.xzero.co.uk/llms.txt

Browse all components