Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/use-cookie-consent/use-cookie-consent
Smol (~1kB gzipped) hook for managing GDPR cookie consent state.
https://github.com/use-cookie-consent/use-cookie-consent
cookies gdpr hooks react
Last synced: 3 months ago
JSON representation
Smol (~1kB gzipped) hook for managing GDPR cookie consent state.
- Host: GitHub
- URL: https://github.com/use-cookie-consent/use-cookie-consent
- Owner: bring-shrubbery
- License: mit
- Created: 2021-05-23T19:11:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-18T20:23:51.000Z (over 1 year ago)
- Last Synced: 2024-05-02T05:06:45.379Z (7 months ago)
- Topics: cookies, gdpr, hooks, react
- Language: TypeScript
- Homepage: https://use-cookie-consent.js.org
- Size: 1.56 MB
- Stars: 401
- Watchers: 2
- Forks: 15
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# useCookieConsent hook for pure JavaScript projects
[![Build](https://img.shields.io/github/checks-status/use-cookie-consent/use-cookie-consent-core/main?style=flat&colorA=5C5C5C&colorB=000000)](https://github.com/use-cookie-consent/use-cookie-consent-core/actions)
[![NPM Version](https://img.shields.io/npm/v/@use-cookie-consent/core?style=flat&colorA=5C5C5C&colorB=000000)](https://www.npmjs.com/package/@use-cookie-consent/core)
[![NPM Downloads](https://img.shields.io/npm/dm/@use-cookie-consent/core?style=flat&colorA=5C5C5C&colorB=000000)](https://www.npmjs.com/package/@use-cookie-consent/core)
[![Codecov](https://img.shields.io/codecov/c/github/use-cookie-consent/use-cookie-consent-core?style=flat&colorA=5C5C5C&colorB=000000)](https://github.com/use-cookie-consent/use-cookie-consent-core/actions/workflows/codecov.yml)
[![gzipped size](https://img.badgesize.io/https:/unpkg.com/@use-cookie-consent/core@latest/build/esm/index.js?label=gzipped&compression=gzip&style=flat&color=000000)](https://unpkg.com/@use-cookie-consent/core@latest/build/esm/index.js)
[![License](https://img.shields.io/npm/l/@use-cookie-consent/core?style=flat&colorA=5C5C5C&color=000000)](https://github.com/use-cookie-consent/use-cookie-consent-core/blob/main/LICENSE)[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/bring.shrubbery)
> 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.
## 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
- [`@use-cookie-consent/react`](https://github.com/use-cookie-consent/use-cookie-consent-react)
## Description
This package is following [this](https://gdpr.eu/cookies) 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](https://gdpr.eu/cookies) 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 cookiesThe hook in this repository will provide a way to manage these types of cookies.
## Installation
```bash
npm i @use-cookie-consent/coreyarn 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
```tsx
import { useCookieConsent } from '@use-cookie-consent/core';export const YourComponent = () => {
const { consent, acceptAllCookies, declineAllCookies, acceptCookies } =
useCookieConsent();return (
{`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`}
{`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`}
Accept all
acceptCookies({ necessary: true, thirdParty: true })}>
Accept third-party
acceptCookies({ necessary: true, firstParty: true })}>
Accept first-party
Reject all
);
};
```### With custom cookie attributes
```tsx
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](https://github.com/js-cookie/js-cookie).
## 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.
```ts
useCookieConsent({
defaultConsent?: CookieConsent,
consentCookieAttributes?: CookieAttributes;
})
```This hook function returns following object:
```ts
{
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
- [x] Add package bundler ([rollup](https://rollupjs.org/) was added)
- [ ] Add support for Storage API
- [ ] Add support for custom cookie categories
- [x] Create documentation website [here](https://use-cookie-consent.js.org/)
- [ ] Create supporting library packages
- [x] React [here](https://github.com/use-cookie-consent/use-cookie-consent-react)
- [ ] 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](https://github.com/use-cookie-consent/use-cookie-consent/blob/main/CONTRIBUTING.md) first.
## Acknowledgements
Following package was used as a starter for this project:
- [easy-npm-package-react](https://github.com/bring-shrubbery/easy-npm-package-react)
## Discussions and Questions
For non-issues please consider joining our Discord [here](https://discord.gg/pa8epvzJbb)!
## Contributors
- [Antoni Silvestrovic (author)](https://github.com/bring-shrubbery)
## License
[MIT](https://github.com/bring-shrubbery/use-cookie-consent/blob/master/LICENSE)