diff options
author | Michael Muré <batolettre@gmail.com> | 2021-03-07 22:09:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-07 22:09:41 +0100 |
commit | 501e1678b2af280300a4c35d762c54379e3142e4 (patch) | |
tree | 8577cf83b4d661184b9e38b17fee9652daf4b153 /webui/src/components/Themer.tsx | |
parent | 2a21b02af157275e20c4ce6d308b45f9a3d7ebaf (diff) | |
parent | c2c5c40e752663e238bdd29c54d3a5634ba9615c (diff) | |
download | git-bug-501e1678b2af280300a4c35d762c54379e3142e4.tar.gz |
Merge pull request #588 from GlancingMind/upstream-dark-colorscheme
Dark Colorscheme
Diffstat (limited to 'webui/src/components/Themer.tsx')
-rw-r--r-- | webui/src/components/Themer.tsx | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/webui/src/components/Themer.tsx b/webui/src/components/Themer.tsx new file mode 100644 index 00000000..b4877974 --- /dev/null +++ b/webui/src/components/Themer.tsx @@ -0,0 +1,65 @@ +import React, { createContext, useContext, useState } from 'react'; + +import { fade, ThemeProvider } from '@material-ui/core'; +import IconButton from '@material-ui/core/IconButton/IconButton'; +import Tooltip from '@material-ui/core/Tooltip/Tooltip'; +import { Theme } from '@material-ui/core/styles'; +import { NightsStayRounded, WbSunnyRounded } from '@material-ui/icons'; +import { makeStyles } from '@material-ui/styles'; + +const ThemeContext = createContext({ + toggleMode: () => {}, + mode: '', +}); + +const useStyles = makeStyles((theme: Theme) => ({ + iconButton: { + color: fade(theme.palette.primary.contrastText, 0.5), + }, +})); + +const LightSwitch = () => { + const { mode, toggleMode } = useContext(ThemeContext); + const nextMode = mode === 'light' ? 'dark' : 'light'; + const description = `Switch to ${nextMode} theme`; + const classes = useStyles(); + + return ( + <Tooltip title={description}> + <IconButton + onClick={toggleMode} + aria-label={description} + className={classes.iconButton} + > + {mode === 'light' ? <WbSunnyRounded /> : <NightsStayRounded />} + </IconButton> + </Tooltip> + ); +}; + +type Props = { + children: React.ReactNode; + lightTheme: Theme; + darkTheme: Theme; +}; +const Themer = ({ children, lightTheme, darkTheme }: Props) => { + const savedMode = localStorage.getItem('themeMode'); + const preferedMode = savedMode != null ? savedMode : 'light'; + const [mode, setMode] = useState(preferedMode); + + const toggleMode = () => { + const preferedMode = mode === 'light' ? 'dark' : 'light'; + localStorage.setItem('themeMode', preferedMode); + setMode(preferedMode); + }; + + const preferedTheme = mode === 'dark' ? darkTheme : lightTheme; + + return ( + <ThemeContext.Provider value={{ toggleMode: toggleMode, mode: mode }}> + <ThemeProvider theme={preferedTheme}>{children}</ThemeProvider> + </ThemeContext.Provider> + ); +}; + +export { Themer as default, LightSwitch }; |