-
Notifications
You must be signed in to change notification settings - Fork 351
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
WebUI Dark Mode #8137
WebUI Dark Mode #8137
Changes from 11 commits
ad29996
d85156d
d20503f
0cc2096
a84a25f
8cf055f
78f0060
a513ff3
7f4b625
54e427d
2527613
d9362e0
291e7b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, {FC, useContext} from "react"; | ||
import {DarkModeSwitch} from "react-toggle-dark-mode"; | ||
import {AppActionType, AppContext} from "../hooks/appContext"; | ||
|
||
const DarkModeToggle: FC = () => { | ||
const {state, dispatch} = useContext(AppContext); | ||
|
||
const toggleDarkMode = (isOn: boolean) => { | ||
dispatch({ | ||
type: AppActionType.setDarkMode, | ||
value: isOn, | ||
}); | ||
}; | ||
|
||
return ( | ||
<DarkModeSwitch | ||
style={{marginRight: '2rem'}} | ||
checked={state.settings.darkMode} | ||
onChange={toggleDarkMode} | ||
size={28} | ||
sunColor={"white"} | ||
/> | ||
); | ||
}; | ||
|
||
export default DarkModeToggle; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import React, {FC, useState} from "react"; | ||
import React, {FC, useContext, useState} from "react"; | ||
import { Outlet, useOutletContext } from "react-router-dom"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint is inconsistent in the WebUI code - |
||
import { StorageConfigProvider } from "../hooks/storageConfig"; | ||
|
||
import TopNav from './navbar'; | ||
import { AppContext } from "../hooks/appContext"; | ||
|
||
type LayoutOutletContext = [(isLoggedIn: boolean) => void]; | ||
|
||
const Layout: FC<{logged: boolean}> = ({ logged }) => { | ||
const [isLogged, setIsLogged] = useState(logged ?? true); | ||
|
||
// handle global dark mode here | ||
const {state} = useContext(AppContext); | ||
document.documentElement.setAttribute('data-bs-theme', state.settings.darkMode ? 'dark' : 'light') | ||
|
||
return ( | ||
<> | ||
<TopNav logged={isLogged}/> | ||
<div className="main-app"> | ||
<StorageConfigProvider> | ||
<Outlet context={[setIsLogged] satisfies LayoutOutletContext} /> | ||
<Outlet context={[setIsLogged] satisfies LayoutOutletContext}/> | ||
</StorageConfigProvider> | ||
</div> | ||
</> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { createContext, useReducer } from "react"; | ||
|
||
type AppContextType = { | ||
settings: AppContext; | ||
}; | ||
|
||
type AppContext = { | ||
darkMode: boolean; | ||
}; | ||
|
||
const localStorageKeys = { | ||
darkMode: 'darkMode', | ||
}; | ||
|
||
enum AppActionType { | ||
setDarkMode = 'setDarkMode', | ||
} | ||
|
||
interface Action { | ||
type: AppActionType; | ||
value: boolean; | ||
} | ||
|
||
const initialLocalSettings: AppContext = { | ||
darkMode: window.localStorage.getItem(localStorageKeys.darkMode) === String(true), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏽 |
||
}; | ||
|
||
const initialAppContext: AppContextType = { | ||
settings: initialLocalSettings, | ||
}; | ||
|
||
const appContextReducer = (state: AppContextType, action: Action) => { | ||
switch (action.type) { | ||
case AppActionType.setDarkMode: | ||
window.localStorage.setItem(localStorageKeys.darkMode, String(action.value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏽 |
||
return {...state, settings: {...state.settings, darkMode: action.value}}; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
type ContextType = { | ||
state: AppContextType, | ||
dispatch: React.Dispatch<Action>, | ||
}; | ||
|
||
const AppContext = createContext<ContextType>({ | ||
state: initialAppContext, | ||
dispatch: () => null | ||
}); | ||
|
||
// @ts-expect-error - it doesn't like the "children" prop | ||
const WithAppContext: React.FC = ({children}) => { | ||
const [state, dispatch] = useReducer(appContextReducer, initialAppContext); | ||
|
||
return ( | ||
<AppContext.Provider value={{state, dispatch}}> | ||
{children} | ||
</AppContext.Provider> | ||
) | ||
} | ||
|
||
export { WithAppContext, AppContext, AppActionType }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The UI component of the toggle button.