react-cookie-consent/src/index.js

421 lines
12 KiB
JavaScript
Raw Normal View History

2018-02-02 19:09:23 +01:00
import React, { Component } from "react";
import PropTypes from "prop-types";
import Cookies from "js-cookie";
export const OPTIONS = {
TOP: "top",
BOTTOM: "bottom",
NONE: "none",
2018-02-02 19:09:23 +01:00
};
export const SAME_SITE_OPTIONS = {
2020-05-17 18:09:53 +02:00
STRICT: "strict",
LAX: "lax",
NONE: "none",
};
2020-05-17 18:09:53 +02:00
2018-02-02 19:09:23 +01:00
class CookieConsent extends Component {
constructor(props) {
super(props);
this.accept.bind(this);
this.state = {
visible: false,
2018-02-02 19:09:23 +01:00
style: {
alignItems: "baseline",
2018-02-02 19:09:23 +01:00
background: "#353535",
color: "white",
display: "flex",
2018-05-31 20:00:26 +02:00
flexWrap: "wrap",
justifyContent: "space-between",
2018-02-02 19:09:23 +01:00
left: "0",
position: "fixed",
width: "100%",
zIndex: "999",
2018-02-02 19:09:23 +01:00
},
buttonStyle: {
background: "#ffd42d",
border: "0",
2018-02-02 19:09:23 +01:00
borderRadius: "0px",
boxShadow: "none",
color: "black",
cursor: "pointer",
flex: "0 0 auto",
2018-05-30 14:22:24 +02:00
padding: "5px 10px",
margin: "15px",
},
declineButtonStyle: {
2019-04-13 22:09:16 +02:00
background: "#c12a2a",
border: "0",
borderRadius: "0px",
boxShadow: "none",
2019-04-13 22:09:16 +02:00
color: "#e5e5e5",
cursor: "pointer",
flex: "0 0 auto",
padding: "5px 10px",
margin: "15px",
},
contentStyle: {
flex: "1 0 300px",
margin: "15px",
},
overlayStyle: {
position: "absolute",
left: 0,
top: 0,
width: "100%",
height: "100%",
zIndex: "999",
backgroundColor: "rgba(0,0,0,0.3)",
},
2018-02-02 19:09:23 +01:00
};
this.handleScroll = this.handleScroll.bind(this);
2018-02-02 19:09:23 +01:00
}
componentDidMount() {
const { debug } = this.props;
2018-02-02 19:09:23 +01:00
// if cookie undefined or 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) {
window.addEventListener("scroll", this.handleScroll, { passive: true });
}
}
}
componentWillUnmount() {
// remove listener if still set
window.removeEventListener("scroll", this.handleScroll);
}
/**
* checks whether scroll has exceeded set amount and fire accept if so.
*/
handleScroll() {
// (top / height) - height * 100
let rootNode = document.documentElement,
body = document.body,
top = "scrollTop",
height = "scrollHeight";
let percentage =
((rootNode[top] || body[top]) /
((rootNode[height] || body[height]) - rootNode.clientHeight)) *
100;
if (percentage > this.props.acceptOnScrollPercentage) {
this.accept({ acceptedByScrolling: true });
}
2018-02-02 19:09:23 +01:00
}
/**
* Set a persistent accept cookie
2018-02-02 19:09:23 +01:00
*/
accept({ acceptedByScrolling = false }) {
const { cookieName, cookieValue, hideOnAccept, onAccept } = this.props;
this.setCookie(cookieName, cookieValue);
// fire onAccept
onAccept({ acceptedByScrolling });
2019-02-04 20:38:46 +01:00
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);
2018-02-02 19:09:23 +01:00
2018-06-25 22:35:16 +02:00
if (hideOnAccept) {
this.setState({ visible: false });
}
2018-02-02 19:09:23 +01:00
}
/**
* Set a persistent decline cookie
*/
decline() {
2019-04-13 21:30:58 +02:00
const {
cookieName,
declineCookieValue,
expires,
hideOnDecline,
onDecline,
extraCookieOptions,
setDeclineCookie,
2019-04-13 21:30:58 +02:00
} = this.props;
if (setDeclineCookie) {
this.setCookie(cookieName, declineCookieValue);
}
// fire onDecline
onDecline();
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);
if (hideOnDecline) {
this.setState({ visible: false });
}
}
/**
* 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) {
2020-05-26 17:16:01 +02:00
const { extraCookieOptions, expires, sameSite } = this.props;
let { cookieSecurity } = this.props;
if (cookieSecurity === undefined) {
cookieSecurity = location ? location.protocol === "https:" : true;
}
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;
}
2018-02-02 19:09:23 +01:00
render() {
// If the bar shouldn't be visible don't render anything.
if (!this.state.visible) {
return null;
}
const {
location,
style,
buttonStyle,
declineButtonStyle,
contentStyle,
2018-02-02 19:09:23 +01:00
disableStyles,
2018-06-07 11:32:43 +02:00
buttonText,
declineButtonText,
2018-06-07 11:32:43 +02:00
containerClasses,
contentClasses,
buttonClasses,
buttonWrapperClasses,
declineButtonClasses,
buttonId,
declineButtonId,
disableButtonStyles,
enableDeclineButton,
2019-04-13 22:17:03 +02:00
flipButtons,
ButtonComponent,
overlay,
overlayClasses,
overlayStyle,
2018-02-02 19:09:23 +01:00
} = this.props;
let myStyle = {};
let myButtonStyle = {};
let myDeclineButtonStyle = {};
let myContentStyle = {};
let myOverlayStyle = {};
2018-02-02 19:09:23 +01:00
if (disableStyles) {
2019-02-04 20:38:46 +01:00
// if styles are disabled use the provided styles (or none)
2018-02-02 19:09:23 +01:00
myStyle = Object.assign({}, style);
myButtonStyle = Object.assign({}, buttonStyle);
myDeclineButtonStyle = Object.assign({}, declineButtonStyle);
myContentStyle = Object.assign({}, contentStyle);
myOverlayStyle = Object.assign({}, overlayStyle);
2018-02-02 19:09:23 +01:00
} else {
// if styles aren't disabled merge them with the styles that are provided (or use default styles)
myStyle = Object.assign({}, { ...this.state.style, ...style });
myContentStyle = Object.assign({}, { ...this.state.contentStyle, ...contentStyle });
myOverlayStyle = Object.assign({}, { ...this.state.overlayStyle, ...overlayStyle });
2019-02-04 20:38:46 +01:00
// switch to disable JUST the button styles
2019-04-13 21:30:58 +02:00
if (disableButtonStyles) {
2019-02-04 20:38:46 +01:00
myButtonStyle = Object.assign({}, buttonStyle);
myDeclineButtonStyle = Object.assign({}, declineButtonStyle);
2019-04-13 21:30:58 +02:00
} else {
2019-02-04 20:38:46 +01:00
myButtonStyle = Object.assign({}, { ...this.state.buttonStyle, ...buttonStyle });
2019-04-13 21:30:58 +02:00
myDeclineButtonStyle = Object.assign(
{},
{ ...this.state.declineButtonStyle, ...declineButtonStyle }
);
2019-02-04 20:38:46 +01:00
}
2018-02-02 19:09:23 +01:00
}
// syntactic sugar to enable user to easily select top / bottom
switch (location) {
case OPTIONS.TOP:
myStyle.top = "0";
break;
case OPTIONS.BOTTOM:
myStyle.bottom = "0";
break;
}
2019-04-13 22:17:03 +02:00
const buttonsToRender = [];
// add decline button
enableDeclineButton &&
buttonsToRender.push(
<ButtonComponent
2019-04-13 22:17:03 +02:00
key="declineButton"
style={myDeclineButtonStyle}
className={declineButtonClasses}
id={declineButtonId}
2018-02-02 19:09:23 +01:00
onClick={() => {
2019-04-13 22:17:03 +02:00
this.decline();
2018-02-02 19:09:23 +01:00
}}
>
2019-04-13 22:17:03 +02:00
{declineButtonText}
</ButtonComponent>
2019-04-13 22:17:03 +02:00
);
// add accept button
buttonsToRender.push(
<ButtonComponent
key="acceptButton"
style={myButtonStyle}
className={buttonClasses}
id={buttonId}
onClick={() => {
this.accept({ acceptedByScrolling: false });
2019-04-13 22:17:03 +02:00
}}
>
{buttonText}
</ButtonComponent>
);
if (flipButtons) {
buttonsToRender.reverse();
}
const Wrapper = !overlay
? (props) => <div {...props} />
: (props) => <div {...props} style={myOverlayStyle} className={overlayClasses} />;
2019-04-13 22:17:03 +02:00
return (
<Wrapper>
<div className={`${containerClasses}`} style={myStyle}>
<div style={myContentStyle} className={contentClasses}>
{this.props.children}
</div>
<div className={`${buttonWrapperClasses}`}>
2020-06-17 10:29:15 +02:00
{buttonsToRender.map((button) => {
return button;
})}
</div>
</div>
</Wrapper>
2018-02-02 19:09:23 +01:00
);
}
}
CookieConsent.propTypes = {
location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])),
sameSite: PropTypes.oneOf(Object.keys(SAME_SITE_OPTIONS).map((key) => SAME_SITE_OPTIONS[key])),
2018-02-02 19:09:23 +01:00
style: PropTypes.object,
buttonStyle: PropTypes.object,
declineButtonStyle: PropTypes.object,
contentStyle: PropTypes.object,
2018-02-02 19:09:23 +01:00
children: PropTypes.any, // eslint-disable-line react/forbid-prop-types
disableStyles: PropTypes.bool,
2018-06-25 22:35:16 +02:00
hideOnAccept: PropTypes.bool,
hideOnDecline: PropTypes.bool,
2018-02-02 19:09:23 +01:00
onAccept: PropTypes.func,
onDecline: PropTypes.func,
2019-04-13 21:30:58 +02:00
buttonText: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.element]),
declineButtonText: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.element]),
2018-05-31 20:00:26 +02:00
cookieName: PropTypes.string,
2019-04-13 21:30:58 +02:00
cookieValue: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number]),
declineCookieValue: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number]),
setDeclineCookie: PropTypes.bool,
debug: PropTypes.bool,
2018-06-07 11:32:43 +02:00
expires: PropTypes.number,
containerClasses: PropTypes.string,
contentClasses: PropTypes.string,
buttonClasses: PropTypes.string,
buttonWrapperClasses: PropTypes.string,
declineButtonClasses: PropTypes.string,
buttonId: PropTypes.string,
declineButtonId: PropTypes.string,
acceptOnScroll: PropTypes.bool,
acceptOnScrollPercentage: PropTypes.number,
2019-02-04 20:38:46 +01:00
extraCookieOptions: PropTypes.object,
disableButtonStyles: PropTypes.bool,
enableDeclineButton: PropTypes.bool,
2019-04-13 22:17:03 +02:00
flipButtons: PropTypes.bool,
ButtonComponent: PropTypes.elementType,
cookieSecurity: PropTypes.bool,
overlay: PropTypes.bool,
overlayClasses: PropTypes.string,
overlayStyle: PropTypes.object,
2018-02-02 19:09:23 +01:00
};
2018-05-31 20:00:26 +02:00
2018-02-02 19:09:23 +01:00
CookieConsent.defaultProps = {
disableStyles: false,
2018-06-25 22:35:16 +02:00
hideOnAccept: true,
hideOnDecline: true,
acceptOnScroll: false,
acceptOnScrollPercentage: 25,
2018-02-02 19:09:23 +01:00
location: OPTIONS.BOTTOM,
onAccept: () => {},
onDecline: () => {},
2018-02-02 19:09:23 +01:00
cookieName: "CookieConsent",
cookieValue: true,
declineCookieValue: false,
setDeclineCookie: true,
2018-05-31 20:00:26 +02:00
buttonText: "I understand",
declineButtonText: "I decline",
debug: false,
2018-06-07 11:32:43 +02:00
expires: 365,
containerClasses: "CookieConsent",
contentClasses: "",
buttonClasses: "",
buttonWrapperClasses: "",
declineButtonClasses: "",
buttonId: "rcc-confirm-button",
declineButtonId: "rcc-decline-button",
2019-02-04 20:38:46 +01:00
extraCookieOptions: {},
disableButtonStyles: false,
enableDeclineButton: false,
2019-04-13 22:17:03 +02:00
flipButtons: false,
sameSite: SAME_SITE_OPTIONS.NONE,
ButtonComponent: ({ children, ...props }) => <button {...props}>{children}</button>,
overlay: false,
overlayClasses: "",
2018-02-02 19:09:23 +01:00
};
export default CookieConsent;
2019-04-13 21:30:58 +02:00
export { Cookies };