9 Commits
6.4.0 ... 7.2.1

9 changed files with 920 additions and 749 deletions

View File

@@ -5,6 +5,35 @@ 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/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [[7.2.0]](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/7.2.0)
- Added `onOverlayClick` which allows you to react to a click on the overlay
- Added `acceptOnOverlayClick` which accepts the cookies when the overlay is clicked and runs `onOverlayClick`
## [[7.1.1]](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/7.1.1)
- `customContentAttributes` and `customContainerAttributes` are now optional in the typing file as they should be
## [[7.1.0](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/7.1.0)]
- Added custom attribute props for content and container
## [[7.0.1](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/7.0.1)]
- Configured webpack to remove self from build artefact. Should now work in Nextjs and Gatsby (only tested those..)
## [[7.0.0](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/7.0.0)]
- Switched from CommonJS to UMD module
## [[6.4.1](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/6.4.1))]
- Added missing typing
## [[6.4.0](https://github.com/Mastermindzh/react-cookie-consent/releases/tag/6.4.0))]
- Added visible prop
## [[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.

View File

@@ -138,7 +138,7 @@ That option would be interesting if you want to allow user to change their conse
## Props
| 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. |
| visible | string, "show", "hidden" or "byCookieValue" | "byCookieValue" | Force the consent bar visibility. If "byCookieValue", visibility are defined by cookie consent existence. |
| children | string or React component | | Content to appear inside the bar |
@@ -179,6 +179,10 @@ That option would be interesting if you want to allow user to change their conse
| 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 |
| customContentAttributes | object | `{}` | Allows you to set custom (data) attributes on the content div |
| customContainerAttributes | object | `{}` | Allows you to set custom (data) attributes on the container div |
| onOverlayClick | function | `() => {}` | allows you to react to a click on the overlay |
| acceptOnOverlayClick | boolean | false | Determines whether the cookies should be accepted after clicking on the overlay |
## Debugging it

18
build/index.d.ts vendored
View File

