diff --git a/README.md b/README.md index 8dc9c5f..a7a9d62 100644 --- a/README.md +++ b/README.md @@ -123,49 +123,62 @@ import CookieConsent, { Cookies, getCookieConsentValue } from "react-cookie-cons console.log(getCookieConsentValue()); ``` +### reset the cookies value in your own code + +react-cookie-consent exports a function called `resetCookieConsentValue`. You can use it in order to remove cookie in client-site: + +```js +import CookieConsent, { Cookies, resetCookieConsentValue } from "react-cookie-consent"; + +console.log(resetCookieConsentValue()); +``` + +That option would be interesting if you want to allow user to change their consent. If you want to show again the consent bar, you must force "visible" prop to show again the bar. + ## 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. | -| 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) | -| hideOnAccept | boolean | true | If disabled the component will not hide it self after the accept button has been clicked. You will need to hide yourself (see onAccept) | -| buttonText | string or React component | "I understand" | Text to appear on the button | -| declineButtonText | string or React component | "I decline" | Text to appear on the decline button | -| cookieName | string | "CookieConsent" | Name of the cookie used to track whether the user has agreed. | -| cookieValue | string or boolean or number | true | Value to be saved under the cookieName. | -| declineCookieValue | string or boolean or number | false | Value to be saved under the cookieName when declined. | -| setDeclineCookie | boolean | true | Whether to set a cookie when the user clicks "decline" | -| onAccept | function | `() => {}` | Function to be called after the accept button has been clicked. | -| onDecline | function | `() => {}` | Function to be called after the decline 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 | -| overlay | boolean | false | Whether to show a page obscuring overlay or not. | -| containerClasses | string | "" | CSS classes to apply to the surrounding container | -| buttonClasses | string | "" | CSS classes to apply to the button | -| buttonWrapperClasses | string | "" | CSS classes to apply to the div wrapping the buttons | -| declineButtonClasses | string | "" | CSS classes to apply to the decline button | -| buttonId | string | "" | Id to apply to the button | -| declineButtonId | string | "" | Id to apply to the decline button | -| contentClasses | string | "" | CSS classes to apply to the content | -| overlayClasses | string | "" | CSS classes to apply to the surrounding overlay | -| style | object | [look at source][style] | React styling object for the bar. | -| buttonStyle | object | [look at source][buttonstyle] | React styling object for the button. | -| declineButtonStyle | object | [look at source][declinebuttonstyle] | React styling object for the decline button. | -| contentStyle | object | [look at source][contentstyle] | React styling object for the content. | -| overlayStyle | object | [look at source][overlaystyle] | React styling object for the overlay. | -| disableButtonStyles | boolean | false | If enabled the button will have no default style. (you can still supply style through props) | -| enableDeclineButton | boolean | false | If enabled the decline button will be rendered | -| flipButtons | boolean | false | If enabled the accept and decline buttons will be flipped | -| ButtonComponent | React component | button | React Component to render as a button. | +| ------------------------ | :-----------------------------------------: | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | +| 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 | +| disableStyles | boolean | false | If enabled the component will have no default style. (you can still supply style through props) | +| hideOnAccept | boolean | true | If disabled the component will not hide it self after the accept button has been clicked. You will need to hide yourself (see onAccept) | +| buttonText | string or React component | "I understand" | Text to appear on the button | +| declineButtonText | string or React component | "I decline" | Text to appear on the decline button | +| cookieName | string | "CookieConsent" | Name of the cookie used to track whether the user has agreed. | +| cookieValue | string or boolean or number | true | Value to be saved under the cookieName. | +| declineCookieValue | string or boolean or number | false | Value to be saved under the cookieName when declined. | +| setDeclineCookie | boolean | true | Whether to set a cookie when the user clicks "decline" | +| onAccept | function | `() => {}` | Function to be called after the accept button has been clicked. | +| onDecline | function | `() => {}` | Function to be called after the decline 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 | +| overlay | boolean | false | Whether to show a page obscuring overlay or not. | +| containerClasses | string | "" | CSS classes to apply to the surrounding container | +| buttonClasses | string | "" | CSS classes to apply to the button | +| buttonWrapperClasses | string | "" | CSS classes to apply to the div wrapping the buttons | +| declineButtonClasses | string | "" | CSS classes to apply to the decline button | +| buttonId | string | "" | Id to apply to the button | +| declineButtonId | string | "" | Id to apply to the decline button | +| contentClasses | string | "" | CSS classes to apply to the content | +| overlayClasses | string | "" | CSS classes to apply to the surrounding overlay | +| style | object | [look at source][style] | React styling object for the bar. | +| buttonStyle | object | [look at source][buttonstyle] | React styling object for the button. | +| declineButtonStyle | object | [look at source][declinebuttonstyle] | React styling object for the decline button. | +| contentStyle | object | [look at source][contentstyle] | React styling object for the content. | +| overlayStyle | object | [look at source][overlaystyle] | React styling object for the overlay. | +| disableButtonStyles | boolean | false | If enabled the button will have no default style. (you can still supply style through props) | +| enableDeclineButton | boolean | false | If enabled the decline button will be rendered | +| flipButtons | boolean | false | If enabled the accept and decline buttons will be flipped | +| ButtonComponent | React component | button | React Component to render as a button. | | sameSite | string, "strict", "lax" or "none" | none | Cookies sameSite attribute value | -| 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 | -| 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 | +| 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 | +| 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 diff --git a/src/index.js b/src/index.js index b51479a..e13543e 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,12 @@ export const SAME_SITE_OPTIONS = { NONE: "none", }; +export const VISIBLE_OPTIONS = { + HIDDEN: 'hidden', + SHOW: 'show', + BY_COOKIE_VALUE: 'byCookieValue' +} + /** * Returns the value of the consent cookie * Retrieves the regular value first and if not found the legacy one according @@ -30,6 +36,15 @@ export const getCookieConsentValue = (name = defaultCookieConsentName) => { return cookieValue; }; +/** + * 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 const resetCookieConsentValue = (name = defaultCookieConsentName) => { + Cookies.remove(name); +}; + /** * Get the legacy cookie name by the regular cookie name * @param {string} name of cookie to get @@ -227,8 +242,16 @@ class CookieConsent extends Component { render() { // If the bar shouldn't be visible don't render anything. - if (!this.state.visible) { - return null; + switch (this.props.visible) { + case VISIBLE_OPTIONS.HIDDEN: + return null; + case VISIBLE_OPTIONS.BY_COOKIE_VALUE: + if (!this.state.visible) { + return null; + } + break; + default: + break; } const { @@ -366,6 +389,7 @@ class CookieConsent extends Component { CookieConsent.propTypes = { location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])), + visible: PropTypes.oneOf(Object.keys(VISIBLE_OPTIONS).map((key) => VISIBLE_OPTIONS[key])), sameSite: PropTypes.oneOf(Object.keys(SAME_SITE_OPTIONS).map((key) => SAME_SITE_OPTIONS[key])), style: PropTypes.object, buttonStyle: PropTypes.object, @@ -412,6 +436,7 @@ CookieConsent.defaultProps = { hideOnAccept: true, hideOnDecline: true, location: OPTIONS.BOTTOM, + visible: VISIBLE_OPTIONS.BY_COOKIE_VALUE, onAccept: () => {}, onDecline: () => {}, cookieName: defaultCookieConsentName,