page.mdx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/code-export")
  3. # Code Export
  4. Export generated UI as standalone code for your framework.
  5. ## Overview
  6. While json-render is designed for dynamic rendering, you can export generated UI as static code. The code generation is intentionally project-specific so you have full control over:
  7. - Component templates (standalone, no json-render dependencies)
  8. - Package.json and project structure
  9. - Framework-specific patterns (Next.js, Remix, etc.)
  10. - How data is passed to components
  11. ## Architecture
  12. Code export is split into two parts:
  13. ### 1. @json-render/codegen (utilities)
  14. Framework-agnostic utilities for building code generators:
  15. ```typescript
  16. import {
  17. traverseSpec, // Walk the UI spec
  18. collectUsedComponents, // Get all component types used
  19. collectStatePaths, // Get all data binding paths
  20. collectActions, // Get all action names
  21. serializeProps, // Convert props to JSX string
  22. } from '@json-render/codegen';
  23. ```
  24. ### 2. Your Project (generator)
  25. Custom code generator specific to your project and framework:
  26. ```typescript
  27. // lib/codegen/generator.ts
  28. import { collectUsedComponents, serializeProps } from '@json-render/codegen';
  29. export function generateNextJSProject(spec: Spec): GeneratedFile[] {
  30. const components = collectUsedComponents(spec);
  31. return [
  32. { path: 'package.json', content: '...' },
  33. { path: 'app/page.tsx', content: '...' },
  34. // ... component files
  35. ];
  36. }
  37. ```
  38. ## Example: Next.js Export
  39. See the dashboard example for a complete implementation that exports:
  40. - `package.json` - Dependencies and scripts
  41. - `tsconfig.json` - TypeScript config
  42. - `next.config.js` - Next.js config
  43. - `app/layout.tsx` - Root layout
  44. - `app/globals.css` - Global styles
  45. - `app/page.tsx` - Generated page with data
  46. - `components/ui/*.tsx` - Standalone components
  47. ## Standalone Components
  48. The exported components are standalone with no json-render dependencies. They receive data as props instead of using hooks:
  49. ```tsx
  50. // Generated component (standalone)
  51. interface MetricProps {
  52. label: string;
  53. statePath: string;
  54. data?: Record<string, unknown>;
  55. }
  56. export function Metric({ label, statePath, data }: MetricProps) {
  57. const value = data ? getByPath(data, statePath) : undefined;
  58. return (
  59. <div>
  60. <span>{label}</span>
  61. <span>{formatValue(value)}</span>
  62. </div>
  63. );
  64. }
  65. ```
  66. ## Using the Utilities
  67. ### traverseSpec
  68. ```typescript
  69. import { traverseSpec } from '@json-render/codegen';
  70. traverseSpec(spec, (element, key, depth, parent) => {
  71. console.log(' '.repeat(depth * 2) + `${key}: ${element.type}`);
  72. });
  73. ```
  74. ### collectUsedComponents
  75. ```typescript
  76. import { collectUsedComponents } from '@json-render/codegen';
  77. const components = collectUsedComponents(spec);
  78. // Set { 'Card', 'Metric', 'Chart', 'Table' }
  79. // Generate only the needed component files
  80. for (const component of components) {
  81. files.push({
  82. path: `components/ui/${component.toLowerCase()}.tsx`,
  83. content: componentTemplates[component],
  84. });
  85. }
  86. ```
  87. ### serializeProps
  88. ```typescript
  89. import { serializeProps } from '@json-render/codegen';
  90. const propsStr = serializeProps({
  91. title: 'Dashboard',
  92. columns: 3,
  93. disabled: true,
  94. });
  95. // 'title="Dashboard" columns={3} disabled'
  96. ```
  97. ## Try It
  98. Run the dashboard example and click "Export Project" to see code generation in action:
  99. ```bash
  100. cd examples/dashboard
  101. pnpm dev
  102. # Open http://dashboard-demo.json-render.localhost:1355
  103. # Generate a widget, then click "Export Project"
  104. ```