MCPcopy Create free account
hub / github.com/apitable/apitable / ThemeWrapper

Function ThemeWrapper

packages/datasheet/src/theme_wrapper.tsx:28–81  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

26import { getEnvVariables } from 'pc/utils/env';
27
28const ThemeWrapper: React.FC<React.PropsWithChildren<unknown>> = (props) => {
29 const [theme, setTheme] = useLocalStorageState<ThemeName>('theme', {
30 defaultValue: (getEnvVariables().SYSTEM_CONFIGURATION_DEFAULT_THEME as ThemeName) || ThemeName.Light,
31 });
32 const [systemTheme, setSystemTheme] = useLocalStorageState<SystemTheme>('systemTheme', { defaultValue: SystemTheme.Close });
33 const dispatch = useDispatch();
34 const cacheTheme = useAppSelector(Selectors.getTheme);
35
36 useEffect(() => {
37 const curTheme = theme || ThemeName.Light;
38 const html = document.querySelector('html');
39 html?.setAttribute('data-theme', curTheme);
40 dispatch(StoreActions.setTheme(curTheme));
41 }, [dispatch, theme]);
42
43 useEffect(() => {
44 const interval = setInterval(() => {
45 let newSystemTheme: null | string = null;
46 try {
47 newSystemTheme = localStorage.getItem('systemTheme');
48 } catch (e) {}
49 if (newSystemTheme && JSON.parse(newSystemTheme) !== systemTheme) {
50 setSystemTheme(systemTheme === SystemTheme.Close ? SystemTheme.Open : SystemTheme.Close);
51 }
52 }, 5 * 1000);
53 return () => {
54 clearInterval(interval);
55 };
56 }, [setSystemTheme, systemTheme]);
57
58 useEffect(() => {
59 if (!systemTheme || systemTheme === SystemTheme.Close) return;
60 const themeMedia = window.matchMedia('(prefers-color-scheme: light)');
61 // Reset the theme when the systemTheme state changes to open
62 setTheme(themeMedia.matches ? ThemeName.Light : ThemeName.Dark);
63 const listener = (e: MediaQueryListEvent) => {
64 if (e.matches) {
65 setTheme(ThemeName.Light);
66 } else {
67 setTheme(ThemeName.Dark);
68 }
69 };
70 themeMedia.addEventListener('change', listener);
71 return () => {
72 themeMedia.removeEventListener('change', listener);
73 };
74 }, [setTheme, systemTheme]);
75
76 useKeyPress(['shift.meta.l'], () => {
77 theme === ThemeName.Light ? setTheme(ThemeName.Dark) : setTheme(ThemeName.Light);
78 });
79
80 return <ThemeProvider theme={cacheTheme}>{props.children}</ThemeProvider>;
81};
82
83export default ThemeWrapper;

Callers

nothing calls this directly

Calls 8

getEnvVariablesFunction · 0.90
useDispatchFunction · 0.85
dispatchFunction · 0.85
setThemeFunction · 0.85
getItemMethod · 0.80
parseMethod · 0.65
addEventListenerMethod · 0.65
removeEventListenerMethod · 0.65

Tested by

no test coverage detected