mirror of
https://github.com/Mastermindzh/react-cookie-consent.git
synced 2025-01-20 18:41:44 +01:00
Added the (optional) scrolling effect back in as it is declared legal in some countries now.
This commit is contained in:
parent
e52811b3a8
commit
696200262e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
node_modules
|
node_modules
|
||||||
build/index.js.LICENSE.txt
|
build/index.js.LICENSE.txt
|
||||||
|
example/*
|
||||||
|
@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [[6.3.0](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/6.2.3))]
|
||||||
|
|
||||||
|
- Added the (optional) scrolling effect back in as it is declared legal in some countries now.
|
||||||
|
|
||||||
## [[6.2.3](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/6.2.3)]
|
## [[6.2.3](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/6.2.3)]
|
||||||
|
|
||||||
- Added support for IE11, the webpack generated runtime-code should not use arrow functions
|
- Added support for IE11, the webpack generated runtime-code should not use arrow functions
|
||||||
|
30
README.md
30
README.md
@ -84,12 +84,17 @@ You can optionally set some props like this (next chapter will show all props):
|
|||||||
</CookieConsent>
|
</CookieConsent>
|
||||||
```
|
```
|
||||||
|
|
||||||
One of the props (onAccept) is a function, this function will be called after the user has clicked the accept button. You can provide a function like so:
|
One of the props (onAccept) is a function, this function will be called after the user has clicked the accept button. It is called with an object containing a boolean property `acceptedByScrolling` to indicate if the acceptance was triggered by the user scrolling You can provide a function like so:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
<CookieConsent
|
<CookieConsent
|
||||||
onAccept={() => {
|
onAccept={(acceptedByScrolling) => {
|
||||||
|
if (acceptedByScrolling) {
|
||||||
|
// triggered if user scrolls past threshold
|
||||||
|
alert("Accept was triggered by user scrolling");
|
||||||
|
} else {
|
||||||
alert("Accept was triggered by clicking the Accept button");
|
alert("Accept was triggered by clicking the Accept button");
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
></CookieConsent>
|
></CookieConsent>
|
||||||
```
|
```
|
||||||
@ -118,7 +123,7 @@ console.log(getCookieConsentValue());
|
|||||||
## Props
|
## Props
|
||||||
|
|
||||||
| Prop | Type | Default value | Description |
|
| Prop | Type | Default value | Description |
|
||||||
| -------------------- | :-------------------------------: | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
|
| ------------------------ | :-------------------------------: | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| location | string, "top", "bottom" or "none" | "bottom" | Syntactic sugar to easily enable you to place the bar at the top or the bottom of the browser window. Use "none" to disable. |
|
| location | string, "top", "bottom" or "none" | "bottom" | Syntactic sugar to easily enable you to place the bar at the top or the bottom of the browser window. Use "none" to disable. |
|
||||||
| children | string or React component | | Content to appear inside the bar |
|
| children | string or React component | | Content to appear inside the bar |
|
||||||
| disableStyles | boolean | false | If enabled the component will have no default style. (you can still supply style through props) |
|
| disableStyles | boolean | false | If enabled the component will have no default style. (you can still supply style through props) |
|
||||||
@ -156,6 +161,8 @@ console.log(getCookieConsentValue());
|
|||||||
| cookieSecurity | boolean | undefined | Cookie security level. Defaults to true unless running on http. |
|
| cookieSecurity | boolean | undefined | Cookie security level. Defaults to true unless running on http. |
|
||||||
| ariaAcceptLabel | string | Accept cookies | Aria label to set on the accept button |
|
| ariaAcceptLabel | string | Accept cookies | Aria label to set on the accept button |
|
||||||
| ariaDeclineLabel | string | Decline cookies | Aria label to set on the decline button |
|
| ariaDeclineLabel | string | Decline cookies | Aria label to set on the decline button |
|
||||||
|
| acceptOnScroll | boolean | false | Defines whether "accept" should be fired after the user scrolls a certain distance (see acceptOnScrollPercentage) |
|
||||||
|
| acceptOnScrollPercentage | number | 25 | Percentage of the page height the user has to scroll to trigger the accept function if acceptOnScroll is enabled |
|
||||||
|
|
||||||
## Debugging it
|
## Debugging it
|
||||||
|
|
||||||
@ -229,6 +236,23 @@ 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.
|
||||||
|
It is legal in some use-cases, [Italy](https://www.garanteprivacy.it/web/guest/home/docweb/-/docweb-display/docweb/9679893) being one of them. Consult your legislation on whether this is allowed.
|
||||||
|
|
||||||
|
```js
|
||||||
|
<CookieConsent
|
||||||
|
acceptOnScroll={true}
|
||||||
|
acceptOnScrollPercentage={50}
|
||||||
|
onAccept={(byScroll) => {
|
||||||
|
alert(`consent given. \n\n By scrolling? ${byScroll}`);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Hello scroller :)
|
||||||
|
</CookieConsent>
|
||||||
|
```
|
||||||
|
|
||||||
#### Flipping the buttons
|
#### Flipping the buttons
|
||||||
|
|
||||||
If you enable the decline button you can pass along the "flipButtons" property to turn the buttons around:
|
If you enable the decline button you can pass along the "flipButtons" property to turn the buttons around:
|
||||||
|
12166
package-lock.json
generated
12166
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
4
src/index.d.ts
vendored
4
src/index.d.ts
vendored
@ -12,7 +12,7 @@ export interface CookieConsentProps {
|
|||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
disableStyles?: boolean;
|
disableStyles?: boolean;
|
||||||
hideOnAccept?: boolean;
|
hideOnAccept?: boolean;
|
||||||
onAccept?: Function;
|
onAccept?: (acceptedByScrolling?: boolean) => void;
|
||||||
onDecline?: Function;
|
onDecline?: Function;
|
||||||
buttonText?: Function | React.ReactNode;
|
buttonText?: Function | React.ReactNode;
|
||||||
declineButtonText?: Function | React.ReactNode;
|
declineButtonText?: Function | React.ReactNode;
|
||||||
@ -39,6 +39,8 @@ export interface CookieConsentProps {
|
|||||||
overlayStyle?: object;
|
overlayStyle?: object;
|
||||||
ariaAcceptLabel?: string;
|
ariaAcceptLabel?: string;
|
||||||
ariaDeclineLabel?: string;
|
ariaDeclineLabel?: string;
|
||||||
|
acceptOnScroll?: boolean;
|
||||||
|
acceptOnScrollPercentage?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class CookieConsent extends React.Component<CookieConsentProps, {}> {}
|
export default class CookieConsent extends React.Component<CookieConsentProps, {}> {}
|
||||||
|
56
src/index.js
56
src/index.js
@ -116,21 +116,31 @@ class CookieConsent extends Component {
|
|||||||
// if cookie undefined or debug
|
// if cookie undefined or debug
|
||||||
if (this.getCookieValue() === undefined || debug) {
|
if (this.getCookieValue() === undefined || debug) {
|
||||||
this.setState({ visible: true });
|
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) {
|
||||||
|
window.addEventListener("scroll", this.handleScroll, { passive: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
// remove listener if still set
|
||||||
|
this.removeScrollListener();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a persistent accept cookie
|
* Set a persistent accept cookie
|
||||||
*/
|
*/
|
||||||
accept() {
|
accept(acceptedByScrolling = false) {
|
||||||
const { cookieName, cookieValue, hideOnAccept, onAccept } = this.props;
|
const { cookieName, cookieValue, hideOnAccept, onAccept } = this.props;
|
||||||
|
|
||||||
this.setCookie(cookieName, cookieValue);
|
this.setCookie(cookieName, cookieValue);
|
||||||
|
|
||||||
onAccept();
|
onAccept(acceptedByScrolling ?? false);
|
||||||
|
|
||||||
if (hideOnAccept) {
|
if (hideOnAccept) {
|
||||||
this.setState({ visible: false });
|
this.setState({ visible: false });
|
||||||
|
this.removeScrollListener();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,13 +148,8 @@ class CookieConsent extends Component {
|
|||||||
* Set a persistent decline cookie
|
* Set a persistent decline cookie
|
||||||
*/
|
*/
|
||||||
decline() {
|
decline() {
|
||||||
const {
|
const { cookieName, declineCookieValue, hideOnDecline, onDecline, setDeclineCookie } =
|
||||||
cookieName,
|
this.props;
|
||||||
declineCookieValue,
|
|
||||||
hideOnDecline,
|
|
||||||
onDecline,
|
|
||||||
setDeclineCookie,
|
|
||||||
} = this.props;
|
|
||||||
|
|
||||||
if (setDeclineCookie) {
|
if (setDeclineCookie) {
|
||||||
this.setCookie(cookieName, declineCookieValue);
|
this.setCookie(cookieName, declineCookieValue);
|
||||||
@ -191,6 +196,35 @@ class CookieConsent extends Component {
|
|||||||
return getCookieConsentValue(cookieName);
|
return getCookieConsentValue(cookieName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks whether scroll has exceeded set amount and fire accept if so.
|
||||||
|
*/
|
||||||
|
handleScroll = () => {
|
||||||
|
const { acceptOnScrollPercentage } = this.props;
|
||||||
|
|
||||||
|
// (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 > acceptOnScrollPercentage) {
|
||||||
|
this.accept(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
removeScrollListener = () => {
|
||||||
|
const { acceptOnScroll } = this.props;
|
||||||
|
if (acceptOnScroll) {
|
||||||
|
window.removeEventListener("scroll", this.handleScroll);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
// If the bar shouldn't be visible don't render anything.
|
// If the bar shouldn't be visible don't render anything.
|
||||||
if (!this.state.visible) {
|
if (!this.state.visible) {
|
||||||
@ -369,6 +403,8 @@ CookieConsent.propTypes = {
|
|||||||
overlayStyle: PropTypes.object,
|
overlayStyle: PropTypes.object,
|
||||||
ariaAcceptLabel: PropTypes.string,
|
ariaAcceptLabel: PropTypes.string,
|
||||||
ariaDeclineLabel: PropTypes.string,
|
ariaDeclineLabel: PropTypes.string,
|
||||||
|
acceptOnScroll: PropTypes.bool,
|
||||||
|
acceptOnScrollPercentage: PropTypes.number,
|
||||||
};
|
};
|
||||||
|
|
||||||
CookieConsent.defaultProps = {
|
CookieConsent.defaultProps = {
|
||||||
@ -403,6 +439,8 @@ CookieConsent.defaultProps = {
|
|||||||
overlayClasses: "",
|
overlayClasses: "",
|
||||||
ariaAcceptLabel: "Accept cookies",
|
ariaAcceptLabel: "Accept cookies",
|
||||||
ariaDeclineLabel: "Decline cookies",
|
ariaDeclineLabel: "Decline cookies",
|
||||||
|
acceptOnScroll: false,
|
||||||
|
acceptOnScrollPercentage: 25,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CookieConsent;
|
export default CookieConsent;
|
||||||
|
Loading…
Reference in New Issue
Block a user