Added script for handling dark/light mode transition.
This commit is contained in:
parent
41c1a901ad
commit
00e5d779ae
1 changed files with 22 additions and 0 deletions
|
|
@ -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 />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue