-
I'm working on implementing dark and light mode again in my applications that are using I'm implementing a button that users can use the switch between light and dark mode: public sealed partial class ThemeSwitcher : ComponentBase
{
private DesignThemeModes Mode { get; set; }
private string ClassValue { get; set; } = "";
private void ChangeTheme()
{
// Switch theme
if (Mode is DesignThemeModes.System or DesignThemeModes.Light)
{
Mode = DesignThemeModes.Dark;
ClassValue = "darkTheme";
}
else
{
Mode = DesignThemeModes.Light;
ClassValue = "lightTheme";
}
}
} I noticed
On my laptop my How do you make this ThemeSwitcher act like it is supposed to work? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Where do SystemLight and SystemaDark come from? If you look at settings in Edge or Firefox there are also only 3 settings. If you look how we implemented the setting of the theme in the demo (using the loading theme web component), the site will show in dark mode when system is set to dark mode. |
Beta Was this translation helpful? Give feedback.
-
The 3 options are mapped to the Browser possibilities: The You can find an example in our documentation: https://www.fluentui-blazor.net/DesignTheme Or a very small example used during our presentation. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I think I found a solution. The <FluentDesignTheme StorageName="theme"
CustomColor="#CC3300"
OnLoaded="OnloadedTheme"
@bind-Mode="@Mode"/> private void OnloadedTheme(LoadedEventArgs loadedEventArgs)
{
if (loadedEventArgs.Mode == DesignThemeModes.System)
{
if (loadedEventArgs.IsDark)
{
Mode = DesignThemeModes.Dark;
ClassValue = "toolbarButton darkTheme";
}
} In the When the user now clicks on the ThemeSwitch button and it is already in SystemDark it will switch to Light mode without the need for a second click. @vnbaaij Is this a correct implementation or do I make wrong assumptions? |
Beta Was this translation helpful? Give feedback.
I think I found a solution.
The
FluentDesignTheme
has anOnLoaded
event, which I can use.In the
OnLoaded
event I check if the Mode isDesignThemeModes.System
and then I check if the System prefers dark mode.If so I exp…