MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / useColorScheme

Function useColorScheme

apps/start/src/components/facehash/facehash.tsx:184–210  ·  view source on GitHub ↗

* Hook to detect system color scheme preference

(colorScheme: ColorScheme)

Source from the content-addressed store, hash-verified

182 * Hook to detect system color scheme preference
183 */
184function useColorScheme(colorScheme: ColorScheme): 'light' | 'dark' {
185 const [systemScheme, setSystemScheme] = React.useState<'light' | 'dark'>(
186 () => {
187 if (typeof window === 'undefined') return 'light';
188 return window.matchMedia('(prefers-color-scheme: dark)').matches
189 ? 'dark'
190 : 'light';
191 },
192 );
193
194 React.useEffect(() => {
195 if (colorScheme !== 'auto') return;
196
197 const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
198 const handler = (e: MediaQueryListEvent) => {
199 setSystemScheme(e.matches ? 'dark' : 'light');
200 };
201
202 mediaQuery.addEventListener('change', handler);
203 return () => mediaQuery.removeEventListener('change', handler);
204 }, [colorScheme]);
205
206 if (colorScheme === 'auto') {
207 return systemScheme;
208 }
209 return colorScheme;
210}
211
212export const Facehash = React.forwardRef<HTMLDivElement, FacehashProps>(
213 (

Callers 1

facehash.tsxFile · 0.85

Calls 1

addEventListenerMethod · 0.80

Tested by

no test coverage detected