Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zengenti-react-cookie-control

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zengenti-react-cookie-control

A cookie control for React projects.

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31
increased by121.43%
Maintainers
1
Weekly downloads
 
Created
Source

A cookie control for React projects.

Install

npm install zengenti-react-cookie-control

Using

withCookieProvider

This HOC should be added as high up the tree as possible to expose the context to all the application.

This will automatically add the Cookie Control and Update Preferences components to the DOM.

import { withCookieProvider } from 'zengenti-react-cookie-control';

const settings = {
  ...
};

const AppRoot = () => {
  ...
}

export default withCookieProvider(settings)(AppRoot);

The HOC take a settings object. The available properties are:

PropertyTypeDescription
cookieControlCookieControlPropsProperties to use in Cookie Control component
defaultCookiePreferences{ analytics: boolean; functional: boolean; marketing: boolean; }The default values to render the "toggles" if no preferences have been set by the user
updatePreferencesUpdatePreferencesPropsProperties to use in Update Preferences component
themeThemeCSS properties to be applied to the modules components

CookieControlProps
PropertyTypeDescription
contentstringThe content to display in the Cookie Control component

UpdatePreferencesProps
PropertyTypeDescription
contentstringThe content to display at the top of the Update Preferences component
analyticsstringDescription to display above the toggle for analytics cookies
functionalstringDescription to display above the toggle for functional cookies
marketingstringDescription to display above the toggle for marketing cookies
necessarystringDescription to display for necessary cookies

Theme

The following type definition outlines the available properties that can be passed to the module.

All components rendered by this module have class names applied which can be targeted using css.

The button property also has a customStyles property where you can pass custom css to. Doing this will remove any default button styles applied to this modules components.

theme?: {
    background?: string;
    button?: {
        borderRadius?: string;
        borderWidth?: string;
        borderColor?: string;
        color?: string;
        customStyles?: string | FlattenSimpleInterpolation;
        fontSize?: string;
        lineHeight?: string;
        padding?: string;
        solid?: {
            background?: string;
            color?: string;
            hover?: {
                background?: string;
                color?: string;
            };
        };
    };
    containerWidth?: string;
    divideColor?: string;
    focusOutlineColor?: string;
    h1?: {
        color?: string;
        fontFamily?: string;
        fontSize?: string;
        fontWeight?: string;
        lineHeight?: string;
    };
    h2?: {
        color?: string;
        fontFamily?: string;
        fontSize?: string;
        fontWeight?: string;
        lineHeight?: string;
    };
    iconColor?: string;
    linkColor?: string;
    text?: {
        color?: string;
        fontFamily?: string;
        fontSize?: string;
        fontWeight?: string;
        lineHeight?: string;
    };
    toggle?: {
        off?: {
            background?: string;
            hover?: string;
        };
        on?: {
            background?: string;
            hover?: string;
        };
    };
    zIndex?: number;
};



This component will be rendered if the user has not set any cookie preferences for the site




Update Preferences Component

This component can be toggled to show using the toggleShowUpdatePreferences function from the useCookieControl hook.

Alternatively you can import the ToggleUpdatePreferences component and style accordingly.




ToggleUpdatePreferences

This is a button element that will toggle the visibility of the Update Preferences component.

This button can be added to any component / template and styled accordingly to suit the design needs.




useCookieControl hook

This hook should be added to isolated components to prevent unnecessary re-rendering of elements in the tree.

The following properties are available from the hook

PropertyTypeDescription
acceptAll() => voidAccept all cookie permissions
analyticsbooleanAnalytics cookies accepted/declined
declineAll() => voidDecline all cookie permissions
defaultCookiePreferences{ analytics: boolean; functional: boolean; marketing: boolean; }Default preferences to use as toggle values if no user preferences have been set
functionalbooleanFunctional cookies accepted/declined
marketingbooleanMarketing cookies accepted/declined
setAnalyticsreact.Dispatch<react.SetStateAction>Update analytics cookie preference
setFunctionalreact.Dispatch<react.SetStateAction>Update functional cookie preference
setMarketingreact.Dispatch<react.SetStateAction>Update marketing cookie preference
showUpdatePreferencesbooleanShould the update preferences component be displayed
showCookieControlbooleanShould the cookie control be displayed
toggleShowUpdatePreferences() => voidToggle visibility of update preferences component
updatePreferences() => voidSave cookie preferences

An example component using this hook to inject scripts and "pixels" to the DOM

import React from 'react';
import { useCookieControl } from 'zengenti-react-cookie-control';

// function to inject scripts to the DOM
const loadScript = (props: { id: string; src: string; }) => {
  ...
}

const InjectScripts = () => {
  const { analytics, marketing } = useCookieControl();

  useEffect(() => {
    loadScript({
      id: 'trackingScripts-populo',
      src: '/static/trackingScripts/populo.js',
    });
    ...
  }, [analytics]);

  useEffect(() => {
    loadScript({
      id: 'trackingScripts-linkedIn',
      src: '/static/trackingScripts/linkedIn.js',
    });
    ...
  }, [marketing]);

  return (
    <>
      {marketing && (
        <img
          height="1"
          width="1"
          style={{ display: 'none' }}
          alt=""
          src="https://px.ads.linkedin.com/collect/?pid=123456&fmt=gif"
        />
      )}
    </>
  );
};

export default InjectScripts;

FAQs

Package last updated on 13 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc