Scroll Area
Augments native scroll functionality for custom, cross-browser styling.
tsx
import { ScrollArea } from "@/components/ui/scroll-area"import { Separator } from "@/components/ui/separator"
const tags = Array.from({ length: 50 }).map( (_, i, a) => `v1.2.0-beta.${a.length - i}`)
export function ScrollAreaDemo() { return ( <ScrollArea className="h-72 w-48 rounded-md border"> <div className="p-4"> <h4 className="mb-4 text-sm font-medium leading-none">Tags</h4> {tags.map((tag) => ( <React.Fragment key={tag}> <div className="text-sm"> {tag} </div> <Separator className="my-2" /> </React.Fragment> ))} </div> </ScrollArea> )}
Installation
CLI
bash
npx fivui add scroll-area
Manual
Install the following dependencies:
bash
npm install @radix-ui/react-scroll-area
Usage
tsx
import { ScrollArea } from "@/components/ui/scroll-area"
tsx
<ScrollArea className="h-[200px] w-[350px] rounded-md border p-4"> Jokester began sneaking into the castle in the middle of the night and leaving jokes all over the place: under the king's pillow, in his soup, even in the royal toilet. The king was furious, but he couldn't seem to stop Jokester. And then, one day, the people of the kingdom discovered that the jokes left by Jokester were so funny that they couldn't help but laugh. And once they started laughing, they couldn't stop.</ScrollArea>
Examples
Horizontal Scrolling
Use horizontal scrolling for content that extends beyond the container width.
Horizontal Scroll
Scrollable content in horizontal direction
tsx
import { ScrollArea } from "@/components/ui/scroll-area"
export function ScrollAreaHorizontal() { return ( <ScrollArea className="w-96 whitespace-nowrap rounded-md border"> <div className="flex w-max space-x-4 p-4"> {Array.from({ length: 20 }).map((_, i) => ( <div key={i} className="shrink-0 rounded-md border border-slate-200 bg-white p-4 shadow-sm" > <div className="h-20 w-20 rounded-md bg-slate-100" /> <div className="mt-2 text-sm font-medium">Item {i + 1}</div> </div> ))} </div> </ScrollArea> )}
Chat Messages
A common use case for scroll areas is displaying chat messages or conversation history.
Chat Interface
Scrollable chat messages with alternating layout
tsx
import { ScrollArea } from "@/components/ui/scroll-area"
export function ScrollAreaChat() { const messages = [ "Hey there! How's it going?", "I'm doing well, thanks for asking!", "That's great to hear. What have you been up to lately?", "Just working on some new projects. How about you?", "Same here! I've been learning React and Next.js.", "That's awesome! Those are great technologies to learn.", "Yeah, I'm really enjoying it so far.", "If you need any help, feel free to ask!", "Thanks, I appreciate that!", "No problem at all. Good luck with your learning!", ]
return ( <ScrollArea className="h-64 w-full rounded-md border p-4"> <div className="space-y-4"> {messages.map((message, i) => ( <div key={i} className={`flex ${ i % 2 === 0 ? "justify-start" : "justify-end" }`} > <div className={`max-w-xs rounded-lg px-3 py-2 text-sm ${ i % 2 === 0 ? "bg-muted text-muted-foreground" : "bg-primary text-primary-foreground" }`} > {message} </div> </div> ))} </div> </ScrollArea> )}
API Reference
ScrollArea
Contains all the parts of a scroll area.
Prop | Type | Default | Description |
---|---|---|---|
type | auto | always | scroll | hover | hover | Describes the nature of scrollbar visibility. |
scrollHideDelay | number | 600 | If type is set to either scroll or hover, this prop determines the length of time, in milliseconds, before the scrollbars are hidden after the user stops interacting with scrollbars. |
dir | ltr | rtl | ltr | The reading direction of the scroll area. |
External Documentation
Scroll Area is built on top of Radix UI Scroll Area. For more advanced usage and API details, refer to the official documentation.
View Radix UI Scroll Area Documentation