Skip to content

Commit

Permalink
Apply global color-scheme to reflect theme in use (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
ProH4Ck authored Oct 24, 2024
1 parent 286b4f1 commit 456877a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit 456877a

Please sign in to comment.