• Stars
    star
    402
  • Rank 107,380 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Smol (~1kB gzipped) hook for managing GDPR cookie consent state.

useCookieConsent hook for pure JavaScript projects

Build NPM Version NPM Downloads Codecov gzipped size License

"Buy Me A Coffee"

Headless state management for GDPR cookie consent

  • Headless - bring your own styles, we will provide the logic.
  • Hook-based - extremely intuitive for React developers, but can be used in any JavaScript application.
  • Small - Less than 2kB gzipped.

carbon

Library-specific packages

This repo was made to be framework-agnostic, so you can use it in any JavaScript project. If you use a UI library that we support, you should use the package for your library for best experience

Description

This package is following this GDPR cookie guide which describes what you need for GDPR compliance. This hook mainly focuses handling the consent state of the different types of cookies as described in "Types of Cookies" in this page. Summarizing the mentioned document, there are three different ways to classify cookies:

  • Cookie Duration
    • Session cookies
    • Persistent cookies
  • Cookie Provenance
    • First-party cookies
    • Third-party cookies
  • Cookie Purpose
    • Strictly necessary cookies
    • Preferences cookies
    • Statistics cookies
    • Marketing cookies

The hook in this repository will provide a way to manage these types of cookies.

Installation

npm i @use-cookie-consent/core

yarn add @use-cookie-consent/core

pnpm add @use-cookie-consent/core

⚠️ Note: to have React v18 support, temporarily install @use-cookie-consent/[email protected]. All further development will support React v18, so this note will disappear in the future.

Usage in React

import { useCookieConsent } from '@use-cookie-consent/core';

export const YourComponent = () => {
  const { consent, acceptAllCookies, declineAllCookies, acceptCookies } =
    useCookieConsent();

  return (
    <div>
      <h3>
        {`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`}
      </h3>
      <h3>
        {`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`}
      </h3>

      <button onClick={acceptAllCookies}>Accept all</button>
      <button onClick={() => acceptCookies({ necessary: true, thirdParty: true })}>
        Accept third-party
      </button>
      <button onClick={() => acceptCookies({ necessary: true, firstParty: true })}>
        Accept first-party
      </button>
      <button onClick={declineAllCookies}>Reject all</button>
    </div>
  );
};

With custom cookie attributes

import { useCookieConsent } from '@use-cookie-consent/core';

export const YourComponent = () => {
  const { consent, acceptAllCookies, declineAllCookies, acceptCookies } = useCookieConsent({ 
      consentCookieAttributes: { expires: 180  } // 180 days
    }); 

  return (
    // ...
  );
};

Cookie attributes for the underlying js-cookie package, more info here.

API

useCookieConsent(options)

useCookieConsent is the main hook in this library. You call it whenever you need to accept, decline, set or get cookies - so anything to do with cookies.

useCookieConsent({
  defaultConsent?: CookieConsent,
  consentCookieAttributes?: CookieAttributes;
})

This hook function returns following object:

{
  consent: {
    session?: boolean;
    persistent?: boolean;
    necessary?: boolean;
    preferences?: boolean;
    statistics?: boolean;
    marketing?: boolean;
    firstParty?: boolean;
    thirdParty?: boolean;
  };
  acceptCookies: (cookies: CookieTypes) => void;
  declineAllCookies: () => void;
  acceptAllCookies: () => void;
  didAcceptAll: () => boolean;
  didDeclineAll: (opts?: CookieDeclineOptions) => boolean;
  cookies: CookieWrapper;
}

Roadmap to v1

  • Monorepo
  • Add package bundler (rollup was added)
  • Add support for Storage API
  • Add support for custom cookie categories
  • Create documentation website here
  • Create supporting library packages
    • React here
    • Vue (planned)
    • Svelte (planned)
    • Solid (planned)
    • Preact (planned)
    • Alpine (planned)
    • Lit (planned)
  • Change CookiesWrapper API to something that doesn't require a specific dependency (maybe just Storage API step?)

Contributing

If you want to contribute to this project, read our contributing guidelines first.

Acknowledgements

Following package was used as a starter for this project:

Discussions and Questions

For non-issues please consider joining our Discord here!

Contributors

License

MIT

More Repositories

1

SVG-to-SwiftUI

Tool to convert SVG to SwiftUI's Shape structure.
TypeScript
937
star
2

squircle-js

A squircle element for your favourite frontend framework!
TypeScript
96
star
3

easy-npm-package-react

React and Typescript NPM package setup, just add your code and you should be able to publish it to NPM
JavaScript
12
star
4

next-graphql-let-plugin

Frictionless automatic GraphQL Typescript generation setup for Next.js
JavaScript
8
star
5

golang-rpc-example

Example setup of golang's built-in RPC
Go
5
star
6

excuse-generator

Find an excuse when you run out of them.
TypeScript
4
star
7

liverpool-dissertation

Simulink in the browser (almost)
JavaScript
4
star
8

ssh.antoni.ai

My resumé which is accessible through ssh
Go
3
star
9

macOS-Airport-Tool

This is an attempt to replicate 'Airport' tool found in the depths of macOS. It also allows connecting to a specific bssid.
Swift
3
star
10

st-andrews-dissertation

Code for my MSc Artificial Intelligence dissertation at the University of St Andrews
Jupyter Notebook
3
star
11

sp-hooks

Use search params as a state in your web application.
TypeScript
3
star
12

bring-shrubbery

2
star
13

styled-latex

Styled Latex Components
TypeScript
2
star
14

polyrhythm

Polyrhythm generation algorithm written in every language I need.
Swift
2
star
15

blog.antoni.ai

JavaScript
2
star
16

transfer-go-coding-task

Coding Task For TransferGo (ghosted by them)
TypeScript
2
star
17

antoni.ai

My portfolio built with Astro, Tailwind and Solid.js
TypeScript
2
star
18

nextjs-typescript-graphql-template

TypeScript and GraphQL Example with better folder structure
TypeScript
2
star
19

use-step-machine

State machine hook for all things that can make steps.
TypeScript
2
star
20

weblightProject

y2 project
JavaScript
1
star
21

test-table-grouping

Created with CodeSandbox
TypeScript
1
star
22

next-radix-stitches-typescript-eslint-template

Next.js + RadixUI + Stitches.js + Typescript + ESLint + Prettier
TypeScript
1
star
23

advent-of-code-2021

My bad attempt at solving stuff at Advent of Code 2021
JavaScript
1
star
24

advent-of-code-2022

My bad attempt at Advent of Code 2022
TypeScript
1
star
25

ts-jest-gts-template

Template for Node.js projects using GTS with Jest and TypeScript
TypeScript
1
star
26

bring-shrubbery.github.io

personal portfolio website
HTML
1
star
27

my-tech

1
star
28

stm32-rs-workspace-example

bug reproduction
Rust
1
star
29

msagl-js

JavaScript graph layout library
TypeScript
1
star
30

use-crud

Definition-first API hook.
TypeScript
1
star
31

tensorflow.js-template

Run tensorflow.js quickly from your browser on in node.
HTML
1
star