Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
videoag
frontend
Commits
9f97c0e7
Commit
9f97c0e7
authored
2 months ago
by
Simon Künzel
Browse files
Options
Downloads
Patches
Plain Diff
Add ThemeProvider
parent
fac49a9b
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/pages/_app.tsx
+7
-4
7 additions, 4 deletions
src/pages/_app.tsx
src/videoag/miscellaneous/Theme.tsx
+37
-0
37 additions, 0 deletions
src/videoag/miscellaneous/Theme.tsx
src/videoag/site/DefaultLayout.tsx
+11
-32
11 additions, 32 deletions
src/videoag/site/DefaultLayout.tsx
with
55 additions
and
36 deletions
src/pages/_app.tsx
+
7
−
4
View file @
9f97c0e7
...
...
@@ -6,6 +6,7 @@ import { AuthStatusProvider } from "@/videoag/authentication/AuthStatus";
import
{
showErrorToast
}
from
"
@/videoag/error/ErrorDisplay
"
;
import
{
LanguageProvider
}
from
"
@/videoag/localization/LanguageProvider
"
;
import
{
ReloadBoundary
}
from
"
@/videoag/miscellaneous/ReloadBoundary
"
;
import
{
ThemeProvider
}
from
"
@/videoag/miscellaneous/Theme
"
;
import
Title
from
"
@/videoag/miscellaneous/TitleComponent
"
;
import
{
RealBackendProvider
}
from
"
@/videoag/api/ApiProvider
"
;
import
{
EditModeProvider
}
from
"
@/videoag/object_management/EditModeProvider
"
;
...
...
@@ -40,10 +41,12 @@ export default function App({ Component, pageProps }: AppProps) {
<
AuthStatusProvider
>
<
EditModeProvider
>
<
LanguageProvider
>
<
ThemeProvider
>
<
ToastContainer
/>
<
DefaultLayout
>
<
Component
{
...
pageProps
}
/>
</
DefaultLayout
>
</
ThemeProvider
>
</
LanguageProvider
>
</
EditModeProvider
>
</
AuthStatusProvider
>
...
...
This diff is collapsed.
Click to expand it.
src/videoag/miscellaneous/Theme.tsx
0 → 100644
+
37
−
0
View file @
9f97c0e7
import
type
React
from
"
react
"
;
import
{
createContext
,
useContext
,
useEffect
}
from
"
react
"
;
import
{
useLocalStorageState
}
from
"
@/videoag/miscellaneous/Storage
"
;
type
ThemeContextType
=
{
theme
:
string
;
setTheme
:
(
newValue
:
string
|
((
oldValue
:
string
)
=>
string
))
=>
void
;
};
const
ThemeContext
=
createContext
<
ThemeContextType
>
({
theme
:
"
dark
"
,
setTheme
:
()
=>
{},
});
export
function
ThemeProvider
({
children
}:
{
children
:
React
.
ReactNode
})
{
const
[
theme
,
setTheme
]
=
useLocalStorageState
<
string
>
(
"
theme
"
,
"
dark
"
);
useEffect
(()
=>
{
try
{
let
currentTheme
=
document
.
documentElement
.
getAttribute
(
"
data-bs-theme
"
);
if
(
theme
!==
currentTheme
)
{
document
.
documentElement
.
classList
.
add
(
"
no-transition
"
);
document
.
documentElement
.
setAttribute
(
"
data-bs-theme
"
,
theme
);
document
.
documentElement
.
classList
.
remove
(
"
no-transition
"
);
}
}
catch
(
e
)
{
console
.
error
(
"
Could not update theme on document
"
,
e
);
}
},
[
theme
]);
return
<
ThemeContext
.
Provider
value
=
{
{
theme
,
setTheme
}
}
>
{
children
}
</
ThemeContext
.
Provider
>;
}
export
function
useTheme
()
{
return
useContext
(
ThemeContext
);
}
This diff is collapsed.
Click to expand it.
src/videoag/site/DefaultLayout.tsx
+
11
−
32
View file @
9f97c0e7
...
...
@@ -13,9 +13,9 @@ import UserField from "@/videoag/authentication/UserField";
import
{
showErrorToast
,
ErrorComponent
}
from
"
@/videoag/error/ErrorDisplay
"
;
import
{
FallbackErrorBoundary
}
from
"
@/videoag/error/FallbackErrorBoundary
"
;
import
{
useLanguage
}
from
"
@/videoag/localization/LanguageProvider
"
;
import
{
storageGetOrDefault
,
storageSet
}
from
"
@/videoag/miscellaneous/Storage
"
;
import
{
ReloadBoundary
}
from
"
@/videoag/miscellaneous/ReloadBoundary
"
;
import
{
StylizedText
}
from
"
@/videoag/miscellaneous/StylizedText
"
;
import
{
useTheme
}
from
"
@/videoag/miscellaneous/Theme
"
;
import
{
useEditMode
}
from
"
@/videoag/object_management/EditModeProvider
"
;
import
AnnouncementComponent
from
"
@/videoag/site/AnnouncementComponent
"
;
import
{
basePath
}
from
"
@/../basepath
"
;
...
...
@@ -108,38 +108,9 @@ export function Search({
function
NavBar
({
status
}:
{
status
?:
GetStatusResponse
})
{
const
[
navbarOpen
,
setNavbarOpen
]
=
useState
(
false
);
const
{
setTheme
}
=
useTheme
();
const
{
language
,
setLanguageCode
}
=
useLanguage
();
useEffect
(()
=>
{
try
{
let
currentTheme
=
document
.
documentElement
.
getAttribute
(
"
data-bs-theme
"
);
if
(
storageGetOrDefault
(
"
theme
"
,
currentTheme
)
!==
currentTheme
)
{
document
.
documentElement
.
classList
.
add
(
"
no-transition
"
);
document
.
documentElement
.
setAttribute
(
"
data-bs-theme
"
,
storageGetOrDefault
(
"
theme
"
,
currentTheme
),
);
document
.
documentElement
.
classList
.
remove
(
"
no-transition
"
);
}
}
catch
(
e
)
{
console
.
error
(
"
Could not load theme from local storage
"
,
e
);
}
},
[]);
const
switchTheme
=
()
=>
{
const
theme
=
document
.
documentElement
.
getAttribute
(
"
data-bs-theme
"
);
if
(
theme
===
"
dark
"
)
{
document
.
documentElement
.
setAttribute
(
"
data-bs-theme
"
,
"
light
"
);
}
else
{
document
.
documentElement
.
setAttribute
(
"
data-bs-theme
"
,
"
dark
"
);
}
try
{
storageSet
(
"
theme
"
,
theme
===
"
dark
"
?
"
light
"
:
"
dark
"
);
}
catch
(
e
)
{
console
.
error
(
"
Could not save theme to local storage
"
,
e
);
}
};
const
switchLanguage
=
()
=>
{
setLanguageCode
(
language
.
getCode
()
===
"
de
"
?
"
en
"
:
"
de
"
);
};
...
...
@@ -267,7 +238,15 @@ function NavBar({ status }: { status?: GetStatusResponse }) {
<
UserField
isUnavailable
=
{
status
?.
status
===
"
unavailable
"
}
/>
<
div
>
<
button
className
=
{
"
btn
"
}
type
=
"button"
onClick
=
{
switchTheme
}
>
<
button
className
=
{
"
btn
"
}
type
=
"button"
onClick
=
{
()
=>
setTheme
((
oldValue
)
=>
oldValue
===
"
dark
"
?
"
light
"
:
"
dark
"
,
)
}
>
<
span
aria-hidden
=
"true"
className
=
{
"
bi bi-moon-stars
"
}
/>
</
button
>
</
div
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment