Skip to content

Commit cb6ef7b

Browse files
committed
Add example for Svelte state management with WXT
Use latest Vite and WXT
1 parent b0174b1 commit cb6ef7b

25 files changed

+2518
-151
lines changed

examples/svelte-state-rune/README.md

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.

examples/svelte-state-rune/index.html

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>

0 commit comments

Comments
 (0)