Repository navigation
Expand file tree
/
Copy pathConsentControls.tsx
More file actions
192 lines (177 loc) · 6.29 KB
/
Copy pathConsentControls.tsx
File metadata and controls
192 lines (177 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use client';
import PillButton, { accentFillSx } from '@components/pill-button/PillButton';
import { Box, Stack, Switch, type SxProps, type Theme, Typography } from '@mui/material';
import { colors } from '@styles/tokens';
import {
type ConsentChoices,
ESSENTIAL_ONLY,
PURPOSE_KEYS,
readConsent,
saveConsent,
} from '@util/consent/consentStore';
import { type ReactElement, type ReactNode, useId, useState } from 'react';
/** The words a visitor sees for one optional purpose. */
interface PurposeCopy {
/** Short name of the purpose. */
label: string;
/** What allowing it does, naming the vendors involved. */
description: string;
}
/** The copy for every optional purpose; keyed by purpose, so a purpose without copy fails to compile. */
const PURPOSE_COPY: Readonly<Record<keyof ConsentChoices, PurposeCopy>> = {
analytics: {
label: 'Analytics',
description:
'Google Firebase and Vercel Speed Insights measure visits and page speed, and Sentry reports errors in your browser. Never used for advertising.',
},
media: {
label: 'Embedded videos',
description: 'YouTube project previews, which set YouTube’s own cookies.',
},
offline: {
label: 'Offline access',
description: 'Saves pages on this device so the site loads faster and works offline.',
},
linkIcons: {
label: 'Link icons',
description: 'Shows each linked site’s own icon, fetched through DuckDuckGo without cookies.',
},
};
interface ConsentControlsProps {
/** Called after the choices are saved. */
onSaved?: () => void;
/** Further buttons shown in the same row as "Save choices". */
actions?: ReactNode;
}
/** The site's accent on a checked switch; the track uses the lighter accent, which stays visible on the surface. */
const switchSx: SxProps<Theme> = {
flexShrink: 0,
marginTop: -0.75,
'& .MuiSwitch-switchBase.Mui-checked': { color: colors.link },
'& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track': { backgroundColor: colors.accentHover, opacity: 1 },
};
interface ControlRowProps {
/** Short name of the purpose. */
label: string;
/** What the purpose does. */
description: string;
/** Prefix for the label, description, and control IDs. */
idPrefix: string;
/** Whether the row has an input its label should toggle when clicked. */
labelsInput?: boolean;
/** The control shown at the row's end. */
children: ReactNode;
}
/** Renders one purpose: its label and description, with its control aligned to the label's first line. */
function ControlRow({ label, description, idPrefix, labelsInput, children }: Readonly<ControlRowProps>): ReactElement {
return (
<Box component='li' sx={{ alignItems: 'flex-start', display: 'flex', gap: 2, justifyContent: 'space-between' }}>
{/* A real <label> when the row has an input, so clicking its name or description toggles the switch. */}
<Box
component={labelsInput ? 'label' : 'div'}
htmlFor={labelsInput ? `${idPrefix}-input` : undefined}
sx={{ cursor: labelsInput ? 'pointer' : 'default' }}
>
{/* A heavier, larger name over a smaller muted description, so each purpose reads as a title and its detail. */}
<Typography id={`${idPrefix}-label`} sx={{ fontSize: '1.0625rem', fontWeight: 600 }}>
{label}
</Typography>
<Typography
color='text.secondary'
id={`${idPrefix}-description`}
sx={{ fontSize: '0.8125rem', letterSpacing: 0, lineHeight: 1.5, marginTop: 0.25 }}
variant='body2'
>
{description}
</Typography>
</Box>
{children}
</Box>
);
}
/**
* Renders a switch per optional purpose, after an always-on row for essential processing, and saves the choices.
*
* Shared by the consent banner's Customize view and the policy dialog, so both edit the same stored choice.
*/
export default function ConsentControls({ actions, onSaved }: Readonly<ConsentControlsProps>): ReactElement {
const idPrefix = useId();
/** The choices being edited, starting from what is stored. */
const [draft, setDraft] = useState<ConsentChoices>(() => ({ ...(readConsent() ?? ESSENTIAL_ONLY) }));
/** Whether the current draft has been saved, shown as a status message. */
const [saved, setSaved] = useState(false);
const handleSave = () => {
saveConsent(draft);
setSaved(true);
onSaved?.();
};
return (
<Stack spacing={2}>
<Stack component='ul' spacing={2} sx={{ listStyle: 'none', margin: 0, padding: 0 }}>
<ControlRow
description='Hosting logs, error reports, and the cookie remembering this choice.'
idPrefix={`${idPrefix}-essential`}
label='Essential'
>
<Typography color='text.secondary' sx={{ flexShrink: 0, paddingTop: 0.25 }} variant='body2'>
Always on
</Typography>
</ControlRow>
{PURPOSE_KEYS.map((key) => (
<ControlRow
description={PURPOSE_COPY[key].description}
idPrefix={`${idPrefix}-${key}`}
key={key}
label={PURPOSE_COPY[key].label}
labelsInput
>
<Switch
checked={draft[key]}
onChange={(event) => {
const { checked } = event.target;
setSaved(false);
setDraft((current) => ({ ...current, [key]: checked }));
}}
slotProps={{
input: {
'aria-describedby': `${idPrefix}-${key}-description`,
// The name is the label alone; the <label> also wraps the description, which describes it instead.
'aria-labelledby': `${idPrefix}-${key}-label`,
id: `${idPrefix}-${key}-input`,
},
}}
sx={switchSx}
/>
</ControlRow>
))}
</Stack>
{/* "Save choices" sits at the start edge and any extra action at the end edge, even after wrapping onto its own line. */}
<Stack
direction='row'
sx={{
alignItems: 'center',
columnGap: 2,
flexWrap: 'wrap',
justifyContent: 'space-between',
rowGap: 1,
}}
>
<Stack direction='row' sx={{ alignItems: 'center' }}>
<PillButton onClick={handleSave} size='small' sx={accentFillSx}>
Save choices
</PillButton>
{/* Spaced only while it has text, so the empty status takes no room from the action beside it. */}
<Typography
color='text.secondary'
role='status'
sx={{ '&:not(:empty)': { marginInlineStart: 2 } }}
variant='body2'
>
{saved ? 'Choices saved.' : ''}
</Typography>
</Stack>
{actions ? <Box sx={{ marginInlineStart: 'auto' }}>{actions}</Box> : null}
</Stack>
</Stack>
);
}