Skip to content

Example using Svelte 5 runes for state management #21

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions examples/svelte-state-rune/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Svelte State Rune
description: A uses a state rune with WXT's storage to enable clean subscriptions in Svelte (and TS) as well as persisting state.
---

```sh
npm install
npm run dev
```

Demonstrates how the browser.storage API allows different parts of the extension share state and reflect activity on the current page.

- The page fires a "counter:updated" event every second.
- The Content Script handles these events by pushing the event payload into session storage.
- The CounterState class watches the store and updates its reactive state property on change
- App.svelte reflects the value of `counterState.state`

Open dev tools or the extension service worker logs, click the extension's action icon and watch the events being fired by the active page update the counter.

When closing and re-opening the Popup, the state is persisted.
24 changes: 24 additions & 0 deletions examples/svelte-state-rune/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Emit events</h1>

<script>
let counter = 0;
setInterval(() => {
document.dispatchEvent(
new CustomEvent("counter:updated", {
detail: {
counter: counter++,
},
})
);
}, 1000);
</script>
</body>
</html>
Loading