File tree Expand file tree Collapse file tree 4 files changed +42
-5
lines changed
automatic-dark-mode/react
class-high-contrast-mode/react Expand file tree Collapse file tree 4 files changed +42
-5
lines changed Original file line number Diff line number Diff line change @@ -33,8 +33,16 @@ function Example() {
3333
3434 toggleDarkTheme (prefersDark .matches );
3535
36+ const setDarkThemeFromMediaQuery = (mediaQuery : MediaQueryListEvent ) => {
37+ toggleDarkTheme (mediaQuery .matches );
38+ };
39+
3640 // Listen for changes to the prefers-color-scheme media query
37- prefersDark .addEventListener (' change' , (mediaQuery ) => toggleDarkTheme (mediaQuery .matches ));
41+ prefersDark .addEventListener (' change' , setDarkThemeFromMediaQuery );
42+
43+ return () => {
44+ prefersDark .removeEventListener (' change' , setDarkThemeFromMediaQuery );
45+ };
3846 }, []);
3947
4048 return (
Original file line number Diff line number Diff line change @@ -49,8 +49,16 @@ function Example() {
4949 // value of the prefers-color-scheme media query
5050 initializeDarkTheme (prefersDark .matches );
5151
52+ const setDarkThemeFromMediaQuery = (mediaQuery : MediaQueryListEvent ) => {
53+ initializeDarkTheme (mediaQuery .matches );
54+ };
55+
5256 // Listen for changes to the prefers-color-scheme media query
53- prefersDark .addEventListener (' change' , (mediaQuery ) => initializeDarkTheme (mediaQuery .matches ));
57+ prefersDark .addEventListener (' change' , setDarkThemeFromMediaQuery );
58+
59+ return () => {
60+ prefersDark .removeEventListener (' change' , setDarkThemeFromMediaQuery );
61+ };
5462 }, []);
5563
5664 return (
Original file line number Diff line number Diff line change @@ -49,8 +49,16 @@ function Example() {
4949 // value of the prefers-color-scheme media query
5050 initializeDarkPalette (prefersDark .matches );
5151
52+ const setDarkPaletteFromMediaQuery = (mediaQuery : MediaQueryListEvent ) => {
53+ initializeDarkPalette (mediaQuery .matches );
54+ };
55+
5256 // Listen for changes to the prefers-color-scheme media query
53- prefersDark .addEventListener (' change' , (mediaQuery ) => initializeDarkPalette (mediaQuery .matches ));
57+ prefersDark .addEventListener (' change' , setDarkPaletteFromMediaQuery );
58+
59+ return () => {
60+ prefersDark .removeEventListener (' change' , setDarkPaletteFromMediaQuery );
61+ };
5462 }, []);
5563
5664 return (
Original file line number Diff line number Diff line change @@ -66,9 +66,22 @@ function Example() {
6666 initializeDarkPalette (prefersDark .matches );
6767 initializeHighContrastPalette (prefersHighContrast .matches );
6868
69+ const setDarkPaletteFromMediaQuery = (mediaQuery : MediaQueryListEvent ) => {
70+ initializeDarkPalette (mediaQuery .matches );
71+ };
72+
73+ const setHighContrastPaletteFromMediaQuery = (mediaQuery : MediaQueryListEvent ) => {
74+ initializeHighContrastPalette (mediaQuery .matches );
75+ };
76+
6977 // Listen for changes to the media queries
70- prefersDark .addEventListener (' change' , (mediaQuery ) => initializeDarkPalette (mediaQuery .matches ));
71- prefersHighContrast .addEventListener (' change' , (mediaQuery ) => initializeHighContrastPalette (mediaQuery .matches ));
78+ prefersDark .addEventListener (' change' , setDarkPaletteFromMediaQuery );
79+ prefersHighContrast .addEventListener (' change' , setHighContrastPaletteFromMediaQuery );
80+
81+ return () => {
82+ prefersDark .removeEventListener (' change' , setDarkPaletteFromMediaQuery );
83+ prefersHighContrast .removeEventListener (' change' , setHighContrastPaletteFromMediaQuery );
84+ };
7285 }, []);
7386
7487 return (
You can’t perform that action at this time.
0 commit comments