Scroll Area

A region that scrolls inside a set size, with scrollbars that match the system.

The browser still does the scrolling, so the wheel, touch, the keyboard and find in page work as usual; only the scrollbars are drawn by the system. Give it a size, and an aria-label to make it a named region screen readers can jump to.

Preview
basic.tsx
import { inlineVariants } from "~/components/ui/inline"
import { ScrollArea } from "~/components/ui/scroll-area"
import { Text, textVariants } from "~/components/ui/text"

const chapters = Array.from({ length: 30 }, (_, index) => ({
  number: index + 1,
  words: 2400 + ((index * 731) % 2600),
}))

export default function Example() {
  return (
    <div className="h-72 w-full max-w-xs rounded-md border border-border">
      <ScrollArea aria-label="Chapters" className="h-full">
        <div className="p-md">
          <h3 className={textVariants({ size: "sm", weight: "medium", className: "pb-sm" })}>
            Chapters
          </h3>
          <ul className="divide-y divide-border">
            {chapters.map((chapter) => (
              <li
                key={chapter.number}
                className={inlineVariants({
                  justify: "between",
                  className: `py-sm ${textVariants({ size: "sm" })}`,
                })}
              >
                <span>Chapter {chapter.number}</span>
                <Text render={<span />} tone="muted" tabular>
                  {chapter.words.toLocaleString("en-US")} words
                </Text>
              </li>
            ))}
          </ul>
        </div>
      </ScrollArea>
    </div>
  )
}

Sideways

Set orientation="horizontal" to scroll sideways, and type="auto" to keep the scrollbar in view so the row reads as one that scrolls. orientation="both" scrolls either way.

Preview
horizontal.tsx
import { inlineVariants } from "~/components/ui/inline"
import { ScrollArea } from "~/components/ui/scroll-area"
import { textVariants } from "~/components/ui/text"

const genres = [
  "Gothic",
  "Adventure",
  "Romance",
  "Mystery",
  "Satire",
  "Epistolary",
  "Horror",
  "Fantasy",
  "Historical",
  "Picaresque",
  "Tragedy",
  "Fable",
]

export default function Example() {
  return (
    <div className="w-full max-w-sm">
      <ScrollArea orientation="horizontal" aria-label="Genres" type="auto">
        <ul className={inlineVariants({ gap: "sm", wrap: false, className: "pb-md" })}>
          {genres.map((genre) => (
            <li
              key={genre}
              className={textVariants({
                size: "sm",
                className: "shrink-0 rounded-md border border-border px-md py-xs",
              })}
            >
              {genre}
            </li>
          ))}
        </ul>
      </ScrollArea>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesNothing else in the system.
Used byConversationblockDialogMenuNav SheetblockPopoverSelectform itemSheetSidebarTableWorkspaceblock
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/scroll-area.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Then copy the source:

components/ui/scroll-area.tsx
import {
  ScrollArea as ScrollAreaPrimitive,
  ScrollAreaContent,
  ScrollAreaCorner,
  ScrollAreaScrollbar as ScrollAreaScrollbarPrimitive,
  ScrollAreaThumb,
  ScrollAreaViewport,
} from "@ui-sdk/react/scroll-area"
import type * as React from "react"

import { cnState } from "~/lib/utils"

type ScrollAreaProps = Omit<React.ComponentProps<typeof ScrollAreaPrimitive>, "children"> & {
  children?: React.ReactNode
  /** Which ways the content scrolls. A horizontal scroll area lets its content grow as wide as it needs. */
  orientation?: "vertical" | "horizontal" | "both"
  /** Names the scrolling region, which makes it a landmark screen readers can jump to. */
  "aria-label"?: string
}

/**
 * A region that scrolls inside a set size, with scrollbars that match the
 * system. The browser still scrolls it, so the wheel, touch, keyboard and find
 * in page all work. Give it a height (or width, to scroll sideways).
 */
function ScrollArea({
  orientation = "vertical",
  "aria-label": label,
  className,
  children,
  ...props
}: ScrollAreaProps) {
  return (
    <ScrollAreaPrimitive
      className={cnState("relative flex flex-col overflow-hidden", className)}
      {...props}
    >
      {/* Filled by flex, not 100% height, so it scrolls when a parent shrinks the area. */}
      <ScrollAreaViewport
        role={label === undefined ? undefined : "region"}
        aria-label={label}
        className="min-h-0 w-full flex-1 outline-none focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:ring-inset"
      >
        {/* At least the viewport's size, and a column, so a child can fill it (`flex-1`) or stretch across it. */}
        <ScrollAreaContent className="flex min-h-full min-w-full flex-col">
          {children}
        </ScrollAreaContent>
      </ScrollAreaViewport>
      {orientation !== "horizontal" && <ScrollBar orientation="vertical" />}
      {orientation !== "vertical" && <ScrollBar orientation="horizontal" />}
      {orientation === "both" && <ScrollAreaCorner />}
    </ScrollAreaPrimitive>
  )
}

/** One axis's scrollbar: shown while the pointer is over the area or it scrolls. ScrollArea adds its own. */
function ScrollBar({
  className,
  ...props
}: React.ComponentProps<typeof ScrollAreaScrollbarPrimitive>) {
  return (
    <ScrollAreaScrollbarPrimitive
      className={cnState(
        [
          "flex p-px opacity-0 transition-opacity duration-150 select-none data-visible:opacity-100",
          "data-[orientation=vertical]:w-2 data-[orientation=horizontal]:h-2 data-[orientation=horizontal]:flex-col",
        ].join(" "),
        className,
      )}
      {...props}
    >
      <ScrollAreaThumb className="relative flex-1 bg-foreground/20 transition-colors hover:bg-foreground/35 data-dragging:bg-foreground/45" />
    </ScrollAreaScrollbarPrimitive>
  )
}

export { ScrollArea, ScrollBar }
export type { ScrollAreaProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

ScrollArea

A region that scrolls inside a set size, with scrollbars that match the system. The browser still scrolls it, so the wheel, touch, keyboard and find in page all work. Give it a height (or width, to scroll sideways).

aria-labelours

Names the scrolling region, which makes it a landmark screen readers can jump to.

Type string

orientationours

Which ways the content scrolls. A horizontal scroll area lets its content grow as wide as it needs.

Type "both" | "horizontal" | "vertical" · Default vertical

className

Type string | ((state: NoInfer<ScrollAreaState>) => string)

controller

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

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

debug

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

Type boolean

render

Type RenderProp<NoInfer<ScrollAreaState>>

scrollHideDelay

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

Type number · Default 600

stickToEnd

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.

Type boolean · Default false

style

Type CSSProperties | ((state: NoInfer<ScrollAreaState>) => CSSProperties)

type

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.

Type ScrollAreaType · Default "hover"

ScrollBar

One axis's scrollbar: shown while the pointer is over the area or it scrolls. ScrollArea adds its own.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: ScrollAreaScrollbarState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: ScrollAreaScrollbarState) => string)

orientation

The axis the scrollbar scrolls.

Type "horizontal" | "vertical" · Default "vertical"

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<ScrollAreaScrollbarState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: ScrollAreaScrollbarState) => CSSProperties)