'use client' import { Cookie } from 'lucide-react' import { useEffect, useRef, useState } from 'react' import { CONSENT_CATEGORIES, type ConsentCategory } from './categories.js' import { useConsentContext } from './ConsentContext.js' import { useIsMounted } from './useIsMounted.js' /** A button on the banner (first level). */ export type CookieBannerAction = 'accept' | 'reject' | 'settings' /** A button under the category list (settings view). */ export type CookieSettingsAction = 'accept' | 'back' | 'reject' | 'save' /** * Optional class overrides — for when tokens aren't enough, e.g. turning the * bottom bar into a centred modal. Each slot replaces its default classes * outright. Per-button slots (acceptButton, rejectButton…) fall back to * primaryButton / secondaryButton, so styling those two still restyles all. */ export type CookieBannerClassNames = { /** Button "Accept all" — falls back to primaryButton. */ acceptButton?: string /** Button row on the banner. */ actions?: string /** Settings view: button "Back" (closes the dialog in modal mode) — falls back to secondaryButton. */ backButton?: string /** Settings view: badge next to Necessary (CookieSettings → alwaysActiveLabel). */ badge?: string /** Banner inner wrapper (width / centring). */ body?: string /** Settings view: list of categories. */ categories?: string /** Settings view: one category row/card. */ category?: string /** Detailed window: the card around one category (row + cookie list). */ categoryCard?: string categoryDescription?: string categoryTitle?: string /** Settings dialog (modal mode): the ✕ button. */ closeButton?: string /** Banner: text + buttons row. */ content?: string /** Settings view: the switch track or the checkbox. */ control?: string /** Settings view: the switch knob (controls="switch"). */ controlThumb?: string /** Detailed window: one cookie in a category's list. */ cookie?: string /** Detailed window: list of cookies under a category. */ cookieList?: string /** Detailed window: provider / purpose / duration grid of one cookie. */ cookieMeta?: string /** Detailed window: cookie name. */ cookieName?: string /** Detailed window:
wrapping a category's cookie list. */ cookies?: string /** Detailed window: the that expands the cookie list. */ cookiesToggle?: string /** Settings dialog box (modal mode; detailed mode uses a wider default). */ dialog?: string /** Small label above the banner title (CookieSettings → eyebrow). */ eyebrow?: string /** Cookie icon next to the banner text and the settings title. */ icon?: string /** Privacy-policy link inside the message. */ link?: string message?: string /** Backdrop behind the settings dialog (modal mode). */ overlay?: string primaryButton?: string /** Button "Reject" — falls back to primaryButton with equalChoice, else secondaryButton. */ rejectButton?: string root?: string /** Settings view: button "Save preferences" — falls back to primaryButton. */ saveButton?: string secondaryButton?: string /** Settings view: button row under the categories. */ settingsActions?: string /** Button "Settings" on the banner — falls back to secondaryButton. */ settingsButton?: string /** Settings view: intro under the title (CookieSettings → settingsDescription). */ settingsDescription?: string settingsTitle?: string /** Banner: block with eyebrow, title and message. */ text?: string /** Banner heading (CookieSettings → title). */ title?: string } /** * Colours and radius come from CSS custom properties with built-in fallbacks, * so the banner looks right with no setup, and re-skinning per client means * declaring a few variables — no imports, no props, no specificity fights: * * ```css * :root { * --ipal-primary: #16a34a; * --ipal-radius: 1rem; * } * ``` * * Available: --ipal-surface, --ipal-border, --ipal-text, --ipal-text-strong, * --ipal-text-muted, --ipal-primary, --ipal-primary-hover, --ipal-primary-text, * --ipal-hover, --ipal-radius. Each has a `-dark` counterpart used under * Tailwind's `dark:` variant (e.g. --ipal-surface-dark). * * Layout stays in Tailwind: spacing and flow aren't things a brand changes, and * exposing them as tokens would mean re-inventing CSS one variable at a time. * For a different layout use classNames (every element has a slot). */ const surface = 'bg-[var(--ipal-surface,#fff)] dark:bg-[var(--ipal-surface-dark,#171717)]' const border = 'border-[var(--ipal-border,#e5e5e5)] dark:border-[var(--ipal-border-dark,#262626)]' const radius = 'rounded-[var(--ipal-radius,0.375rem)]' const accent = 'text-[var(--ipal-primary,#2563eb)]' const bodyText = 'text-[var(--ipal-text,#404040)] dark:text-[var(--ipal-text-dark,#d4d4d4)]' const strongText = 'text-[var(--ipal-text-strong,#171717)] dark:text-[var(--ipal-text-strong-dark,#f5f5f5)]' const mutedText = 'text-[var(--ipal-text-muted,#737373)] dark:text-[var(--ipal-text-muted-dark,#a3a3a3)]' const primaryBg = 'bg-[var(--ipal-primary,#2563eb)]' const defaults: Required = { acceptButton: '', actions: 'flex shrink-0 gap-2', backButton: '', badge: `shrink-0 rounded-full border px-2 py-0.5 text-xs font-medium ${border} ${accent}`, body: 'mx-auto max-w-4xl', categories: 'flex flex-col gap-3', category: 'flex items-start justify-between gap-4', categoryCard: `flex flex-col gap-3 ${radius} border p-4 ${border}`, categoryDescription: `text-xs ${mutedText}`, categoryTitle: `text-sm font-medium ${strongText}`, closeButton: `shrink-0 ${radius} p-1 ${mutedText} hover:bg-[var(--ipal-hover,#f5f5f5)] dark:hover:bg-[var(--ipal-hover-dark,#262626)]`, content: 'flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between', control: 'mt-1 h-4 w-4 shrink-0 accent-[var(--ipal-primary,#2563eb)]', controlThumb: 'pointer-events-none inline-block size-5 rounded-full bg-white shadow transition-transform translate-x-0.5 data-[checked=true]:translate-x-[1.375rem]', cookie: `flex flex-col gap-1 border-t pt-2 first:border-t-0 first:pt-0 ${border}`, cookieList: 'mt-3 flex flex-col gap-2', cookieMeta: `grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 text-xs ${mutedText}`, cookieName: `font-mono text-xs font-semibold ${strongText}`, cookies: 'group', cookiesToggle: `inline-flex cursor-pointer select-none items-center gap-1 text-xs font-medium ${accent} hover:underline [&::-webkit-details-marker]:hidden`, dialog: `relative max-h-[90vh] w-full max-w-2xl overflow-y-auto ${radius} border p-6 shadow-2xl ${surface} ${border}`, eyebrow: `mb-1 text-xs font-semibold uppercase tracking-wider ${accent}`, icon: `h-6 w-6 shrink-0 ${accent}`, link: `${accent} underline hover:no-underline`, message: `text-justify text-sm ${bodyText}`, overlay: 'fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4', primaryButton: `${radius} px-3 py-1.5 text-sm font-medium ${primaryBg} text-[var(--ipal-primary-text,#fff)] hover:bg-[var(--ipal-primary-hover,#1d4ed8)]`, rejectButton: '', root: `fixed inset-x-0 bottom-0 z-50 border-t p-4 shadow-lg ${surface} ${border}`, saveButton: '', secondaryButton: `${radius} px-3 py-1.5 text-sm font-medium text-[var(--ipal-text,#404040)] hover:bg-[var(--ipal-hover,#f5f5f5)] dark:text-[var(--ipal-text-dark,#e5e5e5)] dark:hover:bg-[var(--ipal-hover-dark,#262626)]`, settingsActions: 'flex justify-end gap-2', settingsButton: '', settingsDescription: `text-sm ${bodyText}`, settingsTitle: `flex items-center gap-2 text-base font-semibold ${strongText}`, text: 'flex items-start gap-3', title: `mb-1 text-base font-semibold ${strongText}`, } /** Default switch track (controls="switch") — the checkbox default doesn't fit a ) const toggle = (cat: ConsentCategory) => { if (cat === 'necessary') {return} setChoices((prev) => ({ ...prev, [cat]: !prev[cat] })) } const control = (cat: ConsentCategory) => { const checked = cat === 'necessary' ? true : choices[cat] if (cat === 'necessary' && texts.alwaysActiveLabel) { return {texts.alwaysActiveLabel} } if (controls === 'switch') { return ( ) } return ( toggle(cat)} type="checkbox" /> ) } const cookieList = (cat: ConsentCategory) => { const rows = texts.cookies?.filter((row) => row.category === cat) ?? [] const labels = texts.details if (!labels || rows.length === 0) {return null} return (
{labels.cookies} ({rows.length})
    {rows.map((row) => (
  • {row.name}
    {row.provider && ( <>
    {labels.provider}
    {row.provider}
    )} {row.purpose && ( <>
    {labels.purpose}
    {row.purpose}
    )} {row.duration && ( <>
    {labels.duration}
    {row.duration}
    )} {row.storage && row.storage !== 'cookie' && labels.storage && ( <>
    {labels.storage}
    {labels.storageTypes?.[row.storage] ?? row.storage}
    )}
  • ))}
) } const categoryRow = (cat: ConsentCategory) => ( <>
{control(cat)} ) const settingsView = (
{modal && ( )}
{texts.settingsDescription &&

{texts.settingsDescription}

}
    {CONSENT_CATEGORIES.map((cat) => detailed ? (
  • {categoryRow(cat)}
    {cookieList(cat)}
  • ) : (
  • {categoryRow(cat)}
  • ), )}
{settingsActions.map(button)}
) if (modal && showSettings) { return (
{ if (event.target === event.currentTarget) {setShowSettings(false)} }} // klik w tło zamyka dialog; klawiatura: Escape (listener wyżej) i przycisk ✕ role="presentation" >
{settingsView}
) } return (
{!showSettings ? (
{icon &&
{order.map(button)}
) : ( settingsView )}
) }