|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import styled, { keyframes } from 'styled-components' |
| 3 | +import { Link } from 'react-router-dom' |
| 4 | + |
| 5 | +type ConsentValue = 'granted' | 'denied' | null |
| 6 | + |
| 7 | +const STORAGE_KEY = 'cookie_consent' |
| 8 | + |
| 9 | +function updateGoogleConsent(value: 'granted' | 'denied') { |
| 10 | + if (typeof window.gtag === 'function') { |
| 11 | + window.gtag('consent', 'update', { |
| 12 | + analytics_storage: value, |
| 13 | + ad_storage: value, |
| 14 | + ad_user_data: value, |
| 15 | + ad_personalization: value, |
| 16 | + }) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const CookieBanner = () => { |
| 21 | + const [consent, setConsent] = useState<ConsentValue>(null) |
| 22 | + const [visible, setVisible] = useState(false) |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + const stored = localStorage.getItem(STORAGE_KEY) as ConsentValue |
| 26 | + if (stored === 'granted' || stored === 'denied') { |
| 27 | + // Re-apply saved choice on every page load so Consent Mode is always in sync |
| 28 | + updateGoogleConsent(stored) |
| 29 | + setConsent(stored) |
| 30 | + } else { |
| 31 | + // No prior choice — show banner |
| 32 | + setVisible(true) |
| 33 | + } |
| 34 | + }, []) |
| 35 | + |
| 36 | + const handleAccept = () => { |
| 37 | + localStorage.setItem(STORAGE_KEY, 'granted') |
| 38 | + updateGoogleConsent('granted') |
| 39 | + setConsent('granted') |
| 40 | + setVisible(false) |
| 41 | + } |
| 42 | + |
| 43 | + const handleDecline = () => { |
| 44 | + localStorage.setItem(STORAGE_KEY, 'denied') |
| 45 | + updateGoogleConsent('denied') |
| 46 | + setConsent('denied') |
| 47 | + setVisible(false) |
| 48 | + } |
| 49 | + |
| 50 | + if (!visible) return null |
| 51 | + |
| 52 | + return ( |
| 53 | + <Banner role="dialog" aria-label="Cookie consent"> |
| 54 | + <Text> |
| 55 | + We use cookies for analytics and advertising. See our{' '} |
| 56 | + <PolicyLink to="/privacy-policy">Privacy Policy</PolicyLink>. |
| 57 | + </Text> |
| 58 | + <Buttons> |
| 59 | + <DeclineButton onClick={handleDecline}>Essential only</DeclineButton> |
| 60 | + <AcceptButton onClick={handleAccept}>Accept all</AcceptButton> |
| 61 | + </Buttons> |
| 62 | + </Banner> |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +const slideUp = keyframes` |
| 67 | + from { transform: translateY(100%); opacity: 0; } |
| 68 | + to { transform: translateY(0); opacity: 1; } |
| 69 | +` |
| 70 | + |
| 71 | +const Banner = styled.div` |
| 72 | + position: fixed; |
| 73 | + bottom: 0; |
| 74 | + left: 0; |
| 75 | + right: 0; |
| 76 | + z-index: 1000; |
| 77 | + background: rgba(15, 15, 15, 0.97); |
| 78 | + border-top: 1px solid rgba(255, 215, 0, 0.25); |
| 79 | + backdrop-filter: blur(10px); |
| 80 | + padding: 1rem 2rem; |
| 81 | + display: flex; |
| 82 | + align-items: center; |
| 83 | + justify-content: space-between; |
| 84 | + gap: 1.5rem; |
| 85 | + animation: ${slideUp} 0.35s ease-out; |
| 86 | +
|
| 87 | + @media (max-width: 600px) { |
| 88 | + flex-direction: column; |
| 89 | + align-items: flex-start; |
| 90 | + padding: 1rem 1.25rem; |
| 91 | + } |
| 92 | +` |
| 93 | + |
| 94 | +const Text = styled.p` |
| 95 | + margin: 0; |
| 96 | + color: rgba(255, 255, 255, 0.75); |
| 97 | + font-size: 0.9rem; |
| 98 | + line-height: 1.5; |
| 99 | + flex: 1; |
| 100 | +` |
| 101 | + |
| 102 | +const PolicyLink = styled(Link)` |
| 103 | + color: #ffd700; |
| 104 | + text-decoration: underline; |
| 105 | + &:hover { |
| 106 | + color: #ffe566; |
| 107 | + } |
| 108 | +` |
| 109 | + |
| 110 | +const Buttons = styled.div` |
| 111 | + display: flex; |
| 112 | + gap: 0.75rem; |
| 113 | + flex-shrink: 0; |
| 114 | +
|
| 115 | + @media (max-width: 600px) { |
| 116 | + width: 100%; |
| 117 | + justify-content: flex-end; |
| 118 | + } |
| 119 | +` |
| 120 | + |
| 121 | +const BaseButton = styled.button` |
| 122 | + padding: 0.5rem 1.25rem; |
| 123 | + border-radius: 6px; |
| 124 | + font-size: 0.875rem; |
| 125 | + font-weight: 600; |
| 126 | + cursor: pointer; |
| 127 | + transition: all 0.2s ease; |
| 128 | + border: none; |
| 129 | +` |
| 130 | + |
| 131 | +const DeclineButton = styled(BaseButton)` |
| 132 | + background: transparent; |
| 133 | + border: 1px solid rgba(255, 255, 255, 0.25); |
| 134 | + color: rgba(255, 255, 255, 0.65); |
| 135 | + &:hover { |
| 136 | + border-color: rgba(255, 255, 255, 0.5); |
| 137 | + color: rgba(255, 255, 255, 0.9); |
| 138 | + } |
| 139 | +` |
| 140 | + |
| 141 | +const AcceptButton = styled(BaseButton)` |
| 142 | + background: #ffd700; |
| 143 | + color: #111; |
| 144 | + &:hover { |
| 145 | + background: #ffe566; |
| 146 | + } |
| 147 | +` |
| 148 | + |
| 149 | +export default CookieBanner |
0 commit comments