1.0.2 • Published 4 years ago

react-cookie-preferences v1.0.2

Weekly downloads
6
License
ISC
Repository
github
Last release
4 years ago

React Cookie Preferences

A react package that allows users select their cookie preferences. It opens up an additional dialog window with additional cookie preference options. This component can be used for your website or app as part of GDPR compliance.

  • built with the latest standards - React Hooks, Context API
  • uses js-cookie a lightweight VanillaJS library as a dependency
  • No other plugins or CSS frameworks so its pretty lightweight :)

Preview Cookie Banner Screenshot

Preview Cookie Dialog Screenshot

Installation

npm install --save react-cookie-preferences

Usage

Import the various components at the root level of your app. Remember to wrap all these components within the Context provider in order to maintain the global states of the cookie banner and dialog

import { CookieBanner, CookieDialog } from '../../src';
import { CookieContextProvider } from '../../src';
import { PreferenceCheckbox, MarketingCheckbox, StatisticsCheckbox } from '../../src';

const App = () => {

  return (

    <div className="App">
      <header></header>
      <main></main>
      <footer></footer>
      <CookieContextProvider>
        <CookieBanner>
          <CookieDialog>
            <PreferenceCheckbox />
            <MarketingCheckbox />
            <StatisticsCheckbox />
          </CookieDialog>
        </CookieBanner>
      </CookieContextProvider>
    </div>
  )
};

render(<App />, document.getElementById("root"));

Options for

<CookieBanner>...</CookieBanner>
NameTypeDefaultDescription
classNamestring''Wrapper class for overriding styles
titlestringYour privacyCustom text of the banner title
messagestringThis site uses cookies to offer you a better browsing experience. By accepting, you consent to the use of cookiesCustom text of the banner description
policyTextstringTo find out more, read our updated policy terms in the linkText for the Privacy Policy page
policyLinkTextstringCookie PolicyText for the privacy policy link
policyLinkstringhttps://gdpr.eu/cookies/Link for the Privacy Policy page. Can be external or internal
acceptButtonTextstringI am OK with itButton text
manageConsentTextstringManage PreferencesButton text

Options for

<CookieDialog>...</CookieDialog>
NameTypeDefaultDescription
classNamestring''Wrapper class for overriding styles
titlestringYour privacy optionsCustom text of the dialog title
messagestringPlease review and manage your privacy settings belowsCustom text of the dialog description
confirmTextstringSave PreferencesButton text
cancelTextstringCancelButton text

Options for Checkbox options

All the props are the same for each preference option. You can import the ones that are required as it's not mandatory to have all 3 options.

<PreferenceCheckbox />
NameTypeDefaultDescription
titlestringPreference CookieTitle of the option
messagestringThis cookie option manages and tracks some marketing dataCustom text of the option
namestringpreference_cookieName of the Cookie
expirynumber365Expiry for Cookie
onPreferenceTogglefunction{}Callback on Checkbox toggle
<MarketingCheckbox />
NameTypeDefaultDescription
titlestringMarketing CookieTitle of the option
messagestringThis cookie option manages and tracks some marketing dataCustom text of the option
namestringmarketing_cookieName of the Cookie
expirynumber365Expiry for Cookie
onMarketingTogglefunction{}Callback on Checkbox toggle
<StatisticsCheckbox />
NameTypeDefaultDescription
titlestringStatistics CookieTitle of the option
messagestringThis cookie option manages and tracks some marketing dataCustom text of the option
namestringstatistics_cookieName of the Cookie
expirynumber365Expiry for Cookie
onStatisticsTogglefunction{}Callback on Checkbox toggle

Styles

All styles are done in SCSS at the moment although I might consider moving them to styled-components. CSS classes are provided as hooks for overriding styles as necessary in the above options table

Contributing

I could not find a well-maintaned react component library creator so I've used a pretty basic Webpack based tool. If I come across a package, I'll look at migrating this package. Happy for suggestions and PR requests are welcome. A few initial tasks to consider

  • Add some unit tests with Jest
  • More options for styles, theming and different layouts
  • Styled components

Licence

See the MIT License