feat: implement theme toggling

This commit is contained in:
Jeremy Kahn 2022-09-02 09:49:00 -05:00
parent 96740b352b
commit 8b22aa8df9
3 changed files with 44 additions and 14 deletions

View File

@ -40,7 +40,15 @@ export const ChatTranscript = ({
}, [messageLog.length]) }, [messageLog.length])
return ( return (
<Box ref={boxRef} className={cx('ChatTranscript flex flex-col', className)}> <Box
ref={boxRef}
className={cx('ChatTranscript', className)}
sx={theme => ({
display: 'flex',
flexDirection: 'column',
paddingTop: theme.spacing(1),
})}
>
{messageLog.map(message => { {messageLog.map(message => {
return ( return (
// This wrapper div is necessary for accurate layout calculations // This wrapper div is necessary for accurate layout calculations

View File

@ -62,9 +62,9 @@ export const Message = ({ message, userId }: MessageProps) => {
if (message.authorId === userId) { if (message.authorId === userId) {
backgroundColor = isMessageReceived(message) backgroundColor = isMessageReceived(message)
? 'primary.dark' ? 'primary.dark'
: 'primary.main' : 'primary.light'
} else { } else {
backgroundColor = 'grey.700' backgroundColor = 'secondary.light'
} }
return ( return (
@ -72,7 +72,6 @@ export const Message = ({ message, userId }: MessageProps) => {
<Typography <Typography
variant="caption" variant="caption"
display="block" display="block"
color="grey.400"
sx={{ sx={{
textAlign: message.authorId === userId ? 'right' : 'left', textAlign: message.authorId === userId ? 'right' : 'left',
}} }}
@ -81,6 +80,7 @@ export const Message = ({ message, userId }: MessageProps) => {
</Typography> </Typography>
<Box <Box
sx={{ sx={{
color: 'primary.contrastText',
backgroundColor, backgroundColor,
margin: 0.5, margin: 0.5,
padding: '0.5em 0.75em', padding: '0.5em 0.75em',

View File

@ -9,9 +9,8 @@ import {
} from 'react' } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import CssBaseline from '@mui/material/CssBaseline' import CssBaseline from '@mui/material/CssBaseline'
import { styled, useTheme } from '@mui/material/styles'
import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar' import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar'
import { ThemeProvider, createTheme } from '@mui/material/styles' import { ThemeProvider, createTheme, styled } from '@mui/material/styles'
import Drawer from '@mui/material/Drawer' import Drawer from '@mui/material/Drawer'
import Toolbar from '@mui/material/Toolbar' import Toolbar from '@mui/material/Toolbar'
import Box from '@mui/material/Box' import Box from '@mui/material/Box'
@ -31,6 +30,8 @@ import ListItemButton from '@mui/material/ListItemButton'
import ListItemIcon from '@mui/material/ListItemIcon' import ListItemIcon from '@mui/material/ListItemIcon'
import ListItemText from '@mui/material/ListItemText' import ListItemText from '@mui/material/ListItemText'
import Home from '@mui/icons-material/Home' import Home from '@mui/icons-material/Home'
import Brightness4Icon from '@mui/icons-material/Brightness4'
import Brightness7Icon from '@mui/icons-material/Brightness7'
import { ShellContext } from 'ShellContext' import { ShellContext } from 'ShellContext'
import { AlertOptions } from 'models/shell' import { AlertOptions } from 'models/shell'
@ -96,14 +97,7 @@ const DrawerHeader = styled('div')(({ theme }) => ({
justifyContent: 'flex-end', justifyContent: 'flex-end',
})) }))
const darkTheme = createTheme({
palette: {
mode: 'dark',
},
})
export const Shell = ({ children }: ShellProps) => { export const Shell = ({ children }: ShellProps) => {
const theme = useTheme()
const [isAlertShowing, setIsAlertShowing] = useState(false) const [isAlertShowing, setIsAlertShowing] = useState(false)
const [isDrawerOpen, setIsDrawerOpen] = useState(false) const [isDrawerOpen, setIsDrawerOpen] = useState(false)
const [doShowPeers, setDoShowPeers] = useState(false) const [doShowPeers, setDoShowPeers] = useState(false)
@ -131,6 +125,22 @@ export const Shell = ({ children }: ShellProps) => {
[numberOfPeers, setDoShowPeers, setNumberOfPeers, setTitle, showAlert] [numberOfPeers, setDoShowPeers, setNumberOfPeers, setTitle, showAlert]
) )
const [mode, setMode] = useState<'light' | 'dark'>('dark')
const handleColorModeToggleClick = () => {
setMode(prevMode => (prevMode === 'light' ? 'dark' : 'light'))
}
const theme = useMemo(
() =>
createTheme({
palette: {
mode,
},
}),
[mode]
)
const handleAlertClose = ( const handleAlertClose = (
_event?: SyntheticEvent | Event, _event?: SyntheticEvent | Event,
reason?: string reason?: string
@ -156,7 +166,7 @@ export const Shell = ({ children }: ShellProps) => {
return ( return (
<ShellContext.Provider value={shellContextValue}> <ShellContext.Provider value={shellContextValue}>
<ThemeProvider theme={darkTheme}> <ThemeProvider theme={theme}>
<CssBaseline /> <CssBaseline />
<Box <Box
className="Chitchatter" className="Chitchatter"
@ -248,6 +258,18 @@ export const Shell = ({ children }: ShellProps) => {
</ListItemButton> </ListItemButton>
</ListItem> </ListItem>
</Link> </Link>
<ListItem disablePadding>
<ListItemButton onClick={handleColorModeToggleClick}>
<ListItemIcon>
{theme.palette.mode === 'dark' ? (
<Brightness7Icon />
) : (
<Brightness4Icon />
)}
</ListItemIcon>
<ListItemText primary="Change theme" />
</ListItemButton>
</ListItem>
</List> </List>
<Divider /> <Divider />
</Drawer> </Drawer>