Cookie consent
A React component in Beamline's Feedback category, in the free sample.
Asks once which cookies a visitor allows, with Reject all and Accept all given equal weight; Customise grows the notice in place into each category with its switch, purpose and the cookies behind it; the choice is remembered (and asked again when your policy changes), and a browser's Global Privacy Control keeps marketing off.
Use it for
- Asking a site's or an app's visitors which optional cookies (analytics, marketing, functional) they allow, before any of them are set.
- Letting people change that choice later from a Cookie settings link (open it again with `open`).
Not for
- News, offers or maintenance notices (announcement-bar).
- Notification permissions or other browser prompts (alert).
- Account privacy settings with many options (settings).
Anatomy
- notice: a card docked to a corner, a bar across the bottom, or in the page's flow; a size container
- summary: what cookies are for in one sentence, a link to the policy, and Reject all · Customise · Accept all in one weight
- preferences (the same card, grown): one row per category with its name, purpose, a switch (Always on for the necessary ones) and a fold with the cookies it sets (name, provider, purpose, how long it is kept); shared Table presents cookie details as labelled records in narrow rooms
- a line when the browser sends Global Privacy Control: marketing stays off unless switched on here
- footer of the preferences: Reject all · Accept all · Save choices
- saved: a check and “Saved” for a moment, then the notice folds away
- save error: a visible alert within the notice; retry uses the retained choices
Variants
- position: bottom-start, bottom-end, bottom, inline
States
- asking (no remembered choice, or the policy version changed)
- customising (preferences open)
- saving (awaiting the caller; controls disabled, no saved claim yet)
- save error (the notice stays open with the choices retained and retry available)
- saved (a moment, then closed)
- closed (a choice is remembered)
- global privacy control (marketing off by default, said in words)
Keyboard
- The notice does not take focus or trap it: the page stays usable; Tab reaches it in reading order (it is a labelled region)
- Customise moves focus to the first switch; Back returns it to Customise
- Space toggles a switch; Enter or Space opens a category's cookie list
- Escape in the preferences goes back to the summary; it never accepts or rejects anything
Motion
The notice rises in from its edge once. Customise grows the same card in place to the preferences (its width and height ease, the summary's buttons give way to the categories rising in one after another) and Back shrinks it again. Switches slide; a category's cookie list opens to its height. A decision shows a drawn check and Saved, then the card folds down and fades. Reduced motion keeps the fades.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
categories | ConsentCategory[] | — | { id, label, description, required?, cookies?: { name, provider, purpose, duration }[] }. Default: necessary, functional, analytics, marketing. |
value / defaultValue / onValueChange | ConsentChoice / (choice: ConsentChoice) => void | Promise<void> | — | value: Controlled selected value; omit to use defaultValue. defaultValue: Initial selection when uncontrolled; omitted by default. onValueChange: A returned promise is awaited before confirming or remembering the choice. Reject to keep the notice open. |
open / defaultOpen / onOpenChange | boolean | — | open: Controlled open state; omit to use defaultOpen. defaultOpen: Initial open state when uncontrolled; defaults to false. onOpenChange: Reports an open-state change; no callback by default. |
position | "bottom-start" | "bottom-end" | "bottom" | "inline" | — | Default bottom-start. |
storageKey | string | null | — | Where the decision is remembered; null keeps nothing. |
version | string | number | — | Change it when the policy changes: everyone is asked again. |
policyHref / policyLabel | string | — | The link in the summary. Default label “Cookie policy”. |
title / description | ReactNode | — | The summary's heading and sentence. |
globalPrivacyControl | boolean | "auto" | — | The browser's Global Privacy Control signal: "auto" reads it from the browser; on the server, pass what the Sec-GPC request header said; false ignores it. |
className / classNames / style / ref | root | — | className: Classes on the documented visual root; no override by default. classNames: Classes for the named visual parts; no overrides by default. style: Inline styles on the documented visual root; no overrides by default. ref: Receives the documented native target or imperative handle; omitted by default. |
value | ConsentChoice | — | Controlled selected value; omit to use defaultValue. |
defaultValue | ConsentChoice | — | Initial selection when uncontrolled; omitted by default. |
onValueChange | (choice: ConsentChoice) => void | Promise<void> | — | A returned promise is awaited before confirming or remembering the choice. Reject to keep the notice open. |
open | boolean | — | Controlled open state; omit to use defaultOpen. |
defaultOpen | boolean | — | Initial open state when uncontrolled; defaults to false. |
onOpenChange | ((open: boolean) => void) | — | Reports an open-state change; no callback by default. |
policyHref | string | — | |
policyLabel | string | — | |
title | ReactNode | — | |
description | ReactNode | — | |
className | string | — | Classes on the documented visual root; no override by default. |
classNames | Partial<Record<"root" | "title" | "summary" | "actions" | "categories" | "category" | "… | — | Classes for the named visual parts; no overrides by default. |
style | CSSProperties | — | Inline styles on the documented visual root; no overrides by default. |
ref | Ref<HTMLElement> | — | Receives the documented native target or imperative handle; omitted by default. |
Built from
- button
- switch
- table
Dependencies
lucide-react, @radix-ui/react-switch
Import
import { CookieConsent, CONSENT_CATEGORIES } from "@beamline/cookie-consent";
Get it
Cookie consent is part of Beamline UI. Install the parts you need in your React app as compiled packages or editable source. Cookie consent is included in the free MIT sample. Get Beamline · Connect your agent
More in Feedback
- Timer: A duration that counts down (or up) from your clock: Start, Pause, Resume, Reset and the finish reported once; the time left drawn as a bar you drag (or…
- Notification centre: Updates that wait to be read: a bell with the unread count opens All / Unread, new and earlier groups, and per item mark read or dismiss.
- Usage meter: An allowance and what fills it: one bar split by source, the room left, a warning near the limit and the overflow in its own red well past a notch at the…
- Alert: A message that stays until it is dealt with: what happened, what to do, an action; danger and warning are announced; dismissing folds its height away.
- Empty state: What a place will hold and the first step to fill it, instead of a blank; a compact size for panels and filtered tables.
- Spinner: A ring that spins while work runs and closes into a drawn check or cross when it ends.
- Announcement bar: A banner across the top for news and offers: messages take turns, a countdown ticks, and dismissing folds it away and can remember it.
- Progress: How much of a known task is done, or a moving bar when the length is unknown, with an optional label and value.
- Skeleton: Holds the shape of content while it loads, then crossfades into it, so nothing jumps.