Skip to content

Commit

Permalink
work on dark mode toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Nov 17, 2024
1 parent 4254489 commit 9979b8b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/components/Nav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
let pathname = '';
let isDark = false;
onMount(() => {
isDark = document.documentElement.classList.contains('dark');
// Set up a mutation observer to watch for class changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.attributeName === 'class') {
isDark = document.documentElement.classList.contains('dark');
}
});
});
observer.observe(document.documentElement, { attributes: true });
});
onMount(() => {
session = getSession();
if (session) {
Expand Down Expand Up @@ -49,9 +62,12 @@
<button
class="icon-button"
on:click={() => {
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const isDark = window.matchMedia(
'(prefers-color-scheme: dark)',
).matches;
const override = localStorage.getItem('theme');
const newTheme = override === 'dark' || (!override && isDark) ? 'light' : 'dark';
const newTheme =
override === 'dark' || (!override && isDark) ? 'light' : 'dark';
localStorage.setItem('theme', newTheme);
document.documentElement.classList.toggle('dark', newTheme === 'dark');
}}
Expand Down
1 change: 1 addition & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const primaryDark = COLORS.warm.lighter,

a {
color: var(--primary);
opacity: 0.9;
}

code {
Expand Down

0 comments on commit 9979b8b

Please sign in to comment.