page.tsx 3.8 KB

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