import { useId, useRef, useState, useSyncExternalStore } from 'react'; import { analytics } from './analytics.mjs'; import './consent.css'; export function ConsentControls({ className = '' }: { className?: string }) { const consent = useSyncExternalStore(analytics.subscribe, analytics.getConsent, () => 'unset'); const status = useSyncExternalStore(analytics.subscribe, analytics.getStatus, () => 'unconfigured'); const [expanded, setExpanded] = useState(false); const [dismissed, setDismissed] = useState(false); const settings = useRef(null); const headingId = useId(); const controlsId = useId(); const show = expanded || (!dismissed && consent === 'unset' && status !== 'unconfigured'); function close() { setExpanded(false); setDismissed(true); // An automatic first-visit prompt must not scroll the visitor to the footer. if (expanded) settings.current?.focus({ preventScroll: true }); else document.getElementById('main-content')?.focus({ preventScroll: true }); } function choose(choice: 'granted' | 'denied') { analytics.setConsent(choice); close(); } return
{ if (event.key === 'Escape' && show) { event.preventDefault(); event.stopPropagation(); close(); } }}>
; }