page.mdx 2.9 KB

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