react-cookie-consent/src/index.js

237 lines
5.8 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
};
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",
flex: "0 0 auto",
2018-05-30 14:22:24 +02:00
padding: "5px 10px",
margin: "15px"
},
contentStyle: {
flex: "1 0 300px",
margin: "15px"
}
2018-02-02 19:09:23 +01:00
};
this.handleScroll = this.handleScroll.bind(this);
2018-02-02 19:09:23 +01:00
}
componentDidMount() {
const { cookieName, debug } = this.props;
2018-02-02 19:09:23 +01:00
// if cookie undefined or debug
if (Cookies.get(cookieName) === undefined || debug) {
this.setState({ visible: true });
2018-02-02 19:09:23 +01:00
}
// if acceptOnScroll 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();
}
2018-02-02 19:09:23 +01:00
}
/**
* Set a persistent cookie
*/
accept() {
const { cookieName, cookieValue, expires, hideOnAccept, onAccept, extraCookieOptions } = this.props;
// fire onAccept
onAccept();
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
Cookies.set(cookieName, cookieValue, { expires: expires, ...extraCookieOptions });
2018-06-25 22:35:16 +02:00
if (hideOnAccept) {
this.setState({ visible: false });
}
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,
contentStyle,
2018-02-02 19:09:23 +01:00
disableStyles,
2018-06-07 11:32:43 +02:00
buttonText,
containerClasses,
contentClasses,
buttonClasses,
buttonId,
2019-02-04 20:38:46 +01:00
disableButtonStyles
2018-02-02 19:09:23 +01:00
} = this.props;
let myStyle = {};
let myButtonStyle = {};
let myContentStyle = {};
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);
myContentStyle = Object.assign({}, contentStyle);
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 });
2019-02-04 20:38:46 +01:00
// switch to disable JUST the button styles
if(disableButtonStyles){
myButtonStyle = Object.assign({}, buttonStyle);
}else{
myButtonStyle = Object.assign({}, { ...this.state.buttonStyle, ...buttonStyle });
}
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";
2018-06-07 11:32:43 +02:00
myStyle.position = "fixed";
2018-02-02 19:09:23 +01:00
break;
case OPTIONS.BOTTOM:
myStyle.bottom = "0";
2018-06-07 11:32:43 +02:00
myStyle.position = "fixed";
2018-02-02 19:09:23 +01:00
break;
}
return (
2018-06-07 11:32:43 +02:00
<div className={`cookieConsent ${containerClasses}`} style={myStyle}>
<div style={myContentStyle} className={contentClasses}>
{this.props.children}
</div>
2018-02-02 19:09:23 +01:00
<button
style={myButtonStyle}
2018-06-07 11:32:43 +02:00
className={buttonClasses}
id={buttonId}
2018-02-02 19:09:23 +01:00
onClick={() => {
this.accept();
}}
>
{buttonText}
</button>
</div>
);
}
}
CookieConsent.propTypes = {
2018-07-28 13:24:44 +02:00
location: PropTypes.oneOf(Object.keys(OPTIONS).map(key => OPTIONS[key])),
2018-02-02 19:09:23 +01:00
style: PropTypes.object,
buttonStyle: 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,
2018-02-02 19:09:23 +01:00
onAccept: PropTypes.func,
buttonText: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.element
]),
2018-05-31 20:00:26 +02:00
cookieName: PropTypes.string,
cookieValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.number
]),
debug: PropTypes.bool,
2018-06-07 11:32:43 +02:00
expires: PropTypes.number,
containerClasses: PropTypes.string,
contentClasses: PropTypes.string,
buttonClasses: PropTypes.string,
buttonId: PropTypes.string,
acceptOnScroll: PropTypes.bool,
acceptOnScrollPercentage: PropTypes.number,
2019-02-04 20:38:46 +01:00
extraCookieOptions: PropTypes.object,
disableButtonStyles: PropTypes.bool
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,
acceptOnScroll: false,
acceptOnScrollPercentage: 25,
2018-02-02 19:09:23 +01:00
location: OPTIONS.BOTTOM,
onAccept: () => { },
2018-02-02 19:09:23 +01:00
cookieName: "CookieConsent",
cookieValue: true,
2018-05-31 20:00:26 +02:00
buttonText: "I understand",
debug: false,
2018-06-07 11:32:43 +02:00
expires: 365,
containerClasses: "",
contentClasses: "",
buttonClasses: "",
buttonId: "",
2019-02-04 20:38:46 +01:00
extraCookieOptions: {},
disableButtonStyles: false
2018-02-02 19:09:23 +01:00
};
export default CookieConsent;
export { Cookies };