import React, { Component } from "react";
import PropTypes from "prop-types";
import Cookies from "js-cookie";
export const OPTIONS = {
TOP: "top",
BOTTOM: "bottom",
NONE: "none",
};
export const SAMESITE_OPTIONS = {
STRICT: "strict",
LAX: "lax",
NONE: "none",
}
class CookieConsent extends Component {
constructor(props) {
super(props);
this.accept.bind(this);
this.state = {
visible: false,
style: {
alignItems: "baseline",
background: "#353535",
color: "white",
display: "flex",
flexWrap: "wrap",
justifyContent: "space-between",
left: "0",
position: "fixed",
width: "100%",
zIndex: "999",
},
buttonStyle: {
background: "#ffd42d",
border: "0",
borderRadius: "0px",
boxShadow: "none",
color: "black",
cursor: "pointer",
flex: "0 0 auto",
padding: "5px 10px",
margin: "15px",
},
declineButtonStyle: {
background: "#c12a2a",
border: "0",
borderRadius: "0px",
boxShadow: "none",
color: "#e5e5e5",
cursor: "pointer",
flex: "0 0 auto",
padding: "5px 10px",
margin: "15px",
},
contentStyle: {
flex: "1 0 300px",
margin: "15px",
},
};
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
const { cookieName, debug } = this.props;
// if cookie undefined or debug
if (Cookies.get(cookieName) === 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 });
}
}
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 });
}
}
/**
* Set a persistent accept cookie
*/
accept({ acceptedByScrolling = false }) {
const {
cookieName,
cookieValue,
expires,
hideOnAccept,
onAccept,
extraCookieOptions,
sameSite,
} = this.props;
// fire onAccept
onAccept({ acceptedByScrolling });
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);
if (sameSite === SAMESITE_OPTIONS.NONE) {
Cookies.set(cookieName, cookieValue, { expires, sameSite, secure: true, ...extraCookieOptions });
// Fallback for older browsers where can not set SameSite=None, SEE: https://web.dev/sameSite-cookie-recipes/#handling-incompatible-clients
Cookies.set(cookieName, cookieValue, { expires, secure: true, ...extraCookieOptions });
} else {
Cookies.set(cookieName, cookieValue, { expires, sameSite, ...extraCookieOptions });
}
if (hideOnAccept) {
this.setState({ visible: false });
}
}
/**
* Set a persistent decline cookie
*/
decline() {
const {
cookieName,
declineCookieValue,
expires,
hideOnDecline,
onDecline,
extraCookieOptions,
setDeclineCookie,
sameSite,
} = this.props;
// fire onDecline
onDecline();
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);
if (setDeclineCookie && sameSite === SAMESITE_OPTIONS.NONE) {
Cookies.set(cookieName, declineCookieValue, { expires, sameSite, secure: true, ...extraCookieOptions });
// Fallback for older browsers where can not set SameSite=None, SEE: https://web.dev/sameSite-cookie-recipes/#handling-incompatible-clients
Cookies.set(cookieName, declineCookieValue, { expires, secure: true, ...extraCookieOptions });
} else if (setDeclineCookie) {
Cookies.set(cookieName, declineCookieValue, { expires, sameSite, ...extraCookieOptions });
}
if (hideOnDecline) {
this.setState({ visible: false });
}
}
render() {
// If the bar shouldn't be visible don't render anything.
if (!this.state.visible) {
return null;
}
const {
location,
style,
buttonStyle,
declineButtonStyle,
contentStyle,
disableStyles,
buttonText,
declineButtonText,
containerClasses,
contentClasses,
buttonClasses,
buttonWrapperClasses,
declineButtonClasses,
buttonId,
declineButtonId,
disableButtonStyles,
enableDeclineButton,
flipButtons,
ButtonComponent,
} = this.props;
let myStyle = {};
let myButtonStyle = {};
let myDeclineButtonStyle = {};
let myContentStyle = {};
if (disableStyles) {
// if styles are disabled use the provided styles (or none)
myStyle = Object.assign({}, style);
myButtonStyle = Object.assign({}, buttonStyle);
myDeclineButtonStyle = Object.assign({}, declineButtonStyle);
myContentStyle = Object.assign({}, contentStyle);
} 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 });
// switch to disable JUST the button styles
if (disableButtonStyles) {
myButtonStyle = Object.assign({}, buttonStyle);
myDeclineButtonStyle = Object.assign({}, declineButtonStyle);
} else {
myButtonStyle = Object.assign({}, { ...this.state.buttonStyle, ...buttonStyle });
myDeclineButtonStyle = Object.assign(
{},
{ ...this.state.declineButtonStyle, ...declineButtonStyle }
);
}
}
// 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;
}
const buttonsToRender = [];
// add decline button
enableDeclineButton &&
buttonsToRender.push(