@serene-pub/ui-preview 0.6.0-pr-1

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.
package/src/app.html ADDED
@@ -0,0 +1,34 @@
1
+ <!doctype html>
2
+ <html lang="en" data-theme="cerberus" data-mode="dark">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <link rel="icon" href="%sveltekit.assets%/favicon.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <title>Serene Pub — surface harness</title>
8
+ <script>
9
+ /**
10
+ * The persisted theme, applied before first paint.
11
+ *
12
+ * The harness renders client-only (plugin UI is client-only by
13
+ * contract, 10 §9), so without this the attributes above are on
14
+ * screen for the whole bundle load and every reload flashes the
15
+ * default theme. Blocking and tiny on purpose: it has to run before
16
+ * the first paint or it is not solving anything. Keep the fallbacks
17
+ * in step with `src/lib/theme.svelte.ts`.
18
+ */
19
+ try {
20
+ var t = JSON.parse(localStorage.getItem('serene-pub:harness-theme') || 'null')
21
+ if (t && typeof t.theme === 'string')
22
+ document.documentElement.setAttribute('data-theme', t.theme)
23
+ if (t && (t.mode === 'light' || t.mode === 'dark'))
24
+ document.documentElement.setAttribute('data-mode', t.mode)
25
+ } catch (e) {
26
+ /* a corrupt preference is not worth a blank page */
27
+ }
28
+ </script>
29
+ %sveltekit.head%
30
+ </head>
31
+ <body data-sveltekit-preload-data="hover">
32
+ <div style="display: contents">%sveltekit.body%</div>
33
+ </body>
34
+ </html>
@@ -0,0 +1,224 @@
1
+ <script lang="ts">
2
+ /**
3
+ * An in-document component (10 §2's virtual tier), mounted through the same
4
+ * three-call ABI core uses — `mount(el, props, ctx)` / `update` / `destroy`
5
+ * for vanilla, Svelte 5's `mount`/`unmount` for a Svelte default export.
6
+ *
7
+ * Two honesties the harness keeps:
8
+ *
9
+ * · **Errors are caught and shown, not swallowed.** Core wraps every ABI
10
+ * call in try/catch (10 §7) precisely because a throwing surface must
11
+ * not take the page with it; a harness that let the throw escape would
12
+ * be teaching the opposite of what ships.
13
+ * · **No adapter is faked.** Only the frameworks a declaration may name
14
+ * (`svelte`, `vanilla`) are mounted; anything else exports nothing
15
+ * mountable and says so.
16
+ */
17
+ import { mount as svelteMount, unmount as svelteUnmount, untrack } from 'svelte'
18
+ import type { PreviewTarget } from '@serene-pub/sdk'
19
+ import { components } from 'virtual:serene-pub/surfaces'
20
+ import { theme } from './theme.svelte.js'
21
+ import type { Fixtures } from './fixtures.js'
22
+
23
+ interface Props {
24
+ target: PreviewTarget
25
+ fixtures: Fixtures
26
+ /** Values from the surface's generated form — what `ctx.settings` is. */
27
+ settings?: Record<string, unknown>
28
+ }
29
+ let { target, fixtures, settings = {} }: Props = $props()
30
+
31
+ let host = $state<HTMLDivElement | null>(null)
32
+ let error = $state<string | null>(null)
33
+ let mounted: { destroy: () => void; update?: (p: unknown) => void } | null = null
34
+
35
+ /**
36
+ * The live props object handed to the component at mount, and mutated
37
+ * afterwards.
38
+ *
39
+ * This indirection is the ABI, not a convenience: Svelte 5's `mount()`
40
+ * reads the props object once, so passing a fresh object on every change
41
+ * would do nothing at all — the component would render its first values
42
+ * forever. 10 §7 names the shape ("Svelte 5 via `mount`/`unmount` with a
43
+ * `$state` props object"), and it is also what makes the recycling
44
+ * contract real (10 §4): props move, the handle does not, and nothing
45
+ * remounts because a setting changed.
46
+ */
47
+ const live = $state<Record<string, unknown>>({})
48
+
49
+ /**
50
+ * What the component receives at render. `settings` is the generated
51
+ * form's output — component-side settings, which is the only kind that
52
+ * may reach a browser (12 §6) — and `theme` is the active theme id, which
53
+ * 10 §6 rules is a prop rather than an event channel.
54
+ */
55
+ const ctx = $derived({
56
+ ...fixtures.ctx,
57
+ settings: { ...((fixtures.ctx.settings as object) ?? {}), ...settings },
58
+ theme: theme.current.theme,
59
+ mode: theme.current.mode,
60
+ surface: target.point
61
+ })
62
+ const incoming = $derived({ ...fixtures.props, ctx })
63
+
64
+ $effect(() => {
65
+ const el = host
66
+ const load = components[target.id]
67
+ if (!el || !load) return
68
+ // Named `current`, not `live`: the props object below is `live`, and a
69
+ // shadowing local here silently handed the component `true` as its
70
+ // entire props.
71
+ let current = true
72
+ error = null
73
+
74
+ load()
75
+ .then((mod) => {
76
+ if (!current) return
77
+ teardown()
78
+ // The default export, or the module itself for the vanilla ABI's
79
+ // `{ mount, update, destroy }` shape. A named Svelte export is
80
+ // deliberately not guessed at: core mounts what the declaration
81
+ // points to, and a harness that searched for a component core
82
+ // would not find is a harness that lies.
83
+ const exported = (mod.default ?? mod) as any
84
+ if (typeof exported?.mount === 'function') {
85
+ // Vanilla ABI: the module owns its own root.
86
+ const handle = exported.mount(el, live, ctx)
87
+ mounted = {
88
+ destroy: () => exported.destroy?.(handle),
89
+ update: (p) => exported.update?.(handle, p)
90
+ }
91
+ } else if (typeof exported === 'function') {
92
+ const instance = svelteMount(exported, { target: el, props: live })
93
+ mounted = { destroy: () => svelteUnmount(instance) }
94
+ } else {
95
+ error =
96
+ `${target.entry} exports nothing mountable — expected a Svelte component as ` +
97
+ `the default export, or a { mount, update, destroy } module.`
98
+ }
99
+ })
100
+ .catch((e) => {
101
+ if (current) error = `${e?.message ?? e}`
102
+ })
103
+
104
+ return () => {
105
+ current = false
106
+ teardown()
107
+ }
108
+ })
109
+
110
+ /**
111
+ * Prop changes go through `update` where the ABI has one — recycling a
112
+ * handle instead of destroy-then-mount is the contract (10 §4), and the
113
+ * harness should exercise the path core takes.
114
+ *
115
+ * Deep-read for the same reason the frame stage does it: naming the object
116
+ * subscribes to the reference, and every edit in the generated settings
117
+ * form changes a field inside it, not the object itself.
118
+ */
119
+ const signature = $derived.by(() => {
120
+ try {
121
+ return JSON.stringify(incoming)
122
+ } catch {
123
+ return String(Math.random())
124
+ }
125
+ })
126
+
127
+ $effect(() => {
128
+ // Deep-read for the dependency, then fold into the live object the
129
+ // component is already holding. The fold is untracked because diffing
130
+ // keys reads `live` and writing it would otherwise be a read and a
131
+ // write of the same state in one effect — a wedged page, not an error.
132
+ void signature
133
+ untrack(() => {
134
+ for (const k of Object.keys(live)) if (!(k in incoming)) delete live[k]
135
+ Object.assign(live, incoming)
136
+ })
137
+ try {
138
+ mounted?.update?.(live)
139
+ } catch (e) {
140
+ error = `update() threw: ${(e as Error).message}`
141
+ }
142
+ })
143
+
144
+ function teardown() {
145
+ try {
146
+ mounted?.destroy()
147
+ } catch (e) {
148
+ error = `destroy() threw: ${(e as Error).message}`
149
+ }
150
+ mounted = null
151
+ if (host) host.innerHTML = ''
152
+ }
153
+ </script>
154
+
155
+ <div class="stage">
156
+ <div class="bar">
157
+ <span class="muted">{target.point}</span>
158
+ <span class="muted">{target.framework ?? 'svelte'}</span>
159
+ <span class="grow"></span>
160
+ <code class="muted">{target.entry}</code>
161
+ </div>
162
+ {#if error}
163
+ <div class="problems">
164
+ <h3>The component did not mount</h3>
165
+ <pre>{error}</pre>
166
+ </div>
167
+ {/if}
168
+ <div class="body"><div bind:this={host} class="mount"></div></div>
169
+
170
+ <details class="received">
171
+ <summary>What the component received</summary>
172
+ <pre>{JSON.stringify(live, null, 2)}</pre>
173
+ </details>
174
+ </div>
175
+
176
+ <style>
177
+ .stage {
178
+ display: flex;
179
+ flex-direction: column;
180
+ flex: 1;
181
+ min-height: 0;
182
+ }
183
+ .bar {
184
+ display: flex;
185
+ gap: 0.6rem;
186
+ align-items: center;
187
+ padding: 0.5rem 0.8rem;
188
+ border-bottom: 1px solid var(--line);
189
+ }
190
+ .grow {
191
+ flex: 1;
192
+ }
193
+ .body {
194
+ flex: 1;
195
+ min-height: 0;
196
+ overflow: auto;
197
+ padding: 1.2rem;
198
+ }
199
+ .problems {
200
+ margin: 0.8rem;
201
+ }
202
+ .received {
203
+ border-top: 1px solid currentColor;
204
+ padding: 0 0.8rem 0.6rem;
205
+ max-height: 30vh;
206
+ overflow: auto;
207
+ }
208
+ .received summary {
209
+ cursor: pointer;
210
+ padding: 0.5rem 0;
211
+ font-size: 0.8rem;
212
+ letter-spacing: 0.06em;
213
+ text-transform: uppercase;
214
+ opacity: 0.7;
215
+ }
216
+ .received pre {
217
+ font-size: 0.74rem;
218
+ margin: 0;
219
+ }
220
+ pre {
221
+ margin: 0;
222
+ white-space: pre-wrap;
223
+ }
224
+ </style>