Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Scroll Area for Svelte UI SDK

A box of fixed height that scrolls its own content, for a tag list, a chat log or a strip of photos, with a thin scrollbar. For Svelte on UI SDK.

On this page9 sections

A scroll area is a box with a set height or width that scrolls its own content, so a long list stays inside a sidebar, a popover or a card instead of stretching the page. It suits a tag list, a chat log or a row of thumbnails.

When the items should snap one at a time and come with previous and next buttons, use a Carousel.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Scroll Area anatomy: root, viewport, content, scrollbar, thumb

Usage

<script lang="ts">
import { ScrollArea } from "@/components/ui/scroll-area";
</script>

The height on ScrollArea is what makes it scroll. Its content can be any length.

const tags = Array.from({ length: 50 }, (_, i) => `v1.2.0-beta.${50 - i}`);

const Tags = () => (
  <ScrollArea className="h-72 w-48 rounded-md border">
    <div className="p-4">
      <h4 className="mb-4 font-medium text-sm leading-none">Tags</h4>
      {tags.map((tag) => (
        <div className="border-b py-2 text-sm last:border-0" key={tag}>
          {tag}
        </div>
      ))}
    </div>
  </ScrollArea>
);

Accessibility

UI SDK hides the browser's scrollbar and draws its own, which takes no focus and is hidden from screen readers. The box inside still scrolls natively, and UI SDK makes it a tab stop while its content overflows and nothing inside it can take focus, so keyboard users can Tab in and scroll with the keys below. The component draws a focus ring there. The box has no role or name of its own, so when it holds a whole section of the page, such as a chat log, put a heading above it.

Tab
Moves focus to the scrolling box when it is in the tab order, then on to the links and buttons inside it.
Arrow Down
When the box has focus, scrolls it down a short step.
Arrow Up
When the box has focus, scrolls it up a short step.
Arrow Right
When the box has focus and its content is wider than it, scrolls right.
Arrow Left
When the box has focus and its content is wider than it, scrolls left.
Page Down
When the box has focus, scrolls down by about the height of the box.
Page Up
When the box has focus, scrolls up by about the height of the box.
Home
When the box has focus, scrolls to the top.
End
When the box has focus, scrolls to the bottom.

Demos

Horizontal

Three photos with their photographers' names sit in one row that doesn't wrap, w-max inside a box w-96 wide, so the extra width becomes the scroll range. The area renders a horizontal bar beside the vertical one and shows each only when its axis overflows, so nothing is added for it. Use it for a strip of thumbnails or logos that must stay one row tall.

Preview width

RTL

The tag list again, with an Arabic heading and dir set to rtl on the area, so the heading and rows align right and the scrollbar sits on the left edge. UI SDK places its bars with logical insets and reads the direction from the page, so they follow the dir of the area or of any parent. Use it for an Arabic or Hebrew interface.

Preview width

FAQ

Why won't my Scroll Area scroll?

A Scroll Area scrolls only when its own box is shorter than its content, so it needs a height that doesn't come from the content: a class such as h-72, as the demo has, or h-full inside a parent whose height is set.

The area is size-full with min-h-0 already, so inside a parent with a set height, a flex column included, it fills that parent and scrolls with no class of its own. If the parent's height comes from its content, nothing is left to scroll.

How do I give a Scroll Area a max height instead of a fixed one?

Put the class on ScrollArea, such as max-h-72. The area then stays as short as its content and starts to scroll once it reaches the limit, which suits a dropdown of results or a comment thread whose length you don't know in advance.

The area is a flex column, and the viewport inside it, the box that actually scrolls, may shrink below its content, so it stops at the area's limit instead of growing past it.

How do I make a Scroll Area scroll horizontally?

Make the content wider than the area and keep it from wrapping: a row with w-max, and whitespace-nowrap on the area, as the Horizontal demo does.

The area already renders a horizontal bar beside the vertical one and shows each only when its axis overflows, so there is nothing to add.

How do I keep the Scroll Area scrollbar always visible?

It already is whenever there is something to scroll. This Scroll Area passes type set to auto to UI SDK, which shows a bar whenever its axis overflows and hides it when the content fits. Set type to always on ScrollArea to keep the track in view even then, or to hover or scroll to show the bar only while the pointer is over the area or the content moves.

How do I change the color of the Scroll Area scrollbar?

Edit ScrollAreaScrollbar in components/ui/scroll-area, which is your code once installed. The thumb is the element whose data-slot is scroll-area-thumb, colored by its bg-border class, so swap that for another color, such as bg-muted-foreground/40, and give ScrollAreaScrollbar a background to show the track behind it. Set scrollFade on ScrollArea to also fade the top and bottom edges while there is more to scroll.

How do I scroll a Scroll Area to the bottom when a new message arrives?

Set stickToEnd on ScrollArea, and UI SDK does it for you: the area opens at the bottom and keeps each new message in view while the reader is at the end. It stops following once they scroll up, and starts again when they scroll back to the bottom.

Scroll only when the reader was already at the end, so someone who moved up to an older message isn't pulled back down. A chat log, a build output and an activity feed all want that exception.

