Skip to content

Commit

Permalink
docs: <svelte:html> tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Dec 18, 2024
1 parent 2ed7da6 commit ab524ff
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let darkmode = $state(false);
</script>

<svelte:html />

<button onclick={() => darkmode = !darkmode}>toggle dark mode</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let darkmode = $state(false);
</script>

<svelte:html class={darkmode ? 'dark' : 'light'} />

<button onclick={() => darkmode = !darkmode}>toggle dark mode</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: <svelte:html>
---

The `<svelte:html>` element allows you to listen to events _and_ set attributes on the `<html>`. This is useful to for example set the `lang` tag dynamically, or to update `class`es that influence the whole page.

Add a conditional `class` attribute to the `<svelte:html>` tag...

```svelte
<!--- file: App.svelte --->
<svelte:html
+++class={darkmode ? 'dark' : 'light'}+++
/>
```

...and click the button to toggle the mode.

0 comments on commit ab524ff

Please sign in to comment.