Skip to content

Commit 8bd7fe0

Browse files
authored
Add optional dark mode flash prevention styles to index.html and App.razor (#4485)
1 parent 618e1f3 commit 8bd7fe0

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ body[data-theme="dark"] {
6464
}
6565
```
6666

67+
## Flash effect (optional)
68+
69+
When using the dark theme, you may notice a brief white "flash" before the page fully renders.
70+
This occurs because the browser's default background color is white, and the FluentUI styles may not load instantly—especially on slower network connections.
71+
72+
To prevent this flash, add the following CSS to your `index.html`, `App.razor`, or `_layout.cshtml` file (in the `<head>...</head>` HTML section).
73+
This sets a dark background color immediately, before the FluentUI styles are applied.
74+
75+
```html
76+
<style>
77+
@media (prefers-color-scheme: dark) {
78+
body {
79+
background-color: #292929;
80+
color: #ffffff;
81+
}
82+
}
83+
</style>
84+
```
85+
86+
**Notes:** In Razor/CSHTML files, escape the `@` symbol by doubling it: use `@@media` instead of `@media`.
87+
6788
## themeChanged event
6889

6990
A JavaScript `themeChanged` event is triggered each time the `data-theme` attribute changes.

examples/Demo/FluentUI.Demo.Client/wwwroot/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
<base href="/" />
1414
<link rel="stylesheet" href="app.css" />
1515
<link rel="stylesheet" href="FluentUI.Demo.Client.styles.css" />
16+
<style>
17+
/* Set the default dark mode styles,
18+
used before the Fluent UI theme switcher is initialized
19+
This is to avoid a flash of white when in dark mode.
20+
Update as needed to match your dark theme.
21+
*/
22+
@media (prefers-color-scheme: dark) {
23+
body {
24+
background-color: #292929;
25+
color: #ffffff;
26+
}
27+
}
28+
</style>
1629
</head>
1730

1831
<body>

examples/Demo/FluentUI.Demo/Components/App.razor

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
<link rel="stylesheet" href="FluentUI.Demo.styles.css" />
1919
<link rel="icon" type="image/x-icon" href="favicon.ico" />
2020
<HeadOutlet @rendermode="@DemoRenderMode" />
21+
22+
<style>
23+
/* Set the default dark mode styles,
24+
used before the Fluent UI theme switcher is initialized
25+
This is to avoid a flash of white when in dark mode.
26+
Update as needed to match your dark theme.
27+
*/
28+
@@media (prefers-color-scheme: dark) {
29+
body {
30+
background-color: #292929;
31+
color: #ffffff;
32+
}
33+
}
34+
</style>
2135
</head>
2236

2337
<body use-reboot="@reboot">

0 commit comments

Comments
 (0)