Skip to content
ComponentsCookieBanner

CookieBanner for SvelteKit

Markdown
Loading…

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.

src/routes/+layout.svelte
<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 changeSet
The wording, in any languagei18n, see Translations
Colours, corner radius, fonttheme, see Theme tokens
Which buttons appearregulation: "GDPR" shows Accept / Reject / Customise, "CCPA" shows Do Not Sell
The categories behind Customisecategories, 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:

src/consent.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

On this page