@tantainnovative/ndpr-toolkit
Version:
Nigerian Data Protection Compliance Toolkit for implementing NDPR and DPA compliant features
257 lines (243 loc) • 8.71 kB
text/typescript
import React$1, { ReactNode } from 'react';
export { ConsentBanner as UnstyledConsentBanner, ConsentSettings as UnstyledConsentSettings, ConsentToggle as UnstyledConsentToggle } from './unstyled.mjs';
interface ConsentCategories {
necessary: boolean;
analytics: boolean;
marketing: boolean;
functional: boolean;
[key: string]: boolean;
}
interface ConsentState$1 {
hasUserConsented: boolean;
consentState: ConsentCategories;
showBanner: boolean;
showSettings: boolean;
}
interface ConsentActions$1 {
acceptAll: () => void;
rejectAll: () => void;
savePreferences: (preferences: Partial<ConsentCategories>) => void;
openSettings: () => void;
closeSettings: () => void;
updateConsent: (category: keyof ConsentCategories, value: boolean) => void;
}
interface ConsentContextValue$1 extends ConsentState$1, ConsentActions$1 {
}
declare const ConsentContext: React$1.Context<ConsentContextValue$1 | undefined>;
interface ConsentProviderProps$1 {
children: ReactNode;
initialConsent?: Partial<ConsentCategories>;
onConsentChange?: (consent: ConsentCategories) => void;
storageKey?: string;
}
declare function ConsentProvider({ children, initialConsent, onConsentChange, storageKey, }: ConsentProviderProps$1): React$1.JSX.Element;
declare function useConsent(): ConsentContextValue$1;
/**
* Base consent categories interface that can be extended
*/
interface BaseConsentCategories {
necessary: boolean;
[key: string]: boolean;
}
/**
* Default consent categories provided by the toolkit
*/
interface DefaultConsentCategories extends BaseConsentCategories {
necessary: boolean;
analytics: boolean;
marketing: boolean;
functional: boolean;
}
/**
* Generic consent state interface
*/
interface ConsentState<T extends BaseConsentCategories = DefaultConsentCategories> {
hasUserConsented: boolean;
consentState: T;
showBanner: boolean;
showSettings: boolean;
}
/**
* Generic consent actions interface
*/
interface ConsentActions<T extends BaseConsentCategories = DefaultConsentCategories> {
acceptAll: () => void;
rejectAll: () => void;
savePreferences: (preferences: Partial<T>) => void;
openSettings: () => void;
closeSettings: () => void;
updateConsent: (category: keyof T, value: boolean) => void;
}
/**
* Combined consent context value
*/
interface ConsentContextValue<T extends BaseConsentCategories = DefaultConsentCategories> extends ConsentState<T>, ConsentActions<T> {
}
/**
* Consent provider props with generic support
*/
interface ConsentProviderProps<T extends BaseConsentCategories = DefaultConsentCategories> {
children: React.ReactNode;
initialConsent?: Partial<T>;
onConsentChange?: (consent: T) => void;
storageKey?: string;
categories?: Array<{
id: keyof T;
name: string;
description: string;
required?: boolean;
}>;
}
/**
* Factory function to create a typed consent context
*/
declare function createConsentContext<T extends BaseConsentCategories = DefaultConsentCategories>(): {
ConsentContext: React$1.Context<ConsentContextValue<T> | undefined>;
ConsentProvider: ({ children, initialConsent, onConsentChange, storageKey, categories, }: ConsentProviderProps<T>) => React$1.JSX.Element;
useConsent: () => ConsentContextValue<T>;
};
interface ConsentBannerProps {
renderBanner?: (props: BannerProps) => ReactNode;
theme?: {
primaryColor?: string;
textColor?: string;
backgroundColor?: string;
};
position?: 'top' | 'bottom' | 'center';
animation?: 'slide' | 'fade' | 'none';
fullWidth?: boolean;
maxWidth?: string;
unstyled?: boolean;
className?: string;
children?: ReactNode;
}
interface ConsentBannerComponent extends React$1.FC<ConsentBannerProps> {
Message: React$1.FC<{
children: ReactNode;
className?: string;
}>;
Actions: React$1.FC<{
children: ReactNode;
className?: string;
}>;
}
declare const ConsentBanner: ConsentBannerComponent;
interface ConsentSettingsProps {
renderSettings?: (props: SettingsProps) => ReactNode;
theme?: {
primaryColor?: string;
textColor?: string;
backgroundColor?: string;
};
unstyled?: boolean;
className?: string;
children?: ReactNode;
}
declare const ConsentSettings: React$1.FC<ConsentSettingsProps>;
interface BannerProps {
onAcceptAll: () => void;
onRejectAll: () => void;
onOpenSettings: () => void;
}
interface SettingsProps {
consentState: ConsentCategories;
onUpdateConsent: (category: keyof ConsentCategories, value: boolean) => void;
onSave: () => void;
onClose: () => void;
}
interface RenderProps {
consents: ConsentCategories;
actions: ConsentActions$1;
ui: {
showBanner: boolean;
showSettings: boolean;
openSettings: () => void;
closeSettings: () => void;
};
}
interface ConsentManagerProps extends Omit<ConsentProviderProps$1, 'children'> {
children?: ReactNode | ((props: RenderProps) => ReactNode);
headless?: boolean;
renderBanner?: (props: BannerProps) => ReactNode;
renderSettings?: (props: SettingsProps) => ReactNode;
components?: {
Banner?: React$1.ComponentType<any>;
Settings?: React$1.ComponentType<any>;
};
theme?: {
primaryColor?: string;
textColor?: string;
backgroundColor?: string;
};
position?: 'top' | 'bottom' | 'center';
animation?: 'slide' | 'fade' | 'none';
fullWidth?: boolean;
maxWidth?: string;
}
interface ConsentManagerComponent extends React$1.FC<ConsentManagerProps> {
Banner: typeof ConsentBanner;
Settings: typeof ConsentSettings;
}
declare const ConsentManager: ConsentManagerComponent;
/**
* Hook to access only the consent state without actions
*/
declare function useConsentState(): {
hasUserConsented: boolean;
consentState: ConsentCategories;
showBanner: boolean;
showSettings: boolean;
};
/**
* Hook to access only the consent actions without state
*/
declare function useConsentActions(): {
acceptAll: () => void;
rejectAll: () => void;
savePreferences: (preferences: Partial<ConsentCategories>) => void;
openSettings: () => void;
closeSettings: () => void;
updateConsent: (category: keyof ConsentCategories, value: boolean) => void;
};
type ConsentEventType = 'consent:accepted' | 'consent:rejected' | 'consent:updated' | 'banner:shown' | 'banner:hidden' | 'settings:opened' | 'settings:closed';
type ConsentEventListener = (data?: any) => void;
interface ConsentEventManager {
on: (event: ConsentEventType, listener: ConsentEventListener) => () => void;
off: (event: ConsentEventType, listener: ConsentEventListener) => void;
emit: (event: ConsentEventType, data?: any) => void;
}
/**
* Advanced hook that provides event-driven consent management
*/
declare function useConsentManager(): ConsentEventManager & ReturnType<typeof useConsent>;
type ConsentType = 'necessary' | 'functional' | 'analytics' | 'marketing';
interface ConsentRecord {
id: string;
userId?: string;
consents: Record<ConsentType, boolean>;
timestamp: string;
ipAddress?: string;
userAgent?: string;
}
interface ConsentHistoryEntry extends ConsentRecord {
changeReason?: string;
}
declare const consentService: {
saveConsent: (consents: Record<ConsentType, boolean>, userId?: string) => ConsentRecord;
getCurrentConsent: () => ConsentRecord | null;
getConsentHistory: () => ConsentHistoryEntry[];
updateConsent: (consents: Record<ConsentType, boolean>, changeReason?: string, userId?: string) => ConsentRecord;
clearConsentData: () => void;
hasConsent: (type: ConsentType) => boolean;
};
declare const cookieUtils: {
set: (name: string, value: string, days: number) => void;
get: (name: string) => string | null;
delete: (name: string) => void;
};
declare const consentStorage: {
save: (key: string, data: any) => void;
load: (key: string) => any | null;
remove: (key: string) => void;
};
export { type BannerProps, type BaseConsentCategories, type ConsentActions$1 as ConsentActions, ConsentBanner, type ConsentCategories, ConsentContext, type ConsentContextValue$1 as ConsentContextValue, ConsentManager, type ConsentManagerProps, ConsentProvider, type ConsentProviderProps, ConsentSettings, type ConsentState$1 as ConsentState, type DefaultConsentCategories, type RenderProps, type SettingsProps, consentService, consentStorage, cookieUtils, createConsentContext, useConsent, useConsentActions, useConsentManager, useConsentState };