JSX Preview
A component that dynamically renders JSX strings with streaming support for AI-generated UI.
The JSXPreview component renders JSX strings dynamically, supporting streaming scenarios where JSX may be incomplete. It automatically closes unclosed tags during streaming, making it ideal for displaying AI-generated UI components in real-time.
Loading preview…
Installation
npm i zjkm-ui-ai-elementsimport { JSXPreview, JSXPreviewContent, JSXPreviewError,} from "zjkm-ui-ai-elements/jsx-preview";First time? Add the package's styles: see Setup.
Features
- Renders JSX strings dynamically using
react-jsx-parser - Streaming support with automatic tag completion
- Custom component injection for rendering your own components
- Error handling with customizable error display
- Context-based architecture for flexible composition
Usage with AI SDK
The JSXPreview component integrates with the AI SDK to render generated UI in real-time:
"use client";
import {
JSXPreview,
JSXPreviewContent,
JSXPreviewError,
} from "zjkm-ui-ai-elements/jsx-preview";
type GeneratedUIProps = {
jsx: string;
isStreaming: boolean;
};
export const GeneratedUI = ({ jsx, isStreaming }: GeneratedUIProps) => (
<JSXPreview
jsx={jsx}
isStreaming={isStreaming}
onError={(error) => console.error("JSX Parse Error:", error)}
>
<JSXPreviewContent />
<JSXPreviewError />
</JSXPreview>
);With Custom Components
You can inject custom components to be used within the rendered JSX:
"use client";
import {
JSXPreview,
JSXPreviewContent,
} from "zjkm-ui-ai-elements/jsx-preview";
import { Button } from "zjkm-ui-ai-elements/ui/button";
import { Card } from "zjkm-ui-ai-elements/ui/card";
const customComponents = {
Button,
Card,
};
export const GeneratedUIWithComponents = ({ jsx }: { jsx: string }) => (
<JSXPreview jsx={jsx} components={customComponents}>
<JSXPreviewContent />
</JSXPreview>
);Props
<JSXPreview />
Prop
Type
<JSXPreviewContent />
Prop
Type
<JSXPreviewError />
Prop
Type