Cookie consent banner
A fair cookie banner: 'Reject all' is as easy as 'Accept all', and Preferences expands in place into clear toggles with plain explanations.
"use client";
import { useState } from "react";
import "./cookie-banner.css";
type Cat = "necessary" | "analytics" | "marketing";
const CATS: { id: Cat; title: string; text: string; locked?: boolean }[] = [
{
id: "necessary",
title: "Necessary",
text: "Keep you signed in and remember this choice. Always on.",
locked: true,
},
{
id: "analytics",
title: "Analytics",
text: "Count visits anonymously so we know which pages help people.",
},
{
id: "marketing",
title: "Marketing",
text: "Let ad partners show you our products on other sites.",
},
];
/**
* CookieBanner
* - Non-blocking: a region at the bottom, not a modal, so the page stays usable.
* - Nothing optional is on by default. Save the choice wherever your consent tool expects it.
*/
export function CookieBanner({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [open, setOpen] = useState(true);
const [prefs, setPrefs] = useState(false);
const [choice, setChoice] = useState<Record<Cat, boolean>>({
necessary: true,
analytics: false,
marketing: false,
});
const [saved, setSaved] = useState("");
const finish = (c: Record<Cat, boolean>, label: string) => {
setChoice(c);
setSaved(label);
setOpen(false);
// e.g. localStorage.setItem("consent", JSON.stringify(c)) and load scripts accordingly
};
return (
<div className="gu-cookie-wrap" data-theme={theme}>
<div className="gu-cookie__page" aria-hidden="true">
<i />
<i />
<i />
</div>
{open ? (
<section
className="gu-cookie"
role="region"
aria-labelledby="gu-cookie-title"
>
<div className="gu-cookie__top">
<span className="gu-cookie__icon" aria-hidden="true">
<svg viewBox="0 0 32 32">
<path d="M16 3a13 13 0 1 0 13 13 5 5 0 0 1-6-5 5 5 0 0 1-5-6 4 4 0 0 1-2-2z" />
<circle cx="11" cy="13" r="1.8" />
<circle cx="19" cy="20" r="1.8" />
<circle cx="11" cy="21" r="1.4" />
</svg>
</span>
<div>
<h2 id="gu-cookie-title">Cookies, your call</h2>
<p>
We use a few cookies to run the site. With your OK we'd
also measure visits and show relevant ads.{" "}
<a href="#privacy">Privacy policy</a>
</p>
</div>
</div>
<div
className="gu-cookie__prefs"
data-open={prefs ? "" : undefined}
id="gu-cookie-prefs"
>
<div>
{CATS.map((c) => (
<label key={c.id} className="gu-cookie__row">
<span>
<b>{c.title}</b>
<small>{c.text}</small>
</span>
<input
type="checkbox"
role="switch"
checked={choice[c.id]}
disabled={c.locked}
tabIndex={prefs ? 0 : -1}
onChange={(e) =>
setChoice((x) => ({ ...x, [c.id]: e.target.checked }))
}
/>
<i className="gu-cookie__switch" aria-hidden="true" />
</label>
))}
</div>
</div>
<div className="gu-cookie__actions">
<button
type="button"
className="gu-cookie__link"
aria-expanded={prefs}
aria-controls="gu-cookie-prefs"
onClick={() => setPrefs((p) => !p)}
>
{prefs ? "Hide preferences" : "Preferences"}
</button>
{prefs ? (
<button
type="button"
className="gu-cookie__btn"
onClick={() => finish(choice, "Saved your preferences.")}
>
Save choices
</button>
) : (
<button
type="button"
className="gu-cookie__btn"
onClick={() =>
finish(
{ necessary: true, analytics: false, marketing: false },
"Only necessary cookies are on.",
)
}
>
Reject all
</button>
)}
<button
type="button"
className="gu-cookie__btn"
onClick={() =>
finish(
{ necessary: true, analytics: true, marketing: true },
"All cookies are on.",
)
}
>
Accept all
</button>
</div>
</section>
) : (
<div className="gu-cookie__done" role="status">
{saved}{" "}
<button type="button" onClick={() => setOpen(true)}>
Change cookie settings
</button>
</div>
)}
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Cookie consent banner
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A fair, non-blocking cookie consent banner pinned to the bottom of the page. "Reject all" and "Accept all" look identical (no dark patterns). "Preferences" expands in place into three switch rows with plain explanations; only "Necessary" is on by default and locked.
## Layout and look
- Banner card: radius 22px, padding 20px, soft shadow, over the page (demo shows a fake page behind).
- Top: a 44px orange tile with a cookie icon (bite taken out), title "Cookies, your call" (18px, weight 900) and a muted explanation with a bold "Privacy policy" link.
- Preferences: rows 60px with a hairline top border, title + muted description, a 46×28 switch (orange when on, dimmed when locked).
- Actions: underlined "Preferences" text button on the left; two identical Ink pill buttons on the right ("Reject all"/"Save choices" and "Accept all"), orange on hover. Under 520px they stack full width.
- After choosing: a small status card "Only necessary cookies are on. Change cookie settings".
## Behavior and motion
1. Banner slides up 40px and fades in (600ms cubic-bezier(0.16, 1, 0.3, 1)).
2. Preferences expand with grid-template-rows 0fr → 1fr (450ms); when open, "Reject all" becomes "Save choices".
3. Save the choice to your consent storage and only then load optional scripts.
## Accessibility
- A labelled region (not a modal, so the page stays usable). Switches are checkboxes with role="switch". The Preferences button uses aria-expanded/aria-controls; hidden switches are removed from the tab order.
- prefers-reduced-motion: no slide or expand animation.
## Props
theme. Edit CATS and the finish() storage.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### CookieBanner.tsx
```tsx
"use client";
import { useState } from "react";
import "./cookie-banner.css";
type Cat = "necessary" | "analytics" | "marketing";
const CATS: { id: Cat; title: string; text: string; locked?: boolean }[] = [
{
id: "necessary",
title: "Necessary",
text: "Keep you signed in and remember this choice. Always on.",
locked: true,
},
{
id: "analytics",
title: "Analytics",
text: "Count visits anonymously so we know which pages help people.",
},
{
id: "marketing",
title: "Marketing",
text: "Let ad partners show you our products on other sites.",
},
];
/**
* CookieBanner
* - Non-blocking: a region at the bottom, not a modal, so the page stays usable.
* - Nothing optional is on by default. Save the choice wherever your consent tool expects it.
*/
export function CookieBanner({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [open, setOpen] = useState(true);
const [prefs, setPrefs] = useState(false);
const [choice, setChoice] = useState<Record<Cat, boolean>>({
necessary: true,
analytics: false,
marketing: false,
});
const [saved, setSaved] = useState("");
const finish = (c: Record<Cat, boolean>, label: string) => {
setChoice(c);
setSaved(label);
setOpen(false);
// e.g. localStorage.setItem("consent", JSON.stringify(c)) and load scripts accordingly
};
return (
<div className="gu-cookie-wrap" data-theme={theme}>
<div className="gu-cookie__page" aria-hidden="true">
<i />
<i />
<i />
</div>
{open ? (
<section
className="gu-cookie"
role="region"
aria-labelledby="gu-cookie-title"
>
<div className="gu-cookie__top">
<span className="gu-cookie__icon" aria-hidden="true">
<svg viewBox="0 0 32 32">
<path d="M16 3a13 13 0 1 0 13 13 5 5 0 0 1-6-5 5 5 0 0 1-5-6 4 4 0 0 1-2-2z" />
<circle cx="11" cy="13" r="1.8" />
<circle cx="19" cy="20" r="1.8" />
<circle cx="11" cy="21" r="1.4" />
</svg>
</span>
<div>
<h2 id="gu-cookie-title">Cookies, your call</h2>
<p>
We use a few cookies to run the site. With your OK we'd
also measure visits and show relevant ads.{" "}
<a href="#privacy">Privacy policy</a>
</p>
</div>
</div>
<div
className="gu-cookie__prefs"
data-open={prefs ? "" : undefined}
id="gu-cookie-prefs"
>
<div>
{CATS.map((c) => (
<label key={c.id} className="gu-cookie__row">
<span>
<b>{c.title}</b>
<small>{c.text}</small>
</span>
<input
type="checkbox"
role="switch"
checked={choice[c.id]}
disabled={c.locked}
tabIndex={prefs ? 0 : -1}
onChange={(e) =>
setChoice((x) => ({ ...x, [c.id]: e.target.checked }))
}
/>
<i className="gu-cookie__switch" aria-hidden="true" />
</label>
))}
</div>
</div>
<div className="gu-cookie__actions">
<button
type="button"
className="gu-cookie__link"
aria-expanded={prefs}
aria-controls="gu-cookie-prefs"
onClick={() => setPrefs((p) => !p)}
>
{prefs ? "Hide preferences" : "Preferences"}
</button>
{prefs ? (
<button
type="button"
className="gu-cookie__btn"
onClick={() => finish(choice, "Saved your preferences.")}
>
Save choices
</button>
) : (
<button
type="button"
className="gu-cookie__btn"
onClick={() =>
finish(
{ necessary: true, analytics: false, marketing: false },
"Only necessary cookies are on.",
)
}
>
Reject all
</button>
)}
<button
type="button"
className="gu-cookie__btn"
onClick={() =>
finish(
{ necessary: true, analytics: true, marketing: true },
"All cookies are on.",
)
}
>
Accept all
</button>
</div>
</section>
) : (
<div className="gu-cookie__done" role="status">
{saved}{" "}
<button type="button" onClick={() => setOpen(true)}>
Change cookie settings
</button>
</div>
)}
</div>
);
}
```
### cookie-banner.css
```css
/* CookieBanner: change these colors to match your brand. */
.gu-cookie-wrap {
--page: #f3eee3;
--card: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.12);
--btn: #0e0e10;
--btn-text: #f3eee3;
--on: #ff5a1f;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
position: relative;
display: grid;
align-content: end;
width: min(100%, 760px);
min-height: 480px;
padding: 16px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 24px;
background: var(--page);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-cookie-wrap[data-theme="dark"] {
--page: #0e0e10;
--card: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.12);
--btn: #f3eee3;
--btn-text: #0e0e10;
--focus: #d7ff3a;
}
/* Fake page content behind the banner */
.gu-cookie__page {
position: absolute;
inset: 24px 24px auto;
display: grid;
gap: 10px;
}
.gu-cookie__page i {
height: 14px;
border-radius: 7px;
background: var(--line);
}
.gu-cookie__page i:first-child {
width: 60%;
height: 34px;
}
.gu-cookie__page i:last-child {
width: 80%;
}
.gu-cookie {
position: relative;
display: grid;
gap: 14px;
padding: 20px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--card);
box-shadow: 0 24px 50px -24px rgb(0 0 0 / 0.35);
animation: gu-cookie-up 0.6s var(--ease);
}
@keyframes gu-cookie-up {
from {
opacity: 0;
transform: translateY(40px);
}
}
.gu-cookie__top {
display: grid;
grid-template-columns: 44px 1fr;
gap: 14px;
}
.gu-cookie__icon {
display: grid;
place-items: center;
width: 44px;
height: 44px;
border-radius: 14px;
background: var(--on);
}
.gu-cookie__icon svg {
width: 26px;
fill: #f3eee3;
}
.gu-cookie__icon circle {
fill: #0e0e10;
}
.gu-cookie h2 {
margin: 0 0 4px;
font-size: 1.15rem;
font-weight: 900;
letter-spacing: -0.02em;
}
.gu-cookie p {
margin: 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.5;
}
.gu-cookie a {
color: var(--text);
font-weight: 700;
}
.gu-cookie :focus-visible,
.gu-cookie__done button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
/* Preferences expand in place */
.gu-cookie__prefs {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.45s var(--ease);
}
.gu-cookie__prefs[data-open] {
grid-template-rows: 1fr;
}
.gu-cookie__prefs > div {
overflow: hidden;
}
.gu-cookie__row {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
min-height: 60px;
padding: 8px 2px;
border-top: 1px solid var(--line);
cursor: pointer;
}
.gu-cookie__row span {
display: grid;
}
.gu-cookie__row small {
color: var(--muted);
}
.gu-cookie__row input {
position: absolute;
opacity: 0;
}
.gu-cookie__switch {
position: relative;
flex: none;
width: 46px;
height: 28px;
border-radius: 99px;
background: var(--line);
transition: background-color 0.25s;
}
.gu-cookie__switch::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 22px;
height: 22px;
border-radius: 50%;
background: #fff;
box-shadow: 0 1px 4px rgb(0 0 0 / 0.3);
transition: translate 0.3s var(--ease);
}
.gu-cookie__row input:checked + .gu-cookie__switch {
background: var(--on);
}
.gu-cookie__row input:checked + .gu-cookie__switch::after {
translate: 18px 0;
}
.gu-cookie__row input:disabled + .gu-cookie__switch {
opacity: 0.55;
}
.gu-cookie__row input:focus-visible + .gu-cookie__switch {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-cookie__actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.gu-cookie button {
font: inherit;
cursor: pointer;
}
.gu-cookie__link {
min-height: 48px;
margin-right: auto;
padding: 0 4px;
border: 0;
background: none;
color: var(--text);
font-weight: 700 !important;
text-decoration: underline;
text-underline-offset: 4px;
}
/* Reject and Accept look the same on purpose: no dark patterns */
.gu-cookie__btn {
min-height: 48px;
padding: 0 20px;
border: 0;
border-radius: 999px;
background: var(--btn);
color: var(--btn-text);
font-weight: 800 !important;
}
.gu-cookie__btn:hover {
background: var(--on);
color: #0e0e10;
}
.gu-cookie__done {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 8px;
padding: 14px;
border-radius: 16px;
background: var(--card);
font-weight: 600;
}
.gu-cookie__done button {
border: 0;
background: none;
color: inherit;
font: inherit;
font-weight: 800;
text-decoration: underline;
cursor: pointer;
}
@container (max-width: 520px) {
.gu-cookie__actions {
flex-direction: column;
align-items: stretch;
}
.gu-cookie__link {
order: 3;
margin: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-cookie {
animation: none;
}
.gu-cookie__prefs,
.gu-cookie__switch,
.gu-cookie__switch::after {
transition: none;
}
}
```
You might also want
Next piece: Copy fieldAnnouncement bar
Components, beginner
Cart drawer
Components, advanced
Draggable bottom sheet
Components, advanced