-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathuseTheme.js
More file actions
175 lines (149 loc) · 4.32 KB
/
useTheme.js
File metadata and controls
175 lines (149 loc) · 4.32 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
import { useState, useEffect } from 'react';
const COLOR_MODES = {
SYSTEM: 'system',
LIGHT: 'light',
DARK: 'dark'
};
const HIGH_CONTRAST_MODES = {
SYSTEM: 'high-contrast-system',
ON: 'high-contrast-on',
OFF: 'high-contrast-off'
};
export const THEME_TYPES = {
COLOR: 'color',
HIGH_CONTRAST: 'high-contrast'
};
class ThemeManager {
constructor({ storageKey, modes, defaultMode, cssClass, classEnabledMode, mediaQueryString }) {
this.storageKey = storageKey;
this.modes = modes;
this.defaultMode = defaultMode;
this.cssClass = cssClass;
this.classEnabledMode = classEnabledMode;
this.mediaQueryString = mediaQueryString;
this.isBrowser = typeof window !== 'undefined' && window.matchMedia && window.localStorage;
}
getMediaQuery() {
if (!this.isBrowser) {
return;
}
return window.matchMedia(this.mediaQueryString);
}
getStoredValue() {
if (!this.isBrowser) {
return;
}
const storedValue = localStorage.getItem(this.storageKey);
return storedValue || this.defaultMode;
}
setStoredValue(value) {
if (!this.isBrowser) {
return;
}
localStorage.setItem(this.storageKey, value);
}
resolve() {
if (!this.isBrowser) {
return this.defaultMode;
}
if (window.matchMedia(this.mediaQueryString).matches) {
return this.classEnabledMode;
}
return this.defaultMode;
}
addClass() {
if (!this.isBrowser) {
return;
}
document.querySelector('html').classList.add(this.cssClass);
}
removeClass() {
if (!this.isBrowser) {
return;
}
document.querySelector('html').classList.remove(this.cssClass);
}
updateClass(mode) {
if (!this.isBrowser) {
return;
}
if (mode === this.modes.SYSTEM) {
if (this.resolve() === this.classEnabledMode) {
this.addClass();
} else {
this.removeClass();
}
} else {
if (mode === this.classEnabledMode) {
this.addClass();
} else {
this.removeClass();
}
}
}
}
const themeRegistry = new Map();
const registerThemeManager = (themeType, manager) => {
themeRegistry.set(themeType, manager);
};
const colorThemeManager = new ThemeManager({
storageKey: 'theme-preference',
modes: COLOR_MODES,
defaultMode: COLOR_MODES.LIGHT,
cssClass: 'pf-v6-theme-dark',
classEnabledMode: COLOR_MODES.DARK,
mediaQueryString: '(prefers-color-scheme: dark)'
});
const highContrastThemeManager = new ThemeManager({
storageKey: 'high-contrast-preference',
modes: HIGH_CONTRAST_MODES,
defaultMode: HIGH_CONTRAST_MODES.OFF,
cssClass: 'pf-v6-theme-high-contrast',
classEnabledMode: HIGH_CONTRAST_MODES.ON,
mediaQueryString: '(prefers-contrast: more)'
});
registerThemeManager(THEME_TYPES.COLOR, colorThemeManager);
registerThemeManager(THEME_TYPES.HIGH_CONTRAST, highContrastThemeManager);
/**
* Unified theme hook that accepts a theme type parameter
* @param {string} themeType - The type of theme to manage (THEME_TYPES.COLOR, THEME_TYPES.HIGH_CONTRAST, instantiate and register new themes above as needed)
* @returns {Object} Theme state and controls specific to the theme type
*/
export const useTheme = (themeType) => {
if (!themeType) {
throw new Error('useTheme requires a theme type parameter. Use THEME_TYPES.COLOR or THEME_TYPES.HIGH_CONTRAST');
}
const theme = themeRegistry.get(themeType);
if (!theme) {
throw new Error(`Theme manager not found for theme type: ${themeType}`);
}
const [mode, setMode] = useState(theme.defaultMode);
const [resolvedTheme, setResolvedTheme] = useState(theme.resolve());
useEffect(() => {
theme.setStoredValue(mode);
theme.updateClass(mode);
}, [theme, mode, resolvedTheme]);
const handlePreferenceChange = () => {
setResolvedTheme(theme.resolve());
};
const mediaQuery = theme.getMediaQuery();
useEffect(() => {
if (mediaQuery.addEventListener) {
mediaQuery.addEventListener('change', handlePreferenceChange);
return () => {
mediaQuery.removeEventListener('change', handlePreferenceChange);
};
} else if (mediaQuery.addListener) {
mediaQuery.addListener(handlePreferenceChange);
return () => {
mediaQuery.removeListener(handlePreferenceChange);
};
}
}, [mediaQuery]);
return {
mode,
setMode,
resolvedTheme,
modes: theme.modes
};
};