Skip to content

Commit 31aa478

Browse files
committed
Add theme switcher code snippets and supporting styles
1 parent 790a329 commit 31aa478

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@inject IJSRuntime JSRuntime
2+
3+
<FluentStack Style="background-color: var(--colorBrandBackgroundHover);"
4+
HorizontalGap="24px"
5+
HorizontalAlignment="HorizontalAlignment.Right">
6+
7+
@* ************************ *@
8+
@* Option 1: Sun/Moon icons *@
9+
@* ************************ *@
10+
<FluentButton Appearance="ButtonAppearance.Transparent"
11+
IconOnly="true"
12+
Class="fluent-header-hover"
13+
Title="Switch to Light/Dark theme"
14+
OnClick="@(async e => await JSRuntime.InvokeVoidAsync("Blazor.theme.switchTheme"))">
15+
@* Dark icon *@
16+
<FluentIcon Class="hidden-if-light"
17+
Value="@(new Icons.Filled.Size20.WeatherSunny().WithColor(SystemColors.Neutral.ForegroundOnBrand))" />
18+
@* Light icon *@
19+
<FluentIcon Class="hidden-if-dark"
20+
Value="@(new Icons.Filled.Size20.WeatherMoon().WithColor(SystemColors.Neutral.ForegroundOnBrand))" />
21+
</FluentButton>
22+
23+
@* ************************* *@
24+
@* Option 2: Dark theme icon *@
25+
@* ************************* *@
26+
<FluentButton Appearance="ButtonAppearance.Transparent"
27+
IconOnly="true"
28+
Class="fluent-header-hover"
29+
Title="Switch to Light/Dark theme"
30+
OnClick="@(async e => await JSRuntime.InvokeVoidAsync("Blazor.theme.switchTheme"))">
31+
@* Dark icon *@
32+
<FluentIcon Class="hidden-if-light"
33+
Value="@(new Icons.Filled.Size20.DarkTheme().WithColor(SystemColors.Neutral.ForegroundOnBrand))" />
34+
@* Light icon *@
35+
<FluentIcon Class="hidden-if-dark"
36+
Style="transform: rotate(180deg);"
37+
Value="@(new Icons.Filled.Size20.DarkTheme().WithColor(SystemColors.Neutral.ForegroundOnBrand))" />
38+
</FluentButton>
39+
40+
</FluentStack>

examples/Demo/FluentUI.Demo.Client/Documentation/Components/Theme/Dark/ThemeDark.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This theme is designed to provide a visually appealing and comfortable user expe
1111
You can easily switch between light and dark themes using the provided JavaScript functions:
1212
`Blazor.theme.setDarkTheme` and `Blazor.theme.setLightTheme`.
1313

14+
> [!TIP] See to the section **Code snippets** at the bottom of this page for ready-to-use code examples.
15+
1416
## Example
1517

1618
```csharp
@@ -91,3 +93,23 @@ Or to automatically switch themes based on system preferences.
9193
});
9294
</script>
9395
```
96+
97+
## Code snippets
98+
99+
In this example, two buttons are created that allows users to switch between light and dark themes.
100+
101+
1. The first/left example uses two icons:
102+
- A **dark icon** (a sun) that is only visible when the light theme is active.
103+
The internal CSS class `hidden-if-light` is used to hide the icon when the light theme is active.
104+
- A **light icon** (a moon) that is only visible when the dark theme is active.
105+
The internal CSS class `hidden-if-dark` is used to hide the icon when the dark theme is active.
106+
107+
2. The second/right example uses the same **DarkTheme icon** but changes its appearance based on the current theme: `Style="transform: rotate(180deg);"`
108+
109+
In these two examples, when the button is **hovered**, the icon color changes to indicate interactivity, using the class `fluent-header-hover`.
110+
111+
When the user clicks the button, the `OnClick` event handler calls the JavaScript function `Blazor.theme.switchTheme` to toggle between light and dark themes.
112+
(note the use of `@inject IJSRuntime JSRuntime` to invoke JavaScript functions from Blazor).
113+
The `body data-theme` attribute is automatically updated, and the `themeChanged` event is triggered.
114+
115+
{{ ThemeSnippet }}

src/Core/Components/Layout/FluentLayoutItem.razor.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@
7373
.fluent-layout[mobile] .fluent-layout-item[area="footer"] {
7474
line-height: var(--lineHeightBase100);
7575
}
76+
77+
/* For external use */
78+
.fluent-header-hover:hover {
79+
background-color: var(--colorBrandBackground);
80+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body[data-theme="light"] .hidden-if-light,
2+
body:not([data-theme]) .hidden-if-light,
3+
body[data-theme=""] .hidden-if-light {
4+
display: none;
5+
}
6+
7+
body[data-theme="dark"] .hidden-if-dark {
8+
display: none;
9+
}

0 commit comments

Comments
 (0)