Cookie Consent
ProNewA cookie banner as an edge bar, a corner card or a modal, with accept, decline and a preferences panel of category switches. The answer is kept in localStorage and handed to onConsent.
Installation
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
Props
| Prop | Type | Default |
|---|---|---|
| layout | "bar" | "card" | "modal"`bar` spans the page edge, `card` floats in a corner, `modal` blocks the page until answered. | "bar" |
| side | "top" | "bottom"Which edge the bar or card sits on. | "bottom" |
| align | "left" | "center" | "right"Where the card sits along that edge. | "right" |
| tone | "surface" | "inverse"`inverse` paints it in the foreground color. | "surface" |
| background | stringYour own background, for brand-colored banners. | — |
| textColor | stringText color to go with `background`. | — |
| color | stringColor of the accept button and the switches. | — |
| buttonTextColor | stringText color on the accept button. | — |
| icon | React.ReactNodePicture before the text. Defaults to a cookie; pass `null` for none. | <CookieIcon /> |
| title | string | — |
| message | React.ReactNode | "We use cookies to keep the site working and to understand how it is used." |
| policyHref | stringAdds a link to your cookie policy after the message. | — |
| policyLabel | string | "Learn more" |
| acceptLabel | string | nullPass `null` to leave the button out, for a notice that is only closed. | "Accept all" |
| declineLabel | string | nullPass `null` for a single-button banner. | "Decline" |
| preferencesLabel | stringLabel of the button that opens the category switches. Shown when `categories` is set. | "Preferences" |
| saveLabel | string | "Save choices" |
| preferencesTitle | stringHeading of the preferences panel. | "Cookie preferences" |
| categories | CookieCategory[]Cookie categories for the preferences panel. Without them there is no panel. | — |
| stacked | booleanPuts the buttons under each other at full width. Defaults to on for the card. | — |
| dismissible | booleanAdds a close button, which answers `dismiss` with only the required categories. | false |
| onConsent | (consent: CookieConsentValue) => voidCalled with the answer. Start or stop your scripts here. | — |
| storageKey | string | nulllocalStorage key the answer is kept under. Pass `null` to keep nothing. | "cookie-consent" |
| open | booleanShows or hides it yourself, e.g. from a "Cookie settings" link. Overrides the stored answer. | — |
| onOpenChange | (open: boolean) => void | — |
| contained | booleanPositions it inside the nearest `relative` parent instead of the window, and does not trap focus. | false |
| className | string | — |
Accessibility
- The bar and card are labelled regions that never take focus; the modal and the preferences panel are dialogs that trap focus and give it back.
- Escape closes the preferences panel, and the modal only when it is dismissible, so a required answer cannot be skipped by accident.
- Each category is a labelled switch with its description attached; required ones are disabled and marked “Always on”.
- Under reduced motion the banner fades instead of sliding and the switches move at once.