nzambello.dev/src/components/ThemeIcon.astro

97 lines
3 KiB
Plaintext
Raw Normal View History

2023-05-31 10:10:31 +02:00
---
2024-01-11 15:44:52 +01:00
2023-05-31 10:10:31 +02:00
---
2024-01-11 15:57:51 +01:00
<button id="themeToggle" title="Toggle theme" aria-label="Toggle theme">
2024-01-11 15:44:52 +01:00
<svg
width="30px"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
aria-label="Toggle theme"
>
2023-05-31 10:10:31 +02:00
<path
class="sun"
fill-rule="evenodd"
d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"
></path>
<path
class="moon"
fill-rule="evenodd"
d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"
></path>
</svg>
</button>
<style>
#themeToggle {
background: none;
border: 1px solid var(--item-bg-color-var);
background: var(--item-bg-color);
2024-01-11 15:44:52 +01:00
box-shadow:
0.25rem 0.25rem 1rem var(--neumorph-shadow-darker),
-0.25rem -0.25rem 1rem var(--neumorph-shadow-lighter) !important;
color: var(--text-color);
padding: 0.5rem;
aspect-ratio: 1;
flex-shrink: 0;
2023-05-31 10:10:31 +02:00
}
.sun {
fill: #fff;
}
.moon {
fill: transparent;
}
[data-theme='light'] .moon,
:root:not([data-theme='dark']) .moon {
fill: #000;
}
[data-theme='light'] .sun,
:root:not([data-theme='dark']) .sun {
fill: transparent;
}
@media only screen and (prefers-color-scheme: dark) {
:root:not([data-theme]) .sun {
fill: #fff;
}
}
[data-theme='dark'] :global(.sun) {
fill: #fff;
}
</style>
<script>
const theme = (() => {
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
return localStorage.getItem('theme');
2023-05-31 10:10:31 +02:00
}
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
return 'dark';
2023-05-31 10:10:31 +02:00
}
return 'light';
})();
2023-05-31 10:10:31 +02:00
if (theme === 'light') {
document.documentElement.setAttribute('data-theme', 'light');
2023-05-31 10:10:31 +02:00
} else {
document.documentElement.setAttribute('data-theme', 'dark');
2023-05-31 10:10:31 +02:00
}
window.localStorage.setItem('theme', theme || 'dark');
2023-05-31 10:10:31 +02:00
const handleToggleClick = () => {
const element = document.documentElement;
const currentTheme = element.getAttribute('data-theme');
const themeToSet = currentTheme === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', themeToSet);
2023-05-31 10:10:31 +02:00
localStorage.setItem('theme', themeToSet);
};
2023-05-31 10:10:31 +02:00
2024-01-11 15:44:52 +01:00
document
.getElementById('themeToggle')
?.addEventListener('click', handleToggleClick);
2023-05-31 10:10:31 +02:00
</script>