@@ -4,6 +4,7 @@ import Cookies from "js-cookie";
export interface CookieConsentProps {
location?: "top" | "bottom" | "none";
sameSite?: "strict" | "lax" | "none";
visible?: "hidden" | "show" | "byCookieValue";
cookieSecurity?: boolean;
style?: object;
buttonStyle?: object;
@@ -37,10 +38,14 @@ export interface CookieConsentProps {
overlay?: boolean;
overlayClasses?: string;
overlayStyle?: object;
onOverlayClick?: () => void;
acceptOnOverlayClick?: boolean;
ariaAcceptLabel?: string;
ariaDeclineLabel?: string;
acceptOnScroll?: boolean;
acceptOnScrollPercentage?: number;
customContentAttributes?: object;
customContainerAttributes?: object;
}
export default class CookieConsent extends React.Component<CookieConsentProps, {}> {}
@@ -53,4 +58,17 @@ export default class CookieConsent extends React.Component<CookieConsentProps, {
*/
export function getCookieConsentValue(name?: string): string;
/**
* Reset the consent cookie
* Remove the cookie on browser in order to allow user to change their consent
* @param {*} name optional name of the cookie
*/
export function resetCookieConsentValue(name?: string);
/**
* Get the legacy cookie name by the regular cookie name
* @param {string} name of cookie to get
*/
export function getLegacyCookieName(name: string);
export { Cookies };

View File

@@ -1,5 +1,14 @@
/*! For license information please see index.js.LICENSE.txt */
!(function () {
!(function (e, t) {
"object" == typeof exports && "object" == typeof module
? (module.exports = t())
: "function" == typeof define && define.amd
? define([], t)
: "object" == typeof exports
? (exports.ReactCookieConsent = t())
: (e.ReactCookieConsent = t());
})("undefined" != typeof self ? self : this, function () {
return (function () {
var e = {
808: function (e, t, n) {
var o, r, i;
@@ -171,7 +180,8 @@
Object.defineProperty(e, "__esModule", { value: !0 });
});
var o = {};
!(function () {
return (
(function () {
"use strict";
n.r(o),
n.d(o, {
@@ -179,17 +189,23 @@
return l();
},
OPTIONS: function () {
return O;
return h;
},
SAME_SITE_OPTIONS: function () {
return C;
},
VISIBLE_OPTIONS: function () {
return g;
},
default: function () {
return w;
return B;
},
getCookieConsentValue: function () {
return S;
},
resetCookieConsentValue: function () {
return k;
},
});
const e = require("react");
var t = n.n(e),
@@ -241,7 +257,7 @@
var n = null != arguments[t] ? arguments[t] : {};
t % 2
? p(Object(n), !0).forEach(function (t) {
h(e, t, n[t]);
m(e, t, n[t]);
})
: Object.getOwnPropertyDescriptors
? Object.defineProperties(e, Object.getOwnPropertyDescriptors(n))
@@ -251,7 +267,7 @@
}
return e;
}
function d(e, t) {
function b(e, t) {
for (var n = 0; n < t.length; n++) {
var o = t[n];
(o.enumerable = o.enumerable || !1),
@@ -260,14 +276,14 @@
Object.defineProperty(e, o.key, o);
}
}
function b(e, t) {
return (b =
function y(e, t) {
return (y =
Object.setPrototypeOf ||
function (e, t) {
return (e.__proto__ = t), e;
})(e, t);
}
function y(e, t) {
function d(e, t) {
if (t && ("object" === a(t) || "function" == typeof t)) return t;
if (void 0 !== t)
throw new TypeError("Derived constructors may only return object or undefined");
@@ -278,14 +294,14 @@
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
return e;
}
function m(e) {
return (m = Object.setPrototypeOf
function O(e) {
return (O = Object.setPrototypeOf
? Object.getPrototypeOf
: function (e) {
return e.__proto__ || Object.getPrototypeOf(e);
})(e);
}
function h(e, t, n) {
function m(e, t, n) {
return (
t in e
? Object.defineProperty(e, t, {
@@ -298,31 +314,36 @@
e
);
}
var O = { TOP: "top", BOTTOM: "bottom", NONE: "none" },
g = { STRICT: "strict", LAX: "lax", NONE: "none" },
var h = { TOP: "top", BOTTOM: "bottom", NONE: "none" },
C = { STRICT: "strict", LAX: "lax", NONE: "none" },
g = { HIDDEN: "hidden", SHOW: "show", BY_COOKIE_VALUE: "byCookieValue" },
S = function () {
var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : k,
var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : j,
t = l().get(e);
return void 0 === t && (t = l().get(C(e))), t;
return void 0 === t && (t = l().get(x(e))), t;
},
C = function (e) {
k = function () {
var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : j;
l().remove(e);
},
x = function (e) {
return "".concat(e, "-legacy");
},
k = "CookieConsent",
x = function (e) {
j = "CookieConsent",
w = function (e) {
var t = e.condition,
n = e.wrapper,
o = e.children;
return t ? n(o) : o;
},
j = (function (e) {
T = (function (e) {
!(function (e, t) {
if ("function" != typeof t && null !== t)
throw new TypeError("Super expression must either be null or a function");
(e.prototype = Object.create(t && t.prototype, {
constructor: { value: e, writable: !0, configurable: !0 },
})),
t && b(e, t);
t && y(e, t);
})(a, e);
var n,
o,
@@ -336,7 +357,10 @@
if ("function" == typeof Proxy) return !0;
try {
return (
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})), !0
Boolean.prototype.valueOf.call(
Reflect.construct(Boolean, [], function () {})
),
!0
);
} catch (e) {
return !1;
@@ -344,12 +368,12 @@
})()),
function () {
var e,
t = m(r);
t = O(r);
if (i) {
var n = m(this).constructor;
var n = O(this).constructor;
e = Reflect.construct(t, arguments, n);
} else e = t.apply(this, arguments);
return y(this, e);
return d(this, e);
});
function a(e) {
var t;
@@ -357,7 +381,7 @@
(function (e, t) {
if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function");
})(this, a),
h(v((t = c.call(this, e))), "handleScroll", function () {
m(v((t = c.call(this, e))), "handleScroll", function () {
var e = t.props.acceptOnScrollPercentage,
n = document.documentElement,
o = document.body,
@@ -365,7 +389,7 @@
i = "scrollHeight";
((n[r] || o[r]) / ((n[i] || o[i]) - n.clientHeight)) * 100 > e && t.accept(!0);
}),
h(v(t), "removeScrollListener", function () {
m(v(t), "removeScrollListener", function () {
t.props.acceptOnScroll && window.removeEventListener("scroll", t.handleScroll);
}),
(t.state = {
@@ -451,6 +475,15 @@
r && (this.setState({ visible: !1 }), this.removeScrollListener());
},
},
{
key: "overlayClick",
value: function () {
var e = this.props,
t = e.acceptOnOverlayClick,
n = e.onOverlayClick;
t && this.accept(), n();
},
},
{
key: "decline",
value: function () {
@@ -474,7 +507,7 @@
c = this.props.cookieSecurity;
void 0 === c && (c = !location || "https:" === location.protocol);
var a = f(f({ expires: r }, o), {}, { sameSite: i, secure: c });
i === g.NONE && l().set(C(e), t, a), l().set(e, t, a);
i === C.NONE && l().set(x(e), t, a), l().set(e, t, a);
},
},
{
@@ -488,7 +521,12 @@
key: "render",
value: function () {
var e = this;
switch (this.props.visible) {
case g.HIDDEN:
return null;
case g.BY_COOKIE_VALUE:
if (!this.state.visible) return null;
}
var n = this.props,
o = n.location,
r = n.style,
@@ -498,61 +536,63 @@
a = n.disableStyles,
s = n.buttonText,
p = n.declineButtonText,
d = n.containerClasses,
b = n.contentClasses,
y = n.buttonClasses,
b = n.containerClasses,
y = n.contentClasses,
d = n.buttonClasses,
v = n.buttonWrapperClasses,
m = n.declineButtonClasses,
h = n.buttonId,
g = n.declineButtonId,
O = n.declineButtonClasses,
m = n.buttonId,
C = n.declineButtonId,
S = n.disableButtonStyles,
C = n.enableDeclineButton,
k = n.flipButtons,
k = n.enableDeclineButton,
x = n.flipButtons,
j = n.ButtonComponent,
w = n.overlay,
T = n.overlayClasses,
B = n.overlayStyle,
T = n.overlay,
B = n.overlayClasses,
E = n.overlayStyle,
P = n.ariaAcceptLabel,
E = n.ariaDeclineLabel,
D = {},
I = {},
D = n.ariaDeclineLabel,
I = n.customContainerAttributes,
_ = n.customContentAttributes,
A = {},
N = {},
_ = {},
R = {};
R = {},
L = {},
V = {};
switch (
(a
? ((D = u({}, r)),
(I = u({}, i)),
(N = u({}, c)),
(_ = u({}, l)),
(R = u({}, B)))
: ((D = u({}, f(f({}, this.state.style), r))),
(_ = u({}, f(f({}, this.state.contentStyle), l))),
(R = u({}, f(f({}, this.state.overlayStyle), B))),
? ((A = u({}, r)),
(N = u({}, i)),
(R = u({}, c)),
(L = u({}, l)),
(V = u({}, E)))
: ((A = u({}, f(f({}, this.state.style), r))),
(L = u({}, f(f({}, this.state.contentStyle), l))),
(V = u({}, f(f({}, this.state.overlayStyle), E))),
S
? ((I = u({}, i)), (N = u({}, c)))
: ((I = u({}, f(f({}, this.state.buttonStyle), i))),
(N = u({}, f(f({}, this.state.declineButtonStyle), c))))),
? ((N = u({}, i)), (R = u({}, c)))
: ((N = u({}, f(f({}, this.state.buttonStyle), i))),
(R = u({}, f(f({}, this.state.declineButtonStyle), c))))),
o)
) {
case O.TOP:
D.top = "0";
case h.TOP:
A.top = "0";
break;
case O.BOTTOM:
D.bottom = "0";
case h.BOTTOM:
A.bottom = "0";
}
var A = [];
var U = [];
return (
C &&
A.push(
k &&
U.push(
t().createElement(
j,
{
key: "declineButton",
style: N,
className: m,
id: g,
"aria-label": E,
style: R,
className: O,
id: C,
"aria-label": D,
onClick: function () {
e.decline();
},
@@ -560,14 +600,14 @@
p
)
),
A.push(
U.push(
t().createElement(
j,
{
key: "acceptButton",
style: I,
className: y,
id: h,
style: N,
className: d,
id: m,
"aria-label": P,
onClick: function () {
e.accept();
@@ -576,23 +616,37 @@
s
)
),
k && A.reverse(),
x && U.reverse(),
t().createElement(
x,
w,
{
condition: w,
wrapper: function (e) {
return t().createElement("div", { style: R, className: T }, e);
condition: T,
wrapper: function (n) {
return t().createElement(
"div",
{
style: V,
className: B,
onClick: function () {
e.overlayClick();
},
},
n
);
},
},
t().createElement(
"div",
{ className: "".concat(d), style: D },
t().createElement("div", { style: _, className: b }, this.props.children),
u({ className: "".concat(b), style: A }, I),
t().createElement(
"div",
u({ style: L, className: y }, _),
this.props.children
),
t().createElement(
"div",
{ className: "".concat(v) },
A.map(function (e) {
U.map(function (e) {
return e;
})
)
@@ -601,19 +655,24 @@
);
},
},
]) && d(n.prototype, o),
]) && b(n.prototype, o),
a
);
})(e.Component);
(j.propTypes = {
(T.propTypes = {
location: i().oneOf(
Object.keys(O).map(function (e) {
return O[e];
Object.keys(h).map(function (e) {
return h[e];
})
),
visible: i().oneOf(
Object.keys(g).map(function (e) {
return g[e];
})
),
sameSite: i().oneOf(
Object.keys(g).map(function (e) {
return g[e];
Object.keys(C).map(function (e) {
return C[e];
})
),
style: i().object,
@@ -650,19 +709,24 @@
overlay: i().bool,
overlayClasses: i().string,
overlayStyle: i().object,
onOverlayClick: i().func,
acceptOnOverlayClick: i().bool,
ariaAcceptLabel: i().string,
ariaDeclineLabel: i().string,
acceptOnScroll: i().bool,
acceptOnScrollPercentage: i().number,
customContentAttributes: i().object,
customContainerAttributes: i().object,
}),
(j.defaultProps = {
(T.defaultProps = {
disableStyles: !1,
hideOnAccept: !0,
hideOnDecline: !0,
location: O.BOTTOM,
location: h.BOTTOM,
visible: g.BY_COOKIE_VALUE,
onAccept: function () {},
onDecline: function () {},
cookieName: k,
cookieName: j,
cookieValue: !0,
declineCookieValue: !1,
setDeclineCookie: !0,
@@ -681,7 +745,7 @@
disableButtonStyles: !1,
enableDeclineButton: !1,
flipButtons: !1,
sameSite: g.LAX,
sameSite: C.LAX,
ButtonComponent: function (e) {
var n = e.children,
o = (function (e, t) {
@@ -710,12 +774,18 @@
},
overlay: !1,
overlayClasses: "",
onOverlayClick: function () {},
acceptOnOverlayClick: !1,
ariaAcceptLabel: "Accept cookies",
ariaDeclineLabel: "Decline cookies",
acceptOnScroll: !1,
acceptOnScrollPercentage: 25,
customContentAttributes: {},
customContainerAttributes: {},
});
const w = j;
const B = T;
})(),
(module.exports = o);
o
);
})();
});

4
package-lock.json generated
View File

@@ -1,11 +1,11 @@
{
"name": "react-cookie-consent",
"version": "6.3.0",
"version": "7.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"version": "6.3.0",
"version": "7.2.0",
"license": "MIT",
"dependencies": {
"js-cookie": "^2.2.1",

View File

@@ -4,7 +4,7 @@
"name": "Rick van Lieshout",
"email": "info@rickvanlieshout.com"
},
"version": "6.3.0",
"version": "7.2.0",
"description": "A small, simple and customizable cookie consent bar for use in React applications.",
"main": "build/index.js",
"types": "build/index.d.ts",

19
src/index.d.ts vendored
View File

@@ -4,6 +4,7 @@ import Cookies from "js-cookie";
export interface CookieConsentProps {
location?: "top" | "bottom" | "none";
sameSite?: "strict" | "lax" | "none";
visible?: "hidden" | "show" | "byCookieValue";
cookieSecurity?: boolean;
style?: object;
buttonStyle?: object;
@@ -12,6 +13,7 @@ export interface CookieConsentProps {
children?: React.ReactNode;
disableStyles?: boolean;
hideOnAccept?: boolean;
hideOnDecline?: boolean;
onAccept?: (acceptedByScrolling?: boolean) => void;
onDecline?: Function;
buttonText?: Function | React.ReactNode;
@@ -37,10 +39,14 @@ export interface CookieConsentProps {
overlay?: boolean;
overlayClasses?: string;
overlayStyle?: object;
onOverlayClick?: () => void;
acceptOnOverlayClick?: boolean;
ariaAcceptLabel?: string;
ariaDeclineLabel?: string;
acceptOnScroll?: boolean;
acceptOnScrollPercentage?: number;
customContentAttributes?: object;
customContainerAttributes?: object;
}
export default class CookieConsent extends React.Component<CookieConsentProps, {}> {}
@@ -53,4 +59,17 @@ export default class CookieConsent extends React.Component<CookieConsentProps, {
*/
export function getCookieConsentValue(name?: string): string;
/**
* Reset the consent cookie
* Remove the cookie on browser in order to allow user to change their consent
* @param {*} name optional name of the cookie
*/
export function resetCookieConsentValue(name?: string);
/**
* Get the legacy cookie name by the regular cookie name
* @param {string} name of cookie to get
*/
export function getLegacyCookieName(name: string);
export { Cookies };

View File

@@ -15,10 +15,10 @@ export const SAME_SITE_OPTIONS = {
};
export const VISIBLE_OPTIONS = {
HIDDEN: 'hidden',
SHOW: 'show',
BY_COOKIE_VALUE: 'byCookieValue'
}
HIDDEN: "hidden",
SHOW: "show",
BY_COOKIE_VALUE: "byCookieValue",
};
/**
* Returns the value of the consent cookie
@@ -159,6 +159,17 @@ class CookieConsent extends Component {
}
}
/**
* Handle a click on the overlay
*/
overlayClick() {
const { acceptOnOverlayClick, onOverlayClick } = this.props;
if (acceptOnOverlayClick) {
this.accept();
}
onOverlayClick();
}
/**
* Set a persistent decline cookie
*/
@@ -279,6 +290,8 @@ class CookieConsent extends Component {
overlayStyle,
ariaAcceptLabel,
ariaDeclineLabel,
customContainerAttributes,
customContentAttributes,
} = this.props;
let myStyle = {};
@@ -367,13 +380,19 @@ class CookieConsent extends Component {
<ConditionalWrapper
condition={overlay}
wrapper={(children) => (
<div style={myOverlayStyle} className={overlayClasses}>
<div
style={myOverlayStyle}
className={overlayClasses}
onClick={() => {
this.overlayClick();
}}
>
{children}
</div>
)}
>
<div className={`${containerClasses}`} style={myStyle}>
<div style={myContentStyle} className={contentClasses}>
<div className={`${containerClasses}`} style={myStyle} {...customContainerAttributes}>
<div style={myContentStyle} className={contentClasses} {...customContentAttributes}>
{this.props.children}
</div>
<div className={`${buttonWrapperClasses}`}>
@@ -425,10 +444,14 @@ CookieConsent.propTypes = {
overlay: PropTypes.bool,
overlayClasses: PropTypes.string,
overlayStyle: PropTypes.object,
onOverlayClick: PropTypes.func,
acceptOnOverlayClick: PropTypes.bool,
ariaAcceptLabel: PropTypes.string,
ariaDeclineLabel: PropTypes.string,
acceptOnScroll: PropTypes.bool,
acceptOnScrollPercentage: PropTypes.number,
customContentAttributes: PropTypes.object,
customContainerAttributes: PropTypes.object,
};
CookieConsent.defaultProps = {
@@ -462,10 +485,14 @@ CookieConsent.defaultProps = {
ButtonComponent: ({ children, ...props }) => <button {...props}>{children}</button>,
overlay: false,
overlayClasses: "",
onOverlayClick: () => {},
acceptOnOverlayClick: false,
ariaAcceptLabel: "Accept cookies",
ariaDeclineLabel: "Decline cookies",
acceptOnScroll: false,
acceptOnScrollPercentage: 25,
customContentAttributes: {},
customContainerAttributes: {},
};
export default CookieConsent;

View File

@@ -6,17 +6,21 @@ module.exports = {
output: {
path: path.resolve(__dirname, "build"),
filename: "index.js",
libraryTarget: "commonjs2", // THIS IS THE MOST IMPORTANT LINE! :mindblow: I wasted more than 2 days until realize this was the line most important in all this guide.
library: {
name: "ReactCookieConsent",
type: "umd",
},
environment: {
arrowFunction: false, // the generated runtime-code should not use arrow functions
},
globalObject: `typeof self !== 'undefined' ? self : this`,
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, "src"),
exclude: /(node_modules|bower_components|build)/,
exclude: /(node_modules|build)/,
use: {
loader: "babel-loader",
options: {