Cookie banner
Cookie banner
Asks once, plainly, then folds into a small cookie button in the corner that grows back into the preferences.
Dependencies
Interaction states
- ask — The card rises 20px out of the corner 400ms after the page (640ms). Reject all and Accept all are the same size, side by side.
- customise — The card’s width and height grow to the preferences (480ms) while the banner blurs out and the preferences blur in. Switch thumbs are thrown with a slight overshoot (400ms). Esc or the close button returns.
- saving — When onConsentChange returns a promise, the pressed button opens a spinner beside its label until it settles.
- saved — A check draws itself on the button and the label morphs ("Accepted", "Rejected", "Saved"); after a beat the card shrinks to a 44px circle and the cookie spins in with a small overshoot.
- revisit — The cookie button, or openCookiePreferences() from a footer link, grows the card back into the preferences. Closing without saving puts the saved choices back.
Installation
Add this component with your preferred package manager.
pnpm dlx shadcn@4.21.5 add @cavemio/cookie-bannerFirst install? Configure @cavemio once
Run npx shadcn@4.21.5 init if shadcn is not set up, then merge this entry into the registries field in your existing components.json. Keep your other settings and registries.
{
"registries": {
"@cavemio": "https://registry.cavemio.dev/r/cavemio-{name}.json"
}
}Where it’s used
Marketing sites
Ask for consent before analytics or ad scripts run.
SaaS products
Record a visitor’s choices and keep them with your account system.
Sites with EU and UK visitors
A banner where rejecting is as easy as accepting.
Footer “Cookie settings” links
Let people change their mind later from any link on the page.
What it handles
- Reject is as easy as Accept — Both buttons are the same size and side by side, and nothing optional is on until people say so.
- Preferences in the same card — Customise grows the card into a switch for each kind of cookie. Use the defaults or your own categories.
- The choice confirms itself — While your server records it, the button shows a spinner, then a drawn check and “Accepted”, “Rejected” or “Saved”.
- It folds into a cookie button — The card shrinks to a small cookie in the corner, or reopens from a footer link with openCookiePreferences().
- It respects Global Privacy Control — Browsers that send the signal get tracking categories off, even on Accept all, with a line saying why.
- It remembers, and asks again when needed — The choice is kept for 365 days, and asks again when you add a new kind of cookie.
Usage
Interactive local fixture. Data and async results are simulated in the preview; the caller supplies application services.
import { CookieBanner } from "@/components/cookie-banner";
<CookieBanner
policyHref="/cookies"
onConsentChange={(consent) => {
if (consent.analytics) loadAnalytics();
if (consent.marketing) loadAdPixels();
}}
/>Props
| Prop | Type | Description |
|---|---|---|
| categories | CookieCategory[] | Necessary, Analytics, Marketing · Each kind of cookie: id, label, description, and required (always on) or tracking (kept off by Global Privacy Control). |
| onConsentChange | (consent, reason) => void | Promise | – · Called with the remembered choice on load and with each new one; return a promise and the button waits for it. |
| message | ReactNode | – · The one sentence on the banner. |
| policyHref | string | – · Adds a "Cookie policy" link after the sentence. |
| position | "bottom-left" | "bottom-right" | "bottom-center" | "bottom-left" · Which corner it sits in and folds into. |
| showReopen | boolean | true · Keep the cookie button after a choice. Without it, use openCookiePreferences() from a link. |
| expiresAfter | number | 365 · Days before asking again. Adding a new kind of cookie asks again too. |
| storageKey | string | "cookie-consent" · Where the choice is kept in localStorage. getCookieConsent(storageKey) reads it anywhere. |
| strategy | "fixed" | "absolute" | "fixed" · Absolute keeps it inside a positioned parent, as in this preview. |
Accessibility & motion
- Nothing optional is on until people say so, and Reject all is as easy and as visible as Accept all, as European regulators expect. - Browsers that send Global Privacy Control get tracking categories off even on Accept all, with a line saying why. - A labelled region, not a modal: the page stays usable. Switches are real switches with their descriptions attached; Esc closes the preferences. - Focus moves with the surface: into the preferences when they open, to the cookie button when it folds. - The choice is announced when it’s saved. Installs Text morph. With reduced motion the card changes size at once and nothing slides.
pnpm dlx shadcn@4.21.5 add @cavemio/cookie-banner