|
| 1 | +import { pageMetadata } from "@/lib/page-metadata" |
| 2 | +export const metadata = pageMetadata("docs/api/svelte") |
| 3 | + |
| 4 | +# @json-render/svelte |
| 5 | + |
| 6 | +Svelte 5 components, providers, and helpers for rendering json-render specs. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +<PackageInstall packages="@json-render/core @json-render/svelte" /> |
| 11 | + |
| 12 | +Peer dependencies: `svelte ^5.0.0` and `zod ^4.0.0`. |
| 13 | + |
| 14 | +<PackageInstall packages="svelte zod" /> |
| 15 | + |
| 16 | +## Components |
| 17 | + |
| 18 | +### Renderer |
| 19 | + |
| 20 | +```svelte |
| 21 | +<Renderer |
| 22 | + spec={spec} // Spec | null |
| 23 | + registry={registry} |
| 24 | + loading={false} |
| 25 | +/> |
| 26 | +``` |
| 27 | + |
| 28 | +Renders a spec with your component registry. If `spec` is `null`, it renders nothing. |
| 29 | + |
| 30 | +### JsonUIProvider |
| 31 | + |
| 32 | +Convenience wrapper around `StateProvider`, `VisibilityProvider`, `ValidationProvider`, and `ActionProvider`. |
| 33 | + |
| 34 | +```svelte |
| 35 | +<JsonUIProvider |
| 36 | + initialState={{}} |
| 37 | + handlers={handlers} |
| 38 | + validationFunctions={validationFunctions} |
| 39 | +> |
| 40 | + <Renderer {spec} {registry} /> |
| 41 | +</JsonUIProvider> |
| 42 | +``` |
| 43 | + |
| 44 | +## defineRegistry |
| 45 | + |
| 46 | +Create a typed component registry and action handlers from a catalog. |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { defineRegistry } from "@json-render/svelte"; |
| 50 | + |
| 51 | +const { registry, handlers, executeAction } = defineRegistry(catalog, { |
| 52 | + components: { |
| 53 | + Card, |
| 54 | + Button, |
| 55 | + }, |
| 56 | + actions: { |
| 57 | + submit: async (params, setState, state) => { |
| 58 | + // custom action logic |
| 59 | + }, |
| 60 | + }, |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +`handlers` is designed for `JsonUIProvider`/`ActionProvider`. `executeAction` is an imperative helper. |
| 65 | + |
| 66 | +## Component Props |
| 67 | + |
| 68 | +Registry components receive `BaseComponentProps<TProps>`: |
| 69 | + |
| 70 | +```typescript |
| 71 | +interface BaseComponentProps<TProps> { |
| 72 | + props: TProps; |
| 73 | + children?: Snippet; |
| 74 | + emit: (event: string) => void; |
| 75 | + bindings?: Record<string, string>; |
| 76 | + loading?: boolean; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Use `emit("eventName")` to trigger handlers declared in the spec `on` bindings. |
| 81 | + |
| 82 | +## Context Helpers |
| 83 | + |
| 84 | +Use these helpers inside Svelte components: |
| 85 | + |
| 86 | +- `getStateValue(path)` - read/write state via `.current` |
| 87 | +- `getBoundProp(() => value, () => bindingPath)` - write back resolved `$bindState` / `$bindItem` values |
| 88 | +- `isVisible(condition)` - evaluate visibility via `.current` |
| 89 | +- `getAction(name)` - read a registered action handler via `.current` |
| 90 | +- `getFieldValidation(ctx, path, config)` - get field validation state + actions |
| 91 | + |
| 92 | +For advanced usage, access full contexts: |
| 93 | + |
| 94 | +- `getStateContext()` |
| 95 | +- `getActionContext()` |
| 96 | +- `getVisibilityContext()` |
| 97 | +- `getValidationContext()` |
| 98 | +- `getOptionalValidationContext()` |
| 99 | + |
| 100 | +## Streaming |
| 101 | + |
| 102 | +### createUIStream |
| 103 | + |
| 104 | +```typescript |
| 105 | +const stream = createUIStream({ |
| 106 | + api: "/api/generate-ui", |
| 107 | + onComplete: (spec) => console.log(spec), |
| 108 | +}); |
| 109 | + |
| 110 | +await stream.send("Create a login form"); |
| 111 | + |
| 112 | +console.log(stream.spec); |
| 113 | +console.log(stream.isStreaming); |
| 114 | +``` |
| 115 | + |
| 116 | +### createChatUI |
| 117 | + |
| 118 | +```typescript |
| 119 | +const chat = createChatUI({ api: "/api/chat-ui" }); |
| 120 | +await chat.send("Build a settings panel"); |
| 121 | +console.log(chat.messages, chat.isStreaming); |
| 122 | +``` |
| 123 | + |
| 124 | +## Schema Export |
| 125 | + |
| 126 | +Use `schema` from `@json-render/svelte` when defining catalogs for Svelte specs. |
0 commit comments