Initial commit
This commit is contained in:
commit
b3a42bb0c8
91 changed files with 9419 additions and 0 deletions
49
src/components/ThemeToggle.astro
Normal file
49
src/components/ThemeToggle.astro
Normal file
|
@ -0,0 +1,49 @@
|
|||
---
|
||||
|
||||
---
|
||||
|
||||
<button id="theme-toggle" class="w-8 h-8 -mr-2 flex items-center justify-center cursor-pointer" aria-label="Change color scheme">
|
||||
<svg class="w-4 h-4 fill-current" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
|
||||
<circle cx="8" cy="8" r="8"></circle>
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<script>
|
||||
document.addEventListener('astro:page-load', () => {
|
||||
const theme = (() => {
|
||||
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
|
||||
return localStorage.getItem('theme') || 'light';
|
||||
}
|
||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
return 'dark';
|
||||
}
|
||||
return 'light';
|
||||
})();
|
||||
|
||||
if (theme === 'light') {
|
||||
document.documentElement.classList.remove('dark');
|
||||
} else {
|
||||
document.documentElement.classList.add('dark');
|
||||
}
|
||||
|
||||
window.localStorage.setItem('theme', theme);
|
||||
|
||||
const handleToggleClick = () => {
|
||||
const element = document.documentElement;
|
||||
element.classList.toggle('dark');
|
||||
|
||||
const isDark = element.classList.contains('dark');
|
||||
localStorage.setItem('theme', isDark ? 'dark' : 'light');
|
||||
};
|
||||
|
||||
document.getElementById('theme-toggle')?.addEventListener('click', handleToggleClick);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
document.addEventListener('astro:after-swap', () => {
|
||||
if (localStorage.theme === 'dark') {
|
||||
document.documentElement.classList.add('dark');
|
||||
}
|
||||
});
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue