generated from autonomic-cooperative/astro-payload-template
Add vanilla theme switcher
This commit is contained in:
parent
aa7a5acf94
commit
e20afa3313
@ -1,9 +1,11 @@
|
|||||||
---
|
---
|
||||||
import { getCurrentYear } from "@/utils/date";
|
import { getCurrentYear } from "@/utils/date";
|
||||||
|
import ThemeSwitcher from "./ThemeSwitcher.astro";
|
||||||
---
|
---
|
||||||
|
|
||||||
<footer>
|
<footer class="flex justify-between">
|
||||||
<p class="text-lg font-semibold">
|
<p class="text-lg font-semibold">
|
||||||
© {getCurrentYear()} Example Website. All rights reserved.
|
© {getCurrentYear()} Example Website. All rights reserved.
|
||||||
</p>
|
</p>
|
||||||
|
<ThemeSwitcher/>
|
||||||
</footer>
|
</footer>
|
12
astro/src/components/ThemeSwitcher.astro
Normal file
12
astro/src/components/ThemeSwitcher.astro
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
---
|
||||||
|
<select id="theme-selector">
|
||||||
|
<option value="autonomic">Autonomic</option>
|
||||||
|
<option value="light">Light</option>
|
||||||
|
<option value="dark">Dark</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { handleThemeChange, switchTheme, initializeThemeSelector } from "@/utils/theme"
|
||||||
|
initializeThemeSelector()
|
||||||
|
</script>
|
21
astro/src/utils/theme.ts
Normal file
21
astro/src/utils/theme.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
export function switchTheme(themeClass: string): void {
|
||||||
|
const bodyElement = document.body;
|
||||||
|
const classesToRemove = ['light', 'dark', 'autonomic'];
|
||||||
|
|
||||||
|
bodyElement.classList.remove(...classesToRemove);
|
||||||
|
bodyElement.classList.add(themeClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleThemeChange(event: Event): void {
|
||||||
|
const selectElement = event.target as HTMLSelectElement;
|
||||||
|
const selectedTheme = selectElement.value;
|
||||||
|
|
||||||
|
switchTheme(selectedTheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function initializeThemeSelector(): void {
|
||||||
|
const themeSelector = document.getElementById('theme-selector');
|
||||||
|
if (themeSelector) {
|
||||||
|
themeSelector.addEventListener('change', handleThemeChange);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user