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. |
|
||||
| debug | boolean | undefined | Bar will be drawn regardless of cookie for debugging purposes. |
|
||||
| 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 |
|
||||
| buttonClasses | string | "" | CSS classes to apply to the button |
|
||||
| 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)
|
||||
|
||||
|
||||
#### 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!](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,
|
||||
expires = _props2.expires,
|
||||
hideOnAccept = _props2.hideOnAccept,
|
||||
onAccept = _props2.onAccept;
|
||||
onAccept = _props2.onAccept,
|
||||
extraCookieOptions = _props2.extraCookieOptions;
|
||||
|
||||
// fire onAccept
|
||||
|
||||
@ -642,7 +643,8 @@ var CookieConsent = function (_Component) {
|
||||
// remove listener if set
|
||||
window.removeEventListener("scroll", this.handleScroll);
|
||||
|
||||
_jsCookie2.default.set(cookieName, true, { expires: expires });
|
||||
_jsCookie2.default.set(cookieName, true, _extends({ expires: expires }, extraCookieOptions));
|
||||
|
||||
if (hideOnAccept) {
|
||||
this.setState({ visible: false });
|
||||
}
|
||||
@ -743,7 +745,8 @@ CookieConsent.propTypes = {
|
||||
contentClasses: _propTypes2.default.string,
|
||||
buttonClasses: _propTypes2.default.string,
|
||||
acceptOnScroll: _propTypes2.default.bool,
|
||||
acceptOnScrollPercentage: _propTypes2.default.number
|
||||
acceptOnScrollPercentage: _propTypes2.default.number,
|
||||
extraCookieOptions: _propTypes2.default.object
|
||||
};
|
||||
|
||||
CookieConsent.defaultProps = {
|
||||
@ -759,7 +762,8 @@ CookieConsent.defaultProps = {
|
||||
expires: 365,
|
||||
containerClasses: "",
|
||||
contentClasses: "",
|
||||
buttonClasses: ""
|
||||
buttonClasses: "",
|
||||
extraCookieOptions: {}
|
||||
};
|
||||
|
||||
exports.default = CookieConsent;
|
||||
|
11
src/index.js
11
src/index.js
@ -89,14 +89,15 @@ class CookieConsent extends Component {
|
||||
* Set a persistent cookie
|
||||
*/
|
||||
accept() {
|
||||
const { cookieName, expires, hideOnAccept, onAccept } = this.props;
|
||||
const { cookieName, expires, hideOnAccept, onAccept, extraCookieOptions } = this.props;
|
||||
|
||||
// fire onAccept
|
||||
onAccept();
|
||||
// remove listener if set
|
||||
window.removeEventListener("scroll", this.handleScroll);
|
||||
|
||||
Cookies.set(cookieName, true, { expires: expires });
|
||||
Cookies.set(cookieName, true, { expires: expires, ...extraCookieOptions });
|
||||
|
||||
if (hideOnAccept) {
|
||||
this.setState({ visible: false });
|
||||
}
|
||||
@ -189,7 +190,8 @@ CookieConsent.propTypes = {
|
||||
contentClasses: PropTypes.string,
|
||||
buttonClasses: PropTypes.string,
|
||||
acceptOnScroll: PropTypes.bool,
|
||||
acceptOnScrollPercentage: PropTypes.number
|
||||
acceptOnScrollPercentage: PropTypes.number,
|
||||
extraCookieOptions: PropTypes.object
|
||||
};
|
||||
|
||||
CookieConsent.defaultProps = {
|
||||
@ -205,7 +207,8 @@ CookieConsent.defaultProps = {
|
||||
expires: 365,
|
||||
containerClasses: "",
|
||||
contentClasses: "",
|
||||
buttonClasses: ""
|
||||
buttonClasses: "",
|
||||
extraCookieOptions: {}
|
||||
};
|
||||
|
||||
export default CookieConsent;
|
||||
|
Loading…
Reference in New Issue
Block a user