mirror of
https://github.com/kogakure/website-astro-stefanimhoff.de.git
synced 2026-02-03 20:15:27 +00:00
fix: wrap theme provider in self-invoking function
This commit is contained in:
@@ -2,25 +2,27 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
<script is:inline>
|
<script is:inline>
|
||||||
const getThemePreference = () => {
|
(function () {
|
||||||
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
|
const getThemePreference = () => {
|
||||||
return localStorage.getItem('theme');
|
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
|
||||||
|
return localStorage.getItem('theme');
|
||||||
|
}
|
||||||
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
|
};
|
||||||
|
|
||||||
|
const isDark = getThemePreference() === 'dark';
|
||||||
|
|
||||||
|
document.documentElement.classList[isDark ? 'add' : 'remove']('dark');
|
||||||
|
|
||||||
|
if (typeof localStorage !== 'undefined') {
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
const isDark = document.documentElement.classList.contains('dark');
|
||||||
|
localStorage.setItem('theme', isDark ? 'dark' : 'light');
|
||||||
|
});
|
||||||
|
observer.observe(document.documentElement, {
|
||||||
|
attributes: true,
|
||||||
|
attributeFilter: ['class'],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
})();
|
||||||
};
|
|
||||||
|
|
||||||
const isDark = getThemePreference() === 'dark';
|
|
||||||
|
|
||||||
document.documentElement.classList[isDark ? 'add' : 'remove']('dark');
|
|
||||||
|
|
||||||
if (typeof localStorage !== 'undefined') {
|
|
||||||
const observer = new MutationObserver(() => {
|
|
||||||
const isDark = document.documentElement.classList.contains('dark');
|
|
||||||
localStorage.setItem('theme', isDark ? 'dark' : 'light');
|
|
||||||
});
|
|
||||||
observer.observe(document.documentElement, {
|
|
||||||
attributes: true,
|
|
||||||
attributeFilter: ['class'],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user