React
@vscookies/react is the React runtime: a headless provider/hook, a skin
registry, and the token contract. It bundles @vscookies/core and
@vscookies/tokens. Banners live in separate skin packages such as
@vscookies/skin-minimalist-react and @vscookies/skin-edito-react.
# 1. Download from https://vs-goods.store/ and unzip into vendor/:# vscookies-react-v0.2.0.zip -> vendor/vscookies/react/# vscookies-skin-minimalist-react-v0.2.0.zip -> vendor/vscookies/skin-minimalist-react/# 2. Install the peer dependencies from npm:npm install react react-domThat is the whole React install: one runtime zip + one skin zip (add more
skin zips if you ship more skins), plus npm react and react-dom (^18 || ^19). See Installing from the zip for the bundler aliases.
Imports
Section titled “Imports”| Specifier | Exports |
|---|---|
@vscookies/react | CookieConsentProvider, useCookieConsent, usePreferencesOpen, cmp, manageCookies, setPreferencesOpen, isPreferencesOpen, onPreferencesChange, countActiveItems, isCookieAllowed, isScriptAllowed, registerSkin, getSkin, getSkins, useSkins, tokens, GLOBAL_TOKEN_MANIFEST, GLOBAL_TOKEN_NAMES, scoped override helpers, core types |
@vscookies/react/headless | CookieConsentProvider, useCookieConsent, usePreferencesOpen, cmp, manageCookies, setPreferencesOpen, isPreferencesOpen, onPreferencesChange, countActiveItems, isCookieAllowed, isScriptAllowed, CookieConsentContextValue, PreferencesOpenListener, ConsentCookieState, ConsentOverrides |
@vscookies/react/registry | registerSkin, getSkin, getSkins, useSkins, SkinDefinition, SkinAttribute |
@vscookies/react/tokens | tokens, GLOBAL_TOKEN_MANIFEST, GLOBAL_TOKEN_NAMES, scoped override helpers, token types |
@vscookies/skin-minimalist-react | MinimalistBanner, MinimalistBannerProps, MinimalistMode, skin |
@vscookies/skin-edito-react | EditoBanner, EditoBannerProps, EditoMode, skin |
@vscookies/react/tokens.css | The design-token variables. |
@vscookies/react/style.css | The aggregated stylesheet; a skin import already injects just its own CSS. |
CookieConsentProvider
Section titled “CookieConsentProvider”Wrap the part of your tree that renders skins or reads consent.
import { CookieConsentProvider } from "@vscookies/react";
<CookieConsentProvider> {/* skins and callers of useCookieConsent live here */}</CookieConsentProvider>;The provider is backed by a single module-level ConsentStore using the core
defaults (storage key vsc_consent, version 1, and the four standard
categories), read through useSyncExternalStore. Because the store is shared,
you can mount several skins in one provider — they all reflect and update the
same decision.
Add your own tags and cookies
Section titled “Add your own tags and cookies”Pass a config prop to give the provider its own consent rules (custom
categories with their scripts / cookies, plus serverEndpoint). Without
config, the shared defaults are used. Local and online scripts are declared
the same way — only the src differs; execution: "async" is for isolated
tags, defer (the default) for ordered/dependent ones:
import { CookieConsentProvider } from "@vscookies/react";import { DEFAULT_CATEGORY_RULES } from "@vscookies/core";import { MinimalistBanner } from "@vscookies/skin-minimalist-react";
<CookieConsentProvider config={{ serverEndpoint: "/api/consent-cookies", // for server: true cookies (Safari ITP) categories: { ...DEFAULT_CATEGORY_RULES, analytics: { ...DEFAULT_CATEGORY_RULES.analytics, scripts: [ { src: "https://www.googletagmanager.com/gtm.js?id=GTM-XXXX", // online title: "Google Tag Manager", description: "Analytics and ads.", setsCookies: ["_ga", "_gid"], // purged automatically when denied }, { src: "/vendor/local-vendor.js", execution: "defer", title: "Local vendor" }, // local ], }, marketing: { ...DEFAULT_CATEGORY_RULES.marketing, cookies: [ { name: "campaign", title: "Campaign ID", description: "Remembers which campaign brought you here.", value: () => new URLSearchParams(location.search).get("utm_campaign") ?? "", options: { maxAge: 604800 }, }, ], }, }, }}> <MinimalistBanner mode="light" /></CookieConsentProvider>;Tags are injected only while their category is allowed and removed on
revocation; cookies are written while allowed and purged on revocation.
Per-cookie title/description show up in the manage view automatically, and
each cookie can be denied individually (deny-only within an allowed category).
Mount a skin
Section titled “Mount a skin”import { CookieConsentProvider } from "@vscookies/react";import { MinimalistBanner } from "@vscookies/skin-minimalist-react";
<CookieConsentProvider> <MinimalistBanner mode="light" /></CookieConsentProvider>;Importing a skin calls registerSkin for you and injects only that skin’s CSS.
In development you can mount several skins at once; in production import only
the skins you ship.
useCookieConsent
Section titled “useCookieConsent”Reads the context value. Throws if called outside a CookieConsentProvider.
import { useCookieConsent } from "@vscookies/react";
const { hasConsented, record, state, catalog, acceptAll, rejectAll, set, reset, manageCookies } = useCookieConsent();| Field | Type | Description |
|---|---|---|
hasConsented | boolean | true once any decision is stored. |
record | ConsentRecord | null | Full record, or null. |
state | ConsentState | Per-category booleans (necessary always true). |
catalog | ConsentCatalog | Resolved category rules with display metadata. |
acceptAll | () => ConsentRecord | Accept all categories. |
rejectAll | () => ConsentRecord | Reject optional categories. |
set | (patch, overrides?) => ConsentRecord | Patch categories and/or per-item overrides — { cookies?, scripts? } (custom decision). Overrides merge with stored ones; record.cookies / record.scripts hold them. |
reset | () => void | Clear the record and show the banner again. |
manageCookies | () => void | Emit a UI signal to open cookie preferences (= setPreferencesOpen(true)). No consent change. |
Manage cookies signal & preference open-state
Section titled “Manage cookies signal & preference open-state”manageCookies() is a UI event, not a consent mutation: it does not clear the
record, touch dataLayer, or stop tags. Use it for a footer “Cookie settings”
link. The shared open-state is readable and writable through
usePreferencesOpen() / setPreferencesOpen(open) / isPreferencesOpen() /
onPreferencesChange(listener) — all banners on the page share one state, so a
footer link and a banner stay in sync. SSR renders the state as false.
import { useCookieConsent, usePreferencesOpen, setPreferencesOpen, onPreferencesChange } from "@vscookies/react";import { useEffect } from "react";
function Footer() { const { manageCookies } = useCookieConsent(); return <button onClick={manageCookies}>Cookie settings</button>;}
function Preferences() { const open = usePreferencesOpen(); return open ? <PreferencePanel onClose={() => setPreferencesOpen(false)} /> : null;}
// Non-React listeners (or effects):useEffect(() => onPreferencesChange((open) => console.log("preferences open:", open)), []);onPreferencesChange(listener) returns an unsubscribe function. The same
functions are exported from @vscookies/vanilla, so vanilla pages use the
identical signal:
import { manageCookies, onPreferencesChange } from "@vscookies/vanilla";
document.querySelector("#cookie-settings").addEventListener("click", manageCookies);const off = onPreferencesChange((open) => open && openPreferences());// off(); // unsubscribeBanner props
Section titled “Banner props”MinimalistBanner and EditoBanner share the same props; only the mode values
differ. Both render null once consent exists, so they are safe to mount
unconditionally.
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | "We use cookies" | Heading text (see the per-skin docs for whether it is rendered). |
description | ReactNode | explanatory copy | Body; links and emphasis allowed. |
acceptLabel | string | "Accept all cookies" | Accept button label. |
rejectLabel | string | "Decline cookies" | Reject button label. |
manageLabel | string | "Manage cookies" | Manage button label. |
showReject | boolean | true | Hide the reject button entirely. |
onAccept | () => void | — | Runs after accepting (post-write). |
onReject | () => void | — | Runs after rejecting (post-write). |
className | string | — | Extra class(es) on the root element. |
mode | MinimalistMode | EditoMode | "light" / "default" | Visual mode. |
Minimalist modes
Section titled “Minimalist modes”light (default), dark, blur (frosted glass).
Edito modes
Section titled “Edito modes”default (stacked card), ticket (accent-lined card, pill accept button).
import { CookieConsentProvider } from "@vscookies/react";import { MinimalistBanner } from "@vscookies/skin-minimalist-react";import { EditoBanner } from "@vscookies/skin-edito-react";
<CookieConsentProvider> <MinimalistBanner mode="blur" /> {/* or */} <EditoBanner mode="ticket" onAccept={() => console.log("accepted")} /></CookieConsentProvider>;Skin registry
Section titled “Skin registry”Every skin package exports a skin descriptor. Skins self-register on import,
and the runtime exposes helpers to inspect them:
import { registerSkin, getSkin, getSkins, useSkins,} from "@vscookies/react";import { skin as minimalist } from "@vscookies/skin-minimalist-react";
getSkin(minimalist.slug); // -> SkinDefinition | undefinedgetSkins(); // -> all registered skinsuseSkins(); // reactive list, backed by useSyncExternalStore// registerSkin(customSkin); // register a skin yourself (idempotent by slug)A SkinDefinition has slug, displayName, modes, defaultMode, a React
component, attributes, and a tokens manifest. useSkins() is handy for dev
tooling — the Storybook Skins/Playground story shows the active counts. See
Skins for the shipped skins.
Token contract
Section titled “Token contract”Both runtimes export the same global token contract. Use it to build your own live token editor (there is no built-in editor):
import { GLOBAL_TOKEN_MANIFEST, GLOBAL_TOKEN_NAMES, setTokenOverrides, clearTokenOverrides, clearAllTokenOverrides,} from "@vscookies/react";
setTokenOverrides({ "--vsc-global-radius-xl": "0.5rem" });setTokenOverrides({ "--vsc-min-color-accent": "#7c3aed" }, { skin: "minimalist" });clearTokenOverrides({ skin: "minimalist" });clearAllTokenOverrides();GLOBAL_TOKEN_MANIFEST describes only the four shared --vsc-state-* values.
UI tokens are exported as skinTokens by each skin package. Omitting skin
writes to :root; passing a slug writes to [data-vsc-skin="<slug>"]. See
Tokens for the full reference.
Theming
Section titled “Theming”Use className for one-off overrides, and the tokens for global
restyling. The root elements expose stable class hooks:
- Minimalist:
.vsc-react-min,.vsc-react-min--{mode} - Edito:
.vsc-react-edito,.vsc-react-edito--{mode} - Buttons:
.vsc-react-btn,.vsc-react-btn--primary,.vsc-react-btn--ghost
<MinimalistBanner className="my-banner" />.my-banner .vsc-react-min__panel { border-radius: var(--vsc-min-radius-lg);}Callbacks and reacting to changes
Section titled “Callbacks and reacting to changes”onAccept / onReject fire after the consent write, so re-reading state there
reflects the new decision. For app-wide reactions, subscribe to state:
import { useCookieConsent } from "@vscookies/react";
function AnalyticsGate() { const { hasConsented, state } = useCookieConsent(); if (hasConsented && state.analytics) { loadAnalytics(); // your side effect } return null;}<EditoBanner onAccept={() => window.dispatchEvent(new Event("consent:changed"))} onReject={() => window.dispatchEvent(new Event("consent:changed"))}/>Icons and a11y
Section titled “Icons and a11y”Each skin renders role="dialog" with aria-live="polite" and an accessible
label. Buttons are real <button> elements with a visible focus ring. See
Accessibility.
Try it live
Section titled “Try it live”Run pnpm storybook. Skins are under Skins/Minimalist/React and
Skins/Edito/React; the headless hook is under React/Headless/UseConsent.
Skins/Playground shows how many React and Web Component skins are active.