Skip to content

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.

Terminal window
# 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-dom

That 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.

SpecifierExports
@vscookies/reactCookieConsentProvider, 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/headlessCookieConsentProvider, useCookieConsent, usePreferencesOpen, cmp, manageCookies, setPreferencesOpen, isPreferencesOpen, onPreferencesChange, countActiveItems, isCookieAllowed, isScriptAllowed, CookieConsentContextValue, PreferencesOpenListener, ConsentCookieState, ConsentOverrides
@vscookies/react/registryregisterSkin, getSkin, getSkins, useSkins, SkinDefinition, SkinAttribute
@vscookies/react/tokenstokens, GLOBAL_TOKEN_MANIFEST, GLOBAL_TOKEN_NAMES, scoped override helpers, token types
@vscookies/skin-minimalist-reactMinimalistBanner, MinimalistBannerProps, MinimalistMode, skin
@vscookies/skin-edito-reactEditoBanner, EditoBannerProps, EditoMode, skin
@vscookies/react/tokens.cssThe design-token variables.
@vscookies/react/style.cssThe aggregated stylesheet; a skin import already injects just its own CSS.

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.

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

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.

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();
FieldTypeDescription
hasConsentedbooleantrue once any decision is stored.
recordConsentRecord | nullFull record, or null.
stateConsentStatePer-category booleans (necessary always true).
catalogConsentCatalogResolved category rules with display metadata.
acceptAll() => ConsentRecordAccept all categories.
rejectAll() => ConsentRecordReject optional categories.
set(patch, overrides?) => ConsentRecordPatch categories and/or per-item overrides — { cookies?, scripts? } (custom decision). Overrides merge with stored ones; record.cookies / record.scripts hold them.
reset() => voidClear the record and show the banner again.
manageCookies() => voidEmit 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(); // unsubscribe

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.

PropTypeDefaultDescription
titlestring"We use cookies"Heading text (see the per-skin docs for whether it is rendered).
descriptionReactNodeexplanatory copyBody; links and emphasis allowed.
acceptLabelstring"Accept all cookies"Accept button label.
rejectLabelstring"Decline cookies"Reject button label.
manageLabelstring"Manage cookies"Manage button label.
showRejectbooleantrueHide the reject button entirely.
onAccept() => void—Runs after accepting (post-write).
onReject() => void—Runs after rejecting (post-write).
classNamestring—Extra class(es) on the root element.
modeMinimalistMode | EditoMode"light" / "default"Visual mode.

light (default), dark, blur (frosted glass).

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>;

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 | undefined
getSkins(); // -> all registered skins
useSkins(); // 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.

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.

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);
}

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

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.

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.