Installation for Svelte
Install the CookieYes SDK and get a working banner in three steps.
Three steps, about five minutes: install the package, write the configuration, and wrap your app in the provider once.
Prerequisites
| Requirement | Minimum |
|---|---|
| Node.js | 20 |
| Svelte | 5.20 |
The components are written in runes mode and ship as .svelte source, so your Svelte toolchain (@sveltejs/vite-plugin-svelte) compiles them.
1. Install
npm install @cookieyes/svelteThis is the only package you install. svelte is a peer dependency, so the copy your app already has is the one used.
2. Write the configuration
Put the configuration in a module of its own, so every render reuses one object instead of starting a new runtime:
import type { CookieYesConfig } from "@cookieyes/svelte";
export const config: CookieYesConfig = {
mode: "cookie-only", // no backend needed
regulation: "GDPR", // "GDPR" | "CCPA"
};<CookieYesProvider> takes the same configuration as initCookieYes() in the other packages, so a configuration copies across unchanged.
3. Render it once
A component starts the SDK and renders the banner, the preferences dialog and the recall button. Everything inside it can use the hooks:
<script lang="ts">
import type { Snippet } from "svelte";
import {
CookieBanner,
CookiePreferences,
CookieYesProvider,
RecallButton,
} from "@cookieyes/svelte";
import { config } from "./cookieyes";
let { children }: { children?: Snippet } = $props();
</script>
<CookieYesProvider {config}>
<CookieBanner />
<CookiePreferences />
<RecallButton />
{@render children?.()}
</CookieYesProvider>Wrap your app in it once. Keep the banner before your page content, so it is early in the DOM: screen readers announce it first, and it paints before the rest of the page.
<script lang="ts">
import CookieYesRoot from "./lib/CookieYesRoot.svelte";
</script>
<CookieYesRoot>
<h1>Your app</h1>
</CookieYesRoot>Load the stylesheet where the app starts:
import "@cookieyes/svelte/styles.css";
import { mount } from "svelte";
import App from "./App.svelte";
const target = document.getElementById("app");
if (target) mount(App, { target });The stylesheet import is required. The components ship no inline styling, so without it they render as plain text, with no error to tell you why.
Done. Reload the page and the banner appears. If it does not, see Common mistakes.
Common mistakes
The banner renders as unstyled text.
The stylesheet import is missing. Keep the @cookieyes/svelte/styles.css import in src/main.ts, where the example above has it.
A component or hook throws "could not find CookieYes".
It is not inside <CookieYesProvider>, or a hook was called outside component initialisation, for example in an event handler. Render it inside <CookieYesRoot>, as in step 3, and call hooks at the top of a component's <script>.
A warning that <CookieYesProvider> ran again with a different config.
The config is written inline, so each render builds a new object and replaces the runtime. Import the one object from src/lib/cookieyes.ts, as in step 2.
Next steps
- Configuration: every option the provider takes, including CCPA and languages
- Integrations: load Google Analytics, Meta Pixel and other tags only after consent
- Styling: match the banner to your brand