themeAndStyleProvider.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {Fragment, useEffect} from 'react';
  2. import {createPortal} from 'react-dom';
  3. import {cache} from '@emotion/css'; // eslint-disable-line @emotion/no-vanilla
  4. import {CacheProvider, ThemeProvider} from '@emotion/react'; // This is needed to set "speedy" = false (for percy)
  5. import {loadPreferencesState} from 'sentry/actionCreators/preferences';
  6. import ConfigStore from 'sentry/stores/configStore';
  7. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  8. import GlobalStyles from 'sentry/styles/global';
  9. import {darkTheme, lightTheme} from 'sentry/utils/theme';
  10. type Props = {
  11. children: React.ReactNode;
  12. };
  13. /**
  14. * Wraps children with emotions ThemeProvider reactively set a theme.
  15. *
  16. * Also injects the sentry GlobalStyles .
  17. */
  18. function ThemeAndStyleProvider({children}: Props) {
  19. useEffect(() => void loadPreferencesState(), []);
  20. const config = useLegacyStore(ConfigStore);
  21. const theme = config.theme === 'dark' ? darkTheme : lightTheme;
  22. return (
  23. <ThemeProvider theme={theme}>
  24. <GlobalStyles isDark={config.theme === 'dark'} theme={theme} />
  25. <CacheProvider value={cache}>{children}</CacheProvider>
  26. {createPortal(
  27. <Fragment>
  28. <meta name="color-scheme" content={config.theme} />
  29. <meta name="theme-color" content={theme.sidebar.background} />
  30. </Fragment>,
  31. document.head
  32. )}
  33. </ThemeProvider>
  34. );
  35. }
  36. export default ThemeAndStyleProvider;