CookieBanner for SvelteKit
The consent banner a visitor sees on their first visit.
What it does
Shows on the first visit and stays until the visitor chooses. Under GDPR it offers Accept All, Reject All and Customise; under CCPA it offers Do Not Sell. After a choice it disappears and does not come back in that browser. Its close button hides it for this page view without saving anything; it returns on the next page load, and the recall button appears meanwhile. It shows and hides itself; you only render it.
You already render it if you followed Installation. Try it in the Playground.
In SvelteKit, <CookieYesRoot> renders it for you. To render it yourself, for example to pass it props, give <CookieYesRoot> a ui snippet: it replaces the stock banner, dialogs and recall button, so keep the others you need.
<script lang="ts">
import {
CookieBanner,
CookiePreferences,
CookieYesRoot,
RecallButton,
} from "@cookieyes/sveltekit";
import { config } from "../lib/cookieyes";
let { data, children } = $props();
</script>
<CookieYesRoot {config} state={data.consent}>
{#snippet ui()}
<CookieBanner class="my-banner" />
<CookiePreferences />
<RecallButton />
{/snippet}
{@render children()}
</CookieYesRoot>Change what it shows
Nothing on the banner is set through props. Everything comes from the config in src/lib/cookieyes.ts, so the banner, the dialogs and the recall button always match.
| To change | Set |
|---|---|
| The wording, in any language | i18n, see Translations |
| Colours, corner radius, font | theme, see Theme tokens |
| Which buttons appear | regulation: "GDPR" shows Accept / Reject / Customise, "CCPA" shows Do Not Sell |
| The categories behind Customise | categories, see Configuration |
Restyle it
It accepts class and style like any element; they land on the banner card and are added to its own styling, not replacing it. To restyle one part, pass classNames or styles, keyed by part name in camelCase (root, title, description, actions, acceptAll, rejectAll, customise, doNotSell, close, branding), for example classNames={{ acceptAll: "my-accept" }}. styles takes camelCase property names or custom properties. Or use its data-cy-part in your CSS:
[data-cy-part="banner"] {
max-width: 32rem;
}
[data-cy-part="accept-all"] {
font-weight: 600;
}The parts are banner, title, description, actions, accept-all, reject-all, customise, do-not-sell, close and branding. For your own markup, not only your own styles, use the Banner primitives.
Under CCPA
Do Not Sell opens the opt-out dialog, so render it next to the banner:
<CookieYesRoot> renders it for you whenever a CCPA regulation can apply: regulation: "CCPA", region.strictest: "CCPA" or a "CCPA" value in region.map. If you render the components in a ui snippet yourself, add it there:
<CookieBanner />
<CookieOptOut />Common mistakes
The banner never appears.
You already chose in this browser. Delete the cookieyes-consent cookie and reload.
The banner is unstyled text.
The stylesheet route is missing. Add src/routes/cookieyes/styles.css/+server.ts, as in Installation.
Do Not Sell does nothing.
<CookieOptOut /> is not rendered.
Next steps
- CookiePreferences: the dialog Customise opens
- RecallButton: lets a visitor change their choice later
- Banner primitives: the same behaviour with your own markup