Skip to content

Commit a71d09f

Browse files
committed
feat: Enhance theme management with type attribute #78
- Added `data-theme-type` attribute to the document element to indicate the current theme type (light or dark). - Updated theme toggle functionality to set the theme type based on user selection and system preferences. - Modified global styles to prevent Chrome's auto dark theme interference by configuring color schemes based on the theme type.
1 parent e660a3f commit a71d09f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/components/widgets/ThemeToggle.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ const { className = "" } = Astro.props as ThemeToggleProps;
6262

6363
// Set new theme
6464
document.documentElement.setAttribute("data-theme", newTheme);
65+
66+
// Set theme type based on which theme is active
67+
const themeType = newTheme === SITE_THEME.dark ? "dark" : "light";
68+
document.documentElement.setAttribute("data-theme-type", themeType);
69+
6570
localStorage.setItem("theme", newTheme);
6671

6772
// Update icons for all theme toggles on the page

src/layouts/BaseLayout.astro

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { title, image, headings = [], showTOC = false } = Astro.props;
1414
---
1515

1616
<!doctype html>
17-
<html lang={SITE_LANGUAGE} class="bg-base-300" data-theme={SITE_THEME.light}>
17+
<html lang={SITE_LANGUAGE} class="bg-base-300" data-theme={SITE_THEME.light} data-theme-type="light">
1818
<head>
1919
<ClientRouter />
2020
<ElementCrossing />
@@ -40,12 +40,21 @@ const { title, image, headings = [], showTOC = false } = Astro.props;
4040
// Apply theme to document
4141
document.documentElement.setAttribute("data-theme", theme);
4242

43+
// Set theme type based on which theme is active
44+
const themeType = theme === SITE_THEME.dark ? "dark" : "light";
45+
document.documentElement.setAttribute("data-theme-type", themeType);
46+
4347
// Listen for system preference changes
4448
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
4549
// Only update if user hasn't explicitly set a theme preference
4650
if (!localStorage.getItem("theme")) {
4751
const newTheme = e.matches ? SITE_THEME.dark : SITE_THEME.light;
4852
document.documentElement.setAttribute("data-theme", newTheme);
53+
54+
// Update theme type attribute when system preference changes
55+
const newThemeType = e.matches ? "dark" : "light";
56+
document.documentElement.setAttribute("data-theme-type", newThemeType);
57+
4958
localStorage.setItem("theme", newTheme);
5059
}
5160
});
@@ -83,6 +92,10 @@ const { title, image, headings = [], showTOC = false } = Astro.props;
8392
if (storedTheme) {
8493
// Apply the stored theme to ensure it persists during navigation
8594
document.documentElement.setAttribute("data-theme", storedTheme);
95+
96+
// Set theme type based on which theme is active
97+
const themeType = storedTheme === SITE_THEME.dark ? "dark" : "light";
98+
document.documentElement.setAttribute("data-theme-type", themeType);
8699
}
87100
});
88101
</script>

src/styles/global.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
@tailwind components;
44
@tailwind utilities;
55

6+
/* Color scheme configuration to prevent Chrome's auto dark theme */
7+
:root,
8+
[data-theme-type="light"] {
9+
color-scheme: light dark; /* This prevents Chrome's auto dark theme from taking effect */
10+
}
11+
12+
[data-theme-type="dark"] {
13+
color-scheme: dark;
14+
}
15+
616
/* Global styles with nesting support */
717
html {
818
@apply scroll-smooth;

0 commit comments

Comments
 (0)