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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cookie-consent-react

v1.2.8

Published

Open-source cookie consent component for React. Compatible w/ Next.js.

Downloads

241

Readme

cookie-consent-react

Customizable, localized cookie consent component for React.

Built for lightweight, flexible cookie consent management in React applications.

Fully typed with TypeScript support.

Compatible with Next.js and other React frameworks.

Update: May 31, 2026

Version: 1.2.8

Changes:

  • numerous visual bugs were resolved
  • the prop 'modalIsOpen' has been renamed to 'componentIsOpen'
  • the README has been updated; example gifs have been added

Examples

Installation

npm install --save cookie-consent-react

or

yarn add cookie-consent-react

or

pnpm add cookie-consent-react

Usage

Pass selected configuration and consent handlers into the component to configure cookie consent behavior.

"use client";

import { CookieConsent } from "cookie-consent-react";

const analyticsHandler = () => {
  console.log("Analytics enabled");
};

const handleAcceptAll = () => {
  console.log("All cookies accepted");
};

const handleAcceptSelection = () => {
  console.log("Selected cookies accepted");
};

export default function CookieConsentProvider() {
  return (
    <CookieConsent
      mode="banner"
      categoriesListStyle="checkboxes"
      handlerFunctions={[
        {
          category: "analytics",
          function: analyticsHandler,
        },
      ]}
      onAcceptAll={(accept) => {
        handleAcceptAll();
        accept();
      }}
      onAcceptSelection={(accept) => {
        handleAcceptSelection();
        accept();
      }}
      privacyPolicyUrl="/privacy-policy"
    />
  );
}

tags, snippets & handlerFunctions

The component includes four default cookie categories:

| Category | Description | |---|---| | essential | Required cookies necessary for application functionality. | | marketing | Advertising, remarketing, and marketing-related cookies. | | analytics | Analytics and performance tracking cookies. | | other | Additional uncategorized cookies. |

All tags, snippets, and handlerFunctions must be assigned to one of these categories.

tags

Use tags for rendering React components after consent is granted for their assigned category.

Example:

<CookieConsent
  mode="banner"
  tags={[
    {
      category: "marketing",
      tag: <GoogleTagManager gtmId="GTM-XXXXXXX" />,
    },
  ]}
/>

snippets

Use snippets for rendering raw JSX/HTML snippets after consent is granted for their assigned category.

Example:

<CookieConsent
  mode="banner"
  snippets={[
    {
      category: "marketing",
      snippet: (
        <iframe
          src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
          height="0"
          width="0"
          style={{
            visibility: "hidden",
            display: "none",
          }}
        />
      ),
    },
  ]}
/>

handlerFunctions

Use handlerFunctions for runtime side effects and initialization logic after consent is granted for their assigned category.

Handler functions execute:

  • whenever the component mounts and consent already exists
  • whenever consent preferences are updated and saved

Example:

const analyticsHandler = () => {
  console.log("Analytics initialized");
};

<CookieConsent
  mode="banner"
  handlerFunctions={[
    {
      category: "analytics",
      function: analyticsHandler,
    },
  ]}
/>

Documentation

| Prop | Type | Description | Default | |---|---|---|---| | mode* | "modal" | "banner" | Determines component display mode. *Required. | — | | tags | {category: "essential" \| "marketing" \| "analytics" \| "other"; tag: JSX.Element }[] | React components rendered after consent is granted for their category. | — | | snippets | {category: "essential" \| "marketing" \| "analytics" \| "other"; snippet: JSX.Element }[] | Raw JSX/HTML snippets rendered after consent is granted for their category. | — | | handlerFunctions | {category: "essential" \| "marketing" \| "analytics" \| "other"; function: () => void }[] | Runtime callbacks executed when consent exists or changes. | — | | categorySettings | { [K in CookieConsentCategory]?: { required?: boolean; label?: string; } } | Customize category labels and required categories. |{ essential: { required: true } }| | categoriesListStyle | "hide" | "checkboxes" | "switches" | Determines how categories are displayed in the UI. | "checkboxes" | | onAcceptAll | (defaultHandler: () => void) => void | Callback executed when all categories are accepted. | — | | onAcceptSelection | (defaultHandler: () => void) => void | Callback executed when selected categories are saved. | — | | onDeclineAll | (defaultHandler: () => void) => void | Callback executed when optional categories are declined. | — | | privacyPolicyUrl | string | URL to the application's privacy policy. | — | | customStorageKey | string | Custom storage key used for persisted consent data. | By default this value is empty; however a default key "cookie-consent" is used instead if this isn't defined. | | componentIsOpen | boolean | Controls modal visibility. | Handled by boolean type internal state which is set to true on render if there aren't any saved settings in localStorage for current storageKey | | customColors | Partial<typeof { primary: "#0070f3", lightPrimary: "#eaeaea", lightSecondary: "#d0d0d0", textPrimary: "#333333", textSecondary: "#666666", background: "#ffffff", backgroundOff: "#f9f9f9", overlay: "rgba(0, 0, 0, 0.5)", shadow: "rgba(0, 0, 0, 0.15)" }> | Override default component colors. | - | | customFontFamily | string | Custom font family used throughout the component. | System font | | language | "en" \| "de" \| "es" \| "fr" \| "it" \| "nl" \| "pt" \| "ja" \| "zh" \| "ar" \| "ru" \| "ko" \| "pl"\| string | Active localization language. | "en" | | customLocales | { "en" \| "de" \| "es" \| "fr" \| "it" \| "nl" \| "pt" \| "ja" \| "zh" \| "ar" \| "ru" \| "ko" \| "pl"\| string : Partial<{ title: string; description: string; required: string; privacyPolicyInfo: string; privacyPolicy: string; acceptAll: string; SaveSelection: string; disableAll: string; cookiePreferences_show: string; cookiePreferences_hide: string; categories: string; essential: string; marketing: string; analytics: string; other: string; }> \| undefined; } | Custom translation overrides. | — |

Built-in Languages

Built-in translations are available for:

  • English (en)
  • German (de)
  • Spanish (es)
  • French (fr)
  • Italian (it)
  • Dutch (nl)
  • Portuguese (pt)
  • Japanese (ja)
  • Chinese (zh)
  • Arabic (ar)
  • Russian (ru)
  • Korean (ko)
  • Polish (pl)

Custom Localization

<CookieConsent
  mode="banner"
  language="en"
  customLocales={{
    en: {
      title: "Custom Cookie Settings",
      acceptAll: "Allow Everything",
    },
  }}
/>

Custom Category Settings

<CookieConsent
  mode="banner"
  categorySettings={{
    essential: {
      required: true,
      label: "Required Cookies",
    },
    analytics: {
      label: "Analytics & Performance",
    },
  }}
/>

Custom Styling

Custom Colors

<CookieConsent
  mode="banner"
  customColors={{
    primary: "#000000",
    background: "#ffffff",
  }}
/>

Custom Font

<CookieConsent
  mode="banner"
  customFontFamily="'Inter', sans-serif"
/>

License

MIT