This package has been deprecated

Author message:

This package was moved to @use-cookie-consent/core. Run 'npm i @use-cookie-consent/core' and update your imports.

use-cookie-consent
TypeScript icon, indicating that this package has built-in type declarations

0.1.19 • Public • Published

use-cookie-consent-core

Build NPM Version NPM Downloads Codecov Lines of code License

⚠️ This repository was transferred: As part of the roadmap for a major release, the repository was transferred to an organisation for easier scoping or library specific packages.

Disclaimer

Although code in this repository is oriented to satisfy GDPR cookie rules, neither author nor contributors to this repository will be responsible for any non-compliance with the law. Please make sure that this repository provides all the functionality to satisfy requirements for your project. If you find something that can be improved please create an issue or send a PR with your fixes!

Description

This React hook is made to make managing cookie consent state easier in a the React hook world. It 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

This package can be easily installed using npm:

npm i use-cookie-consent

Or yarn:

yarn add use-cookie-consent

Usage

import {useCookieConsent} from 'use-cookie-consent';

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({thirdParty: true})}>
        Accept third-party
      </button>
      <button onClick={() => acceptCookies({firstParty: true})}>
        Accept first-party
      </button>
      <button onClick={declineAllCookies}>Reject all</button>
    </div>
  );
};

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

  • [ ] Add package bundler (webpack or rollup)
  • [ ] Add support for Storage API
  • [ ] Add support for custom cookie categories
  • [ ] Create documentation website
  • [ ] Change CookiesWrapper API to something that doesn't require a specific dependency (maybe just Storage API step?)

Acknowledgements

Following package was used as a starter for this project:

Contributors

License

MIT

Package Sidebar

Install

npm i use-cookie-consent

Weekly Downloads

32

Version

0.1.19

License

MIT

Unpacked Size

27.9 kB

Total Files

33

Last publish

Collaborators

  • bring-shrubbery