|
| 1 | +# @gpuix/svelte |
| 2 | + |
| 3 | +Svelte's [custom renderer API](https://github.com/sveltejs/svelte/pull/18042) targeting Zed's GPUI — |
| 4 | +the Svelte counterpart to `@gpuix/react`. |
| 5 | + |
| 6 | +```svelte |
| 7 | +<!-- Counter.svelte --> |
| 8 | +<script> |
| 9 | + let count = $state(0); |
| 10 | +</script> |
| 11 | +
|
| 12 | +<div style="padding: 32px; background-color: #1e1e2e; border-radius: 12px"> |
| 13 | + <div style="font-size: 48px; color: #cdd6f4; cursor: pointer" onclick={() => count++}> |
| 14 | + {count} |
| 15 | + </div> |
| 16 | +</div> |
| 17 | +``` |
| 18 | + |
| 19 | +```js |
| 20 | +import { render_hot } from '@gpuix/svelte'; |
| 21 | + |
| 22 | +render_hot(new URL('./Counter.svelte', import.meta.url), { |
| 23 | + title: 'GPUIX + Svelte', |
| 24 | + width: 820, |
| 25 | + height: 560 |
| 26 | +}); |
| 27 | +``` |
| 28 | +
|
| 29 | +``` |
| 30 | +cd examples && bun run counter-svelte |
| 31 | +``` |
| 32 | +
|
| 33 | +## Setup |
| 34 | +
|
| 35 | +This package depends on an unreleased Svelte branch (`custom-condition`), checked out as a sibling of |
| 36 | +this repo. `bun install` *copies* `file:` dependencies rather than symlinking them, so after every |
| 37 | +install run: |
| 38 | +
|
| 39 | +``` |
| 40 | +./scripts/link-svelte.sh # SVELTE_REPO=... to point elsewhere |
| 41 | +``` |
| 42 | +
|
| 43 | +The `--conditions custom-renderer` flag in the run script is **not optional**. Svelte's `exports` map |
| 44 | +resolves to its *server* build by default outside a browser, and `mount()` doesn't exist there. |
| 45 | +Without the condition you get a descriptive throw from `svelte/internal/flags/custom-renderer`. |
| 46 | +
|
| 47 | +## Hot reload |
| 48 | +
|
| 49 | +`render_hot` watches for `.svelte` changes and remounts. Use `render(Component, options)` instead if |
| 50 | +you don't want a watcher. |
| 51 | +
|
| 52 | +Note that `bun --hot` cannot do this job here, for two independent reasons: |
| 53 | +
|
| 54 | +- `.svelte` files are plugin-loaded, so they never enter Bun's watch graph and editing one triggers |
| 55 | + nothing. (Bun also ignores mtime-only changes — a `touch` is not enough, the content must differ.) |
| 56 | +- A `--hot` reload re-evaluates *Svelte's runtime* too, so the previous component belongs to a module |
| 57 | + instance the new one can't see and `unmount` reports it as never mounted. |
| 58 | +
|
| 59 | +Watching in-process avoids both: one Svelte runtime lives for the life of the process, the old tree |
| 60 | +unmounts properly, and only the component module is re-instantiated. The loader propagates its |
| 61 | +cache-busting `?v=` query to child components so a reload doesn't re-instantiate the root against |
| 62 | +stale children. |
| 63 | +
|
| 64 | +Set `GPUIX_SCREENSHOT=/path/to.png` to have each mount write a PNG — a window can't be inspected |
| 65 | +from a terminal, and Preview.app reloads on write. |
| 66 | +
|
| 67 | +## How it works |
| 68 | +
|
| 69 | +GPUI's tree is flat and id-based, and knows only `div`, `text` and its custom element types. Svelte's |
| 70 | +tree is DOM-shaped and full of **anchor nodes** — comments and empty text nodes marking every |
| 71 | +`{#if}`, `{#each}` and component boundary — plus fragments. None of those exist in GPUI. |
| 72 | +
|
| 73 | +So `src/renderer.js` keeps a **JS shadow tree** and projects it onto GPUI: |
| 74 | +
|
| 75 | +| Svelte node | GPUI | |
| 76 | +|---|---| |
| 77 | +| element | `createElement(id, tag)` | |
| 78 | +| text with content | `createElement(id, 'text')` + `setText` | |
| 79 | +| text that is empty or whitespace-only | *nothing* — materialized if it later gains content | |
| 80 | +| comment | *nothing, ever* | |
| 81 | +| fragment | splatted into the parent on insert | |
| 82 | +
|
| 83 | +Three properties make this tractable: |
| 84 | +
|
| 85 | +- **Virtual nodes are always leaves**, so "the next native node" is a flat scan of following |
| 86 | + siblings. A native node can never hide beneath a virtual one. |
| 87 | +- **Native ids are allocated lazily**, when a node first becomes reachable from the root. Svelte |
| 88 | + renders offscreen constantly (the shared each-block fragment, deferred `{#if}` branches, |
| 89 | + `<svelte:boundary>` pending content); eager creation would leak a Rust node per abandoned render. |
| 90 | +- **`remove()` never destroys.** Svelte removes and re-inserts the same node in consecutive |
| 91 | + statements, so removal only detaches and enqueues; `destroyElement` runs at commit time for nodes |
| 92 | + that are still detached. |
| 93 | +
|
| 94 | +Mutations queue up and ship as a single `applyBatch` call, committed before each `tick()` and |
| 95 | +immediately after each event. |
| 96 | +
|
| 97 | +## Styling |
| 98 | +
|
| 99 | +Svelte hands the renderer the `style` attribute as CSS **text**, never as an object, so `src/style.js` |
| 100 | +translates it into GPUI's camelCase `StyleDesc`: `background-color` → `backgroundColor`, `16px` → `16`, |
| 101 | +while `50%`, `auto` and `#1e1e2e` stay strings. `style:` directives and dynamic values work normally. |
| 102 | +
|
| 103 | +GPUI's nested pseudo-styles have no CSS-text spelling, so they get their own attributes: |
| 104 | +
|
| 105 | +```svelte |
| 106 | +<div style="background-color: #313244" hover="background-color: #45475a" active="opacity: 0.8"> |
| 107 | +``` |
| 108 | +
|
| 109 | +`class` is inert — GPUI has no CSS engine. |
| 110 | +
|
| 111 | +## Limits |
| 112 | +
|
| 113 | +Compile errors under `customRenderer` (enforced by the Svelte compiler): `{@html}`, `bind:` on |
| 114 | +elements, `transition:` / `in:` / `out:`, `animate:`, legacy `on:`, `<svelte:head|window|document|body>`, |
| 115 | +and `css: 'injected'`. Component `bind:` and `bind:this` are fine. |
| 116 | +
|
| 117 | +Beyond that: |
| 118 | +
|
| 119 | +- **No event bubbling.** GPUI dispatches straight to the element, so a parent's `onclick` does not |
| 120 | + fire for a child's click. (Same limitation as `@gpuix/react`.) |
| 121 | +- **Whitespace-only text is dropped.** GPUI lays out with flex, not inline text flow, so an |
| 122 | + inter-element space would render as a stray row. The cost is that a deliberate space between two |
| 123 | + expressions (`{a} {b}`) is lost — put it inside one expression instead. |
| 124 | +- **Unknown tags degrade to `div`** with a warning. GPUI has no `<span>`, `<p>` or `<section>`. |
| 125 | +- Only the events in `src/events.js` exist; anything else is silently not registered. |
| 126 | +
|
| 127 | +## Tests |
| 128 | +
|
| 129 | +``` |
| 130 | +bun --conditions custom-renderer --conditions development test/smoke.js # mount + interact + screenshot |
| 131 | +bun --conditions custom-renderer --conditions development test/reorder.js # keyed {#each} projection |
| 132 | +``` |
| 133 | +
|
| 134 | +Both run against `TestGpuixRenderer`, which drives the real Metal pipeline without opening a window. |
| 135 | +`reorder.js` is the important one: keyed reordering re-inserts nodes before anchors that have no GPUI |
| 136 | +presence, and it is the one place the projection can silently produce the wrong native order. |
0 commit comments