Skip to content
Browse documentation

A cookie banner with a mascot: every cookie you allow flies to Crumb, who catches it, nibbles it and gets rounder and happier.

Used forCookie bannersPrivacy settingsPlayful brandsLaunch pages

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/cookie-consent

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/cookie-consent.json. More

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • Cookie banners

    The first-visit consent card, in a corner or centred, with a mascot people play with.

  • Privacy settings

    The cookie preferences page people come back to, with the same choices.

  • Playful brands

    Consumer apps, games and shops whose voice is warmer than a legal notice.

  • Launch pages

    A consent card that gets shared instead of skipped.

What it handles for you

A cookie banner is usually a paragraph, a Customise link and a modal of switches nobody reads. This block keeps the categories on one card, makes rejecting as easy as accepting, and gives every choice a reaction.

Instead of wiring upa cookie bannera preferences modalcategory switchesaccept and reject buttonsa consent save call
  1. A reaction to every choice

    Each category switched on sends Crumb a cookie; switched off, it hands the cookie back with a bite out of it.

  2. A mood you can read

    Crumb gets rounder and happier with each cookie: hungry, content, then stuffed and sparkling.

  3. Reject as easy as Accept

    Reject all sits beside the main button at the same size, and nothing optional is on until someone says so.

  4. Necessary, plainly

    Required categories show Always on and can’t be switched off.

  5. The right button

    The main button reads Accept all, or Save choices once some but not all are on; both go Saving → Saved.

  6. Kept your way

    onSave gets { [category]: on } for a cookie, your consent platform or your API; a failure shakes and says why.

Overview

Cookie banners are the most skipped screen on the web. This one gives the choice a face. Crumb, an animated bread bun with a golden top, sits on the lit stage with his hands clasped, pleading, hungry. Switch Analytics on and a chocolate-chip cookie leaps from the switch and spins over the card into his hands, and he eats it: bite after bite, the cookie shrinking, crumbs dropping at his feet, then he chews and settles a little fuller. Each cookie plays the next of three eating clips, from hungry to content to full to stuffed, his cheeks puffing a little more each time. Switch one back off and the bitten cookie flies back to its switch while he eases back to how full he was. Reject all hands every cookie back; Accept all feeds the rest. Under the fun it is a proper consent banner: Necessary is always on and says so, Reject all sits beside the main button at the same size, the main button reads Accept all or Save choices depending on what you picked, and onSave gets the choices to keep wherever you keep them.

States

hungry
Crumb sits on the lit stage, hands clasped under his chin, big pleading eyes, breathing; the line above reads “Crumb is hungry”.
feed
A switch on: a chocolate-chip cookie pops out of it and spins along an arc over the card (620ms) into his hands; he leans toward it as it comes.
eat
The next eating clip takes over from the frame with the cookie in his hands and plays at 10 frames a second for 1.8s: three bites, the cookie shrinking, crumbs falling, chewing, then he settles a little fuller.
content / stuffed
Three clips take him from hungry to content to full to stuffed, his cheeks rounder each time; with every category on, “Crumb is stuffed”. Any number of categories maps onto the three.
give back
A switch off: the bitten cookie flies from him back to its switch (640ms) and he cross-fades back to how full he was.
reject / accept all
Reject all hands every cookie back, 140ms apart; Accept all feeds the rest, 170ms apart; the choices are saved once they have landed.
saved
The button goes Saving → Saved with a drawn check and Crumb gives a happy hop.
not saved
If onSave throws, the button shakes into Try again and your message replaces the line under it.

Usage

usage.tsx
import { CookieConsent } from "@/components/cookie-consent";<CookieConsent  onSave={async (choices) => {    document.cookie = `consent=${encodeURIComponent(JSON.stringify(choices))}; max-age=31536000; path=/; samesite=lax`;  }}/>

Works with

Show it once, then load scripts by consent

Show the block until there is a consent cookie, keep the choices in it, and only load analytics and marketing scripts for the categories that are on.

