Preferences for Svelte
Build the consent preferences dialog with your own markup, keeping the category switches, saving, focus handling and Escape to close.
What it does
Preferences.* gives you the preferences dialog in pieces: when it opens, the list of categories, each category's label and switch state, and the buttons that save. You supply the elements and the classes. Focus handling, Escape to close and the dialog roles come with Preferences.Root, so you do not rebuild them.
Use it only when CookiePreferences cannot produce the markup your design needs, most often when a category row must be your own component. For colours and spacing, restyle CookiePreferences instead.
Build a dialog
Render this in place of <CookiePreferences />. Preferences.Root is the overlay: it renders nothing while the dialog is closed, so keep it as the outer element.
<script lang="ts">
import { Preferences } from "@cookieyes/svelte";
</script>
<Preferences.Root class="overlay">
<div class="dialog">
<Preferences.Title class="dialog-title" />
<Preferences.Close class="dialog-close" />
<Preferences.Intro class="dialog-text" />
<Preferences.Categories class="category-list">
{#snippet children(category)}
<Preferences.Category {category} class="category-row">
{#snippet children({ label, description, checked, disabled, toggle })}
<label class="category">
<input
type="checkbox"
{checked}
{disabled}
onchange={(event) => toggle(event.currentTarget.checked)}
/>
<span>{label}</span>
<small>{description}</small>
</label>
{/snippet}
</Preferences.Category>
{/snippet}
</Preferences.Categories>
<div class="dialog-actions">
<Preferences.RejectAll class="btn" />
<Preferences.AcceptAll class="btn" />
<Preferences.Save class="btn btn-primary" />
</div>
</div>
</Preferences.Root>Categories renders its children snippet once per category in your categories list, passing the category id, so custom categories appear without any change here. Category passes its children snippet the resolved text and state for one row; you decide how the row looks. Every piece is also exported on its own, as PreferencesRoot, PreferencesCategory and so on.
Unlike Banner.Root, Preferences.Root renders where you put it; it is not moved into the portal root.
The pieces
| Piece | Renders | What it does |
|---|---|---|
Preferences.Root | <div> | The overlay. Opens and closes the dialog, traps focus, closes on Escape. Keep it as the wrapper |
Preferences.Title | <h2> | The dialog heading |
Preferences.Intro | <p> | The dialog text |
Preferences.Close | <button> | Closes without saving |
Preferences.Categories | <div role="list"> | Renders its children snippet once per category, with the category id |
Preferences.Category | <div> | One row. Takes the category prop and passes its children snippet category, label, description, checked, disabled and toggle(next) |
Preferences.AcceptAll | <button> | Accepts every category and saves |
Preferences.RejectAll | <button> | Rejects every optional category and saves |
Preferences.Save | <button> | Saves the switches as they are |
Preferences.Branding | <a> | The "Powered by" link |
Every piece takes class, style and the other attributes of the element it renders, and renders the translated text for the active language; put your own content between its tags to replace it. The part names for CSS are the same as on CookiePreferences.
Good to know
A switch changes nothing until Save. toggle updates the row; Save, AcceptAll or RejectAll records the choice. Always render at least one of them.
A required category arrives locked. Its checked is true, disabled is true, and toggle does nothing. Show it as text such as "Always active" rather than a switch that does not work.
toggle takes the new value, not the event. Write onchange={(event) => toggle(event.currentTarget.checked)}. A component whose change handler already gives you a boolean, such as a Switch, can take toggle directly.
Use your own button component
Give Close, AcceptAll, RejectAll or Save a child snippet and spread its props onto your own element:
<script lang="ts">
import { Preferences } from "@cookieyes/svelte";
</script>
<Preferences.Save>
{#snippet child({ props })}
<button {...props} class="my-button">Save preferences</button>
{/snippet}
</Preferences.Save>Common mistakes
Switches turn on but never off.
onchange={toggle} passes the event, which counts as true. Use onchange={(event) => toggle(event.currentTarget.checked)}.
Choices are lost when the dialog closes.
There is no Save, AcceptAll or RejectAll in your dialog.
A custom category is missing.
You listed category ids by hand instead of using Preferences.Categories.
The dialog never opens.
Nothing opens it. Render Banner.OpenPreferences, RecallButton, or your own button calling useConsentActions().showPreferences().
Next steps
- Banner: build the banner the same way
- OptOut: the CCPA opt-out dialog
- Translations: the dialog and category text in every language