Fix #56 - add acceptedByScrolling to onAccept

Checking the {acceptedByScrolling} boolean property will allow you to
determine whether the acceptance was triggered by the user scrolling
(if acceptedByScrolling=true) or explicitly clicking the Accept button
(if acceptedByScrolling=false).
This commit is contained in:
Matt Knight
2019-12-19 15:39:17 -07:00
parent 9980d42af6
commit 7cc1fc3905
5 changed files with 139 additions and 122 deletions

2
src/index.d.ts vendored
View File

@@ -10,7 +10,7 @@ export interface CookieConsentProps {
children?: React.ReactNode;
disableStyles?: boolean;
hideOnAccept?: boolean;
onAccept?: Function;
onAccept?: ({ acceptedByScrolling }: { acceptedByScrolling?: boolean }) => void;
onDecline?: Function;
buttonText?: Function | React.ReactNode;
declineButtonText?: Function | React.ReactNode;

View File

@@ -93,14 +93,14 @@ class CookieConsent extends Component {
100;
if (percentage > this.props.acceptOnScrollPercentage) {
this.accept();
this.accept({ acceptedByScrolling: true });
}
}
/**
* Set a persistent accept cookie
*/
accept() {
accept({ acceptedByScrolling = false }) {
const {
cookieName,
cookieValue,
@@ -111,7 +111,7 @@ class CookieConsent extends Component {
} = this.props;
// fire onAccept
onAccept();
onAccept({ acceptedByScrolling });
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);
@@ -245,7 +245,7 @@ class CookieConsent extends Component {
className={buttonClasses}
id={buttonId}
onClick={() => {
this.accept();
this.accept({ acceptedByScrolling: false });
}}
>
{buttonText}