aboutsummaryrefslogtreecommitdiff
path: root/src/console/colorscheme/providers.tsx
blob: 810c8e0f0d93a979bd6c35cb3f86b55ea9f22ab3 (plain) (blame)
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
import React from "react";
import ColorScheme from "../../shared/ColorScheme";
import { DarkTheme, LightTheme } from "./theme";
import { ColorSchemeContext, ColorSchemeUpdateContext } from "./contexts";
import { ThemeProvider } from "styled-components";

export const ColorSchemeProvider: React.FC = ({ children }) => {
  const [colorscheme, setColorScheme] = React.useState(ColorScheme.System);
  const theme = React.useMemo(() => {
    if (colorscheme === ColorScheme.System) {
      if (
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches
      ) {
        return DarkTheme;
      }
    } else if (colorscheme === ColorScheme.Dark) {
      return DarkTheme;
    }
    return LightTheme;
  }, [colorscheme]);

  return (
    <ColorSchemeContext.Provider value={colorscheme}>
      <ColorSchemeUpdateContext.Provider value={setColorScheme}>
        <ThemeProvider theme={theme}>{children}</ThemeProvider>
      </ColorSchemeUpdateContext.Provider>
    </ColorSchemeContext.Provider>
  );
};
export default ColorSchemeProvider;