page.mdx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/react-native")
  3. # @json-render/react-native
  4. React Native renderer with standard components, providers, and hooks.
  5. ## Standard Components
  6. ### Layout
  7. <table>
  8. <thead>
  9. <tr><th>Component</th><th>Props</th><th>Description</th></tr>
  10. </thead>
  11. <tbody>
  12. <tr><td><code>Container</code></td><td><code>padding</code>, <code>background</code>, <code>borderRadius</code>, <code>borderColor</code>, <code>flex</code></td><td>Basic wrapper with styling</td></tr>
  13. <tr><td><code>Row</code></td><td><code>gap</code>, <code>align</code>, <code>justify</code>, <code>flex</code>, <code>wrap</code></td><td>Horizontal flex layout</td></tr>
  14. <tr><td><code>Column</code></td><td><code>gap</code>, <code>align</code>, <code>justify</code>, <code>flex</code></td><td>Vertical flex layout</td></tr>
  15. <tr><td><code>ScrollContainer</code></td><td><code>direction</code></td><td>Scrollable area (vertical or horizontal)</td></tr>
  16. <tr><td><code>SafeArea</code></td><td><code>edges</code></td><td>Safe area insets for notch/home indicator</td></tr>
  17. <tr><td><code>Pressable</code></td><td><code>action</code>, <code>actionParams</code></td><td>Touchable wrapper that triggers actions</td></tr>
  18. <tr><td><code>Spacer</code></td><td><code>size</code>, <code>flex</code></td><td>Fixed or flexible spacing</td></tr>
  19. <tr><td><code>Divider</code></td><td><code>color</code>, <code>thickness</code></td><td>Thin line separator</td></tr>
  20. </tbody>
  21. </table>
  22. ### Content
  23. <table>
  24. <thead>
  25. <tr><th>Component</th><th>Props</th><th>Description</th></tr>
  26. </thead>
  27. <tbody>
  28. <tr><td><code>Heading</code></td><td><code>text</code>, <code>level</code>, <code>align</code>, <code>color</code></td><td>Heading text (levels 1-6)</td></tr>
  29. <tr><td><code>Paragraph</code></td><td><code>text</code>, <code>align</code>, <code>color</code></td><td>Body text</td></tr>
  30. <tr><td><code>Label</code></td><td><code>text</code>, <code>color</code>, <code>bold</code></td><td>Small label text</td></tr>
  31. <tr><td><code>Image</code></td><td><code>uri</code>, <code>width</code>, <code>height</code>, <code>resizeMode</code>, <code>borderRadius</code></td><td>Image display</td></tr>
  32. <tr><td><code>Avatar</code></td><td><code>uri</code>, <code>size</code>, <code>fallback</code></td><td>Circular avatar</td></tr>
  33. <tr><td><code>Badge</code></td><td><code>label</code>, <code>color</code>, <code>textColor</code></td><td>Status badge</td></tr>
  34. <tr><td><code>Chip</code></td><td><code>label</code>, <code>selected</code>, <code>color</code></td><td>Tag/chip</td></tr>
  35. </tbody>
  36. </table>
  37. ### Input
  38. <table>
  39. <thead>
  40. <tr><th>Component</th><th>Props</th><th>Description</th></tr>
  41. </thead>
  42. <tbody>
  43. <tr><td><code>Button</code></td><td><code>label</code>, <code>variant</code>, <code>size</code>, <code>disabled</code>, <code>action</code>, <code>actionParams</code></td><td>Pressable button</td></tr>
  44. <tr><td><code>TextInput</code></td><td><code>placeholder</code>, <code>value</code> (use <code>$bindState</code>), <code>secure</code>, <code>keyboardType</code>, <code>multiline</code></td><td>Text input field</td></tr>
  45. <tr><td><code>Switch</code></td><td><code>checked</code> (use <code>$bindState</code>), <code>label</code></td><td>Toggle switch</td></tr>
  46. <tr><td><code>Checkbox</code></td><td><code>checked</code> (use <code>$bindState</code>), <code>label</code></td><td>Checkbox with label</td></tr>
  47. <tr><td><code>Slider</code></td><td><code>value</code> (use <code>$bindState</code>), <code>min</code>, <code>max</code>, <code>step</code></td><td>Range slider</td></tr>
  48. <tr><td><code>SearchBar</code></td><td><code>placeholder</code>, <code>value</code> (use <code>$bindState</code>)</td><td>Search input</td></tr>
  49. </tbody>
  50. </table>
  51. ### Feedback
  52. <table>
  53. <thead>
  54. <tr><th>Component</th><th>Props</th><th>Description</th></tr>
  55. </thead>
  56. <tbody>
  57. <tr><td><code>Spinner</code></td><td><code>size</code>, <code>color</code></td><td>Loading indicator</td></tr>
  58. <tr><td><code>ProgressBar</code></td><td><code>progress</code>, <code>color</code>, <code>trackColor</code></td><td>Progress indicator</td></tr>
  59. </tbody>
  60. </table>
  61. ### Composite
  62. <table>
  63. <thead>
  64. <tr><th>Component</th><th>Props</th><th>Description</th></tr>
  65. </thead>
  66. <tbody>
  67. <tr><td><code>Card</code></td><td><code>title</code>, <code>subtitle</code>, <code>padding</code></td><td>Card container</td></tr>
  68. <tr><td><code>ListItem</code></td><td><code>title</code>, <code>subtitle</code>, <code>leading</code>, <code>trailing</code>, <code>action</code>, <code>actionParams</code></td><td>List row</td></tr>
  69. <tr><td><code>Modal</code></td><td><code>visible</code>, <code>title</code></td><td>Bottom sheet modal</td></tr>
  70. </tbody>
  71. </table>
  72. ## Providers
  73. ### StateProvider
  74. ```tsx
  75. <StateProvider initialState={object} onStateChange={fn}>
  76. {children}
  77. </StateProvider>
  78. ```
  79. <table>
  80. <thead>
  81. <tr><th>Prop</th><th>Type</th><th>Description</th></tr>
  82. </thead>
  83. <tbody>
  84. <tr><td><code>store</code></td><td><code>StateStore</code></td><td>External store (controlled mode). When provided, <code>initialState</code> and <code>onStateChange</code> are ignored.</td></tr>
  85. <tr><td><code>initialState</code></td><td><code>Record&lt;string, unknown&gt;</code></td><td>Initial state model (uncontrolled mode).</td></tr>
  86. <tr><td><code>onStateChange</code></td><td><code>{'(changes: Array<{ path: string; value: unknown }>) => void'}</code></td><td>Callback when state changes (uncontrolled mode). Called once per <code>set</code> or <code>update</code> with all changed entries.</td></tr>
  87. </tbody>
  88. </table>
  89. #### External Store (Controlled Mode)
  90. Pass a `StateStore` to bypass the internal state and wire json-render to any state management library:
  91. ```tsx
  92. import { createStateStore, type StateStore } from "@json-render/react-native";
  93. const store = createStateStore({ count: 0 });
  94. <StateProvider store={store}>
  95. {children}
  96. </StateProvider>
  97. // Mutate from anywhere — components re-render automatically:
  98. store.set("/count", 1);
  99. ```
  100. The `store` prop is also available on `JSONUIProvider` and `createRenderer`.
  101. ### ActionProvider
  102. ```tsx
  103. <ActionProvider handlers={Record<string, ActionHandler>}>
  104. {children}
  105. </ActionProvider>
  106. ```
  107. ### VisibilityProvider
  108. ```tsx
  109. <VisibilityProvider>
  110. {children}
  111. </VisibilityProvider>
  112. ```
  113. 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.
  114. ### ValidationProvider
  115. ```tsx
  116. <ValidationProvider>
  117. {children}
  118. </ValidationProvider>
  119. ```
  120. ## defineRegistry
  121. Create a type-safe component registry. Standard components are built-in; only register custom components.
  122. ```tsx
  123. import { defineRegistry, type Components } from '@json-render/react-native';
  124. const { registry } = defineRegistry(catalog, {
  125. components: {
  126. Icon: ({ props }) => <Ionicons name={props.name} size={props.size ?? 24} />,
  127. } as Components<typeof catalog>,
  128. });
  129. ```
  130. ## Hooks
  131. ### useUIStream
  132. ```typescript
  133. const {
  134. spec, // Spec | null - current UI state
  135. isStreaming, // boolean - true while streaming
  136. error, // Error | null
  137. send, // (prompt: string) => Promise<void>
  138. clear, // () => void - reset spec and error
  139. } = useUIStream({
  140. api: string,
  141. onComplete?: (spec: Spec) => void,
  142. onError?: (error: Error) => void,
  143. });
  144. ```
  145. ### useStateStore
  146. ```typescript
  147. const { state, get, set, update } = useStateStore();
  148. ```
  149. ### useStateValue
  150. ```typescript
  151. const value = useStateValue(path: string);
  152. ```
  153. ### useStateBinding (deprecated)
  154. > **Deprecated.** Use `useBoundProp` with `$bindState` expressions instead.
  155. ```typescript
  156. const [value, setValue] = useStateBinding(path: string);
  157. ```
  158. ### useActions
  159. ```typescript
  160. const { execute } = useActions();
  161. ```
  162. ### useIsVisible
  163. ```typescript
  164. const isVisible = useIsVisible(condition?: VisibilityCondition);
  165. ```
  166. ## Catalog Exports
  167. ```typescript
  168. import { standardComponentDefinitions, standardActionDefinitions } from "@json-render/react-native/catalog";
  169. import { schema } from "@json-render/react-native/schema";
  170. ```
  171. <table>
  172. <thead>
  173. <tr><th>Export</th><th>Purpose</th></tr>
  174. </thead>
  175. <tbody>
  176. <tr><td><code>standardComponentDefinitions</code></td><td>Catalog definitions for all 25+ standard components</td></tr>
  177. <tr><td><code>standardActionDefinitions</code></td><td>Catalog definitions for standard actions (setState, navigate)</td></tr>
  178. <tr><td><code>schema</code></td><td>React Native element tree schema</td></tr>
  179. </tbody>
  180. </table>