Skip to content

Commit da6c72d

Browse files
Example using Svelte 5 runes for state management (#21)
Co-authored-by: willow <[email protected]>
1 parent 21b27f4 commit da6c72d

25 files changed

+516
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Svelte State Rune
3+
description: A uses a state rune with WXT's storage to enable clean subscriptions in Svelte (and TS) as well as persisting state.
4+
---
5+
6+
```sh
7+
npm install
8+
npm run dev
9+
```
10+
11+
Demonstrates how the browser.storage API allows different parts of the extension share state and reflect activity on the current page.
12+
13+
- The page fires a "counter:updated" event every second.
14+
- The Content Script handles these events by pushing the event payload into session storage.
15+
- The CounterState class watches the store and updates its reactive state property on change
16+
- App.svelte reflects the value of `counterState.state`
17+
18+
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.
19+
20+
When closing and re-opening the Popup, the state is persisted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1>Emit events</h1>
10+
11+
<script>
12+
let counter = 0;
13+
setInterval(() => {
14+
document.dispatchEvent(
15+
new CustomEvent("counter:updated", {
16+
detail: {
17+
counter: counter++,
18+
},
19+
})
20+
);
21+
}, 1000);
22+
</script>
23+
</body>
24+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "svelte-state-rune",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "wxt",
8+
"dev:firefox": "wxt -b firefox",
9+
"build": "wxt build",
10+
"build:firefox": "wxt build -b firefox",
11+
"zip": "wxt zip",
12+
"zip:firefox": "wxt zip -b firefox",
13+
"check": "svelte-check --tsconfig ./tsconfig.json",
14+
"postinstall": "wxt prepare"
15+
},
16+
"devDependencies": {
17+
"@tsconfig/svelte": "^5.0.4",
18+
"@wxt-dev/module-svelte": "^2.0.3",
19+
"svelte": "^5.28.2",
20+
"svelte-check": "^4.1.6",
21+
"tslib": "^2.7.0",
22+
"typescript": "^5.8.2",
23+
"vite": "6.3.3",
24+
"wxt": "^0.20.5"
25+
}
26+
}
3 KB
Loading
559 Bytes
Loading
916 Bytes
Loading
1.3 KB
Loading
2.31 KB
Loading
Lines changed: 15 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)