page.mdx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/vue")
  3. # @json-render/vue
  4. Vue 3 components, providers, and composables.
  5. ## Providers
  6. ### StateProvider
  7. ```vue
  8. <StateProvider :initial-state="object" :on-state-change="fn">
  9. <!-- children -->
  10. </StateProvider>
  11. ```
  12. <table>
  13. <thead>
  14. <tr>
  15. <th>Prop</th>
  16. <th>Type</th>
  17. <th>Description</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <tr>
  22. <td><code>store</code></td>
  23. <td><code>StateStore</code></td>
  24. <td>External store (controlled mode). When provided, <code>initialState</code> and <code>onStateChange</code> are ignored.</td>
  25. </tr>
  26. <tr>
  27. <td><code>initialState</code></td>
  28. <td><code>Record&lt;string, unknown&gt;</code></td>
  29. <td>Initial state model (uncontrolled mode).</td>
  30. </tr>
  31. <tr>
  32. <td><code>onStateChange</code></td>
  33. <td><code>{'(changes: Array<{ path: string; value: unknown }>) => void'}</code></td>
  34. <td>Callback when state changes (uncontrolled mode). Called once per <code>set</code> or <code>update</code> with all changed entries.</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. #### External Store (Controlled Mode)
  39. Pass a `StateStore` to bypass the internal state and wire json-render to any state management library:
  40. ```typescript
  41. import { createStateStore, type StateStore } from "@json-render/vue";
  42. const store = createStateStore({ count: 0 });
  43. ```
  44. ```vue
  45. <StateProvider :store="store">
  46. <!-- children -->
  47. </StateProvider>
  48. ```
  49. ```typescript
  50. // Mutate from anywhere — Vue re-renders automatically:
  51. store.set("/count", 1);
  52. ```
  53. ### ActionProvider
  54. ```vue
  55. <ActionProvider :handlers="Record<string, ActionHandler>" :navigate="fn">
  56. <!-- children -->
  57. </ActionProvider>
  58. // type ActionHandler = (params: Record<string, unknown>) => void | Promise<void>;
  59. ```
  60. ### VisibilityProvider
  61. ```vue
  62. <VisibilityProvider>
  63. <!-- children -->
  64. </VisibilityProvider>
  65. ```
  66. `VisibilityProvider` reads state from the parent `StateProvider` automatically. Conditions in specs use the `VisibilityCondition` format with `$state` paths (e.g. `{ "$state": "/path" }`, `{ "$state": "/path", "eq": value }`). See [visibility](/docs/visibility) for the full syntax.
  67. ### ValidationProvider
  68. ```vue
  69. <ValidationProvider :custom-functions="Record<string, ValidationFunction>">
  70. <!-- children -->
  71. </ValidationProvider>
  72. // type ValidationFunction = (value: unknown, args?: object) => boolean | Promise<boolean>;
  73. ```
  74. ## defineRegistry
  75. Create a type-safe component registry from a catalog. Components receive `props`, `children`, `emit`, `on`, and `loading` with catalog-inferred types.
  76. When the catalog declares actions, the `actions` field is required. When the catalog has no actions (e.g. `actions: {}`), the field is optional. When passing stubs, any `async () => {}` is sufficient.
  77. ```typescript
  78. import { h } from "vue";
  79. import { defineRegistry } from "@json-render/vue";
  80. const { registry } = defineRegistry(catalog, {
  81. components: {
  82. Card: ({ props, children }) =>
  83. h("div", { class: "card" }, [h("h3", null, props.title), children]),
  84. Button: ({ props, emit }) =>
  85. h("button", { onClick: () => emit("press") }, props.label),
  86. },
  87. // Required when catalog declares actions:
  88. actions: {
  89. submit: async (params) => { /* ... */ },
  90. },
  91. });
  92. // Pass to <Renderer>
  93. // <Renderer :spec="spec" :registry="registry" />
  94. ```
  95. ## Components
  96. ### Renderer
  97. ```vue
  98. <Renderer
  99. :spec="Spec" // The UI spec to render
  100. :registry="Registry" // Component registry (from defineRegistry)
  101. :loading="boolean" // Optional loading state
  102. :fallback="Component" // Optional fallback for unknown types
  103. />
  104. ```
  105. ### Component Props (via defineRegistry)
  106. ```typescript
  107. import type { VNode } from "vue";
  108. interface ComponentContext<P> {
  109. props: P; // Typed props from catalog
  110. children?: VNode | VNode[]; // Rendered children (for container components)
  111. emit: (event: string) => void; // Emit a named event (always defined)
  112. on: (event: string) => EventHandle; // Get event handle with metadata
  113. loading?: boolean;
  114. bindings?: Record<string, string>; // State paths from $bindState/$bindItem expressions
  115. }
  116. interface EventHandle {
  117. emit: () => void; // Fire the event
  118. shouldPreventDefault: boolean; // Whether any binding requested preventDefault
  119. bound: boolean; // Whether any handler is bound
  120. }
  121. ```
  122. Use `emit("press")` for simple event firing. Use `on("click")` when you need metadata like `shouldPreventDefault`:
  123. ```typescript
  124. Link: ({ props, on }) => {
  125. const click = on("click");
  126. return h("a", {
  127. href: props.href,
  128. onClick: (e: MouseEvent) => {
  129. if (click.shouldPreventDefault) e.preventDefault();
  130. click.emit();
  131. },
  132. }, props.label);
  133. },
  134. ```
  135. ### BaseComponentProps
  136. Catalog-agnostic base type for building reusable component libraries that are not tied to a specific catalog:
  137. ```typescript
  138. import type { BaseComponentProps } from "@json-render/vue";
  139. const Card = ({ props, children }: BaseComponentProps<{ title?: string }>) =>
  140. h("div", null, [props.title, children]);
  141. ```
  142. ## Composables
  143. ### useStateStore
  144. ```typescript
  145. const {
  146. state, // ShallowRef<StateModel> — access with state.value
  147. get, // (path: string) => unknown
  148. set, // (path: string, value: unknown) => void
  149. update, // (updates: Record<string, unknown>) => void
  150. } = useStateStore();
  151. ```
  152. > **Note:** `state` is a `ShallowRef<StateModel>`, not a plain object. Use `state.value` to read the current state. This differs from the React renderer.
  153. ### useStateValue
  154. ```typescript
  155. const value = useStateValue(path: string); // ComputedRef<T | undefined>
  156. ```
  157. Returns a `ComputedRef` that automatically updates when the state at `path` changes. Use `.value` to access the current value.
  158. ### useStateBinding (deprecated)
  159. > **Deprecated.** Use `$bindState` expressions with `bindings` prop instead.
  160. ```typescript
  161. const [value, setValue] = useStateBinding(path: string);
  162. // value: ComputedRef<T | undefined>
  163. // setValue: (value: T) => void
  164. ```
  165. ### useActions
  166. ```typescript
  167. const { execute } = useActions();
  168. // execute(binding: ActionBinding) => Promise<void>
  169. ```
  170. ### useAction
  171. ```typescript
  172. const { execute, isLoading } = useAction(binding: ActionBinding);
  173. // execute: () => Promise<void>
  174. // isLoading: ComputedRef<boolean>
  175. ```
  176. ### useIsVisible
  177. ```typescript
  178. const isVisible = useIsVisible(condition?: VisibilityCondition);
  179. ```
  180. ### useFieldValidation
  181. ```typescript
  182. const {
  183. state, // ComputedRef<FieldValidationState>
  184. validate, // () => ValidationResult
  185. touch, // () => void
  186. clear, // () => void
  187. errors, // ComputedRef<string[]>
  188. isValid, // ComputedRef<boolean>
  189. } = useFieldValidation(path: string, config?: ValidationConfig);
  190. ```
  191. `ValidationConfig` is `{ checks?: ValidationCheck[], validateOn?: 'change' | 'blur' | 'submit' }`.
  192. ## Differences from `@json-render/react`
  193. <table>
  194. <thead>
  195. <tr>
  196. <th>API</th>
  197. <th>React</th>
  198. <th>Vue</th>
  199. <th>Note</th>
  200. </tr>
  201. </thead>
  202. <tbody>
  203. <tr>
  204. <td><code>useStateStore().state</code></td>
  205. <td><code>StateModel</code> (plain object)</td>
  206. <td><code>ShallowRef&lt;StateModel&gt;</code></td>
  207. <td>Vue reactivity; use <code>state.value</code></td>
  208. </tr>
  209. <tr>
  210. <td><code>useStateValue()</code></td>
  211. <td><code>T | undefined</code></td>
  212. <td><code>ComputedRef&lt;T | undefined&gt;</code></td>
  213. <td>Vue reactivity; use <code>.value</code></td>
  214. </tr>
  215. <tr>
  216. <td><code>useStateBinding()</code></td>
  217. <td><code>[T | undefined, setter]</code></td>
  218. <td><code>[ComputedRef&lt;T | undefined&gt;, setter]</code></td>
  219. <td>Vue reactivity; use <code>value.value</code></td>
  220. </tr>
  221. <tr>
  222. <td><code>useAction().isLoading</code></td>
  223. <td><code>boolean</code></td>
  224. <td><code>ComputedRef&lt;boolean&gt;</code></td>
  225. <td>Vue reactivity; use <code>.value</code></td>
  226. </tr>
  227. <tr>
  228. <td><code>useFieldValidation().state</code></td>
  229. <td><code>FieldValidationState</code></td>
  230. <td><code>ComputedRef&lt;FieldValidationState&gt;</code></td>
  231. <td>Vue reactivity; use <code>.value</code></td>
  232. </tr>
  233. <tr>
  234. <td><code>useFieldValidation().errors</code></td>
  235. <td><code>string[]</code></td>
  236. <td><code>ComputedRef&lt;string[]&gt;</code></td>
  237. <td>Vue reactivity; use <code>.value</code></td>
  238. </tr>
  239. <tr>
  240. <td><code>useFieldValidation().isValid</code></td>
  241. <td><code>boolean</code></td>
  242. <td><code>ComputedRef&lt;boolean&gt;</code></td>
  243. <td>Vue reactivity; use <code>.value</code></td>
  244. </tr>
  245. <tr>
  246. <td><code>VisibilityContextValue.ctx</code></td>
  247. <td><code>CoreVisibilityContext</code></td>
  248. <td><code>ComputedRef&lt;CoreVisibilityContext&gt;</code></td>
  249. <td>Vue reactivity; use <code>ctx.value</code></td>
  250. </tr>
  251. <tr>
  252. <td><code>children</code> type</td>
  253. <td><code>React.ReactNode</code></td>
  254. <td><code>VNode | VNode[]</code></td>
  255. <td>Platform-specific</td>
  256. </tr>
  257. <tr>
  258. <td><code>useBoundProp</code></td>
  259. <td>exported</td>
  260. <td>exported</td>
  261. <td>Same API; returns <code>[value, setValue]</code></td>
  262. </tr>
  263. <tr>
  264. <td><code>VisibilityProviderProps</code></td>
  265. <td>exported</td>
  266. <td>not exported (no props)</td>
  267. <td>Vue uses slot, no prop needed</td>
  268. </tr>
  269. <tr>
  270. <td>Streaming hooks</td>
  271. <td><code>useUIStream</code>, <code>useChatUI</code></td>
  272. <td><code>useUIStream</code>, <code>useChatUI</code></td>
  273. <td>Same API; returns Vue <code>Ref</code> values</td>
  274. </tr>
  275. </tbody>
  276. </table>