example.tsx
"use client";import { useEffect, useState } from "react";import { CookieConsent, type CookieChoices } from "@/components/cookie-consent";const read = (): CookieChoices | null => {  const m = document.cookie.match(/(?:^|; )consent=([^;]*)/);  return m ? JSON.parse(decodeURIComponent(m[1])) : null;};function load(choices: CookieChoices) {  if (choices.analytics) import("@/lib/analytics").then((a) => a.start());  if (choices.marketing) import("@/lib/pixels").then((p) => p.start());}export function Consent() {  const [open, setOpen] = useState(false);  useEffect(() => {    const saved = read();    if (saved) load(saved);    else setOpen(true);  }, []);  if (!open) return null;  return (    <div className="fixed bottom-4 left-4 z-50 w-[min(380px,calc(100vw-2rem))]">      <CookieConsent        policyHref="/cookies"        onSave={async (choices) => {          document.cookie = `consent=${encodeURIComponent(JSON.stringify(choices))}; max-age=31536000; path=/; samesite=lax`;          load(choices);          setTimeout(() => setOpen(false), 1400); // after Crumb's wave        }}      />    </div>  );}

Props

onSave
(choices: CookieChoices) => Promise<unknown>
Gets { [category id]: boolean } with required categories true. Keep it; throw an Error to show its message.
categories= Necessary, Analytics, Marketing, Personalization
CookieCategory[]
{ id, label, description?, required? }. Required ones show Always on; each other one is a cookie for Crumb.
value / defaultValue / onValueChange= — / {} (all optional off)
CookieChoices
The choices, controlled or not.
title / description= "Cookie settings" / "Every cookie you allow goes to Crumb."
string
The heading and the line under it.
name= "Crumb"
string
What the mascot is called in its lines.
policyHref
string
Links “Cookie policy” under the buttons.

Accessibility and motion

  • Each category is a real switch labelled by its name; the mascot is decoration (aria-hidden) and each change is announced from a status region (“Analytics allowed.”).
  • Nothing optional is on by default, and Reject all is the same size as the main button, side by side.
  • Crumb is three short eating clips cut into transparent WebP frame strips (about 200KB of images, with the cookie art) inside crumb-art.ts, so the install is complete. One frame loop outside React steps the frames, flies the cookies in an overlay over the card and sleeps when he’s still; nothing costs React renders.
  • The stage is the lit --primary surface in your theme, light or dark; the crumbs and the pool of light under him are --crumb-* variables you can override. No drop shadows, glow or blur.
  • Built from Switch, Status button and Text morph, which the CLI installs with it. No animation library; with reduced motion nothing flies and Crumb’s mood changes in place.

Requirements

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

The CLI also installs Status button, Switch, Text morph.

Source

"use client";import * as React from "react";import { Crumb, type CrumbHandle, type Point } from "./cookie-crumb";import { StatusButton, type ActionStatus } from "./status-button";import { Switch } from "./switch";import { TextMorph } from "./text-morph";import { useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * COOKIE CONSENT: every cookie you allow goes to Crumb * *   ask       Crumb, an animated bread bun, sits on the lit *             stage pleading, hungry; Necessary is always on *   allow     switch a category on and a cookie leaps from the *             switch into Crumb's hands and he eats it, bite by *             bite, crumbs falling, and settles a little fuller; *             with everything allowed he's stuffed *   take back switch one off and the bitten cookie flies back *             while he eases back to how full he was *   decide    Reject all hands every cookie back; Accept all *             feeds the rest; the main button says Accept all or *             Save choices, and saving gets a happy hop * * The choice is yours to keep: onSave gets { [category]: on } * to write to a cookie, your consent platform or your API. * ───────────────────────────────────────────────────────── */export interface CookieCategory {  id: string;  label: string;  description?: string;  /** Always on and can't be switched off, e.g. sign-in and security. */  required?: boolean;}export type CookieChoices = Record<string, boolean>;export interface CookieConsentProps {  categories?: CookieCategory[];  value?: CookieChoices;  defaultValue?: CookieChoices;  onValueChange?: (choices: CookieChoices) => void;  /** Keep the choices (a cookie, your consent platform, your API); throw an Error to say why not. */  onSave: (choices: CookieChoices) => Promise<unknown>;  title?: string;  description?: string;  /** Your cookie policy, linked under the choices. */  policyHref?: string;  /** What the mascot is called in its lines. */  name?: string;  className?: string;}const CATEGORIES: CookieCategory[] = [  { id: "necessary", label: "Necessary", description: "Sign-in, security and your choices here.", required: true },  { id: "analytics", label: "Analytics", description: "Counts visits so we know what to fix." },  { id: "marketing", label: "Marketing", description: "Measures ads and where you came from." },  { id: "personalization", label: "Personalization", description: "Remembers what you like." },];const EMPTY: CookieChoices = {};const mix = (color: string, amount: number, into = "transparent") => `color-mix(in oklab, ${color} ${amount}%, ${into})`;const CARD: React.CSSProperties = {  backgroundImage: `linear-gradient(180deg, var(--card), ${mix("var(--card)", 95, "var(--foreground)")})`,  boxShadow: `0 0 0 1px var(--border), inset 0 1px 0 ${mix("var(--background)", 85)}`,};const STAGE: React.CSSProperties = {  backgroundColor: "var(--primary)",  backgroundImage: `radial-gradient(90% 80% at 50% 100%, ${mix("var(--primary-foreground)", 20)} 0%, transparent 60%), radial-gradient(120% 90% at 0% 0%, ${mix("var(--primary-foreground)", 16)} 0%, transparent 55%), linear-gradient(175deg, transparent 40%, ${mix("black", 22)} 100%)`,  boxShadow: `inset 0 1px 0 ${mix("var(--primary-foreground)", 36)}, inset 0 0 0 1px ${mix("var(--primary-foreground)", 12)}`,};const LIT: React.CSSProperties = {  backgroundImage: `linear-gradient(180deg, ${mix("var(--primary-foreground)", 16)}, transparent 60%)`,  boxShadow: `inset 0 1px 0 ${mix("var(--primary-foreground)", 32)}, inset 0 0 0 1px ${mix("black", 10)}`,};/* Crumb's cookie crumbs and the pool of light he sits in; each can be overridden. */const CRUMB = {  "--crumb-floor": mix("var(--primary-foreground)", 45),  "--crumb-cookie": "oklch(0.7 0.11 65)",} as React.CSSProperties;export function CookieConsent({  categories = CATEGORIES,  value,  defaultValue = EMPTY,  onValueChange,  onSave,  title = "Cookie settings",  description,  policyHref,  name = "Crumb",  className = "",}: CookieConsentProps) {  const reduced = useInteractionReducedMotion();  const baseId = React.useId();  const [own, setOwn] = React.useState<CookieChoices>(defaultValue);  const choices = value ?? own;  const [act, setAct] = React.useState<{ which: "accept" | "reject" | null; status: ActionStatus; error?: string }>({ which: null, status: "idle" });  const [said, setSaid] = React.useState("");  const sectionRef = React.useRef<HTMLElement>(null);  const overlayRef = React.useRef<SVGSVGElement>(null);  const stageRef = React.useRef<HTMLDivElement>(null);  const crumb = React.useRef<CrumbHandle>(null);  const switches = React.useRef(new Map<string, HTMLElement>());  const optional = categories.filter((c) => !c.required);  const isOn = (c: CookieCategory) => Boolean(c.required || choices[c.id]);  const fed = optional.filter(isOn).length;  const allOn = fed === optional.length;  const busy = act.status === "pending";  const commit = (next: CookieChoices) => {    if (value === undefined) setOwn(next);    onValueChange?.(next);  };  /** Where a category's switch is, in the block's own px. */  const switchAt = (id: string): Point => {    const section = sectionRef.current;    const el = switches.current.get(id);    if (!section || !el) return { x: 0, y: 0 };    const box = section.getBoundingClientRect();    const k = box.width / (section.offsetWidth || box.width) || 1;    const r = el.getBoundingClientRect();    return { x: (r.left + r.width / 2 - box.left) / k, y: (r.top + r.height / 2 - box.top) / k };  };  const toggle = (c: CookieCategory, on: boolean) => {    if (busy || c.required) return;    commit({ ...choices, [c.id]: on });    if (on) crumb.current?.feed(switchAt(c.id));    else crumb.current?.giveBack(switchAt(c.id));    setSaid(`${c.label} ${on ? "allowed" : "off"}.`);    if (act.status !== "idle") setAct({ which: null, status: "idle" });  };  const save = async (which: "accept" | "reject", next: CookieChoices, wait: number) => {    setAct({ which, status: "pending" });    try {      // let the cookies land before the choice is kept      await new Promise((r) => window.setTimeout(r, wait));      await onSave(next);      setAct({ which, status: "success" });      setSaid(which === "reject" ? "Saved: only necessary cookies." : `Saved your cookie choices.`);      crumb.current?.wave();    } catch (e) {      setAct({ which, status: "error", error: e instanceof Error && e.message ? e.message : "Couldn’t save that. Try again." });    }  };  const acceptAll = () => {    if (busy) return;    const off = optional.filter((c) => !choices[c.id]);    const keep = fed > 0 && !allOn;    if (keep) return void save("accept", { ...choices, ...Object.fromEntries(categories.filter((c) => c.required).map((c) => [c.id, true])) }, 0);    const next = Object.fromEntries(categories.map((c) => [c.id, true]));    commit(next);    off.forEach((c, i) => crumb.current?.feed(switchAt(c.id), i * 170));    void save("accept", next, reduced || !off.length ? 0 : off.length * 170 + 620);  };  const rejectAll = () => {    if (busy) return;    const on = optional.filter((c) => choices[c.id]);    const next = Object.fromEntries(categories.map((c) => [c.id, Boolean(c.required)]));    commit(next);    on.forEach((c, i) => crumb.current?.giveBack(switchAt(c.id), i * 140));    void save("reject", next, reduced || !on.length ? 0 : on.length * 140 + 640);  };  const mood = fed === 0 ? `${name} is hungry` : allOn ? `${name} is stuffed` : `${name} could eat more`;  const mainLabel = fed > 0 && !allOn ? "Save choices" : "Accept all";  return (    <section ref={sectionRef} aria-labelledby={`${baseId}-title`} className={`relative w-full max-w-[380px] rounded-[28px] p-2 text-card-foreground ${className}`} style={{ ...CARD, ...CRUMB }}>      {/* Its natural width where the parent sizes to its content, without forcing it on narrow screens. */}      <div aria-hidden="true" data-natural-width className="grid h-0" style={{ gridTemplateColumns: "minmax(0, 380px)" }} />      <div className="@container w-full">        {/* The one lit surface: Crumb's stage. */}        <div ref={stageRef} className="relative h-[172px] overflow-clip rounded-[22px] text-primary-foreground" style={STAGE}>          <p className="absolute left-3.5 top-3 z-10 text-[12.5px] font-medium text-primary-foreground/80">            <TextMorph>{mood}</TextMorph>          </p>          <Crumb ref={crumb} fed={fed} total={optional.length} reduced={reduced} overlay={overlayRef} stage={stageRef} />        </div>        <div className="px-2 pt-3">          <h2 id={`${baseId}-title`} className="px-1 text-[15px] font-medium tracking-[-0.01em]">            {title}          </h2>          <p className="mt-0.5 px-1 text-[12.5px] text-muted-foreground">{description ?? `Every cookie you allow goes to ${name}.`}</p>          <ul className="mt-2">            {categories.map((c) => (              <li key={c.id} className="flex min-h-12 items-center gap-3 border-t border-border/70 px-1 py-2 first:border-t-0">                <span className="min-w-0 flex-1">                  <span id={`${baseId}-${c.id}`} className="block text-[13.5px] font-medium">                    {c.label}                  </span>                  {c.description && <span className="block truncate text-[12px] text-muted-foreground">{c.description}</span>}                </span>                {c.required ? (                  <span className="shrink-0 rounded-full bg-muted px-2 py-0.5 text-[11.5px] text-muted-foreground">Always on</span>                ) : (                  <span                    ref={(el) => {                      if (el) switches.current.set(c.id, el);                      else switches.current.delete(c.id);                    }}                    className="flex shrink-0"                  >                    <Switch size="sm" checked={isOn(c)} aria-labelledby={`${baseId}-${c.id}`} disabled={busy} onCheckedChange={(on) => toggle(c, on)} />                  </span>                )}              </li>            ))}          </ul>          <div className="mt-2.5 grid grid-cols-2 gap-2">            <StatusButton              variant="outline"              status={act.which === "reject" ? act.status : "idle"}              labels={{ idle: "Reject all", pending: "Saving", success: "Saved", error: "Try again" }}              disabled={busy && act.which !== "reject"}              onClick={rejectAll}              className="h-10 w-full text-[13.5px]"            />            <StatusButton              status={act.which === "accept" ? act.status : "idle"}              labels={{ idle: mainLabel, pending: "Saving", success: "Saved", error: "Try again" }}              disabled={busy && act.which !== "accept"}              onClick={acceptAll}              className="h-10 w-full text-[13.5px]"              style={act.which === "accept" && act.status === "error" ? undefined : LIT}            />          </div>          <p role={act.error ? "alert" : undefined} className={`mt-2 pb-1 text-center text-[12px] ${act.error ? "text-red-600 dark:text-red-400" : "text-muted-foreground"}`}>            {act.error ?? (              <>                Change this any time{policyHref ? " · " : "."}                {policyHref && (                  <a href={policyHref} className="underline decoration-border underline-offset-2 hover:text-foreground">                    Cookie policy                  </a>                )}              </>            )}          </p>        </div>      </div>      {/* Cookies fly over the whole block, from a switch to the mouth and back. */}      <svg ref={overlayRef} aria-hidden="true" className="pointer-events-none absolute inset-0 z-20 size-full overflow-visible" />      <span className="sr-only" role="status">        {said}      </span>    </section>  );}export default CookieConsent;

Registry item: /r/cookie-consent.json