Banner for SvelteKit
Build the consent banner with your own markup and classes, keeping CookieYes behaviour for showing, accepting, rejecting and translated text.
What it does
Banner.* gives you the banner's behaviour in pieces: when it shows, what each button does, the translated text. You supply the elements and the classes. The result behaves exactly like CookieBanner.
Use it only when CookieBanner cannot produce the markup your design needs. For colours, spacing or fonts, restyle CookieBanner instead; it is much less work.
Build a banner
Write the banner as a component, then render it in place of <CookieBanner /> in the ui snippet of <CookieYesRoot>. Banner.Root decides when the banner shows and hides, so keep it as the outer element.
<script lang="ts">
import { Banner, useRegulation } from "@cookieyes/sveltekit";
const regulation = useRegulation();
</script>
<Banner.Root class="banner">
<Banner.Title class="banner-title" />
<Banner.Description class="banner-text" />
<Banner.Actions class="banner-actions">
{#if regulation.current === "CCPA"}
<Banner.DoNotSell class="btn" />
{:else}
<Banner.AcceptAll class="btn btn-primary" />
<Banner.RejectAll class="btn" />
{/if}
<Banner.OpenPreferences class="btn btn-link" />
</Banner.Actions>
</Banner.Root>Every piece is also exported on its own, as BannerRoot, BannerTitle, BannerAcceptAll and so on. Both name the same component; use whichever reads better.
The ui snippet of <CookieYesRoot> replaces everything it renders by default, so list the other components you still want:
<script lang="ts">
import {
CookieOptOut,
CookiePreferences,
CookieYesRoot,
RecallButton,
} from "@cookieyes/sveltekit";
import ConsentBanner from "../lib/ConsentBanner.svelte";
import { config } from "../lib/cookieyes";
let { data, children } = $props();
</script>
<CookieYesRoot {config} state={data.consent}>
{#snippet ui()}
<ConsentBanner />
<CookiePreferences />
<CookieOptOut />
<RecallButton />
{/snippet}
{@render children()}
</CookieYesRoot>Inside <CookieYesRoot> your banner gets the consent the handle read from the request, so a returning visitor gets no banner in the server HTML. Banner.Root moves itself into #cookieyes-portal-root, a child of <body>, once mounted.
Each piece renders with the translated text for the active language. Put your own content between its tags (its children snippet) to replace it.
The pieces
| Piece | Renders | What it does |
|---|---|---|
Banner.Root | <div> | Shows the banner until the visitor chooses, then hides it. Keep it as the wrapper |
Banner.Title | <h2> | The banner heading |
Banner.Description | <p> | The banner text; switches to the CCPA wording automatically |
Banner.Actions | <div> | A plain container for the buttons |
Banner.AcceptAll | <button> | Accepts every category |
Banner.RejectAll | <button> | Rejects every optional category |
Banner.OpenPreferences | <button> | Opens the preferences dialog, so render CookiePreferences too |
Banner.DoNotSell | <button> | Opens the CCPA opt-out dialog, so render CookieOptOut too |
Banner.Close | <button> | Hides the banner for this page view. Saves nothing; the banner returns on the next page load |
Banner.Branding | <a> | The "Powered by" link |
Every piece takes class, style and the other attributes of the element it renders. The part names for CSS are the same as on CookieBanner.
Use your own button component
Give a button piece a child snippet. It receives { props }: the attributes and click handler of the piece. Spread them onto your own element, and the piece renders that instead of its own <button>:
<script lang="ts">
import { Banner } from "@cookieyes/sveltekit";
</script>
<Banner.AcceptAll>
{#snippet child({ props })}
<a {...props} href="#accepted">Accept all</a>
{/snippet}
</Banner.AcceptAll>Your own click handler, passed as onclick, runs first; call event.preventDefault() in it to stop the consent action.
Common mistakes
The banner shows for visitors who already chose.
Banner.Root is missing, or something else wraps the pieces. It must be the outer element.
A button renders inside another button.
You put your button component between the piece's tags. Use a child snippet and spread props onto it.
Customise does nothing.
CookiePreferences is not rendered. Banner.OpenPreferences only opens it.
The banner sits behind a sticky header.
Raise the z-index on your Banner.Root class.
Next steps
- Preferences: build the preferences dialog the same way
- OptOut: the CCPA opt-out dialog