Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: [UI] theme switch #1335

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions xinference/web/ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CssBaseline, ThemeProvider } from '@mui/material'
import { CssBaseline } from '@mui/material'
import Snackbar from '@mui/material/Snackbar'
import React, { useEffect, useState } from 'react'
import { useCookies } from 'react-cookie'
Expand All @@ -7,12 +7,11 @@ import { HashRouter } from 'react-router-dom'
import { Alert } from './components/alertComponent'
import { ApiContextProvider } from './components/apiContext'
import AuthAlertDialog from './components/authAlertDialog'
import { ThemeProvider } from './components/themeContext'
import { getEndpoint } from './components/utils'
import WraperRoutes from './router/index'
import { useMode } from './theme'

function App() {
const [theme] = useMode()
const [cookie, setCookie, removeCookie] = useCookies(['token'])
const [msg, setMsg] = useState('')

Expand Down Expand Up @@ -61,25 +60,25 @@ function App() {

return (
<div className="app">
<Snackbar
open={msg !== ''}
autoHideDuration={10000}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
onClose={handleClose}
>
<Alert severity="error" onClose={handleClose} sx={{ width: '100%' }}>
{msg}
</Alert>
</Snackbar>
<HashRouter>
<ThemeProvider theme={theme}>
<ThemeProvider>
<Snackbar
open={msg !== ''}
autoHideDuration={10000}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
onClose={handleClose}
>
<Alert severity="error" onClose={handleClose} sx={{ width: '100%' }}>
{msg}
</Alert>
</Snackbar>
<HashRouter>
<ApiContextProvider>
<CssBaseline />
<AuthAlertDialog />
<WraperRoutes />
</ApiContextProvider>
</ThemeProvider>
</HashRouter>
</HashRouter>
</ThemeProvider>
</div>
)
}
Expand Down
4 changes: 3 additions & 1 deletion xinference/web/ui/src/components/MenuSide.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useEffect, useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'

import icon from '../media/icon.webp'
import ThemeButton from './themeButton'

const navItems = [
{
Expand Down Expand Up @@ -124,7 +125,7 @@ const MenuSide = () => {
</Box>
</Box>

<Box>
<Box sx={{ flexGrow: 1 }}>
<Box width="100%">
<Box m="1.5rem 2rem 2rem 3rem"></Box>
<List>
Expand Down Expand Up @@ -199,6 +200,7 @@ const MenuSide = () => {
</List>
</Box>
</Box>
<ThemeButton sx={{ m: '1rem' }} />
</Drawer>
)
}
Expand Down
7 changes: 1 addition & 6 deletions xinference/web/ui/src/components/Title.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ const Title = ({ title }) => {
return (
<Box mb="30px">
<Stack direction="row" alignItems="center" justifyContent="space-between">
<Typography
variant="h2"
color="#141414"
fontWeight="bold"
sx={{ m: '0 0 5px 0' }}
>
<Typography variant="h2" fontWeight="bold" sx={{ m: '0 0 5px 0' }}>
{title}
</Typography>
{(isValidBearerToken(cookie.token) ||
Expand Down
20 changes: 20 additions & 0 deletions xinference/web/ui/src/components/themeButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import DarkModeIcon from '@mui/icons-material/DarkMode'
import LightModeIcon from '@mui/icons-material/LightMode'
import { Box, IconButton } from '@mui/material'
import React from 'react'

import { useThemeContext } from './themeContext'

const ThemeButton = ({ sx }) => {
const { themeMode, toggleTheme } = useThemeContext()

return (
<Box sx={sx}>
<IconButton size="large" onClick={toggleTheme}>
{themeMode === 'light' ? <LightModeIcon /> : <DarkModeIcon />}
</IconButton>
</Box>
)
}

export default ThemeButton
36 changes: 36 additions & 0 deletions xinference/web/ui/src/components/themeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ThemeProvider as MuiThemeProvider } from '@mui/material'
import { createContext, useContext, useState } from 'react'

import { useMode } from '../theme'

const ThemeContext = createContext()

export function useThemeContext() {
return useContext(ThemeContext)
}

export const ThemeProvider = ({ children }) => {
const themeKey = 'theme'
const systemPreference = window.matchMedia('(prefers-color-scheme: dark)')
.matches
? 'dark'
: 'light'
const initialMode = localStorage.getItem(themeKey) || systemPreference

const [themeMode, setThemeMode] = useState(initialMode)
const theme = useMode(themeMode)[0]

const switchTheme = () => {
const nextTheme = themeMode === 'light' ? 'dark' : 'light'
setThemeMode(nextTheme)
localStorage.setItem(themeKey, nextTheme)
}

return (
<MuiThemeProvider theme={theme}>
<ThemeContext.Provider value={{ themeMode, toggleTheme: switchTheme }}>
{children}
</ThemeContext.Provider>
</MuiThemeProvider>
)
}
9 changes: 6 additions & 3 deletions xinference/web/ui/src/theme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTheme } from '@mui/material/styles'

// mui theme settings
export const themeSettings = () => {
export const themeSettings = (mode) => {
return {
ERROR_COLOR: '#d8342c',
typography: {
Expand Down Expand Up @@ -32,10 +32,13 @@ export const themeSettings = () => {
fontSize: 14,
},
},
palette: {
mode: mode,
},
}
}

export const useMode = () => {
const theme = createTheme(themeSettings())
export const useMode = (mode = 'light') => {
const theme = createTheme(themeSettings(mode))
return [theme]
}
Loading