Skip to main content

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-saved event's detail.values for anything beyond simple script activation - for example calling a vendor SDK's own init function
  • Bump content_version (rendered as data-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_cookie when the server has to read the choice. The cookie then holds the whole record - nothing is kept in localStorage - 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​

SlotRequiredDescription
titleYesDrawer 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​

PropTypeDefaultDescription
storage_keystring-Key the choice is stored under, in localStorage or in a cookie. Resolution order: drawer attribute, then meta tag, then "consent".
use_cookieboolean-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.
idstringconsent-preferencesHTML id of the preferences dialog.
content_versionstring-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.
classesarray-Additional CSS class words merged into the root element.
attributesobject-Additional HTML attributes spread on the root element.

Classes​

ClassCapabilityDefaultDescription
.animatedMotionYesHonors the animations option.
.stillMotion-Forces motion off regardless of the option.

Attributes​

AttributeSet byRequiredDescription
data-consentConsumerYesMarks 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-keyConsumer-Per-drawer override for the storage key. Falls back to the consent-storage-key meta tag, then "consent".
data-consent-versionConsumer-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-cookieConsumer-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-dialogConsumer-HTML id of the preferences dialog associated with this drawer.
data-consent-actionConsumer-On a descendant button, the action the JS performs on click.
data-consent-keyConsumer-On any control inside the preferences dialog, the per-category key recorded in the saved state object.
data-consent-openConsumer-On any element outside the drawer, opens the drawer when clicked. Lets a footer link re-open the consent UI.

Events​

EventDirectionDescription
consent-acceptedEmitsFired on the drawer when the user accepts all categories.
consent-rejectedEmitsFired on the drawer when the user rejects all optional categories.
consent-savedEmitsFired on the drawer when the user saves their per-category choices from the preferences dialog.
consent-canceledEmitsFired on the drawer when the user closes the preferences dialog without saving.
consent-closedEmitsFired on the drawer when the drawer is closed (native dialog close event).
consent-preferences-openedEmitsFired on the drawer when the preferences dialog transitions to the open state.
consent-preferences-closedEmitsFired on the drawer when the preferences dialog transitions to the closed state.

Accessibility​

  • WCAG 2.2 AA
  • Drawer: native <dialog> opened non-modally with show(). 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 with showModal() - 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-motion sets --option-animations to 0, 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 when use_cookie is set), the preferences dialog becomes interactive, gated <script type="text/plain" data-consent="{category}"> placeholders activate automatically once their category is granted, and consent-accepted/consent-rejected/consent-saved events 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
  • modal - dialog primitive used by the preferences overlay
  • footer - typical host for the re-open trigger