MCPcopy Index your code
hub / github.com/Mastermindzh/react-cookie-consent

github.com/Mastermindzh/react-cookie-consent @10.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 10.0.1 ↗ · + Follow
20 symbols 53 edges 24 files 5 documented · 25% 6 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

:cookie: react-cookie-consent :cookie:

A small, simple and customizable cookie consent bar for use in React applications.

NPM

Demo (storybook): https://mastermindzh.github.io/react-cookie-consent/

Downloads Dependent repos (via libraries.io) GitHub contributors Minified size npm type definitions license-mit

Default look

default look

Table of contents

Installation

npm install react-cookie-consent

or use yarn:

yarn add react-cookie-consent

Using it

You can import the cookie bar like this:

import CookieConsent from "react-cookie-consent";

If you want to set/remove cookies yourself you can optionally import Cookies (straight from js-cookie) like this:

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

Then you can use the component anywhere in your React app like so:

<CookieConsent>This website uses cookies to enhance the user experience.</CookieConsent>

You can optionally set some props like this (next chapter will show all props):

<CookieConsent
  location="bottom"
  buttonText="Sure man!!"
  cookieName="myAwesomeCookieName2"
  style={{ background: "#2B373B" }}
  buttonStyle={{ color: "#4e503b", fontSize: "13px" }}
  expires={150}
>
  This website uses cookies to enhance the user experience.{" "}
  <span style={{ fontSize: "10px" }}>This bit of text is smaller :O</span>
</CookieConsent>

One of the props (onAccept) is a function, this function will be called after the user has clicked the accept button. It is called with an object containing a boolean property acceptedByScrolling to indicate if the acceptance was triggered by the user scrolling You can provide a function like so:

<CookieConsent
  onAccept={(acceptedByScrolling) => {
    if (acceptedByScrolling) {
      // triggered if user scrolls past threshold
      alert("Accept was triggered by user scrolling");
    } else {
      alert("Accept was triggered by clicking the Accept button");
    }
  }}
></CookieConsent>

If the decline button is enabled then the (onDecline) prop function can be used, this function will be called after the user has clicked the decline button. You can enable the button and provide a function like so:

<CookieConsent
  enableDeclineButton
  onDecline={() => {
    alert("nay!");
  }}
></CookieConsent>

getting the cookies value in your own code

react-cookie-consent exports a function called getCookieConsentValue(cookieName: string). You can use it in your own code like so:

import CookieConsent, { Cookies, getCookieConsentValue } from "react-cookie-consent";

console.log(getCookieConsentValue("your_custom_cookie_name"));

reset the cookies value in your own code

react-cookie-consent exports a function called resetCookieConsentValue. You can use it in order to remove cookie in client-site:

import CookieConsent, { Cookies, resetCookieConsentValue } from "react-cookie-consent";

console.log(resetCookieConsentValue());

That option would be interesting if you want to allow user to change their consent. If you want to show again the consent bar, you must force "visible" prop to show again the bar.

Props

Prop Type Default value Description
location string, "top", "bottom" or "none" "bottom" Syntactic sugar to easily enable you to place the bar at the top or the bottom of the browser window. Use "none" to disable.
visible string, "show", "hidden" or "byCookieValue" "byCookieValue" Force the consent bar visibility. If "byCookieValue", visibility are defined by cookie consent existence.
children string or React component Content to appear inside the bar
disableStyles boolean false If enabled the component will have no default style. (you can still supply style through props)
hideOnAccept boolean true If disabled the component will not hide it self after the accept button has been clicked. You will need to hide yourself (see onAccept)
buttonText string or React component "I understand" Text to appear on the button
declineButtonText string or React component "I decline" Text to appear on the decline button
cookieName string "CookieConsent" Name of the cookie used to track whether the user has agreed. Note that you also have to pass this to the getCookieConsentValue and resetCookieConsentValue functions as they default to "CookieConsent" as well.
cookieValue string or boolean or number true Value to be saved under the cookieName.
declineCookieValue string or boolean or number false Value to be saved under the cookieName when declined.
setDeclineCookie boolean true Whether to set a cookie when the user clicks "decline"
onAccept function () => {} Function to be called after the accept button has been clicked.
onDecline function () => {} Function to be called after the decline button has been clicked.
debug boolean undefined Bar will be drawn regardless of cookie for debugging purposes.
expires number 365 Number of days before the cookie expires.
extraCookieOptions object {} Extra info (apart from expiry date) to add to the cookie
overlay boolean false Whether to show a page obscuring overlay or not.
containerClasses string "" CSS classes to apply to the surrounding container
buttonClasses string "" CSS classes to apply to the button
buttonWrapperClasses string "" CSS classes to apply to the div wrapping the buttons
customButtonWrapperAttributes React.HTMLAttributes<HTMLDivElement> {} Allows you to set custom (data) attributes on the button wrapper div
declineButtonClasses string "" CSS classes to apply to the decline button
buttonId string "" Id to apply to the button
declineButtonId string "" Id to apply to the decline button
contentClasses string "" CSS classes to apply to the content
overlayClasses string "" CSS classes to apply to the surrounding overlay
style object [look at source][style] React styling object for the bar.
buttonStyle object [look at source][buttonstyle] React styling object for the button.
declineButtonStyle object [look at source][declinebuttonstyle] React styling object for the decline button.
contentStyle object [look at source][contentstyle] React styling object for the content.
overlayStyle object [look at source][overlaystyle] React styling object for the overlay.
disableButtonStyles boolean false If enabled the button will have no default style. (you can still supply style through props)
enableDeclineButton

Extension points exported contracts — how you extend this code

CookieConsentState (Interface)
(no doc)
src/CookieConsent.state.ts
CookieConsentProps (Interface)
(no doc)
src/CookieConsent.props.tsx

Core symbols most depended-on inside this repo

accept
called by 3
src/CookieConsent.tsx
getLegacyCookieName
called by 2
src/utilities.ts
overlayClick
called by 2
src/CookieConsent.tsx
setCookie
called by 2
src/CookieConsent.tsx
getCookieConsentValue
called by 1
src/utilities.ts
decline
called by 1
src/CookieConsent.tsx
getCookieValue
called by 1
src/CookieConsent.tsx
resetCookieConsentValue
called by 0
src/utilities.ts

Shape

Method 8
Function 7
Class 2
Interface 2
Enum 1

Languages

TypeScript100%

Modules by API surface

src/CookieConsent.tsx10 symbols
src/utilities.ts3 symbols
src/CookieConsent.props.tsx2 symbols
stories/defaults/template.tsx1 symbols
stories/defaults/intro.tsx1 symbols
src/models/constants/sameSiteOptions.ts1 symbols
src/components/ConditionalWrapper.tsx1 symbols
src/CookieConsent.state.ts1 symbols

For agents

$ claude mcp add react-cookie-consent \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact