The banner is a non-modal <dialog open> with a <form method="dialog">: every button closes it without JavaScript, the settings panel is a <details>, the switches are checkboxes. Entry and exit are CSS transitions with @starting-style. The client part is small: it saves the choice to localStorage, reopens the banner after a reset and calls the callbacks. A one-line inline script hides the banner before the first paint if a choice is already stored, so it never flashes.
Browser Support
Displays the consent banner.
Styles the banner based on its state.
Animates the banner entrance.
Animates the banner exit.
Preview
Here the banner is rendered inside the frame with position="absolute". Answer it, then press the button to bring it back.
no answer yetInstallation
Usage
Put the banner once in the root layout. It is a server component: the HTML arrives with the banner, and the inline script removes it at once for people who already answered.
import { CookieConsent } from "@/components/ui/cookie-consent"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<CookieConsent />
</body>
</html>
)
}Examples
Corner card
variant="card" takes less space. side picks the corner.
no answer yet<CookieConsent variant="card" side="left" />Categories
Pass categories to show a Customize button with switches. Required categories are locked on.
no answer yet<CookieConsent
variant="card"
categories={[
{ id: "necessary", label: "Necessary", required: true },
{ id: "analytics", label: "Analytics", defaultEnabled: true },
{ id: "marketing", label: "Marketing" },
]}
/>Reading the choice
useCookieConsent reads the same storage key and updates when the choice changes. This card follows the banner from the previous example.
import { useCookieConsent } from "@/components/ui/cookie-consent"
function Analytics() {
const { isAllowed } = useCookieConsent()
if (!isAllowed("analytics")) return null
return <script src="/analytics.js" async />
}Callbacks and event
onAccept fires for Accept all and for saved preferences (status "custom"), onDecline for Decline and the close button. Functions can be passed only from a client component. From a server layout listen to the bubbling "cookieconsent" event, its detail is the stored value.
<CookieConsent
storageKey="my-site-consent"
delay={800}
labels={{ title: "Cookies", accept: "OK" }}
onAccept={(value) => console.log(value.status, value.categories)}
onDecline={() => console.log("declined")}
/>
// In a server layout
// window.addEventListener("cookieconsent", (e) => console.log(e.detail))Props
| Prop | Type | Default | Description |
|---|---|---|---|
| storageKey | string | "cookie-consent" | localStorage key for the choice |
| variant | "bar" | "card" | "bar" | Bottom bar or corner card |
| position | "fixed" | "absolute" | "fixed" | fixed pins to the viewport, absolute stays inside the nearest positioned parent |
| side | "left" | "right" | "right" | Corner for the card variant |
| categories | CookieCategory[] | - | Categories with switches, see the type below |
| labels | Partial<CookieConsentLabels> | - | Texts: title, description, accept, decline, customize, save, close |
| delay | number | 0 | Delay before showing, ms |
| showIcon | boolean | true | Show the cookie icon |
| onAccept | (value: CookieConsentValue) => void | - | Called on accept or saved preferences (client components only) |
| onDecline | (value: CookieConsentValue) => void | - | Called on decline or close (client components only) |
| className | string | - | Additional CSS classes for the wrapper |
type CookieCategory = {
id: string
label: string
description?: string
required?: boolean
defaultEnabled?: boolean
}
type CookieConsentValue = {
status: "accepted" | "declined" | "custom"
categories: Record<string, boolean>
timestamp: number
}useCookieConsent
Exported from the same file. Takes the storage key ("cookie-consent" by default) and returns:
| Field | Type | Description |
|---|---|---|
| consent | CookieConsentValue | null | Stored value { status, categories, timestamp } or null if there is no answer |
| isLoaded | boolean | False on the server and during hydration |
| isAllowed | (category: string) => boolean | Checks whether a category was allowed |
| reset | () => void | Clears the choice and shows the banner again |
Features
- No flash: an inline script removes the banner during HTML parsing if a choice is stored
- Works without JavaScript: the buttons close the <dialog> through <form method="dialog">, Customize is a <details>
- Synced: every banner and hook with the same key updates together, including other tabs
- CSS animation: @starting-style and transition-behavior: allow-discrete, off for reduced motion
- Accessible: a labelled dialog, real buttons and checkboxes with role="switch"; Enter in a switch does not submit the form