Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Cookie Consent

ProNew

A 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

pnpm dlx shadcn@latest add @viewport/cookie-consent

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above comes with Truffle Pro.

Props

PropTypeDefault
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"
backgroundstring

Your own background, for brand-colored banners.

—
textColorstring

Text color to go with `background`.

—
colorstring

Color of the accept button and the switches.

—
buttonTextColorstring

Text color on the accept button.

—
iconReact.ReactNode

Picture before the text. Defaults to a cookie; pass `null` for none.

<CookieIcon />
titlestring—
messageReact.ReactNode"We use cookies to keep the site working and to understand how it is used."
policyHrefstring

Adds a link to your cookie policy after the message.

—
policyLabelstring"Learn more"
acceptLabelstring | null

Pass `null` to leave the button out, for a notice that is only closed.

"Accept all"
declineLabelstring | null

Pass `null` for a single-button banner.

"Decline"
preferencesLabelstring

Label of the button that opens the category switches. Shown when `categories` is set.

"Preferences"
saveLabelstring"Save choices"
preferencesTitlestring

Heading of the preferences panel.

"Cookie preferences"
categoriesCookieCategory[]

Cookie categories for the preferences panel. Without them there is no panel.

—
stackedboolean

Puts the buttons under each other at full width. Defaults to on for the card.

—
dismissibleboolean

Adds a close button, which answers `dismiss` with only the required categories.

false
onConsent(consent: CookieConsentValue) => void

Called with the answer. Start or stop your scripts here.

—
storageKeystring | null

localStorage key the answer is kept under. Pass `null` to keep nothing.

"cookie-consent"
openboolean

Shows or hides it yourself, e.g. from a "Cookie settings" link. Overrides the stored answer.

—
onOpenChange(open: boolean) => void—
containedboolean

Positions it inside the nearest `relative` parent instead of the window, and does not trap focus.

false
classNamestring—

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.