Skip to content

Focused hooks for SvelteKit

Markdown
Loading…

Ten small hooks that each answer one question: is this category granted, which law applies, is the banner showing, and more.

What they are

useConsent() gives you everything and updates on every change. The hooks on this page each answer one question. A hook that answers with one value (useConsentCategory, useRegulation, useBannerVisibility, usePreferencesOpen, useOptOutOpen) returns it as { current }, because a function cannot return a reactive value; the Returns column is the type of current. The others return an object whose fields stay reactive when read in a template or a $derived; destructuring reads them once. Every one of them throws if the component is not rendered inside <CookieYesRoot>, and must be called while the component initialises.

You want to knowHookReturns
Is one category granted?useConsentCategory("analytics")boolean, the saved value
Which privacy law applies?useRegulation()"GDPR" or "CCPA"
Why that law?useRegion(){ region, regulation, source, confidence }
Which categories are configured?useCategories(){ list, ids, requiredIds, isDefault }
Is the banner on screen?useBannerVisibility()boolean
Is a dialog open?usePreferencesOpen(), useOptOutOpen()boolean
What is the text in the active language?useTranslations()the translation map
Which language is active, and how do I switch it?useLanguage(){ language, direction, languages, setLanguage }
Does a change need a page reload?useReloadNotice(){ required, reasons, dismiss }
src/lib/AnalyticsWidget.svelte
<script lang="ts">
  import { useConsentCategory } from "@cookieyes/sveltekit";

  const allowed = useConsentCategory("analytics");
</script>

{#if allowed.current}
  <iframe title="Dashboard" src="https://analytics.example.com/embed"></iframe>
{:else}
  <p>Enable analytics cookies to see your dashboard.</p>
{/if}

The category can also be a getter, such as useConsentCategory(() => category) for a category prop, so it stays reactive.

useConsentCategory reads the saved value, so a switch flipped in the dialog but not saved changes nothing here. For an <iframe> with a ready-made placeholder, GatedFrame does this for you.

Which law applies, and why

useRegulation() returns the law in effect: "GDPR" or "CCPA".

useRegion() tells you how that was decided, which is the tool when a visitor sees an unexpected banner. source is "manual" when you set regulation yourself, "detected" when region.detect matched your region.map, and "strictest" when detection found nothing and region.strictest applied. confidence is "low" when the region was guessed.

Build your own category list

useCategories() returns your configured categories in order (list), their ids, the ids that are always on (requiredIds), and isDefault, which is true when the built-in five are in effect. Render from list rather than writing the five names by hand, so custom categories appear automatically. The list is fixed when the runtime starts, so it is a plain object that never changes.

The banner and the dialogs

useBannerVisibility() is true while the banner is on screen: the visitor has not chosen, has not closed it, and no dialog is open. Use it to move your own floating elements out of the way. It is not "has the visitor decided": for that, read useConsent().hasActed.

usePreferencesOpen() and useOptOutOpen() report the two dialogs.

Language and text

useTranslations() returns the text for the active language, the same strings the components show, and updates when the language changes. useLanguage() returns the active language, its direction for right-to-left layouts and the loaded languages as reactive fields, plus setLanguage(tag), which returns a promise. Translations has a picker built on it.

A custom reload notice

useReloadNotice() returns required, the tool ids in reasons, and dismiss(). Most sites should render ReloadNotice instead; use the hook only for your own markup.

Common mistakes

"Ask for consent" shows while the visitor is inside the dialog. You used useBannerVisibility() for "has not decided yet". Use useConsent().hasActed.

A custom category is missing from your list. You wrote the five built-in names by hand. Render from useCategories().list.

Next steps

On this page