Skip to content

CookiePreferences for Nuxt

Markdown
Loading…

The dialog where a visitor turns each cookie category on or off.

What it does

Opens when a visitor clicks Customise on the banner, or the recall button. It lists your categories with a switch each; the required category shows "Always active" instead of a switch. Nothing is saved until the visitor presses Save, Accept All or Reject All.

You already render it if you followed Installation. Without it, Customise does nothing.

In Nuxt, <CookieYesRoot /> renders it for you. If you list the components inside <CookieYesRoot> yourself, its slot replaces the default contents, so keep the dialog there:

app/app.vue
<template>
  <CookieYesRoot>
    <CookieBanner />
    <CookiePreferences />
    <RecallButton />
  </CookieYesRoot>
  <NuxtPage />
</template>

Change what it shows

Nothing on the dialog is set through props. Everything comes from the cookieyes key in nuxt.config.ts and app/app.config.ts:

To changeSet
Which categories appear, and their ordercategories, see Configuration
The wording, labels and descriptions, in any languagei18n, see Translations
Colours, corner radius, fonttheme, see Theme tokens

Open it from your own button

A "Cookie settings" link in your footer is the usual place:

app/components/CookieSettingsLink.vue
<script setup lang="ts">
const { showPreferences } = useConsentActions();
</script>

<template>
  <button type="button" @click="showPreferences">Cookie settings</button>
</template>

useConsentActions and the component itself are auto-imported: use <CookieSettingsLink /> in any page or layout.

Restyle it

It accepts class and style like any element; they land on the dialog card and are added to its own styling. To restyle one part, pass classNames or styles, keyed by part name in camelCase (overlay, root, title, close, intro, category, categoryLabel, categoryDescription, toggle, acceptAll, rejectAll, save, branding). Or use its data-cy-part in your CSS:

app/consent.css
[data-cy-part="dialog"] {
  max-width: 40rem;
}

The parts are overlay, dialog, title, close, intro, category, category-label, category-description, toggle, accept-all, reject-all, save and branding. For your own markup, use the Preferences primitives.

Changing categories re-prompts every returning visitor

Saved choices are tied to your category list. If you add or remove a category, rename an id, or change which one is required, every returning visitor sees the banner again and chooses afresh. Changing a label or description does not. Decide your category ids early; change the wording as often as you like.

Common mistakes

Customise does nothing. <CookiePreferences /> is not rendered.

A category is missing. Your categories list was rejected and the built-in five are shown instead. The console says why. See Configuration.

The Necessary switch cannot be turned off. Correct. It is required: true, and required cookies need no consent.

Next steps

On this page