Cookie Consent
FreeA GDPR / ePrivacy cookie banner: a bottom banner, a corner card or a modal dialog, "Reject all" exactly as prominent as "Accept all", per-category native switches with nothing optional pre-ticked, and a consent record — the choices, an ISO timestamp and your policy version — for you to store and hand back.
import { SvCookieConsentComponent } from '@styloviz/cookie-consent';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
Your own categories
<sv-cookie-consent version="2026-10" privacyHref="/privacy" [categories]="[ { id: 'necessary', label: 'Strictly necessary', required: true }, { id: 'analytics', label: 'Analytics', description: 'Anonymous visit counts.' }, { id: 'marketing', label: 'Marketing' } ]" (recorded)="store($event)" />
A "Cookie settings" link in the footer
<!-- Withdrawing consent must be as easy as giving it. --> <sv-cookie-consent #cookies version="2026-10" [record]="saved()" (recorded)="saved.set($event)" /> <footer> <button type="button" (click)="cookies.openPreferences()">Cookie settings</button> </footer>
Re-ask when the policy changes
<!-- A stored record with an older version re-opens the banner, says the policy changed and starts from the visitor's previous choices. --> <sv-cookie-consent version="2026-10" [record]="storedRecord" (recorded)="persist($event)" />
Let it keep the record (opt-in)
<!-- Versioned envelope in localStorage, read once in the browser after the first render — never on the server. Without storageKey it stores nothing. --> <sv-cookie-consent version="2026-10" storageKey="acme.cookie-consent" (restored)="loadAllowed($event)" />
When to use
Any public site that sets non-essential cookies or similar storage (analytics, marketing pixels, embedded players) for visitors covered by the GDPR, the UK GDPR or the ePrivacy Directive. Gating third-party scripts: load each one only once `(recorded)` or `(restored)` says its category is allowed. A "Cookie settings" page or footer link, through `openPreferences()` or `[(preferencesOpen)]`. Not for terms-of-service acceptance or marketing-email opt-ins — those are separate consents with their own records.
Async states
None — this component has nothing to load, so it has no loading, empty or error state to drive.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| categories | readonly SvCookieCategory[] | [] | The purposes the visitor chooses between. Empty uses four built-in ones — strictly necessary (required), functional, analytics and marketing — worded from the labels, so they translate with the rest. Rows with an empty or duplicate `id` are skipped. |
| consent | SvCookieConsentState (two-way) | {} | The visitor's choices, category id → allowed. Two-way: bind `[(consent)]` to restore and keep them. Updated only when the visitor decides — toggling a switch changes nothing until "Save choices". Unknown ids are ignored and a missing optional id counts as refused. |
| record | SvCookieConsentRecord | null | null | The visitor's stored record, if any. With the current `version` the banner starts closed and `consent` is restored from it; with an older one the banner re-asks, says the policy changed and starts from the old choices. |
| version | string | '1' | Your cookie policy's version, stamped into every record. Change it when the policy changes and returning visitors are asked again. |
| open | boolean (two-way) | true | Whether the banner (or, for `layout="modal"`, the dialog) is showing. The component closes it when the visitor decides. Two-way: `[(open)]`. |
| preferencesOpen | boolean (two-way) | false | Whether the per-category preferences dialog is open. Set it `true` from a "Cookie settings" link so visitors can change their mind later — withdrawing consent must be as easy as giving it. Two-way: `[(preferencesOpen)]`. |
| layout | SvCookieConsentLayout | 'banner' | `banner` (full-width bottom bar), `corner` (compact card) or `modal` (dialog up front). |
| corner | SvCookieConsentCorner | 'end' | Which bottom corner the `corner` card sits in. |
| inline | boolean | false | Render the banner or card in normal flow instead of fixed to the viewport — for previews, docs and settings pages. The preferences dialog stays modal. |
| privacyHref | string | null | null | Your privacy or cookie policy URL. Relative paths, `http(s):` and `mailto:` are kept; anything else (`javascript:`, `data:`) drops the link. |
| labels | Partial<SvCookieConsentLabels> | {} | Per-instance text, merged over the registry's `cookieConsent` labels. |
| storageKey | string | null | null | Opt in to keeping the record in `localStorage` under this key, in a versioned envelope. Read once after the first render (never on the server); written on every decision. `null` keeps nothing — persistence is yours through `(recorded)` and `[record]`. |
Outputs
| Event | Payload type | Description |
|---|---|---|
| recorded | SvCookieConsentRecord | The visitor accepted, rejected or saved: their choices, an ISO timestamp and the policy version. |
| restored | SvCookieConsentRecord | A record was read back from `storageKey` — load the scripts it allows. |
| consentChange | SvCookieConsentState | Emits the new `consent` whenever it changes — the output half of `[(consent)]`. |
| openChange | boolean | Emits the new `open` whenever it changes — the output half of `[(open)]`. |
| preferencesOpenChange | boolean | Emits the new `preferencesOpen` whenever it changes — the output half of `[(preferencesOpen)]`. |
Customization & theming
Three layouts: `banner` (full-width bottom bar), `corner` (compact card, `corner="start" | "end"`) and `modal` (a native dialog up front). `[inline]="true"` renders the banner or card in normal flow — for previews, docs and settings pages. With no `categories`, four built-in ones (strictly necessary, functional, analytics, marketing) are worded from the labels and translate with the kit's language packs. Every string comes from the `cookieConsent` labels — `provideSvLabels(SV_COOKIE_CONSENT_LABELS_DE)` or the per-instance `labels` input. Buttons and switches paint from the `primary-*` tokens, so a template that remaps primary recolours them. Persistence is yours by default (`(recorded)`, `[record]`, `[(consent)]`); `storageKey` opts in to a versioned localStorage envelope.
Best practices
Do: Store the whole `SvCookieConsentRecord`, not just the booleans. The timestamp and policy `version` are what let you show when and under which policy a visitor agreed. Do: Bump `version` whenever the policy or the list of categories changes, so returning visitors are asked again. Do: Load analytics and marketing scripts only after consent — the banner records a choice; it does not block scripts for you. Don't: Don't restyle Reject all to be smaller or fainter than Accept all. Regulators treat unequal prominence as a dark pattern; the component shares one class string between the two on purpose. Don't: Don't mark a category `required` unless the site genuinely cannot work without it. Tip: Validate anything you read back from a cookie or storage with `svReadCookieConsentRecord()` before passing it to `[record]`.
Accessibility
The banner is a `role="region"` landmark with an accessible name. It is not a dialog: it never moves focus or blocks the page, so it does not interrupt a keyboard or screen-reader user. Preferences open in a native `<dialog>` via `showModal()`: the page behind is inert, Escape closes it, focus moves to its heading and returns to the control that opened it. Each category is a native `<input type="checkbox" role="switch">` with a `<label>` and `aria-describedby` pointing at its description, so Space toggles it and its state is announced as on/off. A required category is checked and disabled, with a visible "Always on" badge. Recording a choice is announced through a polite `role="status"` region, because the banner it was made in disappears. Under forced colours the switches fall back to the platform checkbox, so their state stays visible.
Feedback on Cookie Consent
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.