CookieBannerCookie consent banner with customizable categories and GDPR compliance
ConsentPreferencesDetailed consent preferences panel for granular privacy control
GDPR Ready
import {
CookieBanner,
ConsentPreferences,
} from '@sylphx/platform-sdk/react'Consent Categories
Both components support the following consent categories by default. Categories can be customized to match your privacy policy.
necessaryEssential / NecessaryRequired for basic site functionality
analyticsAnalyticsAnonymous usage statistics and performance data
marketingMarketingPersonalized ads and cross-site tracking
functionalFunctionalEnhanced features like preferences and chat
targetingTargetingThird-party tracking for remarketing
ConsentPreferences
A detailed consent preferences panel for users to manage their privacy choices at any time. Can be embedded in settings pages or shown as a modal. Includes granular controls for each consent category.
import { ConsentPreferences } from '@sylphx/platform-sdk/react'
export default function PrivacySettingsPage() {
return (
<div className="max-w-2xl mx-auto py-8">
<h1 className="text-2xl font-bold mb-6">Privacy Settings</h1>
<ConsentPreferences
onSave={(consents) => {
console.log('Saved preferences:', consents)
toast.success('Privacy preferences saved!')
}}
onChange={(category, enabled) => {
console.log(`${category} is now ${enabled ? 'enabled' : 'disabled'}`)
}}
/>
</div>
)
}Props
| Property | Type | Description |
|---|---|---|
onSave | (consents: ConsentState) => void | Callback fired when user saves preferences |
onChange | (category: string, enabled: boolean) => void | Callback fired when a category toggle changes |
onClose | () => void | Callback fired when panel is closed (modal mode) |
categories | ConsentCategory[] | Custom consent categories to display |
layout | "card" | "flat" | "accordion"= "card" | Visual layout style |
showCookieList | boolean= true | Show list of cookies per category |
showScriptList | boolean= true | Show list of third-party scripts per category |
showAcceptAll | boolean= true | Show "Accept All" button |
showDeclineAll | boolean= true | Show "Decline All" button |
showSaveButton | boolean= true | Show save button (vs auto-save) |
autoSave | boolean= false | Auto-save on toggle change |
showConsentHistory | boolean= false | Show consent change history |
showGPCStatus | boolean= true | Show Global Privacy Control status |
showLastUpdated | boolean= true | Show when consent was last updated |
title | string= "Privacy Preferences" | Panel title |
description | string | ReactNode | Panel description text |
saveLabel | string= "Save Preferences" | Save button text |
acceptAllLabel | string= "Accept All" | Accept all button text |
declineAllLabel | string= "Decline All" | Decline all button text |
privacyPolicyUrl | string= "/privacy" | Link to privacy policy |
cookiePolicyUrl | string= "/cookies" | Link to cookie policy |
theme | ThemeVariables | Custom theme overrides |
className | string | Additional CSS classes |
ConsentState Type
interface ConsentState {
necessary: boolean // Always true (required)
analytics: boolean
marketing: boolean
functional: boolean
targeting: boolean
[key: string]: boolean // Custom categories
}
interface ConsentRecord {
timestamp: string // ISO timestamp
action: 'grant' | 'revoke' | 'update'
categories: string[] // Affected categories
source: 'banner' | 'preferences' | 'api'
gpcEnabled?: boolean // GPC signal at time of consent
}Advanced Examples
<ConsentPreferences
layout="accordion"
showCookieList={true}
showScriptList={true}
categories={[
{
id: 'necessary',
name: 'Strictly Necessary',
description: 'These cookies are essential for the website to function and cannot be disabled.',
required: true,
default: true,
cookies: [
'session - Maintains your logged-in session',
'csrf_token - Protects against cross-site attacks',
'sylphx_consent - Stores your cookie preferences',
],
},
{
id: 'analytics',
name: 'Analytics & Performance',
description: 'These cookies help us understand how visitors interact with our website.',
required: false,
default: false,
cookies: [
'_ga - Google Analytics visitor ID',
'_gid - Google Analytics session ID',
'_gat - Google Analytics rate limiter',
],
scripts: [
'google-analytics.com',
'googletagmanager.com',
],
},
{
id: 'marketing',
name: 'Marketing & Advertising',
description: 'These cookies are used to deliver personalized advertisements.',
required: false,
default: false,
cookies: [
'_fbp - Facebook Pixel',
'_gcl_au - Google Ads conversion tracking',
],
scripts: [
'facebook.com',
'doubleclick.net',
'adsrvr.org',
],
},
]}
/>Consent Audit Trail
showConsentHistory={true} to display this history to users.Integration with Analytics
Use the consent state to conditionally load analytics and marketing scripts.
'use client'
import { useConsent } from '@sylphx/platform-sdk/react'
import Script from 'next/script'
export function AnalyticsProvider({ children }) {
const { hasConsent, isLoading } = useConsent()
// Don't render scripts until consent state is loaded
if (isLoading) return <>{children}</>
return (
<>
{/* Only load Google Analytics if user consented */}
{hasConsent('analytics') && (
<>
<Script
src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXX');
`}
</Script>
</>
)}
{/* Only load Facebook Pixel if user consented to marketing */}
{hasConsent('marketing') && (
<Script id="facebook-pixel" strategy="afterInteractive">
{`
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
// ... pixel code
`}
</Script>
)}
{children}
</>
)
}useConsent Hook
Access consent state and methods from anywhere in your app.
import { useConsent } from '@sylphx/platform-sdk/react'
function MyComponent() {
const {
// State
consents, // Current consent state object
hasResponded, // Whether user has made a choice
isLoading, // Loading state
gpcEnabled, // Global Privacy Control signal detected
lastUpdated, // Timestamp of last consent update
// Methods
hasConsent, // Check single category: hasConsent('analytics')
setConsent, // Set single: setConsent('analytics', true)
setConsents, // Set multiple: setConsents({ analytics: true, marketing: false })
acceptAll, // Accept all categories
declineOptional, // Decline all non-required categories
resetConsent, // Clear all consent (re-show banner)
showBanner, // Programmatically show banner
showPreferences, // Programmatically show preferences modal
// History
consentHistory, // Array of ConsentRecord
} = useConsent()
const handleAnalyticsAction = () => {
if (hasConsent('analytics')) {
// Track the event
analytics.track('button_clicked')
} else {
// Prompt for consent
showPreferences()
}
}
return (
<button onClick={handleAnalyticsAction}>
Track Me
</button>
)
}| Property | Type | Description |
|---|---|---|
consents | ConsentState | Current consent state for all categories |
hasResponded | boolean | Whether user has made any consent choice |
isLoading | boolean | True while loading consent state from storage |
gpcEnabled | boolean | True if Global Privacy Control signal is detected |
lastUpdated | Date | null | When consent was last updated |
consentHistory | ConsentRecord[] | Array of consent change records |
hasConsent(id) | (id: string) => boolean | Check if specific category is consented |
setConsent(id, value) | (id: string, value: boolean) => void | Set consent for a single category |
setConsents(consents) | (consents: Partial<ConsentState>) => void | Set multiple consents at once |
acceptAll() | () => void | Accept all consent categories |
declineOptional() | () => void | Decline all non-required categories |
resetConsent() | () => void | Clear all consent data (triggers banner) |
showBanner() | () => void | Programmatically show the cookie banner |
showPreferences() | () => void | Programmatically show preferences panel |
Theming
Both components support theming via the theme prop. Customize colors, fonts, and border radius to match your brand.
import { CookieBanner, ConsentPreferences, type ThemeVariables } from '@sylphx/platform-sdk/react'
const privacyTheme: ThemeVariables = {
colorPrimary: '#10b981',
colorPrimaryHover: '#059669',
colorBackground: '#ffffff',
colorForeground: '#1f2937',
colorMuted: '#f3f4f6',
colorMutedForeground: '#6b7280',
colorBorder: '#e5e7eb',
colorDestructive: '#ef4444',
colorSuccess: '#22c55e',
fontFamily: 'Inter, sans-serif',
fontSize: '14px',
borderRadius: '0.75rem',
}
// Apply to both components
<CookieBanner theme={privacyTheme} />
<ConsentPreferences theme={privacyTheme} />Best Practices
Show Before Tracking
Display the consent banner before loading any non-essential cookies or tracking scripts.
Default to Opt-Out
For GDPR compliance, non-essential cookies should be disabled by default.
Respect GPC Signals
Enable respectGPC to automatically honor Global Privacy Control browser settings.
Provide Granular Control
Let users choose specific categories rather than just accept/reject all.
Maintain Audit Trails
Log consent changes with timestamps for regulatory compliance.
Easy Access to Settings
Make it easy for users to change their preferences at any time from settings or footer links.
Need server-side consent checking?
Use the consent API to check consent status server-side before sending marketing emails or tracking events.