react-cookie-manager
Version:
🍪 The ultimate React cookie consent solution. Automatically block trackers, manage consent preferences, and protect user privacy with an elegant UI. Perfect for modern web applications.
82 lines • 3.48 kB
TypeScript
import { default as React } from 'react';
import { CookieCategories, CookieConsenterProps, DetailedCookieConsent, TranslationObject, TranslationFunction } from '../types/types';
import { GoogleConsentModeOptions } from '../utils/google-consent-mode';
interface CookieConsentContextValue {
hasConsent: boolean | null;
isDeclined: boolean;
detailedConsent: DetailedCookieConsent | null;
showConsentBanner: () => void;
acceptCookies: () => void;
declineCookies: () => void;
updateDetailedConsent: (preferences: CookieCategories) => void;
openPreferencesModal: () => void;
}
export interface CookieManagerProps extends Omit<CookieConsenterProps, "onAccept" | "onDecline" | "forceShow"> {
children: React.ReactNode;
cookieKey?: string;
onManage?: (preferences?: CookieCategories) => void;
onAccept?: () => void;
onDecline?: () => void;
/**
* Called once on mount with the consent state restored from storage.
* Receives the persisted `DetailedCookieConsent`, or `null` when no prior
* consent decision exists. Useful for syncing analytics (e.g. gtag consent
* mode) on load without waiting for the user to interact (issue #42).
*/
onConsentLoaded?: (consent: DetailedCookieConsent | null) => void;
/**
* Enable Google Consent Mode v2. When `true`, the library emits a `denied`
* default on mount and pushes `gtag('consent','update',...)` whenever consent
* changes, mapping Analytics → `analytics_storage`, Advertising →
* `ad_storage`/`ad_user_data`/`ad_personalization`, and Social →
* `personalization_storage`. Pass an options object to customize the mapping,
* defaults, `wait_for_update`, `url_passthrough`, or `ads_data_redaction`.
*/
googleConsentMode?: boolean | GoogleConsentModeOptions;
disableAutomaticBlocking?: boolean;
blockedDomains?: string[];
expirationDays?: number;
/**
* Translations that will be used in the consent UI. It can be one of:
* 1. **TranslationObject**: An object with keys for each TranslationKey, e.g.:
* ```
* {
* title: 'My own consent title',
* message: 'My own consent message',
* // other keys if needed
* }
* ```
* 2. **TranslationFunction**: A function that takes a key with params and returns a string. Useful for i18n libraries where TFunction can be passed like follows:
* ```ts
* const { t } = useTranslation();
* return <CookieConsenter translations={t} />
* ```
*
* By default it uses English translations specified in TranslationKey defaults.
*/
translations?: TranslationObject | TranslationFunction<any, any>;
/**
* Prefix for translation keys when using i18next, e.g.
* ```ts
* // typescript file
* const { t } = useTranslation();
* <CookieConsenter translations={t} translationI18NextPrefix="cookieConsent" />
* ```
* ```json
* // {lng}.json
* {
* "cookieConsent": {
* "title": "My own consent title",
* "message": "My own consent message"
* }
* }
* ```
*/
translationI18NextPrefix?: string;
enableFloatingButton?: boolean;
theme?: "light" | "dark";
}
export declare const CookieManager: React.FC<CookieManagerProps>;
export declare const useCookieConsent: () => CookieConsentContextValue;
export {};
//# sourceMappingURL=CookieConsentContext.d.ts.map