Consent
Cookie / privacy consent drawer with per-category toggles. Slides up from the bottom of the viewport. Does not block the page - content behind remains scrollable and focusable. Pre-styled by Semetrika so every consumer site looks consistent out of the box. Designed for anonymous users and working entirely client-side via localStorage, with a cookie mode for servers that need to render consent-dependent markup conditionally.
Usage
When to use
- Any site that sets optional analytics, marketing, or functional cookies and needs visible consent before activating them
- Sites that need an ICO-compliant revisit mechanism so users can change their choices later
- Projects that want a zero-config consent banner with three sensible default categories out of the box
- Sites that want to gate third-party
<script>tags (analytics, marketing pixels) behind a consent category without writing custom activation JS
When not to use
- Sites that set no optional cookies - consent is not required
- Projects that need an authenticated, server-backed consent record tied to a user account - consent stores anonymous client-side state only
Do
- Give Accept and Reject equal visual weight - Reject must never be demoted to a link or hidden behind Preferences
- Place a re-open trigger in the site footer (
data-consent-open) so users can revisit their choices - Mark a third-party script inert with
<script type="text/plain" data-consent="analytics">- Semetrika activates it (swapping the type to executable) the moment that category is granted, so no custom activation JS is required for the common case - Use the
consent-accepted/consent-savedevent'sdetail.valuesfor anything beyond simple script activation - for example calling a vendor SDK's own init function - Bump
content_version(rendered asdata-consent-version) when the consent content changes outside the keyed controls - for example a privacy-policy update - to force returning visitors to re-consent - Set
use_cookiewhen the server has to read the choice. The cookie then holds the whole record - nothing is kept inlocalStorage- so the cookie's own expiry is what re-prompts a returning visitor
Don't
- Don't auto-grant consent by closing the drawer - dismissing without a choice does NOT grant
- Don't show the drawer repeatedly on every page load once a choice has been made
- Don't nest consent inside a modal - the drawer is intentionally non-blocking
API
Slots
| Slot | Required | Description |
|---|---|---|
title | Yes | Drawer heading. |
description | - | Drawer introductory text. |
preferences_title | - | Preferences dialog heading. |
preferences_description | - | Preferences dialog introductory text. |
preferences_content | - | Slot for controls carrying data-consent-key attributes. The component reads each checked state on save. |
Props
| Prop | Type | Default | Description |
|---|---|---|---|
storage_key | string | - | Key the choice is stored under, in localStorage or in a cookie. Resolution order: drawer attribute, then meta tag, then "consent". |
use_cookie | boolean | - | When true, state is stored in cookies rather than localStorage, so the server can read it and the cookie expiry decides when a returning visitor is re-prompted. |
id | string | consent-preferences | HTML id of the preferences dialog. |
content_version | string | - | Explicit content stamp rendered as data-consent-version and folded into the re-prompt hash. Bump it to force returning visitors to re-consent when the consent content changes outside the keyed controls, such as after a privacy-policy update. |
classes | array | - | Additional CSS class words merged into the root element. |
attributes | object | - | Additional HTML attributes spread on the root element. |
Classes
| Class | Capability | Default | Description |
|---|---|---|---|
.animated | Motion | Yes | Honors the animations option. |
.still | Motion | - | Forces motion off regardless of the option. |
Attributes
| Attribute | Set by | Required | Description |
|---|---|---|---|
data-consent | Consumer | Yes | Marks the consent drawer root. Required for the JS to enhance the drawer. On a script element with type="text/plain" outside the drawer, the same attribute instead names a consent category: the script is activated (its type swapped to executable) when that category is granted. |
data-consent-storage-key | Consumer | - | Per-drawer override for the storage key. Falls back to the consent-storage-key meta tag, then "consent". |
data-consent-version | Consumer | - | Explicit content stamp folded into the re-prompt hash. Bump it to force re-prompting when the consent content changes outside the keyed controls, such as after a privacy-policy update. |
data-consent-cookie | Consumer | - | When present, the JS stores state in cookies under the resolved storage key rather than in localStorage, so the server can read it and the cookie expiry decides when a returning visitor is re-prompted. |
data-consent-dialog | Consumer | - | HTML id of the preferences dialog associated with this drawer. |
data-consent-action | Consumer | - | On a descendant button, the action the JS performs on click. |
data-consent-key | Consumer | - | On any control inside the preferences dialog, the per-category key recorded in the saved state object. |
data-consent-open | Consumer | - | On any element outside the drawer, opens the drawer when clicked. Lets a footer link re-open the consent UI. |
Events
| Event | Direction | Description |
|---|---|---|
consent-accepted | Emits | Fired on the drawer when the user accepts all categories. |
consent-rejected | Emits | Fired on the drawer when the user rejects all optional categories. |
consent-saved | Emits | Fired on the drawer when the user saves their per-category choices from the preferences dialog. |
consent-canceled | Emits | Fired on the drawer when the user closes the preferences dialog without saving. |
consent-closed | Emits | Fired on the drawer when the drawer is closed (native dialog close event). |
consent-preferences-opened | Emits | Fired on the drawer when the preferences dialog transitions to the open state. |
consent-preferences-closed | Emits | Fired on the drawer when the preferences dialog transitions to the closed state. |
Accessibility
- WCAG 2.2 AA
- Drawer: native
<dialog>opened non-modally withshow(). Does not trap focus, does not block page content, and renders no close button - the drawer is dismissed by choosing Accept, Reject, or Preferences. - Preferences dialog: native
<dialog>invoked withshowModal()- full focus trap, Escape to close - Preferences dialog close button:
aria-label="Close" - Re-open trigger should live in the site footer so users can revisit their choices at any time (ICO compliance requirement).
prefers-reduced-motionsets--option-animationsto0, which collapses the slide-up transition to zero duration.- Accept and Reject buttons have equal visual weight - Reject is never demoted to a text link or hidden behind Preferences.
Dependencies
- modal - dialog primitive used by the preferences dialog via
<dialog>+showModal()
Progressive enhancement
- No JS: the drawer is hidden; no consent state exists yet.
- With JS: drawer renders on first visit, state persists in
localStorage(or in cookies whenuse_cookieis set), the preferences dialog becomes interactive, gated<script type="text/plain" data-consent="{category}">placeholders activate automatically once their category is granted, andconsent-accepted/consent-rejected/consent-savedevents fire with the per-category values for anything beyond simple script activation. - How to enable: ships with Semetrika's JS bundle; no opt-in required