react-cookie-consent/src/index.js

161 lines
3.6 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"
};
class CookieConsent extends Component {
constructor(props) {
super(props);
this.accept.bind(this);
this.state = {
visible: true,
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
};
}
componentWillMount() {
const { cookieName, debug } = this.props;
2018-02-02 19:09:23 +01:00
// debug not desired and cookieName not undefined
2018-05-31 20:00:26 +02:00
if (!debug &&
Cookies.get(cookieName) !== undefined
) {
2018-02-02 19:09:23 +01:00
this.setState({ visible: false });
}
}
/**
* Set a persistent cookie
*/
accept() {
const { cookieName } = this.props;
Cookies.set(cookieName, true);
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,
contentStyle,
2018-02-02 19:09:23 +01:00
disableStyles,
onAccept,
buttonText
} = this.props;
let myStyle = {};
let myButtonStyle = {};
let myContentStyle = {};
2018-02-02 19:09:23 +01:00
if (disableStyles) {
// if styles are disabled use the provided styles (or non)
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 });
myButtonStyle = Object.assign(
{},
{ ...this.state.buttonStyle, ...buttonStyle }
);
myContentStyle = Object.assign(
{},
{ ...this.state.contentStyle, ...contentStyle }
);
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;
}
return (
<div className="cookieConsent" style={myStyle}>
<div style={myContentStyle}>{this.props.children}</div>
2018-02-02 19:09:23 +01:00
<button
style={myButtonStyle}
onClick={() => {
this.accept();
onAccept();
}}
>
{buttonText}
</button>
</div>
);
}
}
CookieConsent.propTypes = {
location: PropTypes.oneOf(["top", "bottom"]),
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,
onAccept: PropTypes.func,
buttonText: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.element
]),
2018-05-31 20:00:26 +02:00
cookieName: PropTypes.string,
debug: 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,
location: OPTIONS.BOTTOM,
onAccept: () => {},
cookieName: "CookieConsent",
2018-05-31 20:00:26 +02:00
buttonText: "I understand",
debug: false
2018-02-02 19:09:23 +01:00
};
export default CookieConsent;
export { Cookies };