Usage
Once an AI Elements component is installed, you can import it and use it in your application like any other React component. The components are added as part of your codebase (not hidden in a library), so the usage feels very natural.
Example
After installing AI Elements components, you can use them in your application like any other React component. For example:
'use client';
import { Message, MessageAvatar, MessageContent,} from '@/components/ai-elements/message';import { useChat } from '@ai-sdk/react';import { Response } from '@/components/ai-elements/response';
const Example = () => { const { messages } = useChat();
return ( <> {messages.map(({ role, parts }, index) => ( <Message from={role} key={index}> <MessageContent> {parts.map((part, i) => { switch (part.type) { case 'text': return <Response key={`${role}-${i}`}>{part.text}</Response>; } })} </MessageContent> </Message> ))} </> );};
export default Example;
In the example above, we import the Message
component from our AI Elements directory and include it in our JSX. Then, we compose the component with the MessageContent
and Response
subcomponents. You can style or configure the component just as you would if you wrote it yourself – since the code lives in your project, you can even open the component file to see how it works or make custom modifications.
Extensibility
All AI Elements components take as many primitive attributes as possible. For example, the Message
component extends HTMLAttributes<HTMLDivElement>
, so you can pass any props that a div
supports. This makes it easy to extend the component with your own styles or functionality.
Customization
If you re-install AI Elements by rerunning npx ai-elements@latest
, the CLI
will ask before overwriting the file so you can save any custom changes you
made.
After installation, no additional setup is needed. The component’s styles (Tailwind CSS classes) and scripts are already integrated. You can start interacting with the component in your app immediately.
For example, if you'd like to remove the rounding on Message
, you can go to components/ai-elements/message.tsx
and remove rounded-lg
as follows:
export const MessageContent = ({ children, className, ...props}: MessageContentProps) => ( <div className={cn( 'flex flex-col gap-2 text-sm text-foreground', 'group-[.is-user]:bg-primary group-[.is-user]:text-primary-foreground group-[.is-user]:px-4 group-[.is-user]:py-3', className, )} {...props} > <div className="is-user:dark">{children}</div> </div>);