Skip to content

docs: <svelte:html> tutorial #1040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
docs: <svelte:html> tutorial
  • Loading branch information
dummdidumm committed Dec 18, 2024
commit ab524ffc9092c288215d8dab23f5a12f86e5aa1c
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.
Loading