-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
136 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@inherits BaseMudThemeProvider | ||
|
||
@((MarkupString)BuildTheme()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<MudTooltip Text="Switch Theme"> | ||
<MudIconButton @ref="@_iconButton" Icon="@(_selectedTheme == ThemeOption.System? @Icons.Material.Filled.AutoMode : | ||
_selectedTheme == ThemeOption.Dark? @Icons.Material.Outlined.DarkMode : | ||
@Icons.Material.Outlined.LightMode)" OnClick="@ToggleMode" /> | ||
</MudTooltip> | ||
|
||
@code { | ||
|
||
public enum ThemeOption | ||
{ | ||
System, | ||
Dark, | ||
Light, | ||
} | ||
|
||
[Inject] | ||
private IJSRuntime JsRuntime { get; set; } = null!; | ||
|
||
private MudIconButton _iconButton; | ||
private ThemeOption _selectedTheme; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
|
||
var theme = await JsRuntime.InvokeAsync<string>("getTheme"); | ||
|
||
if (Enum.TryParse<ThemeOption>(theme, out _selectedTheme)) | ||
{ | ||
await JsRuntime.InvokeVoidAsync("setTheme"); | ||
} | ||
} | ||
|
||
private async Task ToggleMode() | ||
{ | ||
switch (_selectedTheme) | ||
{ | ||
case ThemeOption.Dark: | ||
_selectedTheme = ThemeOption.Light; | ||
break; | ||
case ThemeOption.Light: | ||
_selectedTheme = ThemeOption.System; | ||
break; | ||
default: | ||
_selectedTheme = ThemeOption.Dark; | ||
break; | ||
|
||
} | ||
|
||
await JsRuntime.InvokeVoidAsync("storeTheme", _selectedTheme.ToString()); | ||
|
||
await JsRuntime.InvokeVoidAsync("setTheme"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const ThemeOption = { | ||
System: "System", | ||
Dark: "Dark", | ||
Light: "Light" | ||
} | ||
|
||
window.getTheme = () => localStorage.getItem('theme'); | ||
|
||
window.storeTheme = theme => localStorage.setItem('theme', theme); | ||
|
||
window.getThemePreference = () => { | ||
const theme = getTheme() | ||
|
||
if (theme && theme != ThemeOption.System) | ||
return theme; | ||
|
||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light' | ||
} | ||
|
||
window.setTheme = () => { | ||
const theme = getThemePreference(); | ||
|
||
if (theme == ThemeOption.Dark) { | ||
document.body.classList.add( | ||
'dark-theme' | ||
); | ||
} | ||
else { | ||
document.body.classList.remove( | ||
'dark-theme' | ||
); | ||
} | ||
} | ||
|
||
window.onload = (event) => { | ||
console.log("Hello World!"); | ||
setTheme(); | ||
}; |