Skip to content

Cookie Consent

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.

On this page
Used for
  • Cookie banner
  • Analytics opt-in
  • Privacy preferences dialog
  • Footer cookie settings link
  • Consent before scripts load

Examples

Categories

Installation

npx knowns-ui@latest add cookie-consent
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.

Props

categories
{ id, label, description, required? }[]
Defaultnecessary, 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
Defaultfalse
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

  1. 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.
  2. 02Puts Accept all and Reject all side by side in the same style, so rejecting takes one click, with Customize next to them.
  3. 03Opens a preferences dialog with one switch per category from `categories`, where a `required` category shows Always on and cannot be turned off.
  4. 04Saves the choice in localStorage under `storageKey` with a version and a timestamp, and asks again when the version or a category changes.
  5. 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.
  6. 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

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

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" }).

Need help?

Stuck on Cookie Consent, or found something off? We read every message.