page.mdx 2.8 KB

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