page.mdx 5.9 KB

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