• Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A highly configurable cookie banner for Nuxt.

Nuxt Cookie Control

npm version npm downloads

nuxt-cookie-control

Translated for: ar, az, bg, cs, da, de, en, es, fi, fr, hr, hu, it, ja, ko, lt, nl, no, oc, pt, pl, ro, rs, ru, sk, sv, tr and uk

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

Stackblitz

Installation

npm i -D @dargmuesli/nuxt-cookie-control
yarn add -D @dargmuesli/nuxt-cookie-control
pnpm i -D @dargmuesli/nuxt-cookie-control

Configuration

// nuxt.config.js

modules: [
  '@dargmuesli/nuxt-cookie-control'
],
cookieControl: {
  // typed module options
}

// or

modules: [
  ['@dargmuesli/nuxt-cookie-control', {
    // untyped module options
  }]
]

Usage

<!-- app.vue -->

<template>
  <CookieControl locale="en" />
</template>

<script setup lang="ts">
const {
  cookiesEnabled,
  cookiesEnabledIds,
  isConsentGiven,
  isModalActive,
  moduleOptions
} = useCookieControl()

// example: react to a cookie being accepted
watch(
  () => cookiesEnabledIds.value,
  (current, previous) => {
    if (
      (!previous?.includes('google-analytics') &&
        current?.includes('google-analytics'))
    ) {
      // cookie with id `google-analytics` got added
      window.location.reload() // placeholder for your custom change handler
    }
  },
  { deep: true }
)
</script>
//  plugins/analytics.client.ts

// example: initialization based on enabled cookies
const cookieControl = useCookieControl()

if (cookieControl.cookiesEnabledIds.value.includes('google-analytics')) {
    initGoogleAnalytics() // placeholder for your custom initialization
}

API

Module Options

// Position of cookie bar.
// 'top-left', 'top-right', 'top-full', 'bottom-left', 'bottom-right', 'bottom-full'
barPosition: 'bottom-full',

// Switch to toggle if clicking the overlay outside the configuration modal closes the modal.
closeModalOnClickOutside: true,

// Component colors.
// If you want to disable colors set colors property to false.
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',
},

// The cookies that are to be controlled.
// See detailed explanation further down below!
cookies: {
  necessary: [],
  optional: [],
}

// The milliseconds from now until expiry of the cookies that are being set by this module.
cookieExpiryOffsetMs: 1000 * 60 * 60 * 24 * 365, // one year

// Names for the cookies that are being set by this module.
cookieNameIsConsentGiven: 'ncc_c',
cookieNameCookiesEnabledIds: 'ncc_e',

// Options to pass to nuxt's useCookie
cookieOptions: {
  path: '/',
}

// Switch to toggle the "accept necessary" button.
isAcceptNecessaryButtonEnabled: true

// Switch to toggle the button that opens the configuration modal.
isControlButtonEnabled: true,

// Switch to toggle the concatenation of target cookie ids to the cookie description.
isCookieIdVisible: false,

// Switch to toggle the inclusion of this module's css.
// If css is set to false, you will still be able to access your color variables.
isCssEnabled: true,

// Switch to toggle the css variables ponyfill.
isCssPonyfillEnabled: false,

// Switch to toggle the separation of cookie name and description in the configuration modal by a dash.
isDashInDescriptionEnabled: true,

// Switch to toggle the blocking of iframes.
// This can be used to prevent iframes from adding additional cookies.
isIframeBlocked: false,

// Switch to toggle the modal being shown right away, requiring a user's decision.
isModalForced: false,

// The locales to include.
locales: ['en'],

// Translations to override.
localeTexts: {
  en: {
    save: 'Remember',
  }
}

Cookies

Every property the includes a { en: ... } value is a translatable property that could instead only specify a string ('...') or other locales as well ({ de: ..., uk: ... }).

{
  description: {
    en:  'Used for cookie control.'
  },
  id: 'ga', // if unset, `getCookieId(cookie)` returns the cookie's slugified name instead, which e.g. is used to fill the state's `enabledCookieIds` list
  // use a short cookie id to save bandwidth!
  name: {
    en: 'Google Analytics' // you always have to specify a cookie name (in English)
  },
  src: 'https://www.googletagmanager.com/gtag/js?id=<API-KEY>',
  targetCookieIds: ['cookie_control_consent', 'cookie_control_enabled_cookies']
}

Component Slots

Bar

<CookieControl>
  <template #bar>
    <h3>Bar title</h3>
    <p>Bar description (you can use $cookies.text.barDescription)</p>
    <n-link>Go somewhere</n-link>
  </template>
</CookieControl>

Modal

<template #modal>
  <h3>Modal title</h3>
  <p>Modal description</p>
</template>

Cookie

<template #cookie="{config}">
  <span v-for="c in config" :key="c.id" v-text="c.cookies"/>
</template>

Props

  • locale: ['en']
<CookieControl locale="de"/>

More Repositories

1

bottom-news

Bot Tom sends the daily video "Tagesschau in 100 Sekunden auf Arabisch" (German news in 100 seconds in Arabic) to a WhatsApp group with refugees.
Python
8
star
2

spotitag

Tag mp3 files with data from Spotify.
Kotlin
4
star
3

chip-download-change

Swaps the irritating link to the Chip Installer with the normal link for "manual" download.
3
star
4

batch-rename

Rename and sort large amounts of photos and other files at once using a specified pattern.
Visual Basic .NET
2
star
5

jonas-thelemann

The source code of my website.
Vue
2
star
6

dargstack_template

A template for Docker stack project layouts.
JavaScript
2
star
7

dargstack

The DargStack script along with its and the template's documentation.
Shell
2
star
8

github-actions

JavaScript
1
star
9

cuda-basics_slides

Slides for my presentation about CUDA Basics.
HTML
1
star
10

asyl-db

A database for improved individual skill support of refugees in Landkreis Kassel.
Visual Basic
1
star
11

good-zuesch

Writes "züsch" for a player in chat who goes to bed on a Minecraft server.
Java
1
star
12

PowerShell-Lib

A library of helpful PowerShell functions.
PowerShell
1
star
13

troll-em

Adds a trollface or trolltext to some (more or less) large websites.
1
star
14

your-own-webplayer

Your own webplayer with Nuxt.js & Amazon S3.
Vue
1
star
15

creal_strapi

Headless API CMS for cReal's website.
JavaScript
1
star
16

jonas-thelemann_stack

dargstack configuration for my web services.
Shell
1
star
17

randomwinpicker

A website that chooses a "true" random winner for CS:GO case openings.
PHP
1
star
18

rbsg

Team B's game of summer semester 2019's module "software engineering" (SE) at UniKasselVersität.
Java
1
star
19

dadamus

A MusicXML to Dada speech converter.
Java
1
star
20

spotilist

Validate m3u playlists against Spotify.
Kotlin
1
star
21

dadasong

Generator for dadaistic song texts.
Java
1
star
22

maze-generator_game

A maze game in which you capture sheep.
Java
1
star
23

one-player-shutdown

Automatically shuts a Minecraft server down when only one player is connected.
Java
1
star
24

parallel-processing

My take on the parallel-processing project tasks for Kassel university.
Java
1
star
25

cover-extract

A simple tool that extracts one frame from .mp4 videos in a folder and a specified point in time.
Visual Basic
1
star
26

creal

DJ cReal's website.
TypeScript
1
star