Nuxt Cookie Control
✅ Translated for: ar, az, be, bg, ca, cs, da, de, en, es, fi, fr, hr, hu, id, it, ja, km, ko, lt, nl, no, oc, pt, pl, ro, rs, ru, sk, sl, sv, tr, uk and zh-CN
✅ Vue 3 support
⚠️ For Nuxt 2, please use nuxt-cookie-control < 3.0.0
🚩 API changes since continuing Dario Ferderber's work on gitlab.com/broj42/nuxt-cookie-control, make sure to read the README!
🚀 Getting Started
Installation
npx nuxi@latest module add cookie-control
Configuration
modules: [
'@dargmuesli/nuxt-cookie-control'
],
cookieControl: {
}
modules: [
['@dargmuesli/nuxt-cookie-control', {
}]
]
Usage
<template>
<CookieControl locale="en" />
</template>
<script setup lang="ts">
const {
cookiesEnabled,
cookiesEnabledIds,
isConsentGiven,
isModalActive,
moduleOptions,
} = useCookieControl()
watch(
() => cookiesEnabledIds.value,
(current, previous) => {
if (
!previous?.includes('google-analytics') &&
current?.includes('google-analytics')
) {
window.location.reload()
}
},
{ deep: true },
)
</script>
const cookieControl = useCookieControl()
if (cookieControl.cookiesEnabledIds.value.includes('google-analytics')) {
initGoogleAnalytics()
}
API
Module Options
barPosition: 'bottom-full',
closeModalOnClickOutside: true,
colors: {
barBackground: '#000',
barButtonBackground: '#fff',
barButtonColor: '#000',
barButtonHoverBackground: '#333',
barButtonHoverColor: '#fff',
barTextColor: '#fff',
checkboxActiveBackground: '#000',
checkboxActiveCircleBackground: '#fff',
checkboxDisabledBackground: '#ddd',
checkboxDisabledCircleBackground: '#fff',
checkboxInactiveBackground: '#000',
checkboxInactiveCircleBackground: '#fff',
controlButtonBackground: '#fff',
controlButtonHoverBackground: '#000',
controlButtonIconColor: '#000',
controlButtonIconHoverColor: '#fff',
focusRingColor: '#808080',
modalBackground: '#fff',
modalButtonBackground: '#000',
modalButtonColor: '#fff',
modalButtonHoverBackground: '#333',
modalButtonHoverColor: '#fff',
modalOverlay: '#000',
modalOverlayOpacity: 0.8,
modalTextColor: '#000',
modalUnsavedColor: '#fff',
},
cookies: {
necessary: [],
optional: [],
}
cookieExpiryOffsetMs: 1000 * 60 * 60 * 24 * 365,
cookieNameIsConsentGiven: 'ncc_c',
cookieNameCookiesEnabledIds: 'ncc_e',
cookieOptions: {
path: '/',
sameSite: 'strict',
}
isAcceptNecessaryButtonEnabled: true
isControlButtonEnabled: true,
isCookieIdVisible: false,
isCssEnabled: true,
isCssPonyfillEnabled: false,
isDashInDescriptionEnabled: true,
isIframeBlocked: false,
isModalForced: false,
locales: ['en'],
localeTexts: {
en: {
save: 'Remember',
}
}
Cookies
Every property that includes a { en: ... }
value is a translatable property that could instead only specify a string ('...'
) or other locales as well ({ de: ..., uk: ... }
).
{
description: {
en: 'This cookie stores preferences.'
},
id: 'p',
isPreselected: false
name: {
en: 'Preferences'
},
links: {
'https://example.com/privacy': 'Privacy Policy',
'https://example.com/terms': 'Terms of Service',
},
src: 'https://example.com/preferences/js?id=<API-KEY>',
targetCookieIds: ['xmpl_a', 'xmpl_b'],
}
Component Slots
Bar
<CookieControl>
<template #bar>
<h2>Bar title</h2>
<p>Bar description (you can use $cookies.text.barDescription)</p>
<n-link>Go somewhere</n-link>
</template>
</CookieControl>
Modal
<CookieControl>
<template #modal>
<h2>Modal title</h2>
<p>Modal description</p>
</template>
</CookieControl>
Cookie
<CookieControl>
<template #cookie="{ cookie }">
<h3 v-text="cookie.name" />
<span v-html="cookie.description" />
<div v-if="cookie.targetCookieIds">
<b>Cookie ids: </b>
<span v-text="cookie?.targetCookieIds?.join(', ')" />
</div>
</template>
</CookieControl>
Iframe
<CookieIframe>
<template #iframe>
Content to display if iframes are blocked.
</template>
</CookieIframe>
Props
<CookieControl locale="de" />