Add dark mode override for non-native DevExpress themes
Implements a dark mode override system for DevExpress Blazor themes lacking native dark support. Adds a JS function to toggle a dx-dark class on <html>, updates ThemeState to detect native dark themes, and applies targeted CSS variable overrides for consistent dark styling. Disables prerendering to ensure JS interop, and improves theme switching logic and documentation.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
@inherits LayoutComponentBase
|
||||
@implements IDisposable
|
||||
@inject ThemeState ThemeState
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<div class="page @(ThemeState.IsDarkMode ? "app-dark" : "app-light")">
|
||||
<div class="page @(ThemeState.IsDarkMode ? "app-dark" : "app-light") @(ThemeState.IsNativeDarkTheme ? "native-dark" : "")">
|
||||
<div class="sidebar">
|
||||
<NavMenu />
|
||||
</div>
|
||||
@@ -33,9 +34,43 @@
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private bool _isInteractive;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
ThemeState.OnChange += StateHasChanged;
|
||||
ThemeState.OnChange += OnThemeChanged;
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
_isInteractive = true;
|
||||
}
|
||||
await ApplyDxDarkOverrideAsync();
|
||||
}
|
||||
|
||||
private async void OnThemeChanged()
|
||||
{
|
||||
StateHasChanged();
|
||||
if (_isInteractive)
|
||||
{
|
||||
await ApplyDxDarkOverrideAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ApplyDxDarkOverrideAsync()
|
||||
{
|
||||
if (!_isInteractive) return;
|
||||
try
|
||||
{
|
||||
bool needsOverride = ThemeState.IsDarkMode && !ThemeState.IsNativeDarkTheme;
|
||||
await JS.InvokeVoidAsync("setDxDarkOverride", needsOverride);
|
||||
}
|
||||
catch (JSException)
|
||||
{
|
||||
// JS-Funktion noch nicht verfügbar – kein Circuit-Crash
|
||||
}
|
||||
}
|
||||
|
||||
private void ToggleTheme()
|
||||
@@ -45,6 +80,6 @@
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ThemeState.OnChange -= StateHasChanged;
|
||||
ThemeState.OnChange -= OnThemeChanged;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user