mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-25 06:24:19 +00:00
🔧 Save last set theme in the localStorage
This commit is contained in:
parent
20803316ee
commit
f68bbb3d68
1 changed files with 42 additions and 2 deletions
|
@ -12,10 +12,50 @@ import PageSettings from "./routes/PageSettings"
|
|||
|
||||
|
||||
export default function App() {
|
||||
const [theme, setTheme] = useState("ThemeDark");
|
||||
const loadTheme = () => {
|
||||
if(localStorage) {
|
||||
console.debug(`Loading theme from localStorage...`)
|
||||
let value = localStorage.getItem("theme")
|
||||
|
||||
if(value) {
|
||||
console.debug(`Loaded theme ${value}!`)
|
||||
return value
|
||||
}
|
||||
else {
|
||||
console.debug(`There is no theme stored in the localStorage; setting to "ThemeDark"...`)
|
||||
return "ThemeDark"
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.warn(`Can't load theme; localStorage doesn't seem to be available; setting to "ThemeDark"...`)
|
||||
return "ThemeDark"
|
||||
}
|
||||
}
|
||||
|
||||
const [theme, _setTheme] = useState(loadTheme());
|
||||
|
||||
const setTheme = (value) => {
|
||||
console.debug(`Changing theme to ${value}...`)
|
||||
_setTheme(value)
|
||||
}
|
||||
|
||||
const saveTheme = (value) => {
|
||||
if(localStorage) {
|
||||
console.debug(`Saving theme ${value} to localStorage...`)
|
||||
localStorage.setItem("theme", value)
|
||||
}
|
||||
else {
|
||||
console.warn(`Can't save theme; localStorage doesn't seem to be available...`)
|
||||
}
|
||||
}
|
||||
|
||||
const setAndSaveTheme = (value) => {
|
||||
setTheme(value)
|
||||
saveTheme(value)
|
||||
}
|
||||
|
||||
return (
|
||||
<ContextTheme.Provider value={[theme, setTheme]}>
|
||||
<ContextTheme.Provider value={[theme, setAndSaveTheme]}>
|
||||
<BrowserRouter>
|
||||
|
||||
<div className={classNames(Style.App, theme)}>
|
||||
|
|
Loading…
Reference in a new issue