A consent banner with Accept all, Reject all and Customize, and a preferences dialog with one switch per category. It asks once, saves the choice and tells you, and never blocks the page. Rejecting is exactly as easy as accepting. Knowns UI's own site sets one preference cookie (your theme) and no tracking cookies, so it needs no consent banner; this block is for your product.
- Cookie banner
- Analytics opt-in
- Privacy preferences dialog
- Footer cookie settings link
- Consent before scripts load
Examples
Installation
npx knowns-ui@latest add cookie-consentPaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Cookie consent from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/cookie-consent
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add cookie-consent
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- components/ui/knowns/spinner.tsx
- components/ui/knowns/button.tsx
- components/ui/knowns/dialog.tsx
- components/ui/knowns/switch.tsx
- components/ui/knowns/blocks/consent/cookie-consent.tsx
- components/ui/knowns/blocks/consent/consent-utils.ts
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0, class-variance-authority@^0.7.1.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/cookie-consent, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "cookie-consent" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- categories
{ id, label, description, required? }[]- Default
necessary, analytics, marketing - `required` categories are shown as Always on and stay on in every choice.
- storageKey
string|null- Default
"kn-cookie-consent" - localStorage key. The saved value is versioned: a new category asks again. `null` keeps the choice in memory.
- onChange
(choice, source) => void- Called with { id: boolean } and "accept_all", "reject_all" or "custom". Not called for a choice read from storage.
- policy
{ label, href }- A link to your cookie policy, in the banner text.
- title / description
string- The banner copy.
- inline
boolean- Default
false - Render in the flow instead of fixed to the bottom corner.
| Prop | Type | Default | Description |
|---|---|---|---|
| categories | { id, label, description, required? }[] | necessary, analytics, marketing | `required` categories are shown as Always on and stay on in every choice. |
| storageKey | string | null | "kn-cookie-consent" | localStorage key. The saved value is versioned: a new category asks again. `null` keeps the choice in memory. |
| onChange | (choice, source) => void | - | Called with { id: boolean } and "accept_all", "reject_all" or "custom". Not called for a choice read from storage. |
| policy | { label, href } | - | A link to your cookie policy, in the banner text. |
| title / description | string | - | The banner copy. |
| inline | boolean | false | Render in the flow instead of fixed to the bottom corner. |
The banner is a labelled region and does not take or trap focus, so the page stays usable; the preferences dialog is native and traps focus. Nothing renders until the saved choice has been read, so a returning visitor sees no flash. Mount one instance: every instance listens for openCookiePreferences().
What it handles
- 01Renders a small card fixed to the bottom corner, or in the flow with `inline`, that asks once and never blocks the page or takes focus.
- 02Puts Accept all and Reject all side by side in the same style, so rejecting takes one click, with Customize next to them.
- 03Opens a preferences dialog with one switch per category from `categories`, where a `required` category shows Always on and cannot be turned off.
- 04Saves the choice in localStorage under `storageKey` with a version and a timestamp, and asks again when the version or a category changes.
- 05Calls `onChange` with the final choice and its source (accept_all, reject_all or custom) so you load scripts only after it, but not for a choice read from storage.
- 06Reopens the dialog from anywhere through openCookiePreferences(), and renders nothing until it has read storage, so a returning visitor sees no flash.
States
- First render
- Nothing renders until storage is read, then the banner or nothing
- none
- Choose Accept all or Reject all
- The banner is removed at once
- none
- Open Customize
- The dialog comes from its own component
- none
- Toggle a category
- The switch moves from its own component
- none
- Reopen from the footer
- The dialog opens with the saved choice filled in
- none
| State | What moves | Timing |
|---|---|---|
| First render | Nothing renders until storage is read, then the banner or nothing | none |
| Choose Accept all or Reject all | The banner is removed at once | none |
| Open Customize | The dialog comes from its own component | none |
| Toggle a category | The switch moves from its own component | none |
| Reopen from the footer | The dialog opens with the saved choice filled in | none |
Works with
When to use
When to use
- A product that sets analytics or marketing cookies and has to ask first.
- A footer link that reopens the preferences later.
When not to use
- A site-wide message that is not about consent: use Announcement Bar.
- A choice inside a form: use Switch.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI Cookie Consent to my app and wire it to my own data.
- I need a product that sets analytics or marketing cookies and has to ask first. Set that up with the Knowns UI Cookie Consent.
- Which Cookie Consent props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "cookie-consent" }) and search_docs({ query: "cookie consent" }).