mirror of
https://github.com/Mastermindzh/react-cookie-consent.git
synced 2025-01-20 18:41:44 +01:00
added extra cookie options as requested in #23
This commit is contained in:
parent
27833943f0
commit
df6bd5464f
26
README.md
26
README.md
@ -88,6 +88,7 @@ One of the props (onAccept) is a function, this function will be called after th
|
|||||||
| onAccept | function | `() => {}` | Function to be called after the accept button has been clicked. |
|
| onAccept | function | `() => {}` | Function to be called after the accept button has been clicked. |
|
||||||
| debug | boolean | undefined | Bar will be drawn regardless of cookie for debugging purposes. |
|
| debug | boolean | undefined | Bar will be drawn regardless of cookie for debugging purposes. |
|
||||||
| expires | number | 365 | Number of days before the cookie expires. |
|
| 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 |
|
| containerClasses| string | "" | CSS classes to apply to the surrounding container |
|
||||||
| buttonClasses | string | "" | CSS classes to apply to the button |
|
| buttonClasses | string | "" | CSS classes to apply to the button |
|
||||||
| contentClasses| string | "" | CSS classes to apply to the content |
|
| contentClasses| string | "" | CSS classes to apply to the content |
|
||||||
@ -162,6 +163,31 @@ Which results in:
|
|||||||
|
|
||||||
![bootstrap styling](https://github.com/Mastermindzh/react-cookie-consent/blob/master/images/css_classes.png?raw=true)
|
![bootstrap styling](https://github.com/Mastermindzh/react-cookie-consent/blob/master/images/css_classes.png?raw=true)
|
||||||
|
|
||||||
|
|
||||||
|
#### Accept on scroll
|
||||||
|
You can make the cookiebar disappear after scrolling a certain percentage using acceptOnScroll and acceptOnScrollPercentage.
|
||||||
|
|
||||||
|
```js
|
||||||
|
<CookieConsent
|
||||||
|
acceptOnScroll={true}
|
||||||
|
acceptOnScrollPercentage={50}
|
||||||
|
onAccept={() => {alert("consent given")}}
|
||||||
|
>
|
||||||
|
Hello scroller :)
|
||||||
|
</CookieConsent>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Extra cookie options
|
||||||
|
You can add more cookie options using the extraCookieOptions parameter like so:
|
||||||
|
|
||||||
|
```js
|
||||||
|
<CookieConsent
|
||||||
|
extraCookieOptions={{domain: 'myexample.com'}}
|
||||||
|
>
|
||||||
|
cookie bar
|
||||||
|
</CookieConsent>
|
||||||
|
```
|
||||||
|
|
||||||
#### rainbows!
|
#### rainbows!
|
||||||
|
|
||||||
![rainbows!](https://github.com/Mastermindzh/react-cookie-consent/blob/master/images/rainbow.png?raw=true)
|
![rainbows!](https://github.com/Mastermindzh/react-cookie-consent/blob/master/images/rainbow.png?raw=true)
|
||||||
|
@ -634,7 +634,8 @@ var CookieConsent = function (_Component) {
|
|||||||
cookieName = _props2.cookieName,
|
cookieName = _props2.cookieName,
|
||||||
expires = _props2.expires,
|
expires = _props2.expires,
|
||||||
hideOnAccept = _props2.hideOnAccept,
|
hideOnAccept = _props2.hideOnAccept,
|
||||||
onAccept = _props2.onAccept;
|
onAccept = _props2.onAccept,
|
||||||
|
extraCookieOptions = _props2.extraCookieOptions;
|
||||||
|
|
||||||
// fire onAccept
|
// fire onAccept
|
||||||
|
|
||||||
@ -642,7 +643,8 @@ var CookieConsent = function (_Component) {
|
|||||||
// remove listener if set
|
// remove listener if set
|
||||||
window.removeEventListener("scroll", this.handleScroll);
|
window.removeEventListener("scroll", this.handleScroll);
|
||||||
|
|
||||||
_jsCookie2.default.set(cookieName, true, { expires: expires });
|
_jsCookie2.default.set(cookieName, true, _extends({ expires: expires }, extraCookieOptions));
|
||||||
|
|
||||||
if (hideOnAccept) {
|
if (hideOnAccept) {
|
||||||
this.setState({ visible: false });
|
this.setState({ visible: false });
|
||||||
}
|
}
|
||||||
@ -743,7 +745,8 @@ CookieConsent.propTypes = {
|
|||||||
contentClasses: _propTypes2.default.string,
|
contentClasses: _propTypes2.default.string,
|
||||||
buttonClasses: _propTypes2.default.string,
|
buttonClasses: _propTypes2.default.string,
|
||||||
acceptOnScroll: _propTypes2.default.bool,
|
acceptOnScroll: _propTypes2.default.bool,
|
||||||
acceptOnScrollPercentage: _propTypes2.default.number
|
acceptOnScrollPercentage: _propTypes2.default.number,
|
||||||
|
extraCookieOptions: _propTypes2.default.object
|
||||||
};
|
};
|
||||||
|
|
||||||
CookieConsent.defaultProps = {
|
CookieConsent.defaultProps = {
|
||||||
@ -759,7 +762,8 @@ CookieConsent.defaultProps = {
|
|||||||
expires: 365,
|
expires: 365,
|
||||||
containerClasses: "",
|
containerClasses: "",
|
||||||
contentClasses: "",
|
contentClasses: "",
|
||||||
buttonClasses: ""
|
buttonClasses: "",
|
||||||
|
extraCookieOptions: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.default = CookieConsent;
|
exports.default = CookieConsent;
|
||||||
|
13
src/index.js
13
src/index.js
@ -89,14 +89,15 @@ class CookieConsent extends Component {
|
|||||||
* Set a persistent cookie
|
* Set a persistent cookie
|
||||||
*/
|
*/
|
||||||
accept() {
|
accept() {
|
||||||
const { cookieName, expires, hideOnAccept, onAccept } = this.props;
|
const { cookieName, expires, hideOnAccept, onAccept, extraCookieOptions } = this.props;
|
||||||
|
|
||||||
// fire onAccept
|
// fire onAccept
|
||||||
onAccept();
|
onAccept();
|
||||||
// remove listener if set
|
// remove listener if set
|
||||||
window.removeEventListener("scroll", this.handleScroll);
|
window.removeEventListener("scroll", this.handleScroll);
|
||||||
|
|
||||||
Cookies.set(cookieName, true, { expires: expires });
|
Cookies.set(cookieName, true, { expires: expires, ...extraCookieOptions });
|
||||||
|
|
||||||
if (hideOnAccept) {
|
if (hideOnAccept) {
|
||||||
this.setState({ visible: false });
|
this.setState({ visible: false });
|
||||||
}
|
}
|
||||||
@ -189,7 +190,8 @@ CookieConsent.propTypes = {
|
|||||||
contentClasses: PropTypes.string,
|
contentClasses: PropTypes.string,
|
||||||
buttonClasses: PropTypes.string,
|
buttonClasses: PropTypes.string,
|
||||||
acceptOnScroll: PropTypes.bool,
|
acceptOnScroll: PropTypes.bool,
|
||||||
acceptOnScrollPercentage: PropTypes.number
|
acceptOnScrollPercentage: PropTypes.number,
|
||||||
|
extraCookieOptions: PropTypes.object
|
||||||
};
|
};
|
||||||
|
|
||||||
CookieConsent.defaultProps = {
|
CookieConsent.defaultProps = {
|
||||||
@ -198,14 +200,15 @@ CookieConsent.defaultProps = {
|
|||||||
acceptOnScroll: false,
|
acceptOnScroll: false,
|
||||||
acceptOnScrollPercentage: 25,
|
acceptOnScrollPercentage: 25,
|
||||||
location: OPTIONS.BOTTOM,
|
location: OPTIONS.BOTTOM,
|
||||||
onAccept: () => {},
|
onAccept: () => { },
|
||||||
cookieName: "CookieConsent",
|
cookieName: "CookieConsent",
|
||||||
buttonText: "I understand",
|
buttonText: "I understand",
|
||||||
debug: false,
|
debug: false,
|
||||||
expires: 365,
|
expires: 365,
|
||||||
containerClasses: "",
|
containerClasses: "",
|
||||||
contentClasses: "",
|
contentClasses: "",
|
||||||
buttonClasses: ""
|
buttonClasses: "",
|
||||||
|
extraCookieOptions: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CookieConsent;
|
export default CookieConsent;
|
||||||
|
Loading…
Reference in New Issue
Block a user