@startside/swa 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,154 @@
1
+ # SWA in 60 lines
2
+
3
+ **A quickstart for the Startside Widget API (SWA) 1.0.** The full specification, [swa-spec.md](https://startside.app/swa/1.0/spec.md), is the
4
+ reference; this page is what you need to write a widget that conforms to it: one complete widget, the rules as a
5
+ table, and a checklist.
6
+
7
+ ## What a widget is
8
+
9
+ One HTML file, named `<name>.swa.html`: its metadata in the head, its settings as a hidden form, its markup in the
10
+ body, its code in a script, all talking to one global `widget`. A Startside page runs it in a sandboxed frame; opened
11
+ in a browser, it runs on its own page, with a menu, light and dark, and a width you can drag. Nothing to build or
12
+ install.
13
+
14
+ ## A complete widget
15
+
16
+ The latest headlines of a feed, chosen in its settings:
17
+
18
+ ```html
19
+ <!doctype html>
20
+ <html lang="en">
21
+ <head>
22
+ <script src="https://startside.app/swa/1.0/runtime.js"></script>
23
+ <title>Headlines</title>
24
+ <meta name="description" content="The latest headlines of a feed." />
25
+ <meta name="apiVersion" content="1.0" />
26
+ <meta name="category" content="News" />
27
+ <style>
28
+ .news ul { list-style: none; margin: 0; padding: 0; }
29
+ .news li { padding: 3px 0; border-bottom: 1px solid light-dark(#e5e7eb, #333a42); }
30
+ .news a { color: var(--link, #1e5bb8); }
31
+ .news .uwa-read a { color: var(--muted, #6b7280); }
32
+ .news [hidden] { display: none; }
33
+ </style>
34
+ </head>
35
+ <body>
36
+ <form id="preferences" hidden>
37
+ <label>Feed <input type="url" name="feed" value="https://daringfireball.net/feeds/main" /></label>
38
+ <label>Items <input type="range" name="count" min="1" max="20" value="5" /></label>
39
+ </form>
40
+ <div class="news">
41
+ <p class="state waiting">Loading…</p>
42
+ <p class="state failed" role="alert" hidden>Could not read the feed.</p>
43
+ <ul hidden></ul>
44
+ <template><li><a class="ext-link"></a></li></template>
45
+ </div>
46
+ <script>
47
+ const News = {
48
+ show(state) {
49
+ for (const el of widget.body.querySelectorAll('.state')) el.hidden = !el.classList.contains(state);
50
+ },
51
+ draw(feed) {
52
+ const list = widget.body.querySelector('ul');
53
+ const row = widget.body.querySelector('template').content.firstElementChild;
54
+ const items = feed.items.slice(0, widget.settings.get('count'));
55
+ list.replaceChildren(...items.map((item) => {
56
+ const li = row.cloneNode(true);
57
+ const a = li.querySelector('a');
58
+ a.href = item.url;
59
+ a.textContent = item.title || item.url; // text, never markup
60
+ return li;
61
+ }));
62
+ list.hidden = false;
63
+ News.show('none');
64
+ },
65
+ load(event, fresh) {
66
+ const url = String(widget.settings.get('feed'));
67
+ return widget.fetchFeed(url, { signal: event.signal, fresh }).then(News.draw, (error) => {
68
+ if (error.name !== 'AbortError' && !widget.body.querySelector('li')) News.show('failed');
69
+ });
70
+ },
71
+ };
72
+ widget.addEventListener('load', (event) => {
73
+ const list = widget.body.querySelector('ul');
74
+ list.addEventListener('click', (e) => {
75
+ const a = e.target.closest('a');
76
+ if (a) (e.preventDefault(), widget.openURL(a.href));
77
+ });
78
+ widget.readItems(list, 'li');
79
+ event.waitUntil(News.load(event, false));
80
+ });
81
+ widget.addEventListener('refresh', (event) => event.waitUntil(News.load(event, true)));
82
+ </script>
83
+ </body>
84
+ </html>
85
+ ```
86
+
87
+ What it shows, in order:
88
+
89
+ 1. **The runtime** in the head, by its full address, so the file also runs opened from disk.
90
+ 2. **Metadata**: a title and a description; `category` places it in a catalogue.
91
+ 3. **Settings as a hidden form**: a control per setting, its `name` the setting's, its `value` the default, its
92
+ label the setting's label. `widget.settings.get()` gives each typed: a range a number, a checkbox a boolean.
93
+ 4. **Markup in the file**: every state (loading, failed, the list) is there from the start, shown or hidden with
94
+ `hidden`; a `template` gives each row.
95
+ 5. **Code in one object**, wired once in `load`: one listener on the list for every row, the read state from
96
+ `readItems`. `load` and `refresh` give `waitUntil` their promise, and pass on the `signal` that cancels a request
97
+ when the next load starts.
98
+ 6. **Text set with `textContent`**, never fetched text as markup.
99
+ 7. **Colors from the host's properties**, each with a fallback, and `light-dark()` for both modes.
100
+
101
+ ## The rules
102
+
103
+ | | Rule | Section |
104
+ |---|---|---|
105
+ | MUST | Name the file `<name>.swa.html`, begin it with `<!doctype html>` | 4.1, 4.2 |
106
+ | MUST | Load the runtime once, in the head: `https://startside.app/swa/1.0/runtime.js` (where startside.app can't be loaded, the npm package on a CDN, `https://cdn.jsdelivr.net/npm/@startside/swa@1/swa/1.0/runtime.js`; or inlined: `<script data-runtime="swa/1.0">`) | 4.3 |
107
+ | MUST | Declare settings in `<form id="preferences" hidden>`, one named control each | 5 |
108
+ | MUST NOT | Use `localStorage`, `sessionStorage`, IndexedDB or cookies: `widget.storage` or a hidden setting | 4.8, 7.8 |
109
+ | MUST NOT | Use UWA's API (`onLoad`, `getValue`, `UWA.Data`): events, `widget.settings`, `widget.fetch`, `widget.fetchFeed` | 7, 8, 11 |
110
+ | MUST NOT | Style or script the page around the widget on its own page | 10.3 |
111
+ | MUST | Work framed: no inline mode, no access to the page | 10.1, 10.2 |
112
+ | MUST NOT | Use `alert()`, `confirm()` or `prompt()` (off in a frame): ask in the widget's own box | 10.1 |
113
+ | SHOULD | Write the markup in the file and update it in place; `setBody` only for content made whole at each draw | 4.9, 7.3.1 |
114
+ | SHOULD NOT | Put fetched or typed text into markup (`innerHTML`, a `setBody` string) | 7.3 |
115
+ | SHOULD | Keep a game in progress or the person's data in a hidden setting, read defensively, and under the host's limit if it grows (Startside: 20,000 characters a setting) | 7.5, 7.8 |
116
+ | SHOULD | Let the host set the width; the height is the content's; never `100vh` | 7.3.2 |
117
+ | SHOULD | While it loads, show a placeholder at the size of what's coming (a skeleton of its rows, its picture's box), not a one-line "Loading…": the widget doesn't change size as its content arrives | 7.3.1 |
118
+ | SHOULD | Make a board, a grid or a list moved through with the arrow keys one Tab stop, the keys heard on the widget's own element; a list of plain links, as above, keeps its links in the Tab order | 7.3.1, 14 |
119
+ | SHOULD | Mark where the keyboard lands when the host moves the focus into the widget, when that isn't its first control: `autofocus` on a game's board (`<div class="board" tabindex="0" autofocus>`). Call `preventDefault()` on the Escape and arrow keys it uses itself: left uncancelled, a host may use Escape to leave the widget and an arrow to go to the next | 6.8 |
120
+ | SHOULD | Announce results and errors in a `role="status"` element present from the start, nothing as it first draws; keep the focus through updates | 14 |
121
+ | SHOULD | Write colors with `light-dark()`, other dark-mode changes under `:root[data-color-scheme="dark"]`, not `prefers-color-scheme` | 4.7 |
122
+ | SHOULD | Scope every rule under the widget's root class (`.news li`); give each host property a fallback, `var(--text, #222)`; avoid the host's class names (`loading`, `spinner`) | 4.7 |
123
+ | SHOULD | Do nothing at rest: an animation that draws the eye plays a few times; timers only for what changes on screen | 7.3.1 |
124
+ | SHOULD | Follow its width with a `ResizeObserver` on its own element when its layout depends on it | 7.3.2 |
125
+ | SHOULD | Meet WCAG 2.2 AA: keyboard alone, names on controls, 4.5:1 text, `prefers-reduced-motion` | 14 |
126
+ | MUST | Show only pictures it may show (public domain, or a licence or permission that allows it), their required credits kept; a picture opened large carries its own: `widget.openPhoto({ src, alt, credit })` | 7.12 |
127
+ | MAY | Declare actions of its own for its options menu, in the file: `<menu id="actions" hidden>` with one `<button type="button" name="reset">Reset</button>` each, five at most (`disabled` or `hidden` as it starts, `data-confirm="…?"` for a question the host asks first). Answer the `action` event (`event.detail.id`), and change their state as the widget's does with `widget.setActionStates({ reset: { disabled: false } })` | 7.13 |
128
+ | MAY | Hand the person a file it makes, from one of those actions only: `data-save=".cook"` on its button, then `widget.saveFile({ name, type, data }, { action: event })` in its `action` listener | 7.13 |
129
+ | MAY | Carry its icon in the file: `<link rel="icon" href="data:image/svg+xml,…">`, kept when the file is shared by itself (best: an SVG drawn for 16 pixels, or a 64 × 64 PNG) | 4.6 |
130
+
131
+ ## Checklist
132
+
133
+ - [ ] `npx @startside/swa validate name.swa.html` reports no error (Appendix F): it reads the file without running it.
134
+ - [ ] It opens in a browser, from disk and from a server, with no error in the widget or the console.
135
+ - [ ] Each setting changed in the menu (▾) applies, and Refresh keeps what it should.
136
+ - [ ] It reads well light and dark, narrow (300 pixels) and wide, maximized: open it as
137
+ `name.swa.html#mode=dark&width=300`, then light and wider.
138
+ - [ ] Everything works with the keyboard alone; the focus is visible and never lost. A game plays with its keys once the
139
+ focus is on it, and the keys it doesn't use are left uncancelled.
140
+ - [ ] Saved data from an older version, or edited by hand, doesn't break it.
141
+ - [ ] Nothing moves or runs when nobody uses it.
142
+
143
+ ## Next
144
+
145
+ - The types, for an editor: `https://startside.app/swa/1.0/swa.d.ts` (Appendix F).
146
+ - The tools: the npm package `@startside/swa`, its `validate` and `inline` commands (Appendix F).
147
+ - More examples: a blog (Example 4), a game (Example 6), a to-do list with an action of its own in the options menu
148
+ (Example 7), in Appendix A of the specification.
149
+
150
+ ---
151
+
152
+ *Published at https://startside.app/swa/1.0/quickstart.html (Markdown: https://startside.app/swa/1.0/quickstart.md). ©
153
+ 2026 François Hodierne, shared under the [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0/)
154
+ licence (CC BY 4.0).*