react-cookie-consent
Advanced tools
Comparing version 4.1.0 to 5.0.0
@@ -6,2 +6,4 @@ import * as React from "react"; | ||
location?: "top" | "bottom" | "none"; | ||
sameSite?: "strict" | "lax" | "none"; | ||
cookieSecurity?: boolean; | ||
style?: object; | ||
@@ -8,0 +10,0 @@ buttonStyle?: object; |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "A small, simple and customizable cookie consent bar for use in React applications.", | ||
@@ -10,0 +10,0 @@ "main": "build/index.js", |
@@ -92,36 +92,38 @@ # :cookie: react-cookie-consent :cookie: | ||
| 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. | | ||
| 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) | | ||
| 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 | | ||
| 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. | | ||
| 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 | `({ acceptedByScrolling }) => {}` | 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 | | ||
| 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 | | ||
| 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 | | ||
| style | object | [look at source][style] | React styling object for the bar. | | ||
| buttonStyle | object | [look at source][buttonstyle] | React styling object for the button. | | ||
| declineButtonStyle | object | [look at source][declinebuttonstyle] | React styling object for the decline button. | | ||
| contentStyle | object | [look at source][contentstyle] | React styling object for the content. | | ||
| 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. | | ||
| 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. | | ||
| 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) | | ||
| 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 | | ||
| 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. | | ||
| 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 | `({ acceptedByScrolling }) => {}` | 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 | | ||
| 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 | | ||
| 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 | | ||
| style | object | [look at source][style] | React styling object for the bar. | | ||
| buttonStyle | object | [look at source][buttonstyle] | React styling object for the button. | | ||
| declineButtonStyle | object | [look at source][declinebuttonstyle] | React styling object for the decline button. | | ||
| contentStyle | object | [look at source][contentstyle] | React styling object for the content. | | ||
| 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 | location ? location.protocol === "https:" : true | Cookie security level. Defaults to true unless running on http. | | ||
@@ -136,3 +138,3 @@ ## Debugging it | ||
**Note:** Dont forget to remove the `debug`-property for production. | ||
**Note:** Don't forget to remove the `debug`-property for production. | ||
@@ -139,0 +141,0 @@ ## Styling it |
@@ -6,2 +6,4 @@ import * as React from "react"; | ||
location?: "top" | "bottom" | "none"; | ||
sameSite?: "strict" | "lax" | "none"; | ||
cookieSecurity?: boolean; | ||
style?: object; | ||
@@ -8,0 +10,0 @@ buttonStyle?: object; |
@@ -11,2 +11,8 @@ import React, { Component } from "react"; | ||
export const SAME_SITE_OPTIONS = { | ||
STRICT: "strict", | ||
LAX: "lax", | ||
NONE: "none", | ||
}; | ||
class CookieConsent extends Component { | ||
@@ -63,12 +69,12 @@ constructor(props) { | ||
componentDidMount() { | ||
const { cookieName, debug } = this.props; | ||
const { debug } = this.props; | ||
// if cookie undefined or debug | ||
if (Cookies.get(cookieName) === undefined || debug) { | ||
if (this.getCookieValue() === undefined || debug) { | ||
this.setState({ visible: true }); | ||
} | ||
// if acceptOnScroll is set to true and (cookie is undefined or debug is set to true), add a listener. | ||
if (this.props.acceptOnScroll && (Cookies.get(cookieName) === undefined || debug)) { | ||
window.addEventListener("scroll", this.handleScroll, { passive: true }); | ||
// if acceptOnScroll is set to true and (cookie is undefined or debug is set to true), add a listener. | ||
if (this.props.acceptOnScroll) { | ||
window.addEventListener("scroll", this.handleScroll, { passive: true }); | ||
} | ||
} | ||
@@ -106,10 +112,3 @@ } | ||
accept({ acceptedByScrolling = false }) { | ||
const { | ||
cookieName, | ||
cookieValue, | ||
expires, | ||
hideOnAccept, | ||
onAccept, | ||
extraCookieOptions, | ||
} = this.props; | ||
const { cookieName, cookieValue, hideOnAccept, onAccept } = this.props; | ||
@@ -122,3 +121,3 @@ // fire onAccept | ||
Cookies.set(cookieName, cookieValue, { expires: expires, ...extraCookieOptions }); | ||
this.setCookie(cookieName, cookieValue); | ||
@@ -151,3 +150,3 @@ if (hideOnAccept) { | ||
if (setDeclineCookie) { | ||
Cookies.set(cookieName, declineCookieValue, { expires: expires, ...extraCookieOptions }); | ||
this.setCookie(cookieName, declineCookieValue); | ||
} | ||
@@ -160,2 +159,46 @@ | ||
/** | ||
* Get the legacy cookie name by the regular cookie name | ||
* @param {string} name of cookie to get | ||
*/ | ||
getLegacyCookieName(name) { | ||
return `${name}-legacy`; | ||
} | ||
/** | ||
* Function to set the consent cookie based on the provided variables | ||
* Sets two cookies to handle incompatible browsers, more details: | ||
* https://web.dev/samesite-cookie-recipes/#handling-incompatible-clients | ||
*/ | ||
setCookie(cookieName, cookieValue) { | ||
const { extraCookieOptions, expires, sameSite, cookieSecurity } = this.props; | ||
let cookieOptions = { expires, ...extraCookieOptions, sameSite, secure: cookieSecurity }; | ||
// Fallback for older browsers where can not set SameSite=None, SEE: https://web.dev/samesite-cookie-recipes/#handling-incompatible-clients | ||
if (sameSite === SAME_SITE_OPTIONS.NONE) { | ||
Cookies.set(this.getLegacyCookieName(cookieName), cookieValue, cookieOptions); | ||
} | ||
// set the regular cookie | ||
Cookies.set(cookieName, cookieValue, cookieOptions); | ||
} | ||
/** | ||
* Returns the value of the consent cookie | ||
* Retrieves the regular value first and if not found the legacy one according | ||
* to: https://web.dev/samesite-cookie-recipes/#handling-incompatible-clients | ||
*/ | ||
getCookieValue() { | ||
const { cookieName } = this.props; | ||
let cookieValue = Cookies.get(cookieName); | ||
// if the cookieValue is undefined check for the legacy cookie | ||
if (cookieValue === undefined) { | ||
cookieValue = Cookies.get(this.getLegacyCookieName(cookieName)); | ||
} | ||
return cookieValue; | ||
} | ||
render() { | ||
@@ -283,2 +326,3 @@ // If the bar shouldn't be visible don't render anything. | ||
location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])), | ||
sameSite: PropTypes.oneOf(Object.keys(SAME_SITE_OPTIONS).map((key) => SAME_SITE_OPTIONS[key])), | ||
style: PropTypes.object, | ||
@@ -316,2 +360,3 @@ buttonStyle: PropTypes.object, | ||
ButtonComponent: PropTypes.elementType, | ||
cookieSecurity: PropTypes.bool, | ||
}; | ||
@@ -347,2 +392,4 @@ | ||
flipButtons: false, | ||
sameSite: SAME_SITE_OPTIONS.NONE, | ||
cookieSecurity: location ? location.protocol === "https:" : true, | ||
ButtonComponent: ({ children, ...props }) => <button {...props}>{children}</button>, | ||
@@ -349,0 +396,0 @@ }; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170352
2259
264