Chatbot
Loader
A spinning loader component for indicating loading states in AI applications.
The Loader component provides a spinning animation to indicate loading states in your AI applications. It includes both a customizable wrapper component and the underlying icon for flexible usage.
"use client";import { Loader } from "@/components/ai-elements/elements/loader";const Example = () => ( <div className="flex items-center justify-center p-8"> <Loader /> </div>);export default Example;Installation
npx ai-elements@latest add loadernpx shadcn@latest add @ai-elements/loaderimport { cn } from "@repo/shadcn-ui/lib/utils";import type { HTMLAttributes } from "react";type LoaderIconProps = { size?: number;};const LoaderIcon = ({ size = 16 }: LoaderIconProps) => ( <svg height={size} strokeLinejoin="round" style={{ color: "currentcolor" }} viewBox="0 0 16 16" width={size} > <title>Loader</title> <g clipPath="url(#clip0_2393_1490)"> <path d="M8 0V4" stroke="currentColor" strokeWidth="1.5" /> <path d="M8 16V12" opacity="0.5" stroke="currentColor" strokeWidth="1.5" /> <path d="M3.29773 1.52783L5.64887 4.7639" opacity="0.9" stroke="currentColor" strokeWidth="1.5" /> <path d="M12.7023 1.52783L10.3511 4.7639" opacity="0.1" stroke="currentColor" strokeWidth="1.5" /> <path d="M12.7023 14.472L10.3511 11.236" opacity="0.4" stroke="currentColor" strokeWidth="1.5" /> <path d="M3.29773 14.472L5.64887 11.236" opacity="0.6" stroke="currentColor" strokeWidth="1.5" /> <path d="M15.6085 5.52783L11.8043 6.7639" opacity="0.2" stroke="currentColor" strokeWidth="1.5" /> <path d="M0.391602 10.472L4.19583 9.23598" opacity="0.7" stroke="currentColor" strokeWidth="1.5" /> <path d="M15.6085 10.4722L11.8043 9.2361" opacity="0.3" stroke="currentColor" strokeWidth="1.5" /> <path d="M0.391602 5.52783L4.19583 6.7639" opacity="0.8" stroke="currentColor" strokeWidth="1.5" /> </g> <defs> <clipPath id="clip0_2393_1490"> <rect fill="white" height="16" width="16" /> </clipPath> </defs> </svg>);export type LoaderProps = HTMLAttributes<HTMLDivElement> & { size?: number;};export const Loader = ({ className, size = 16, ...props }: LoaderProps) => ( <div className={cn( "inline-flex animate-spin items-center justify-center", className )} {...props} > <LoaderIcon size={size} /> </div>);Usage
import { Loader } from '@/components/ai-elements/loader';<Loader />Usage with AI SDK
Build a simple chat app that displays a loader before it the response streans by using status === "submitted".
Add the following component to your frontend:
'use client';
import {
Conversation,
ConversationContent,
ConversationScrollButton,
} from '@/components/ai-elements/conversation';
import { Message, MessageContent } from '@/components/ai-elements/message';
import {
Input,
PromptInputTextarea,
PromptInputSubmit,
} from '@/components/ai-elements/prompt-input';
import { Loader } from '@/components/ai-elements/loader';
import { useState } from 'react';
import { useChat } from '@ai-sdk/react';
const LoaderDemo = () => {
const [input, setInput] = useState('');
const { messages, sendMessage, status } = useChat();
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (input.trim()) {
sendMessage({ text: input });
setInput('');
}
};
return (
<div className="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]">
<div className="flex flex-col h-full">
<Conversation>
<ConversationContent>
{messages.map((message) => (
<Message from={message.role} key={message.id}>
<MessageContent>
{message.parts.map((part, i) => {
switch (part.type) {
case 'text':
return (
<div key={`${message.id}-${i}`}>{part.text}</div>
);
default:
return null;
}
})}
</MessageContent>
</Message>
))}
{status === 'submitted' && <Loader />}
</ConversationContent>
<ConversationScrollButton />
</Conversation>
<Input
onSubmit={handleSubmit}
className="mt-4 w-full max-w-2xl mx-auto relative"
>
<PromptInputTextarea
value={input}
placeholder="Say something..."
onChange={(e) => setInput(e.currentTarget.value)}
className="pr-12"
/>
<PromptInputSubmit
status={status === 'streaming' ? 'streaming' : 'ready'}
disabled={!input.trim()}
className="absolute bottom-1 right-1"
/>
</Input>
</div>
</div>
);
};
export default LoaderDemo;Add the following route to your backend:
import { streamText, UIMessage, convertToModelMessages } from 'ai';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { model, messages }: { messages: UIMessage[]; model: string } =
await req.json();
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse();
}Features
- Clean, modern spinning animation using CSS animations
- Configurable size with the
sizeprop - Customizable styling with CSS classes
- Built-in
animate-spinanimation with proper centering - Exports both
AILoaderwrapper andLoaderIconfor flexible usage - Supports all standard HTML div attributes
- TypeScript support with proper type definitions
- Optimized SVG icon with multiple opacity levels for smooth animation
- Uses
currentColorfor proper theme integration - Responsive and accessible design
Examples
Different Sizes
Small (16px)
Medium (24px)
Large (32px)
Extra Large (48px)
"use client";import { Loader } from "@/components/ai-elements/elements/loader";const Example = () => ( <div className="flex items-center gap-8 p-8"> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Small (16px)</p> <Loader size={16} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Medium (24px)</p> <Loader size={24} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Large (32px)</p> <Loader size={32} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Extra Large (48px)</p> <Loader size={48} /> </div> </div>);export default Example;Custom Styling
Blue
Green
Purple
Orange
Slow Animation
Fast Animation
With Background
Dark Background
"use client";import { Loader } from "@/components/ai-elements/elements/loader";const Example = () => ( <div className="grid grid-cols-2 gap-6 p-8 md:grid-cols-4"> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Blue</p> <Loader className="text-blue-500" size={24} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Green</p> <Loader className="text-green-500" size={24} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Purple</p> <Loader className="text-purple-500" size={24} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Orange</p> <Loader className="text-orange-500" size={24} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Slow Animation</p> <Loader className="animate-spin text-blue-500" size={24} style={{ animationDuration: "3s" }} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Fast Animation</p> <Loader className="animate-spin text-red-500" size={24} style={{ animationDuration: "0.5s" }} /> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">With Background</p> <div className="flex items-center justify-center rounded-lg bg-gray-100 p-3"> <Loader className="text-gray-700" size={24} /> </div> </div> <div className="text-center"> <p className="mb-2 text-gray-600 text-sm">Dark Background</p> <div className="flex items-center justify-center rounded-lg bg-gray-800 p-3"> <Loader className="text-white" size={24} /> </div> </div> </div>);export default Example;Props
<Loader />
Prop
Type