Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 6x 6x 2x 2x 2x 2x 2x 2x 2x | import React, { useSyncExternalStore } from 'react';
export enum ColorScheme {
Dark = 'dark',
Light = 'light',
NoPreference = 'no-preference',
}
function getSnapshot() {
const matcher = window.matchMedia('(prefers-color-scheme: dark)').matches;
return matcher ? ColorScheme.Dark : ColorScheme.Light;
}
function getServerSnapshot() {
return ColorScheme.NoPreference;
}
function subscribe(callback: () => void) {
const matcher = window.matchMedia('(prefers-color-scheme: dark)');
matcher.addEventListener('change', callback);
return () => {
Eif (matcher) {
matcher.removeEventListener('change', callback);
}
};
}
export function useColorScheme() {
const colorscheme = useSyncExternalStore<ColorScheme>(subscribe, getSnapshot, getServerSnapshot);
return colorscheme;
}
|