page.mdx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/catalog")
  3. # Catalog
  4. The catalog defines what AI can generate. It's your guardrail.
  5. ## What is a Catalog?
  6. A catalog is the vocabulary for your UI. While the [schema](/docs/schemas) defines the grammar (how specs are structured), the catalog defines the vocabulary (what components and actions are available). It lists:
  7. - **Components** — UI elements AI can create (with props and optional slots)
  8. - **Actions** — Operations AI can trigger
  9. - **Functions** — Custom validation or transformation functions
  10. ## Creating a Catalog
  11. `defineCatalog` is from `@json-render/core`. The `schema` import comes from your platform package (`@json-render/react` or `@json-render/react-native`) and defines the element structure the catalog targets. The catalog definition itself is framework-agnostic.
  12. ```typescript
  13. import { defineCatalog } from '@json-render/core';
  14. import { schema } from '@json-render/react'; // or '@json-render/react-native'
  15. import { z } from 'zod';
  16. const catalog = defineCatalog(schema, {
  17. components: {
  18. // Define each component with its props schema
  19. Card: {
  20. props: z.object({
  21. title: z.string(),
  22. description: z.string().nullable(),
  23. padding: z.enum(['sm', 'md', 'lg']).nullable(),
  24. }),
  25. slots: ["default"], // Can contain other components
  26. description: "Container card for grouping content",
  27. },
  28. Metric: {
  29. props: z.object({
  30. label: z.string(),
  31. value: z.union([z.string(), z.number()]),
  32. format: z.enum(['currency', 'percent', 'number']),
  33. }),
  34. description: "Display a single metric value",
  35. },
  36. },
  37. actions: {
  38. submit_form: {
  39. params: z.object({
  40. formId: z.string(),
  41. }),
  42. description: 'Submit a form',
  43. },
  44. export_data: {
  45. params: z.object({
  46. format: z.enum(['csv', 'pdf', 'json']),
  47. }),
  48. description: 'Export data in various formats',
  49. },
  50. },
  51. });
  52. ```
  53. ## Component Definition
  54. Each component in the catalog has:
  55. ```typescript
  56. {
  57. props: z.object({...}), // Zod schema for props (use .nullable() for optional)
  58. slots?: string[], // Named slots for children (e.g., ["default"])
  59. description?: string, // Help AI understand when to use it
  60. }
  61. ```
  62. Use `slots: ["default"]` for components that can contain children. The slot name corresponds to where child elements are rendered.
  63. ## Generating AI Prompts
  64. Use the `catalog.prompt()` method to generate a system prompt for AI:
  65. ```typescript
  66. // Generate a system prompt from your catalog
  67. const systemPrompt = catalog.prompt();
  68. // Or with custom rules for the AI
  69. const customPrompt = catalog.prompt({
  70. customRules: [
  71. "Always use Card as the root element for forms",
  72. "Group related inputs in a Stack with direction=vertical",
  73. ],
  74. });
  75. // Pass this to your AI model as the system prompt
  76. ```
  77. ## Next
  78. Learn how to [register components](/docs/registry) in your registry.