# Cookie banner

> Asks once, plainly, then folds into a small cookie button in the corner that grows back into the preferences.

Most cookie banners are a wall of text with a bright Accept and a hidden Reject. This one is a single hairline card in the corner: one sentence, Customise, and Reject all beside Accept all, equally easy. Customise grows the same card into the preferences, each kind of cookie with a switch whose thumb is thrown to its side. Whichever button you press confirms itself: a spinner while your server records the choice, a drawn check and a label that morphs to "Accepted", "Rejected" or "Saved". Then the card shrinks into a small cookie button in the corner, which grows back into the preferences with your choices as they were. A "Cookie settings" link anywhere can open it too.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/cookie-banner.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/cookie-banner
- Registry item (source): https://hairlineui.com/r/cookie-banner.json

## Usage

```tsx
import { CookieBanner } from "@/components/cookie-banner";

<CookieBanner
  policyHref="/cookies"
  onConsentChange={(consent) => {
    if (consent.analytics) loadAnalytics();
    if (consent.marketing) loadAdPixels();
  }}
/>
```

## In your layout, with a footer link

Mount it once in the root layout, record each choice on your server, and let people change their mind from the footer.

```tsx
// app/layout.tsx
import { CookieBanner } from "@/components/cookie-banner";
import { startTracking } from "@/lib/tracking";

<CookieBanner
  policyHref="/cookies"
  categories={[
    { id: "necessary", label: "Necessary", description: "Keeps you signed in and your basket full.", required: true },
    { id: "analytics", label: "Analytics", description: "Counts visits and what gets used. Never sold." },
    { id: "marketing", label: "Marketing", description: "Shows you our ads on other sites.", tracking: true },
  ]}
  onConsentChange={async (consent, reason) => {
    startTracking(consent);
    if (reason === "save") await fetch("/api/consent", { method: "POST", body: JSON.stringify(consent) });
  }}
/>

// components/footer.tsx
import { openCookiePreferences } from "@/components/cookie-banner";

<button onClick={openCookiePreferences}>Cookie settings</button>
```

## Props

| Prop | Type | Default | 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. |

## 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.

## What it handles

Most cookie banners are a wall of text with a bright Accept and a hidden Reject. This one is calm, treats both choices equally and remembers what people chose.

- **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.

## Replaces

a consent banner, a preferences modal, consent storage code, a reopen button, Global Privacy Control checks

## Where it fits

- **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.

## Notes

- 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.

License: MIT (https://hairlineui.com/license#components)
