page.mdx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. export const metadata = { title: "Visibility" }
  2. # Visibility
  3. Conditionally show or hide components based on data, auth, or logic.
  4. ## VisibilityProvider
  5. Wrap your app with VisibilityProvider to enable conditional rendering:
  6. ```tsx
  7. import { VisibilityProvider } from '@json-render/react';
  8. function App() {
  9. return (
  10. <StateProvider initialState={data}>
  11. <VisibilityProvider>
  12. {/* Components can now use visibility conditions */}
  13. </VisibilityProvider>
  14. </StateProvider>
  15. );
  16. }
  17. ```
  18. ## Path-Based Visibility
  19. Show/hide based on data values:
  20. ```json
  21. {
  22. "type": "Alert",
  23. "props": { "message": "Form has errors" },
  24. "visible": { "path": "/form/hasErrors" }
  25. }
  26. // Visible when /form/hasErrors is truthy
  27. ```
  28. ## Auth-Based Visibility
  29. Show/hide based on authentication state:
  30. ```json
  31. {
  32. "type": "AdminPanel",
  33. "visible": { "auth": "signedIn" }
  34. }
  35. // Options: "signedIn", "signedOut", "admin", etc.
  36. ```
  37. ## Logic Expressions
  38. Combine conditions with logic operators:
  39. ```json
  40. // AND - all conditions must be true
  41. {
  42. "type": "SubmitButton",
  43. "visible": {
  44. "and": [
  45. { "path": "/form/isValid" },
  46. { "path": "/form/hasChanges" }
  47. ]
  48. }
  49. }
  50. // OR - any condition must be true
  51. {
  52. "type": "HelpText",
  53. "visible": {
  54. "or": [
  55. { "path": "/user/isNew" },
  56. { "path": "/settings/showHelp" }
  57. ]
  58. }
  59. }
  60. // NOT - invert a condition
  61. {
  62. "type": "WelcomeBanner",
  63. "visible": {
  64. "not": { "path": "/user/hasSeenWelcome" }
  65. }
  66. }
  67. ```
  68. ## Comparison Operators
  69. ```json
  70. // Equal
  71. {
  72. "visible": {
  73. "eq": [{ "path": "/user/role" }, "admin"]
  74. }
  75. }
  76. // Greater than
  77. {
  78. "visible": {
  79. "gt": [{ "path": "/cart/total" }, 100]
  80. }
  81. }
  82. // Available: eq, ne, gt, gte, lt, lte
  83. ```
  84. ## Complex Example
  85. ```json
  86. {
  87. "type": "RefundButton",
  88. "props": { "label": "Process Refund" },
  89. "visible": {
  90. "and": [
  91. { "auth": "signedIn" },
  92. { "eq": [{ "path": "/user/role" }, "support"] },
  93. { "gt": [{ "path": "/order/amount" }, 0] },
  94. { "not": { "path": "/order/isRefunded" } }
  95. ]
  96. }
  97. }
  98. ```
  99. ## Using in Components
  100. ```tsx
  101. import { useIsVisible } from '@json-render/react';
  102. // The Renderer handles visibility automatically, but you can also use the hook
  103. function ConditionalContent({ condition, children }) {
  104. const isVisible = useIsVisible(condition);
  105. if (!isVisible) return null;
  106. return <div>{children}</div>;
  107. }
  108. ```
  109. ## Next
  110. Learn about [form validation](/docs/validation).