bring-shrubbery/use-cookie-consent
Fork: 15 Star: 403 (更新于 2024-10-30 20:21:16)
license: MIT
Language: TypeScript .
Smol (~1kB gzipped) hook for managing GDPR cookie consent state.
最后发布版本: v0.4.0-dev.3 ( 2022-05-30 03:49:16)
useCookieConsent hook for pure JavaScript projects
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
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/core@0.4.0-dev.4
. 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
最近版本更新:(数据更新于 2024-09-03 11:46:07)
2022-05-30 03:49:16 v0.4.0-dev.3
2022-05-29 19:41:37 v0.4.0-dev.2
2022-05-29 19:36:18 v0.4.0-dev.1
2022-05-29 17:07:51 v0.4.0-dev.0
2022-04-01 17:28:36 v0.3.6
2022-03-08 14:45:24 v0.3.5
2022-03-04 20:23:53 v0.3.4
2022-01-17 23:55:41 v0.3.3
2021-12-11 17:59:02 v0.3.2
2021-12-11 17:50:52 v0.3.1
主题(topics):
cookies, gdpr, hooks, react
bring-shrubbery/use-cookie-consent同语言 TypeScript最近更新仓库
2024-11-05 17:55:23 langgenius/dify
2024-11-05 11:52:31 RSSNext/Follow
2024-11-05 07:18:03 microsoft/genaiscript
2024-11-05 05:06:06 getmaxun/maxun
2024-11-04 14:44:09 x-extends/vxe-table
2024-11-04 13:16:10 lobehub/lobe-chat