154 lines
6.3 KiB
TypeScript
154 lines
6.3 KiB
TypeScript
'use client'
|
|
import { Cookie } from 'lucide-react'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { CONSENT_CATEGORIES, type ConsentCategory } from './categories.js'
|
|
import { useConsentContext } from './ConsentContext.js'
|
|
import { useIsMounted } from './useIsMounted.js'
|
|
|
|
/**
|
|
* Optional class overrides — for when tokens aren't enough, e.g. turning the
|
|
* bottom bar into a centred modal. Replaces the slot's classes outright.
|
|
*/
|
|
export type CookieBannerClassNames = {
|
|
primaryButton?: string
|
|
root?: string
|
|
secondaryButton?: 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.
|
|
*/
|
|
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 defaults = {
|
|
primaryButton: `${radius} px-3 py-1.5 text-sm font-medium bg-[var(--ipal-primary,#2563eb)] text-[var(--ipal-primary-text,#fff)] hover:bg-[var(--ipal-primary-hover,#1d4ed8)]`,
|
|
root: `fixed inset-x-0 bottom-0 z-50 border-t p-4 shadow-lg ${surface} ${border}`,
|
|
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)]`,
|
|
}
|
|
|
|
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)]'
|
|
|
|
export function CookieBanner({ classNames }: { classNames?: CookieBannerClassNames } = {}) {
|
|
const isMounted = useIsMounted()
|
|
const { acceptAll, decided, rejectAll, savePreferences, state, texts } = useConsentContext()
|
|
const [showSettings, setShowSettings] = useState(false)
|
|
const [choices, setChoices] = useState<Record<ConsentCategory, boolean>>(state)
|
|
|
|
useEffect(() => {
|
|
setChoices(state)
|
|
}, [state])
|
|
|
|
// Render nothing until mounted — avoids the React 19 hydration mismatch that
|
|
// projects used to work around with a setTimeout ConsentWrapper. No wrapper
|
|
// needed now.
|
|
if (!isMounted) {return null}
|
|
if (decided) {return null}
|
|
|
|
const toggle = (cat: ConsentCategory) => {
|
|
if (cat === 'necessary') {return}
|
|
setChoices((prev) => ({ ...prev, [cat]: !prev[cat] }))
|
|
}
|
|
|
|
const rootClass = classNames?.root ?? defaults.root
|
|
const primaryClass = classNames?.primaryButton ?? defaults.primaryButton
|
|
const secondaryClass = classNames?.secondaryButton ?? defaults.secondaryButton
|
|
|
|
return (
|
|
<div aria-label="Cookie consent" className={rootClass} data-ipal="banner" role="dialog">
|
|
<div className="mx-auto max-w-4xl">
|
|
{!showSettings ? (
|
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
|
<div className="flex items-start gap-3">
|
|
<Cookie aria-hidden="true" className={`h-6 w-6 shrink-0 ${accent}`} />
|
|
<p className={`text-justify text-sm ${bodyText}`}>
|
|
{texts.message}
|
|
{texts.privacyLink && (
|
|
<>
|
|
{' '}
|
|
<a
|
|
className={`${accent} underline hover:no-underline`}
|
|
href={texts.privacyLink.href}
|
|
>
|
|
{texts.privacyLink.label}
|
|
</a>
|
|
</>
|
|
)}
|
|
</p>
|
|
</div>
|
|
<div className="flex shrink-0 gap-2">
|
|
<button className={secondaryClass} onClick={() => setShowSettings(true)}>
|
|
{texts.buttons.settings}
|
|
</button>
|
|
<button className={secondaryClass} onClick={rejectAll}>
|
|
{texts.buttons.reject}
|
|
</button>
|
|
<button className={primaryClass} onClick={acceptAll}>
|
|
{texts.buttons.acceptAll}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col gap-4">
|
|
<h2 className={`flex items-center gap-2 text-base font-semibold ${strongText}`}>
|
|
<Cookie aria-hidden="true" className={`h-5 w-5 ${accent}`} />
|
|
{texts.settingsTitle}
|
|
</h2>
|
|
<ul className="flex flex-col gap-3">
|
|
{CONSENT_CATEGORIES.map((cat) => (
|
|
<li className="flex items-start justify-between gap-4" key={cat}>
|
|
<div>
|
|
<p className={`text-sm font-medium ${strongText}`}>
|
|
{texts.categories[cat].title}
|
|
</p>
|
|
<p className={`text-xs ${mutedText}`}>{texts.categories[cat].description}</p>
|
|
</div>
|
|
<input
|
|
checked={cat === 'necessary' ? true : choices[cat]}
|
|
className="mt-1 h-4 w-4 shrink-0 accent-[var(--ipal-primary,#2563eb)]"
|
|
disabled={cat === 'necessary'}
|
|
onChange={() => toggle(cat)}
|
|
type="checkbox"
|
|
/>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<div className="flex justify-end gap-2">
|
|
<button className={secondaryClass} onClick={() => setShowSettings(false)}>
|
|
{texts.buttons.back}
|
|
</button>
|
|
<button className={primaryClass} onClick={() => savePreferences(choices)}>
|
|
{texts.buttons.save}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|