Repository navigation
Expand file tree
/
Copy pathaccessibilitySchema.ts
More file actions
91 lines (85 loc) · 2.89 KB
/
Copy pathaccessibilitySchema.ts
File metadata and controls
91 lines (85 loc) · 2.89 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
import { z } from 'zod';
export const accessibilityPresetIdSchema = z.enum([
'custom',
'motor',
'lowVision',
'cognitive',
'screenReader',
]);
export const accessibilitySettingsSchema = z.object({
highContrast: z.boolean(),
reducedMotion: z.boolean(),
// QNBS-v3 (#332/D4): manual opt-in for users who want backdrop-blur GPU cost off without relying
// on the OS `prefers-reduced-transparency` preference (which the existing CSS block already covers).
reducedTransparency: z.boolean(),
largeText: z.boolean(),
screenReader: z.boolean(),
focusIndicators: z.boolean(),
colorBlindMode: z.enum(['none', 'protanopia', 'deuteranopia', 'tritanopia']),
presetId: accessibilityPresetIdSchema,
liveRegionVerbosity: z.enum(['minimal', 'normal', 'verbose']),
comfortableTargets: z.boolean(),
});
export type NormalizedAccessibilitySettings = z.infer<typeof accessibilitySettingsSchema>;
export const DEFAULT_NORMALIZED_ACCESSIBILITY: NormalizedAccessibilitySettings = {
highContrast: false,
reducedMotion: false,
reducedTransparency: false,
largeText: false,
screenReader: false,
focusIndicators: true,
colorBlindMode: 'none',
presetId: 'custom',
liveRegionVerbosity: 'normal',
comfortableTargets: false,
};
/** Merge unknown/partial persisted accessibility with defaults and enforce schema (hydration-safe). */
export function normalizeAccessibilitySettings(input: unknown): NormalizedAccessibilitySettings {
const merged: Record<string, unknown> = {
...DEFAULT_NORMALIZED_ACCESSIBILITY,
...(typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {}),
};
const parsed = accessibilitySettingsSchema.safeParse(merged);
return parsed.success ? parsed.data : DEFAULT_NORMALIZED_ACCESSIBILITY;
}
export function accessibilityPresetDefaults(
preset: Exclude<NormalizedAccessibilitySettings['presetId'], 'custom'>,
): NormalizedAccessibilitySettings {
const base = { ...DEFAULT_NORMALIZED_ACCESSIBILITY };
switch (preset) {
case 'motor':
return normalizeAccessibilitySettings({
...base,
presetId: 'motor',
largeText: true,
focusIndicators: true,
comfortableTargets: true,
});
case 'lowVision':
return normalizeAccessibilitySettings({
...base,
presetId: 'lowVision',
highContrast: true,
largeText: true,
focusIndicators: true,
});
case 'cognitive':
return normalizeAccessibilitySettings({
...base,
presetId: 'cognitive',
reducedMotion: true,
focusIndicators: true,
liveRegionVerbosity: 'minimal',
});
case 'screenReader':
return normalizeAccessibilitySettings({
...base,
presetId: 'screenReader',
screenReader: true,
focusIndicators: true,
liveRegionVerbosity: 'verbose',
});
default:
return DEFAULT_NORMALIZED_ACCESSIBILITY;
}
}