Skip to content

Commit 0ce213a

Browse files
author
morozov
committed
fix: T1260228 - Theme Switcher for Blazor - Applied and selected themes are incorrect when the Boostrap Default Dark theme is selected
1 parent 96f4f0e commit 0ce213a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

CS/switcher/switcher/App.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515

1616
<!DOCTYPE html>
17-
<html lang="en" >
17+
<html lang="en" data-bs-theme="@Themes.ActiveTheme.BootstrapThemeMode">
1818
<head>
1919
<meta charset="utf-8" />
2020
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@@ -38,7 +38,7 @@
3838
</head>
3939
<body>
4040
<div id="switcher-container" data-permanent></div>
41-
<Routes></Routes>
41+
<Routes></Routes>
4242
<script src="_framework/blazor.web.js"></script>
4343
</body>
4444

CS/switcher/switcher/ThemeSwitcher/ThemeSwitcher.razor

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
@inject ThemeService Themes
1+
@implements IDisposable
2+
@inject ThemeService Themes
3+
@inject PersistentComponentState ApplicationState
4+
25
@rendermode InteractiveServer
36
<ThemeJsChangeDispatcher InitialThemeName="blazor-berry"></ThemeJsChangeDispatcher>
47
<div class="@GetHeaderContainerCss()">
@@ -16,6 +19,7 @@
1619
@code {
1720
string _themeName;
1821
bool _themeSwitcherShown;
22+
PersistingComponentStateSubscription _persistingSubscription;
1923

2024
protected string ThemeName {
2125
get { return _themeName; }
@@ -39,7 +43,11 @@
3943
}
4044
}
4145
protected override void OnInitialized() {
42-
ThemeName = Themes.ActiveTheme?.Name ?? string.Empty;
46+
_persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
47+
if (ApplicationState.TryTakeFromJson<string>("ThemeName", out var themeName) && themeName != null)
48+
ThemeName = themeName;
49+
else
50+
ThemeName = Themes.ActiveTheme?.Name ?? string.Empty;
4351
}
4452
void ToggleThemeSwitcherPanel() {
4553
ThemeSwitcherShown = !ThemeSwitcherShown;
@@ -54,4 +62,11 @@
5462
cssClasses.Add("theme-external");
5563
return string.Join(" ", cssClasses);
5664
}
65+
Task PersistData() {
66+
ApplicationState.PersistAsJson("ThemeName", ThemeName);
67+
return Task.CompletedTask;
68+
}
69+
void IDisposable.Dispose() {
70+
_persistingSubscription.Dispose();
71+
}
5772
}

0 commit comments

Comments
 (0)