Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
199eaaa0
authored
Apr 16, 2024
by
kahosan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: dark mode
parent
c9d1842b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
36 deletions
+69
-36
web/src/components/HeaderBar.js
+11
-17
web/src/context/Theme/index.js
+36
-0
web/src/index.js
+22
-19
No files found.
web/src/components/HeaderBar.js
View file @
199eaaa0
import
React
,
{
useContext
,
useEffect
,
useState
}
from
'react'
;
import
{
Link
,
useNavigate
}
from
'react-router-dom'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
useSetTheme
,
useTheme
}
from
'../context/Theme'
;
import
{
API
,
getLogo
,
getSystemName
,
showSuccess
}
from
'../helpers'
;
import
'../index.css'
;
...
...
@@ -34,10 +35,8 @@ const HeaderBar = () => {
let
navigate
=
useNavigate
();
const
[
showSidebar
,
setShowSidebar
]
=
useState
(
false
);
const
[
dark
,
setDark
]
=
useState
(
false
);
const
systemName
=
getSystemName
();
const
logo
=
getLogo
();
var
themeMode
=
localStorage
.
getItem
(
'theme-mode'
);
const
currentDate
=
new
Date
();
// enable fireworks on new year(1.1 and 2.9-2.24)
const
isNewYear
=
...
...
@@ -66,26 +65,19 @@ const HeaderBar = () => {
},
3000
);
};
const
theme
=
useTheme
();
const
setTheme
=
useSetTheme
();
useEffect
(()
=>
{
if
(
theme
Mode
===
'dark'
)
{
switchMode
(
true
);
if
(
theme
===
'dark'
)
{
document
.
body
.
setAttribute
(
'theme-mode'
,
'dark'
);
}
if
(
isNewYear
)
{
console
.
log
(
'Happy New Year!'
);
}
},
[]);
const
switchMode
=
(
model
)
=>
{
const
body
=
document
.
body
;
if
(
!
model
)
{
body
.
removeAttribute
(
'theme-mode'
);
localStorage
.
setItem
(
'theme-mode'
,
'light'
);
}
else
{
body
.
setAttribute
(
'theme-mode'
,
'dark'
);
localStorage
.
setItem
(
'theme-mode'
,
'dark'
);
}
setDark
(
model
);
};
return
(
<>
<
Layout
>
...
...
@@ -132,9 +124,11 @@ const HeaderBar = () => {
<
Switch
checkedText
=
'🌞'
size
=
{
'large'
}
checked
=
{
dark
}
checked
=
{
theme
===
'dark'
}
uncheckedText
=
'🌙'
onChange
=
{
switchMode
}
onChange
=
{(
checked
)
=>
{
setTheme
(
checked
);
}}
/
>
{
userState
.
user
?
(
<>
...
...
web/src/context/Theme/index.js
0 → 100644
View file @
199eaaa0
import
{
createContext
,
useCallback
,
useContext
,
useState
}
from
'react'
;
const
ThemeContext
=
createContext
(
null
);
export
const
useTheme
=
()
=>
useContext
(
ThemeContext
);
const
SetThemeContext
=
createContext
(
null
);
export
const
useSetTheme
=
()
=>
useContext
(
SetThemeContext
);
export
const
ThemeProvider
=
({
children
})
=>
{
const
[
theme
,
_setTheme
]
=
useState
(()
=>
{
try
{
return
localStorage
.
getItem
(
'theme-mode'
)
||
null
;
}
catch
{
return
null
;
}
});
const
setTheme
=
useCallback
((
input
)
=>
{
_setTheme
(
input
?
'dark'
:
'light'
);
const
body
=
document
.
body
;
if
(
!
input
)
{
body
.
removeAttribute
(
'theme-mode'
);
localStorage
.
setItem
(
'theme-mode'
,
'light'
);
}
else
{
body
.
setAttribute
(
'theme-mode'
,
'dark'
);
localStorage
.
setItem
(
'theme-mode'
,
'dark'
);
}
},
[]);
return
(
<
SetThemeContext
.
Provider
value
=
{
setTheme
}
>
<
ThemeContext
.
Provider
value
=
{
theme
}
>
{
children
}
<
/ThemeContext.Provider
>
<
/SetThemeContext.Provider
>
);
};
web/src/index.js
View file @
199eaaa0
...
...
@@ -12,6 +12,7 @@ import 'react-toastify/dist/ReactToastify.css';
import
{
StatusProvider
}
from
'./context/Status'
;
import
{
Layout
}
from
'@douyinfe/semi-ui'
;
import
SiderBar
from
'./components/SiderBar'
;
import
{
ThemeProvider
}
from
'./context/Theme'
;
// initialization
...
...
@@ -22,27 +23,29 @@ root.render(
<
StatusProvider
>
<
UserProvider
>
<
BrowserRouter
>
<
Layout
>
<
Sider
>
<
SiderBar
/>
<
/Sider
>
<
ThemeProvider
>
<
Layout
>
<
Header
>
<
HeaderBar
/>
<
/Header
>
<
Content
style
=
{{
padding
:
'24px'
,
}}
>
<
App
/>
<
/Content
>
<
Layout
.
Footer
>
<
Footer
><
/Footer
>
<
/Layout.Footer
>
<
Sider
>
<
SiderBar
/>
<
/Sider
>
<
Layout
>
<
Header
>
<
HeaderBar
/>
<
/Header
>
<
Content
style
=
{{
padding
:
'24px'
,
}}
>
<
App
/>
<
/Content
>
<
Layout
.
Footer
>
<
Footer
><
/Footer
>
<
/Layout.Footer
>
<
/Layout
>
<
ToastContainer
/>
<
/Layout
>
<
ToastContainer
/>
<
/Layout
>
<
/ThemeProvider
>
<
/BrowserRouter
>
<
/UserProvider
>
<
/StatusProvider
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment