page.mdx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs")
  3. # Introduction
  4. json-render is a framework for **Generative UI** — AI-generated interfaces that are safe, predictable, and render natively on any platform.
  5. ## What is Generative UI?
  6. Most AI integrations treat the interface as fixed. Developers build layouts ahead of time, and AI fills in the data — a chatbot response, a summary, a recommendation. The UI itself never changes.
  7. **Generative UI is different.** The AI generates the interface itself: which components to show, how to arrange them, what data to bind, what actions to wire up. Every response can produce a unique, purpose-built UI tailored to the user's request.
  8. The challenge is that unconstrained AI output is unpredictable. It can hallucinate component names, produce invalid structures, or generate unsafe code. You need a way to let AI be creative with layout and composition while keeping it within boundaries you control.
  9. That is what json-render does. You define a **catalog** of components and actions. AI generates JSON constrained to that catalog. Your components render the result natively — on web or mobile — with full type safety and no arbitrary code execution.
  10. ## How json-render Works
  11. ### 1. Define your catalog
  12. A catalog declares what AI can use: components with typed props, actions with typed params.
  13. ```typescript
  14. import { defineCatalog } from '@json-render/core';
  15. import { schema } from '@json-render/react/schema';
  16. import { z } from 'zod';
  17. export const catalog = defineCatalog(schema, {
  18. components: {
  19. Card: {
  20. props: z.object({ title: z.string() }),
  21. slots: ["default"],
  22. },
  23. Metric: {
  24. props: z.object({
  25. label: z.string(),
  26. value: z.string(),
  27. }),
  28. },
  29. },
  30. });
  31. ```
  32. ### 2. AI generates a spec
  33. Given a prompt like "show me a revenue dashboard", AI outputs a JSON spec — a flat tree of elements constrained to your catalog:
  34. ```json
  35. {
  36. "root": "card-1",
  37. "elements": {
  38. "card-1": {
  39. "type": "Card",
  40. "props": { "title": "Revenue Dashboard" },
  41. "children": ["metric-1", "metric-2"]
  42. },
  43. "metric-1": {
  44. "type": "Metric",
  45. "props": { "label": "Total Revenue", "value": "$48,200" }
  46. },
  47. "metric-2": {
  48. "type": "Metric",
  49. "props": { "label": "Growth", "value": "+12%" }
  50. }
  51. }
  52. }
  53. ```
  54. ### 3. Your components render it
  55. Map catalog types to real components with a registry, then render the spec:
  56. ```tsx
  57. import { Renderer, StateProvider, VisibilityProvider } from '@json-render/react';
  58. <StateProvider initialState={{}}>
  59. <VisibilityProvider>
  60. <Renderer spec={spec} registry={registry} />
  61. </VisibilityProvider>
  62. </StateProvider>
  63. ```
  64. The result is a native UI built from your own components — not an iframe, not markdown, not generated code. The AI chose the structure; you control everything else.
  65. ## Key Concepts
  66. - **[Catalog](/docs/catalog)** — Define the components, actions, and validation functions AI can use. This is the contract between your app and the AI.
  67. - **[Registry](/docs/registry)** — Map catalog types to platform-specific implementations. React components on web, React Native views on mobile.
  68. - **[Specs](/docs/specs)** — The JSON output AI generates. A flat tree of typed elements with props, children, data bindings, and visibility conditions.
  69. - **[Streaming](/docs/streaming)** — Render progressively as the AI responds. Each JSONL patch adds to the spec and the UI updates in real time.
  70. - **[Data Binding](/docs/data-binding)** — Bind props to runtime data with `$state` paths, repeat elements over arrays, and wire two-way input bindings.
  71. - **[Visibility](/docs/visibility)** — Show or hide elements based on state conditions. The AI can generate conditional UIs without writing logic.
  72. - **[Generation Modes](/docs/generation-modes)** — Generate standalone UI (playground/builder) or inline UI within a chat conversation.
  73. ## Next
  74. - [Installation](/docs/installation) — Add json-render to your project
  75. - [Quick Start](/docs/quick-start) — Build your first generative UI in 5 minutes