Skip to content

Commit

Permalink
fix: use unique id for theme toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
bardenHa committed Oct 10, 2023
1 parent b93640b commit f4ee4b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib/layouts/full.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { constants } from '@/config';
<a href="/" title="Home">
<Image src="/h_avatar.svg" alt="Harry Barden's avatar" class="h-12 w-12" />
</a>
<ColorModeSwitcher class="sm:hidden" client:only />
<ColorModeSwitcher id="theme-toggle-mobile" class="sm:hidden" client:only />
</div>
<div class="flex items-center gap-6 sm:justify-center">
<ul class="flex items-center gap-4">
Expand All @@ -25,7 +25,7 @@ import { constants } from '@/config';
))
}
</ul>
<ColorModeSwitcher class="hidden sm:block" client:only />
<ColorModeSwitcher id="theme-toggle" class="hidden sm:block" client:only />
</div>
</nav>
</header>
Expand Down
12 changes: 7 additions & 5 deletions src/views/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ function onThemeToggle(): Theme {
const nextTheme = theme === 'dark' ? 'light' : 'dark';
window.theme.set(nextTheme);

const toggleElements = document.querySelectorAll('#theme-toggle');
for (const element of toggleElements) {
element.setAttribute('aria-checked', nextTheme === 'dark' ? 'true' : 'false');
const themeToggle = document.getElementById('theme-toggle');
const themeToggleMobile = document.getElementById('theme-toggle-mobile');

for (const element of [themeToggle, themeToggleMobile]) {
element?.setAttribute('aria-checked', nextTheme === 'dark' ? 'true' : 'false');
}

return nextTheme;
Expand All @@ -35,10 +37,10 @@ function onThemeToggle(): Theme {
return DEFAULT_THEME;
}

export function ColorModeSwitcher(props: Readonly<{ class?: string }>): JSX.Element {
export function ColorModeSwitcher(props: Readonly<{ id: string; class?: string }>): JSX.Element {
return (
<IconButton
id="theme-toggle"
id={props.id}
icon={<Moon aria-label="Toggle theme" />}
title="Toggle theme"
aria-label="Enable dark mode"
Expand Down

0 comments on commit f4ee4b1

Please sign in to comment.