Banner for Nuxt
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 /> inside <CookieYesRoot>. Banner.Root decides when the banner shows and hides, so keep it as the outer element.
<script setup lang="ts">
import { Banner } from "#imports";
const regulation = useRegulation();
</script>
<template>
<Banner.Root class="banner">
<Banner.Title class="banner-title" />
<Banner.Description class="banner-text" />
<Banner.Actions class="banner-actions">
<Banner.DoNotSell v-if="regulation === 'CCPA'" class="btn" />
<template v-else>
<Banner.AcceptAll class="btn btn-primary" />
<Banner.RejectAll class="btn" />
</template>
<Banner.OpenPreferences class="btn btn-link" />
</Banner.Actions>
</Banner.Root>
</template>Nuxt imports Banner for you in script, but a template only sees Banner.Root and the other pieces when Banner is a binding of the component, so import it from #imports, as above. useRegulation() needs no import.
The slot of <CookieYesRoot> replaces everything it renders by default, so list the other components you still want:
<template>
<CookieYesRoot>
<ConsentBanner />
<CookiePreferences />
<CookieOptOut />
<RecallButton />
</CookieYesRoot>
<NuxtPage />
</template>Inside the slot your banner gets the consent the module read from the request, so a returning visitor gets no banner in the server HTML.
Each piece renders with the translated text for the active language. Put your own content in its default slot 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
Add as-child to a button piece and pass one element. The piece puts its behaviour on your element instead of rendering its own <button>. It throws if the slot holds anything other than exactly one element:
<Banner.AcceptAll as-child>
<MyButton variant="primary">Accept all</MyButton>
</Banner.AcceptAll>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.
Your component already renders a <button>. Add as-child.
Customise does nothing.
CookiePreferences is not rendered. Banner.OpenPreferences only opens it.
Nothing renders, and the console says it failed to resolve Banner.Root.
Banner is not imported in the component. Add import { Banner } from "#imports"; to its <script setup>.
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