Should I use a Scroll Area or plain CSS overflow?

Use Scroll Area when the bar should look the same in every browser: it hides the native scrollbar and draws a thin themed one that sits over the content instead of taking width from it. Plain overflow-y-auto is enough when the platform's own bar is fine, and it runs no JavaScript, while this component measures the content to size and place its thumb.

Neither one touches the page's own scrollbar. A Scroll Area styles the bar of its own box, and the window keeps the browser's.

Does a Scroll Area work in a right-to-left layout?

Yes. UI SDK places its bars with logical insets and reads the direction from the area's own element, so with dir set to rtl on the area or on any parent, the vertical bar moves to the left edge, as in the RTL demo. A horizontal area needs no provider either: its thumb starts at the right and moves left as the row scrolls.

API Reference

ScrollArea

<div> data-slot="scroll-area"

PropType
controller?{ scrollToEnd: (options?: ScrollAreaScrollOptions) → void; scrollToStart: (options?: ScrollAreaScrollOptions) → void; isAtEnd: () → boolean; isAtStart: () → boolean; isSticking: () → boolean; }

Drives it from anywhere. Create one with createScrollAreaController().

Type
{ scrollToEnd: (options?: ScrollAreaScrollOptions) → void; scrollToStart: (options?: ScrollAreaScrollOptions) → void; isAtEnd: () → boolean; isAtStart: () → boolean; isSticking: () → boolean; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

scrollHideDelay?number

How long scrollbars stay after they stop being needed, in ms, with "scroll" and "hover".

Default
600
stickToEnd?boolean

Keeps the end in view while the content grows, like a chat or streamed output: it starts at the bottom and stays there. Scrolling toward the top lets go, and scrolling back to the bottom sticks again.

Default
false
type?ScrollAreaType

When the scrollbars show: "auto" whenever the content overflows, "always" even when it doesn't, "scroll" while scrolling, and "hover" while scrolling or while the pointer is over the scroll area. Each scrollbar has data-visible while it should show; style that. UI SDK's default: hover.

Default
"auto"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-at-end?string

Present while the view is at the end: the bottom, or the inline end when only the horizontal axis overflows.

data-at-start?string

Present while the view is at the start: the top, or the inline start when only the horizontal axis overflows. Also when nothing overflows.

data-at-x-end?string

Present while the view is at the inline end, within a pixel.

data-at-x-start?string

Present while the view is at the inline start, within a pixel.

data-at-y-end?string

Present while the view is at the bottom, within a pixel.

data-at-y-start?string

Present while the view is at the top, within a pixel.

data-hovering?string

Present while a mouse or pen is over the scroll area.

data-overflow-x?string

Present while the content is wider than the viewport.

data-overflow-y?string

Present while the content is taller than the viewport.

data-scrolling?string

Present while the user scrolls, and for a moment after. Keeping the end in view doesn't count.

data-sticking?string

Present while the view follows the end as content grows, with stickToEnd.

CSS variableType
--scroll-area-corner-heightstring

The horizontal scrollbar's height while both show, so the vertical one stops short of it. 0px otherwise.

--scroll-area-corner-widthstring

The vertical scrollbar's width while both scrollbars show, so the horizontal one stops short of it. 0px otherwise.

--scroll-area-overflow-x-endstring

How far the content reaches past the inline end, in pixels.

--scroll-area-overflow-x-startstring

How far the content reaches past the inline start, in pixels, left in LTR and right in RTL.

--scroll-area-overflow-y-endstring

How far the content reaches below the view, in pixels.

--scroll-area-overflow-y-startstring

How far the content reaches above the view, in pixels. Grow a top fade or shadow with it. Not inherited, like the other overflow and progress variables, so scrolling never restyles the content. A child of the root that reads it takes it with --scroll-area-overflow-y-start: inherit.

--scroll-area-progress-xstring

How far along the inline axis the view is, from 0 to 1.

--scroll-area-progress-ystring

How far down the view is, from 0 at the top to 1 at the bottom. A reading progress bar can scale with it.

ScrollBar

<div> data-slot="scroll-area-scrollbar"

PropType
orientation?"horizontal" | "vertical"

The axis the scrollbar scrolls.

Type
"horizontal" | "vertical"
Default
"vertical"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-dragging?string

Present while the thumb is dragged.

data-hovering?string

Present while a mouse or pen is over the scroll area.

data-orientation"vertical" | "horizontal"

The axis the scrollbar scrolls.

Type
"vertical" | "horizontal"
data-overflow-y?string

Present while the content is taller than the viewport.

data-scrolling?string

Present while its own axis scrolls.

data-visible?string

Present while the scrollbar should show, by type. Hidden scrollbars let presses through.

CSS variableType
--scroll-area-thumb-heightstring

On a vertical scrollbar: its thumb's length in pixels. The thumb's inline height reads it.

--scroll-area-thumb-offsetstring

How far its thumb sits from the track's start, in pixels, negative right to left. The thumb's inline transform reads it.

--scroll-area-thumb-widthstring

On a horizontal scrollbar: its thumb's length in pixels. The thumb's inline width reads it.