page.mdx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/visibility")
  3. # Visibility
  4. Conditionally show or hide components based on state values and logic.
  5. ## State-Based Visibility
  6. Show/hide based on state values. Use `$state` with a JSON Pointer path:
  7. ```json
  8. {
  9. "type": "Alert",
  10. "props": { "message": "Form has errors" },
  11. "visible": { "$state": "/form/hasErrors" }
  12. }
  13. ```
  14. Visible when `/form/hasErrors` is truthy.
  15. ### Negation
  16. Use `not: true` to invert a condition:
  17. ```json
  18. {
  19. "type": "WelcomeBanner",
  20. "visible": { "$state": "/user/hasSeenWelcome", "not": true }
  21. }
  22. ```
  23. Visible when `/user/hasSeenWelcome` is falsy.
  24. ## Auth-Based Visibility
  25. Show/hide based on authentication state. Expose your auth state in the state model (e.g. at `/auth/isSignedIn`):
  26. ```json
  27. {
  28. "type": "AdminPanel",
  29. "visible": { "$state": "/auth/isSignedIn" }
  30. }
  31. ```
  32. For signed-out only:
  33. ```json
  34. {
  35. "type": "LoginPrompt",
  36. "visible": { "$state": "/auth/isSignedIn", "not": true }
  37. }
  38. ```
  39. ## Comparison Operators
  40. Compare a state value to a literal or another state path. Use **one operator per condition** -- if multiple are provided, only the first one is evaluated (precedence: `eq` > `neq` > `gt` > `gte` > `lt` > `lte`). Add `"not": true` to invert the result of any condition.
  41. ```json
  42. // Equal
  43. {
  44. "visible": { "$state": "/user/role", "eq": "admin" }
  45. }
  46. // Not equal
  47. {
  48. "visible": { "$state": "/tab", "neq": "home" }
  49. }
  50. // Greater than
  51. {
  52. "visible": { "$state": "/cart/total", "gt": 100 }
  53. }
  54. // Greater than or equal
  55. {
  56. "visible": { "$state": "/cart/itemCount", "gte": 1 }
  57. }
  58. // Less than
  59. {
  60. "visible": { "$state": "/cart/total", "lt": 1000 }
  61. }
  62. // Less than or equal
  63. {
  64. "visible": { "$state": "/cart/itemCount", "lte": 10 }
  65. }
  66. ```
  67. Comparison values can be literals or state references:
  68. ```json
  69. {
  70. "visible": { "$state": "/user/balance", "gte": { "$state": "/order/minimum" } }
  71. }
  72. ```
  73. ## Combining Conditions (AND)
  74. Place multiple conditions in an array for implicit AND:
  75. ```json
  76. {
  77. "type": "SubmitButton",
  78. "visible": [
  79. { "$state": "/form/isValid" },
  80. { "$state": "/form/hasChanges" }
  81. ]
  82. }
  83. ```
  84. All conditions must be true for the element to be visible.
  85. ## OR Conditions
  86. Use `$or` when at least one condition should be true:
  87. ```json
  88. {
  89. "type": "SpecialOffer",
  90. "visible": { "$or": [
  91. { "$state": "/user/isVIP" },
  92. { "$state": "/cart/total", "gt": 200 }
  93. ]}
  94. }
  95. ```
  96. Visible when the user is VIP **or** the cart total exceeds 200. `$or` can contain any visibility conditions, including nested arrays (AND) and comparisons.
  97. ## Explicit AND
  98. Use `$and` when you need to nest AND logic inside `$or`:
  99. ```json
  100. {
  101. "type": "PromoCard",
  102. "visible": { "$or": [
  103. { "$and": [
  104. { "$state": "/user/isVIP" },
  105. { "$state": "/cart/total", "gt": 50 }
  106. ]},
  107. { "$state": "/promo/active" }
  108. ]}
  109. }
  110. ```
  111. For top-level AND, the implicit array form is simpler: `[condition, condition]`. Use `$and` only when nesting inside `$or`.
  112. ## Always / Never
  113. Use boolean literals for constant visibility:
  114. ```json
  115. {
  116. "type": "Footer",
  117. "visible": true
  118. }
  119. ```
  120. ```json
  121. {
  122. "type": "DeprecatedPanel",
  123. "visible": false
  124. }
  125. ```
  126. ## Repeat-Scoped Conditions
  127. Inside a [repeat](/docs/data-binding#repeat), use `$item` and `$index` conditions to show/hide based on the current item:
  128. ### `$item` — Condition on item field
  129. ```json
  130. {
  131. "type": "Badge",
  132. "props": { "label": "Overdue" },
  133. "visible": { "$item": "isOverdue" }
  134. }
  135. ```
  136. With comparison:
  137. ```json
  138. {
  139. "type": "DiscountTag",
  140. "visible": { "$item": "price", "gt": 100 }
  141. }
  142. ```
  143. ### `$index` — Condition on array index
  144. ```json
  145. {
  146. "type": "Divider",
  147. "visible": { "$index": true, "gt": 0 }
  148. }
  149. ```
  150. This shows the divider for every item except the first (index 0).
  151. `$item` and `$index` conditions support the same comparison operators as `$state` (`eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `not`).
  152. ## Complex Example
  153. ```json
  154. {
  155. "type": "RefundButton",
  156. "props": { "label": "Process Refund" },
  157. "visible": [
  158. { "$state": "/auth/isSignedIn" },
  159. { "$state": "/user/role", "eq": "support" },
  160. { "$state": "/order/amount", "gt": 0 },
  161. { "$state": "/order/isRefunded", "not": true }
  162. ]
  163. }
  164. ```
  165. ## Quick Reference
  166. <div className="my-6 overflow-x-auto">
  167. <table className="mdx-table w-full text-sm border-collapse">
  168. <thead>
  169. <tr>
  170. <th>Condition</th>
  171. <th>Syntax</th>
  172. </tr>
  173. </thead>
  174. <tbody>
  175. <tr>
  176. <td>Truthiness</td>
  177. <td><code>{'{ "$state": "/path" }'}</code></td>
  178. </tr>
  179. <tr>
  180. <td>Falsy (not)</td>
  181. <td><code>{'{ "$state": "/path", "not": true }'}</code></td>
  182. </tr>
  183. <tr>
  184. <td>Equal</td>
  185. <td><code>{'{ "$state": "/path", "eq": value }'}</code></td>
  186. </tr>
  187. <tr>
  188. <td>Not equal</td>
  189. <td><code>{'{ "$state": "/path", "neq": value }'}</code></td>
  190. </tr>
  191. <tr>
  192. <td>Greater than</td>
  193. <td><code>{'{ "$state": "/path", "gt": number }'}</code></td>
  194. </tr>
  195. <tr>
  196. <td>Greater or equal</td>
  197. <td><code>{'{ "$state": "/path", "gte": number }'}</code></td>
  198. </tr>
  199. <tr>
  200. <td>Less than</td>
  201. <td><code>{'{ "$state": "/path", "lt": number }'}</code></td>
  202. </tr>
  203. <tr>
  204. <td>Less or equal</td>
  205. <td><code>{'{ "$state": "/path", "lte": number }'}</code></td>
  206. </tr>
  207. <tr>
  208. <td>Item field (repeat)</td>
  209. <td><code>{'{ "$item": "field" }'}</code></td>
  210. </tr>
  211. <tr>
  212. <td>Item comparison</td>
  213. <td><code>{'{ "$item": "field", "eq": value }'}</code></td>
  214. </tr>
  215. <tr>
  216. <td>Index (repeat)</td>
  217. <td><code>{'{ "$index": true, "gt": 0 }'}</code></td>
  218. </tr>
  219. <tr>
  220. <td>AND (implicit)</td>
  221. <td><code>{"[ condition, condition ]"}</code></td>
  222. </tr>
  223. <tr>
  224. <td>AND (explicit)</td>
  225. <td><code>{'{ "$and": [ condition, condition ] }'}</code></td>
  226. </tr>
  227. <tr>
  228. <td>OR</td>
  229. <td><code>{'{ "$or": [ condition, condition ] }'}</code></td>
  230. </tr>
  231. <tr>
  232. <td>Always</td>
  233. <td><code>{"true"}</code></td>
  234. </tr>
  235. <tr>
  236. <td>Never</td>
  237. <td><code>{"false"}</code></td>
  238. </tr>
  239. </tbody>
  240. </table>
  241. </div>
  242. Comparison values can be literals or state references for state-to-state comparisons:
  243. ```json
  244. { "$state": "/a", "eq": { "$state": "/b" } }
  245. ```
  246. ## Usage with React
  247. In `@json-render/react`, wrap your app with `VisibilityProvider` to enable conditional rendering. The `Renderer` handles visibility automatically — elements with unmet conditions are not rendered.
  248. ```tsx
  249. import { VisibilityProvider, StateProvider } from '@json-render/react';
  250. function App() {
  251. return (
  252. <StateProvider initialState={data}>
  253. <VisibilityProvider>
  254. {/* Components can now use visibility conditions */}
  255. </VisibilityProvider>
  256. </StateProvider>
  257. );
  258. }
  259. ```
  260. For advanced use cases, the `useIsVisible` hook lets you evaluate visibility conditions programmatically:
  261. ```tsx
  262. import { useIsVisible } from '@json-render/react';
  263. function ConditionalContent({ condition, children }) {
  264. const isVisible = useIsVisible(condition);
  265. if (!isVisible) return null;
  266. return <div>{children}</div>;
  267. }
  268. ```
  269. See the [@json-render/react API reference](/docs/api/react) for full details.
  270. ## Next
  271. Learn about [form validation](/docs/validation).