Gathering detailed insights and metrics for react-cookie-consent
Gathering detailed insights and metrics for react-cookie-consent
Gathering detailed insights and metrics for react-cookie-consent
Gathering detailed insights and metrics for react-cookie-consent
@fremtind/jkl-cookie-consent-react
Jøkul react cookie consent component
@ctbucha/bs-react-cookie-consent
BuckleScript bindings for react-cookie-consent.
@cookiefirst/cookiefirst-react
Our React helper component for [react cookie consent](https://cookiefirst.com/react-cookie-consent/), built with integration capabilities from CookieFirst, provides a comprehensive solution for managing user consent on your React website. This component h
@binarypaw/react-cookie-consent
A react cookie consent library
A small, simple and customizable cookie consent bar for use in React applications.
npm install react-cookie-consent
Added visible prop
Published on 23 Sept 2021
Fixed IE11 arrow function bug
Published on 19 Feb 2021
Added the `getCookieConsentValue` to the dts file.
Published on 08 Dec 2020
Added the exported function `getCookieConsentValue` to get the cookie value from custom code
Published on 06 Dec 2020
6.1.0
Published on 29 Nov 2020
6.0.0
Published on 02 Nov 2020
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
608 Stars
214 Commits
84 Forks
5 Watching
5 Branches
32 Contributors
Updated on 18 Nov 2024
Minified
Minified + Gzipped
TypeScript (97.17%)
JavaScript (2.45%)
Shell (0.38%)
Cumulative downloads
Total Downloads
Last day
-7.2%
14,531
Compared to previous day
Last week
2.2%
75,202
Compared to previous week
Last month
12.2%
309,532
Compared to previous month
Last year
-3.7%
3,524,860
Compared to previous year
1
1
22
A small, simple and customizable cookie consent bar for use in React applications.
Demo (storybook): https://mastermindzh.github.io/react-cookie-consent/
1npm install react-cookie-consent
or use yarn:
1yarn add react-cookie-consent
You can import the cookie bar like this:
1import CookieConsent from "react-cookie-consent";
If you want to set/remove cookies yourself you can optionally import Cookies
(straight from js-cookie) like this:
1import CookieConsent, { Cookies } from "react-cookie-consent";
Then you can use the component anywhere in your React app like so:
1<CookieConsent>This website uses cookies to enhance the user experience.</CookieConsent>
You can optionally set some props like this (next chapter will show all props):
1<CookieConsent 2 location="bottom" 3 buttonText="Sure man!!" 4 cookieName="myAwesomeCookieName2" 5 style={{ background: "#2B373B" }} 6 buttonStyle={{ color: "#4e503b", fontSize: "13px" }} 7 expires={150} 8> 9 This website uses cookies to enhance the user experience.{" "} 10 <span style={{ fontSize: "10px" }}>This bit of text is smaller :O</span> 11</CookieConsent>
One of the props (onAccept) is a function, this function will be called after the user has clicked the accept button. It is called with an object containing a boolean property acceptedByScrolling
to indicate if the acceptance was triggered by the user scrolling You can provide a function like so:
1<CookieConsent 2 onAccept={(acceptedByScrolling) => { 3 if (acceptedByScrolling) { 4 // triggered if user scrolls past threshold 5 alert("Accept was triggered by user scrolling"); 6 } else { 7 alert("Accept was triggered by clicking the Accept button"); 8 } 9 }} 10></CookieConsent>
If the decline button is enabled then the (onDecline) prop function can be used, this function will be called after the user has clicked the decline button. You can enable the button and provide a function like so:
1<CookieConsent 2 enableDeclineButton 3 onDecline={() => { 4 alert("nay!"); 5 }} 6></CookieConsent>
react-cookie-consent exports a function called getCookieConsentValue(cookieName: string)
. You can use it in your own code like so:
1import CookieConsent, { Cookies, getCookieConsentValue } from "react-cookie-consent"; 2 3console.log(getCookieConsentValue("your_custom_cookie_name"));
react-cookie-consent exports a function called resetCookieConsentValue
. You can use it in order to remove cookie in client-site:
1import CookieConsent, { Cookies, resetCookieConsentValue } from "react-cookie-consent"; 2 3console.log(resetCookieConsentValue());
That option would be interesting if you want to allow user to change their consent. If you want to show again the consent bar, you must force "visible" prop to show again the bar.
Prop | Type | Default value | Description |
---|---|---|---|
location | string, "top", "bottom" or "none" | "bottom" | Syntactic sugar to easily enable you to place the bar at the top or the bottom of the browser window. Use "none" to disable. |
visible | string, "show", "hidden" or "byCookieValue" | "byCookieValue" | Force the consent bar visibility. If "byCookieValue", visibility are defined by cookie consent existence. |
children | string or React component | Content to appear inside the bar | |
disableStyles | boolean | false | If enabled the component will have no default style. (you can still supply style through props) |
hideOnAccept | boolean | true | If disabled the component will not hide it self after the accept button has been clicked. You will need to hide yourself (see onAccept) |
buttonText | string or React component | "I understand" | Text to appear on the button |
declineButtonText | string or React component | "I decline" | Text to appear on the decline button |
cookieName | string | "CookieConsent" | Name of the cookie used to track whether the user has agreed. Note that you also have to pass this to the getCookieConsentValue and resetCookieConsentValue functions as they default to "CookieConsent" as well. |
cookieValue | string or boolean or number | true | Value to be saved under the cookieName. |
declineCookieValue | string or boolean or number | false | Value to be saved under the cookieName when declined. |
setDeclineCookie | boolean | true | Whether to set a cookie when the user clicks "decline" |
onAccept | function | () => {} | Function to be called after the accept button has been clicked. |
onDecline | function | () => {} | Function to be called after the decline button has been clicked. |
debug | boolean | undefined | Bar will be drawn regardless of cookie for debugging purposes. |
expires | number | 365 | Number of days before the cookie expires. |
extraCookieOptions | object | {} | Extra info (apart from expiry date) to add to the cookie |
overlay | boolean | false | Whether to show a page obscuring overlay or not. |
containerClasses | string | "" | CSS classes to apply to the surrounding container |
buttonClasses | string | "" | CSS classes to apply to the button |
buttonWrapperClasses | string | "" | CSS classes to apply to the div wrapping the buttons |
customButtonWrapperAttributes | React.HTMLAttributes<HTMLDivElement> | {} | Allows you to set custom (data) attributes on the button wrapper div |
declineButtonClasses | string | "" | CSS classes to apply to the decline button |
buttonId | string | "" | Id to apply to the button |
declineButtonId | string | "" | Id to apply to the decline button |
contentClasses | string | "" | CSS classes to apply to the content |
overlayClasses | string | "" | CSS classes to apply to the surrounding overlay |
style | object | look at source | React styling object for the bar. |
buttonStyle | object | look at source | React styling object for the button. |
declineButtonStyle | object | look at source | React styling object for the decline button. |
contentStyle | object | look at source | React styling object for the content. |
overlayStyle | object | look at source | React styling object for the overlay. |
disableButtonStyles | boolean | false | If enabled the button will have no default style. (you can still supply style through props) |
enableDeclineButton | boolean | false | If enabled the decline button will be rendered |
flipButtons | boolean | false | If enabled the accept and decline buttons will be flipped |
ButtonComponent | React component | button | React Component to render as a button. |
sameSite | string, "strict", "lax" or "none" | none | Cookies sameSite attribute value |
cookieSecurity | boolean | undefined | Cookie security level. Defaults to true unless running on http. |
ariaAcceptLabel | string | Accept cookies | Aria label to set on the accept button |
ariaDeclineLabel | string | Decline cookies | Aria label to set on the decline button |
acceptOnScroll | boolean | false | Defines whether "accept" should be fired after the user scrolls a certain distance (see acceptOnScrollPercentage) |
acceptOnScrollPercentage | number | 25 | Percentage of the page height the user has to scroll to trigger the accept function if acceptOnScroll is enabled |
customContentAttributes | object | {} | Allows you to set custom (data) attributes on the content div |
customContainerAttributes | object | {} | Allows you to set custom (data) attributes on the container div |
onOverlayClick | function | () => {} | allows you to react to a click on the overlay |
acceptOnOverlayClick | boolean | false | Determines whether the cookies should be accepted after clicking on the overlay |
customButtonProps | object | {} | Allows you to set custom props on the button component |
customDeclineButtonProps | object | {} | Allows you to set custom props on the decline button component |
Because the cookie consent bar will be hidden once accepted, you will have to set the prop debug={true}
to evaluate styling changes:
1<CookieConsent debug={true}></CookieConsent>
Note: Don't forget to remove the debug
-property for production.
The short story is that some browsers don't support the SameSite=None attribute. The modern browsers force you to have SameSite set to something other than none.
So react-cookie-consent fixes this like so:
This happens on lines 29-37
When checking the cookie it'll do it in reverse. If the regular cookie exists, it'll use that. If no regular cookie exists it'll check whether the legacy cookie exists. If both are non-existent no consent was given.
The long story can be found here: pull-request#68
You can provide styling for the bar, the button and the content. Note that the bar has a display: flex
property as default and is parent to its children "content" and "button".
The styling behaves kind of responsive. The minimum content width has been chosen to be "300px" as a default value. If the button does not fit into the same line it is wrapped around into the next line.
You can style each component by using the style
, buttonStyle
and contentStyle
prop. These will append / replace the default styles of the components.
Alternatively you can provide CSS classnames as containerClasses
, buttonClasses
and contentClasses
to apply predefined CSS classes.
You can use disableStyles={true}
to disable any built-in styling.
1<CookieConsent style={{ background: "red" }}></CookieConsent>
1<CookieConsent buttonStyle={{ fontWeight: "bold" }}></CookieConsent>
You can pass predefined CSS classes to the components using the containerClasses
, buttonClasses
and contentClasses
props. The example below uses bootstrap classes:
1<CookieConsent 2 disableStyles={true} 3 location={OPTIONS.BOTTOM} 4 buttonClasses="btn btn-primary" 5 containerClasses="alert alert-warning col-lg-12" 6 contentClasses="text-capitalize" 7> 8 This website uses cookies to enhance the user experience.{" "} 9 <span style={{ fontSize: "10px" }}>This bit of text is smaller :O</span> 10</CookieConsent>
Which results in:
You can make the cookiebar disappear after scrolling a certain percentage using acceptOnScroll and acceptOnScrollPercentage. It is legal in some use-cases, Italy being one of them. Consult your legislation on whether this is allowed.
1<CookieConsent 2 acceptOnScroll={true} 3 acceptOnScrollPercentage={50} 4 onAccept={(byScroll) => { 5 alert(`consent given. \n\n By scrolling? ${byScroll}`); 6 }} 7> 8 Hello scroller :) 9</CookieConsent>
If you enable the decline button you can pass along the "flipButtons" property to turn the buttons around:
1<CookieConsent enableDeclineButton flipButtons> 2 Flipped buttons 3</CookieConsent>
Which results in:
You can add more cookie options using the extraCookieOptions parameter like so:
1<CookieConsent extraCookieOptions={{ domain: "myexample.com" }}>cookie bar</CookieConsent>
If you're crazy enough you can even make a rainbow colored bar:
1<CookieConsent 2 buttonText="OMG DOUBLE RAINBOW" 3 cookieName="myAwesomeCookieName2" 4 style={{ 5 background: "linear-gradient(to right, orange , yellow, green, cyan, blue, violet)", 6 textShadow: "2px 2px black", 7 }} 8 buttonStyle={{ 9 background: "linear-gradient(to left, orange , yellow, green, cyan, blue, violet)", 10 color: "white", 11 fontWeight: "bolder", 12 textShadow: "2px 2px black", 13 }} 14> 15 This website uses cookies to enhance the user experience.{" "} 16 <span style={{ fontSize: "10px" }}>This bit of text is smaller :O</span> 17</CookieConsent>
You can also generate a page-obfuscating overlay that will prevent actions other than interacting with the cookie consent button(s).
1<CookieConsent location="bottom" cookieName="myAwesomeCookieName3" expires={999} overlay> 2 This website uses cookies to enhance the user experience. 3</CookieConsent>
When making a PR please think about the following things:
The list below features the projects which use react-cookie-consent (that I know off):
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More