Skip to content

The Open Standard for Generative UI

3.3 KB
Raw Blame
---
title: "shadcn/ui"
---

<Steps>

<Step title="Wrap your shadcn/ui components" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/shadcn/src/lib/shadcn-genui/index.tsx">

Register each component with defineComponent and a Zod prop schema, then combine them with createLibrary. The example library covers 45+ components, including cards, tables, charts, forms, and dialogs.

```tsx title="src/lib/shadcn-genui/index.tsx"
import { createLibrary, defineComponent } from "@openuidev/react-lang";
import { z } from "zod";
// Components the shadcn CLI added to your project.
import { Badge as ShadcnBadge } from "@/components/ui/badge";
import { Card, CardContent } from "@/components/ui/card";

const Badge = defineComponent({
  name: "Badge",
  props: z.object({
    text: z.string(),
    variant: z.enum(["default", "secondary", "destructive", "outline"]).optional(),
  }),
  description: "Inline label/badge.",
  component: ({ props }) => (
    <ShadcnBadge variant={props.variant ?? "default"}>{props.text}</ShadcnBadge>
  ),
});

const ChatCard = defineComponent({
  name: "Card",
  props: z.object({ children: z.array(Badge.ref) }),
  description: "Vertical container for all content in a chat response.",
  component: ({ props, renderNode }) => (
    <Card>
      <CardContent>{renderNode(props.children)}</CardContent>
    </Card>
  ),
});

export const shadcnChatLibrary = createLibrary({
  root: "Card",
  components: [ChatCard, Badge], // plus every other component you wrap
});
```

</Step>

<Step title="Generate the library spec" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/shadcn/src/lib/cloud-prompt.ts">

Run openui generate --spec on the library file. The chat route passes that spec to generateSystemPrompt(\{ cloud: true \}), so the model can only call components the client can render.

```ts title="src/lib/cloud-prompt.ts"
import { generateSystemPrompt, type LibrarySpec } from "@openuidev/lang-core";
import { readFileSync } from "fs";
import { join } from "path";

// src/library.ts re-exports your library as `library`, and this command writes the spec:
//   openui generate src/library.ts --spec --out src/generated/spec.json
export function cloudInstructions(): string {
  const library = JSON.parse(
    readFileSync(join(process.cwd(), "src/generated/spec.json"), "utf-8"),
  ) as LibrarySpec;
  // The chat route sends this as the system message.
  return generateSystemPrompt({ cloud: true, library });
}
```

</Step>

<Step title="Render the responses" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/shadcn/src/app/page.tsx">

Pass the library to Renderer from @openuidev/react-lang wherever your app shows model output. For a ready-made chat app, the example passes it to AgentInterface as componentLibrary instead.

```tsx title="src/app/page.tsx"
"use client";

import { AgentInterface, fetchLLM, openAIAdapter, openAIMessageFormat } from "@openuidev/react-ui";
// Your wrapped shadcn/ui components from step 1.
import { shadcnChatLibrary } from "@/lib/shadcn-genui";

const llm = fetchLLM({
  url: "/api/chat",
  streamAdapter: openAIAdapter(),
  messageFormat: openAIMessageFormat,
});

export default function Page() {
  return <AgentInterface llm={llm} componentLibrary={shadcnChatLibrary} />;
}
```

</Step>

</Steps>