Skip to content

A powerful, customizable React component for cookie consent management with built-in tracking prevention. This component provides a modern, user-friendly way to obtain and manage cookie consent from your website visitors.

License

Notifications You must be signed in to change notification settings

pouljohn1/react-cookie-manager

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช React Cookie Manager

A powerful, customizable React component for cookie consent management with built-in tracking prevention. This component provides a modern, user-friendly way to obtain and manage cookie consent from your website visitors.

React Cookie Manager

Features

  • ๐ŸŒ Multiple display types (banner, popup, modal)
  • ๐Ÿ›ก๏ธ Automatic tracking prevention (Google Analytics, etc.)
  • ๐ŸŽฏ Granular cookie category controls (Analytics, Social, Advertising)
  • ๐ŸŽจ Light and dark theme support
  • ๐Ÿ“ฑ Responsive design
  • ๐Ÿ”ง Highly customizable UI
  • ๐Ÿ’พ Persistent consent storage
  • ๐Ÿ”’ Privacy-first approach
  • ๐Ÿ‡ช๐Ÿ‡บ Note: Full GDPR compliance requires you to store user consent in your own database.

๐ŸŽฎ Try it out!

See React Cookie Manager in action and explore all its features in our interactive demo.

Automatically Disable Tracking

Unlike other cookie consent managers and React components, this component automatically disables tracking for Google Analytics, Facebook Pixel, and other tracking services. This is done by blocking the tracking scripts from loading. Therefore, you don't need to manually disable tracking, saving you hours of work.

Installation

npm install react-cookie-manager
# or
yarn add react-cookie-manager

Importing Styles

The component requires its CSS file to be imported in your application. Add the following import to your app's entry point (e.g., App.tsx or index.tsx):

import "react-cookie-manager/style.css";

React Cookie Manager Modal

React Cookie Manager Popup

React Cookie Manager Popup Dark

React Cookie Manager Banner

React Cookie Manager Manage Cookies

Basic Usage

import { CookieManager } from "react-cookie-manager";
import "react-cookie-manager/style.css";

function App() {
  return (
    <CookieManager
      translations={{
        title: "Cookie Preferences",
        message: "We use cookies to improve your experience."
      }}
      onManage={(preferences) =>
        console.log("Cookie preferences:", preferences)
      }
    >
      <YourApp />
    </CookieManager>
  );
}

Full Usage

import { CookieManager } from "react-cookie-manager";
import "react-cookie-manager/style.css";

function App() {
  return (
    <CookieManager
      translations={{
        title: "Would You Like A Cookie? ๐Ÿช",
        message: "We value your privacy. Choose which cookies you want to allow. Essential cookies are always enabled as they are necessary for the website to function properly.",
        buttonText: "Accept All",
        declineButtonText: "Decline All",
        manageButtonText: "Manage Cookies",
        privacyPolicyText: "Privacy Policy"
      }}
      showManageButton={true}
      privacyPolicyUrl="https://example.com/privacy"
      theme="light"
      displayType="popup"
      onManage={(preferences) => {
        if (preferences) {
          console.log("Cookie preferences updated:", preferences);
        }
      }}
    >
      <AppContent />
    </CookieManager>
  );
}

Advanced Usage with Hook

import { CookieManager, useCookieConsent } from "react-cookie-manager";

function CookieSettings() {
  const { showConsentBanner, detailedConsent } = useCookieConsent();

  return (
    <div>
      <button onClick={showConsentBanner}>Manage Cookie Settings</button>
      {detailedConsent && (
        <div>
          Analytics:{" "}
          {detailedConsent.Analytics.consented ? "Enabled" : "Disabled"}
          Social: {detailedConsent.Social.consented ? "Enabled" : "Disabled"}
          Advertising:{" "}
          {detailedConsent.Advertising.consented ? "Enabled" : "Disabled"}
        </div>
      )}
    </div>
  );
}

Props

Prop Type Default Description
children React.ReactNode - Your app components
translations TranslationObject | TranslationFunction - Translation object or i18n TFunction
translationI18NextPrefix string - i18next key prefix, e.g. "cookies."
showManageButton boolean false Whether to show the manage cookies button
privacyPolicyUrl string - URL for the privacy policy
localStorageKey string 'cookie-consent' Name of the key to store consent
cookieExpiration number 365 Days until cookie expires
displayType 'banner' | 'popup' | 'modal' 'banner' How the consent UI is displayed
position 'top' | 'bottom' 'bottom' Position of the banner
theme 'light' | 'dark' 'light' Color theme
disableAutomaticBlocking boolean false Disable automatic tracking prevention
blockedDomains string[] [] Additional domains to block
onManage (preferences?: CookieCategories) => void - Callback when preferences are updated

Cookie Categories

The component manages three categories of cookies:

interface CookieCategories {
  Analytics: boolean;
  Social: boolean;
  Advertising: boolean;
}

Hook API

The useCookieConsent hook provides the following:

interface CookieConsentHook {
  hasConsent: boolean | null;
  isDeclined: boolean;
  detailedConsent: DetailedCookieConsent | null;
  showConsentBanner: () => void;
  acceptCookies: () => void;
  declineCookies: () => void;
  updateDetailedConsent: (preferences: CookieCategories) => void;
}

i18next support

import { default as i18next } from "i18next";

function App() {
  return (
    <CookieManager
      translations={i18next.t}
      translationI18NextPrefix="cookies."
      ...
      />
  )
}
// en.json
{
  "cookies": {
    "title": "Would You Like A Cookie? ๐Ÿช",
    "message": "We value your privacy. Choose which cookies you want to allow. Essential cookies are always enabled as they are necessary for the website to function properly.",
    "buttonText": "Accept All",
    "declineButtonText": "Decline All",
    "manageButtonText": "Manage Cookies",
    "privacyPolicyText": "Privacy Policy"
  }
  //...
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT ยฉ Hypership

About

A powerful, customizable React component for cookie consent management with built-in tracking prevention. This component provides a modern, user-friendly way to obtain and manage cookie consent from your website visitors.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.4%
  • JavaScript 1.3%
  • CSS 1.1%
  • HTML 0.2%