page.mdx 7.2 KB

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