Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export function addThemeChangeEvent(dotNetHelper, id) {

if (element) {
element.addEventListener("onchange", (e) => {
UpdateBodyDataSetTheme(e.detail.newValue);
try {
// setTimeout: https://github.com/dotnet/aspnetcore/issues/26809
setTimeout(() => {
Expand All @@ -15,7 +16,8 @@ export function addThemeChangeEvent(dotNetHelper, id) {

try {
// This can fail when localStorage does not contain a valid JSON object
const theme = element.themeStorage.readLocalStorage()
const theme = element.themeStorage.readLocalStorage();
UpdateBodyDataSetTheme(theme.mode);
return theme == null ? theme : JSON.stringify(theme);
} catch (error) {
ClearLocalStorage(id);
Expand Down Expand Up @@ -46,3 +48,12 @@ export function ClearLocalStorage(id) {
element.themeStorage.clearLocalStorage();
}
}

function UpdateBodyDataSetTheme(theme) {
if (theme) {
document.body.dataset.theme = theme;
} else {
const isSystemDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
document.body.dataset.theme = isSystemDark ? 'dark' : 'light';
}
}
10 changes: 10 additions & 0 deletions src/Core/wwwroot/css/reboot.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ body, .body {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

body[data-theme="light"],
body[data-theme="system-light"] {
color-scheme: light;
}

body[data-theme="dark"],
body[data-theme="system-dark"] {
color-scheme: dark;
}

hr {
margin: 1rem 0;
color: inherit;
Expand Down