Added script for handling dark/light mode transition.

This commit is contained in:
Benjamin A. Shelton 2026-06-22 15:03:55 -06:00
commit 00e5d779ae
Signed by: zancarius
GPG key ID: 8BD42DBD6C337E01

View file

@ -18,6 +18,28 @@ const base = import.meta.env.BASE_URL;
<link rel="icon" href={urlfrag(base, 'favicon.png')} /> <link rel="icon" href={urlfrag(base, 'favicon.png')} />
<meta name="generator" content={Astro.generator} /> <meta name="generator" content={Astro.generator} />
<title>{title}</title> <title>{title}</title>
<script>
document.documentElement.classList.toggle(
"dark",
localStorage.theme === "dark" ||
(!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches),
);
const toggle = document.querySelectorAll('[data-toggle-theme]');
toggle.forEach((b) => {
b.addEventListener('click', () => {
console.log(b.attributes);
const theme: string = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
if (theme === 'undefined' || theme === '' || theme === 'light') {
document.documentElement.classList.toggle('dark');
localStorage.theme = 'dark';
} else {
document.documentElement.classList.remove('dark');
localStorage.theme = 'light';
}
});
});
</script>
</head> </head>
<body class="flex flex-col min-h-screen"> <body class="flex flex-col min-h-screen">
<Header /> <Header />