Skip to content

Commit 63bfa5e

Browse files
committed
feat(4.1): Interactivity and Hydration
1 parent 6983b4c commit 63bfa5e

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎astro.config.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ export default /** @type {import('astro').AstroUserConfig} */ {
1919
devOptions: {
2020
// port: 3000, // The port to run the dev server on.
2121
tailwindConfig: './tailwind.config.js', // Path to tailwind.config.js if used, e.g. './tailwind.config.js'
22-
}
22+
},
23+
renderers: [
24+
'@astrojs/renderer-svelte',
25+
]
2326
};

‎src/components/BaseLayout.astro‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<body class="font-sans antialiased bg-gray-100 dark:bg-gray-800 transition-colors">
1+
<body class="font-sans antialiased">
2+
<div class="bg-gray-100 dark:bg-gray-800 transition-colors">
23
<main class="mx-auto max-w-4xl px-4 md:px-0">
34
<slot />
45
</main>
6+
</div>
57
</body>

‎src/components/Header.astro‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
import { SITE } from '$/config'
33
import SvgIcon from './SvgIcon.astro'
4+
import ModeSwitcher from './ModeSwitcher.svelte'
45
---
56

67
<header class="header">
@@ -26,6 +27,21 @@
2627
</SvgIcon>
2728
</a>
2829
</li>
30+
<li>
31+
<ModeSwitcher client:visible>
32+
<SvgIcon>
33+
<circle cx="12" cy="12" r="5"></circle>
34+
<line x1="12" y1="1" x2="12" y2="3"></line>
35+
<line x1="12" y1="21" x2="12" y2="23"></line>
36+
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
37+
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
38+
<line x1="1" y1="12" x2="3" y2="12"></line>
39+
<line x1="21" y1="12" x2="23" y2="12"></line>
40+
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
41+
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
42+
</SvgIcon>
43+
</ModeSwitcher>
44+
</li>
2945
</ul>
3046
</nav>
3147
</div>

‎src/components/ModeSwitcher.svelte‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte'
3+
4+
type ThemeType = 'dark' | 'light'
5+
6+
const THEME_DARK: ThemeType = 'dark'
7+
const THEME_LIGHT: ThemeType = 'light'
8+
let currTheme: ThemeType = THEME_DARK
9+
10+
11+
function toggleTheme() {
12+
window.document.body.classList.toggle(THEME_DARK)
13+
currTheme = localStorage.getItem('theme') === THEME_DARK ? THEME_LIGHT : THEME_DARK
14+
localStorage.setItem('theme', currTheme)
15+
}
16+
17+
onMount(() => {
18+
if (localStorage.getItem('theme') === THEME_DARK || (!('theme' in localStorage) && window.matchMedia(`(prefers-color-scheme: ${THEME_DARK})`).matches)) {
19+
window.document.body.classList.add(THEME_DARK)
20+
currTheme = THEME_DARK
21+
} else {
22+
window.document.body.classList.remove(THEME_DARK)
23+
currTheme = THEME_LIGHT
24+
}
25+
})
26+
</script>
27+
<button on:click={toggleTheme}>
28+
<slot theme={currTheme}/>
29+
</button>

0 commit comments

Comments
 (0)