Skip to content

Use cases

Each recipe maps to a live Storybook story — run pnpm storybook and open Skins/Minimalist/React or React/Headless/UseConsent.

import { CookieConsentProvider } from "@vscookies/react";
import { MinimalistBanner } from "@vscookies/skin-minimalist-react";
<CookieConsentProvider>
<MinimalistBanner />
</CookieConsentProvider>;

The banner renders null after a decision, so you can mount it once and forget it. → Storybook: Skins/Minimalist/React › Light.

<MinimalistBanner showReject={false} />

→ Storybook: Skins/Minimalist/React › Light (set showReject to false in Controls).

<MinimalistBanner
title="We value your privacy"
description={<>We use cookies to run the site and measure traffic. <a href="/privacy">Privacy policy</a>.</>}
acceptLabel="Sounds good"
rejectLabel="Only essential"
/>

Every prop above has a live control in Storybook: Skins/Minimalist/React › Light (edit title, description, accept-label, and reject-label in the Controls panel).

import { useEffect } from "react";
import { useCookieConsent } from "@vscookies/react";
function Analytics() {
const { hasConsented, state } = useCookieConsent();
useEffect(() => {
if (hasConsented && state.analytics) initAnalytics();
}, [hasConsented, state.analytics]);
return null;
}

→ Storybook: UseCases › Analytics Gating.

Build a preferences panel from the hook:

function Categories() {
const { state, set, acceptAll, rejectAll } = useCookieConsent();
return (
<div>
{(["preferences", "analytics", "marketing"] as const).map((c) => (
<label key={c}>
<input
type="checkbox"
checked={state[c]}
onChange={(e) => set({ [c]: e.target.checked })}
/>
{c}
</label>
))}
<button onClick={acceptAll}>Accept all</button>
<button onClick={rejectAll}>Reject all</button>
</div>
);
}

→ Storybook: UseCases › Category Toggles.

When a category owns more than one cookie (for example two analytics cookies and a marketing pixel), keep a typed registry and sync it with the store. The full example — including deleting every cookie in a category when consent is revoked — is in Core › Write multiple cookies per category.

consent.subscribe(() => syncCategoryCookies(consent.get()));
syncCategoryCookies(consent.get());

Bump the consent version so existing records are ignored and the banner returns:

import { ConsentStore } from "@vscookies/core";
const store = new ConsentStore({ version: 2 });

With the React provider (which uses version 1), call reset() when your policy changes, or use core directly for a custom version. See Build your own UI.

Set the theme attribute on the root, or let the system preference decide:

<html data-vsc-theme="dark">
<MinimalistBanner mode="dark" />

→ Storybook: Skins/Minimalist/React › Dark, Skins/Edito/React › Ticket.

With an import map in place (see Installing), import the vanilla runtime and a skin:

<script type="module">
import { defineAll } from "@vscookies/vanilla/define";
import "@vscookies/skin-edito-vanilla";
defineAll();
</script>
<vscookies-edito mode="ticket" title="Cookies"></vscookies-edito>

→ Storybook: Skins/Minimalist/WebComponent, Skins/Edito/WebComponent. Both WebComponent stories expose the same Controls panel as their React counterparts: text, boolean, mode radio and className. The Skins/Playground story shows how many skins are active.