npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zengenti-react-cookie-control

v1.0.9

Published

A react cookie control

Downloads

87

Readme

Zengenti React Cookie Control

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:

| Property | Type | Description | | ------------------------ | ---------------------------------------------------------------- | -------------------------------------------------------------------------------------- | | cookieControl | CookieControlProps | Properties 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 | | updatePreferences | UpdatePreferencesProps | Properties to use in Update Preferences component | | theme | Theme | CSS properties to be applied to the modules components |

CookieControlProps

| Property | Type | Description | | -------- | ------ | -------------------------------------------------------- | | content | string | The content to display in the Cookie Control component |

UpdatePreferencesProps

| Property | Type | Description | | ---------- | ------ | ----------------------------------------------------------------------- | | content | string | The content to display at the top of the Update Preferences component | | analytics | string | Description to display above the toggle for analytics cookies | | functional | string | Description to display above the toggle for functional cookies | | marketing | string | Description to display above the toggle for marketing cookies | | necessary | string | Description 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;
    heading?: {
        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;
        };
    };
};

Cookie Control Component

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 doToggleUpdatePreferences 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

| Property | Type | Description | | --------------------------- | ---------------------------------------------------------------- | -------------------------------------------------------------------------------- | | acceptAll | () => void | Accept all cookie permissions | | analytics | boolean | Analytics cookies accepted/declined | | declineAll | () => void | Decline all cookie permissions | | defaultCookiePreferences | { analytics: boolean; functional: boolean; marketing: boolean; } | Default preferences to use as toggle values if no user preferences have been set | | functional | boolean | Functional cookies accepted/declined | | marketing | boolean | Marketing cookies accepted/declined | | setAnalytics | react.Dispatch<react.SetStateAction> | Update analytics cookie preference | | setFunctional | react.Dispatch<react.SetStateAction> | Update functional cookie preference | | setMarketing | react.Dispatch<react.SetStateAction> | Update marketing cookie preference | | showUpdatePreferences | boolean | Should the update preferences component be displayed | | showCookieControl | boolean | Should the cookie control be displayed | | doToggleUpdatePreferences | () => void | Toggle visibility of update preferences component | | updatePreferences | () => void | Save 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;