page.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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">Path-Based Visibility</h2>
  28. <p className="text-sm text-muted-foreground mb-4">
  29. Show/hide based on data values:
  30. </p>
  31. <Code lang="json">{`{
  32. "type": "Alert",
  33. "props": { "message": "Form has errors" },
  34. "visible": { "path": "/form/hasErrors" }
  35. }
  36. // Visible when /form/hasErrors is truthy`}</Code>
  37. <h2 className="text-xl font-semibold mt-12 mb-4">Auth-Based Visibility</h2>
  38. <p className="text-sm text-muted-foreground mb-4">
  39. Show/hide based on authentication state:
  40. </p>
  41. <Code lang="json">{`{
  42. "type": "AdminPanel",
  43. "visible": { "auth": "signedIn" }
  44. }
  45. // Options: "signedIn", "signedOut", "admin", etc.`}</Code>
  46. <h2 className="text-xl font-semibold mt-12 mb-4">Logic Expressions</h2>
  47. <p className="text-sm text-muted-foreground mb-4">
  48. Combine conditions with logic operators:
  49. </p>
  50. <Code lang="json">{`// AND - all conditions must be true
  51. {
  52. "type": "SubmitButton",
  53. "visible": {
  54. "and": [
  55. { "path": "/form/isValid" },
  56. { "path": "/form/hasChanges" }
  57. ]
  58. }
  59. }
  60. // OR - any condition must be true
  61. {
  62. "type": "HelpText",
  63. "visible": {
  64. "or": [
  65. { "path": "/user/isNew" },
  66. { "path": "/settings/showHelp" }
  67. ]
  68. }
  69. }
  70. // NOT - invert a condition
  71. {
  72. "type": "WelcomeBanner",
  73. "visible": {
  74. "not": { "path": "/user/hasSeenWelcome" }
  75. }
  76. }`}</Code>
  77. <h2 className="text-xl font-semibold mt-12 mb-4">Comparison Operators</h2>
  78. <Code lang="json">{`// Equal
  79. {
  80. "visible": {
  81. "eq": [{ "path": "/user/role" }, "admin"]
  82. }
  83. }
  84. // Greater than
  85. {
  86. "visible": {
  87. "gt": [{ "path": "/cart/total" }, 100]
  88. }
  89. }
  90. // Available: eq, ne, gt, gte, lt, lte`}</Code>
  91. <h2 className="text-xl font-semibold mt-12 mb-4">Complex Example</h2>
  92. <Code lang="json">{`{
  93. "type": "RefundButton",
  94. "props": { "label": "Process Refund" },
  95. "visible": {
  96. "and": [
  97. { "auth": "signedIn" },
  98. { "eq": [{ "path": "/user/role" }, "support"] },
  99. { "gt": [{ "path": "/order/amount" }, 0] },
  100. { "not": { "path": "/order/isRefunded" } }
  101. ]
  102. }
  103. }`}</Code>
  104. <h2 className="text-xl font-semibold mt-12 mb-4">Using in Components</h2>
  105. <Code lang="tsx">{`import { useIsVisible } from '@json-render/react';
  106. function ConditionalContent({ element, children }) {
  107. const isVisible = useIsVisible(element.visible);
  108. if (!isVisible) return null;
  109. return <div>{children}</div>;
  110. }`}</Code>
  111. <h2 className="text-xl font-semibold mt-12 mb-4">Next</h2>
  112. <p className="text-sm text-muted-foreground">
  113. Learn about <Link href="/docs/validation" className="text-foreground hover:underline">form validation</Link>.
  114. </p>
  115. </article>
  116. );
  117. }