Scroll Area

Augments native scroll functionality for custom, cross-browser styling.

Client JS
import { Fragment } from "hono/jsx"
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 class="h-72 w-48 rounded-md border">
      <div class="p-4">
        <h4 class="mb-4 text-sm leading-none font-medium">Tags</h4>
        {tags.map((tag) => (
          <Fragment key={tag}>
            <div class="text-sm">{tag}</div>
            <Separator class="my-2" />
          </Fragment>
        ))}
      </div>
    </ScrollArea>
  )
}

Installation

pnpm dlx shadcnui-hono-jsx@latest add scroll-area

This component uses a small client script. add installs it into public/shadcn/; serve that directory at /shadcn/ and load the script on pages that use the component (see Client Scripts):

<script type="module" src="/shadcn/scroll-area.js"></script>

Usage

import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"
<ScrollArea class="h-[200px] w-[350px] rounded-md border p-4">
  Your scrollable content here.
</ScrollArea>

Composition

Use the following composition to build a ScrollArea:

ScrollArea
└── ScrollBar

Horizontal

Use ScrollBar with orientation="horizontal" for horizontal scrolling.

import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"

export interface Artwork {
  artist: string
  art: string
}

export const works: Artwork[] = [
  {
    artist: "Ornella Binni",
    art: "https://images.unsplash.com/photo-1465869185982-5a1a7522cbcb?auto=format&fit=crop&w=300&q=80",
  },
  {
    artist: "Tom Byrom",
    art: "https://images.unsplash.com/photo-1548516173-3cabfa4607e9?auto=format&fit=crop&w=300&q=80",
  },
  {
    artist: "Vladimir Malyavko",
    art: "https://images.unsplash.com/photo-1494337480532-3725c85fd2ab?auto=format&fit=crop&w=300&q=80",
  },
]

export function ScrollAreaHorizontalDemo() {
  return (
    <ScrollArea class="w-96 rounded-md border whitespace-nowrap">
      <div class="flex w-max space-x-4 p-4">
        {works.map((artwork) => (
          <figure key={artwork.artist} class="shrink-0">
            <div class="overflow-hidden rounded-md">
              <img
                src={artwork.art}
                alt={`Photo by ${artwork.artist}`}
                class="aspect-[3/4] h-fit w-fit object-cover"
                width={300}
                height={400}
              />
            </div>
            <figcaption class="pt-2 text-xs text-muted-foreground">
              Photo by{" "}
              <span class="font-semibold text-foreground">
                {artwork.artist}
              </span>
            </figcaption>
          </figure>
        ))}
      </div>
      <ScrollBar orientation="horizontal" />
    </ScrollArea>
  )
}

RTL

To enable RTL support in shadcn/ui, see the RTL configuration guide.

// This example sets dir and uses useTranslation because this site is not RTL.

// In an RTL app you won't need them; see the RTL guide.

import { Fragment } from "hono/jsx"
import {
  type Translations,
  useTranslation,
} from "@/components/language-selector"
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}`
)

const translations: Translations = {
  en: {
    dir: "ltr",
    values: {
      tags: "Tags",
    },
  },
  ar: {
    dir: "rtl",
    values: {
      tags: "العلامات",
    },
  },
  he: {
    dir: "rtl",
    values: {
      tags: "תגיות",
    },
  },
}

export function ScrollAreaRtl() {
  const { dir, t } = useTranslation(translations, "ar")

  return (
    <ScrollArea class="h-72 w-48 rounded-md border" dir={dir}>
      <div class="p-4">
        <h4 class="mb-4 text-sm leading-none font-medium">{t.tags}</h4>
        {tags.map((tag) => (
          <Fragment key={tag}>
            <div class="text-sm">{tag}</div>
            <Separator class="my-2" />
          </Fragment>
        ))}
      </div>
    </ScrollArea>
  )
}

Notes

  • The custom scrollbars need the client script /shadcn/scroll-area.js (<script type="module" src="/shadcn/scroll-area.js">); without it the area scrolls with the browser's own scrollbar.
  • overflowEdgeThreshold is not supported.
  • Accepts class instead of className.
  • Upstream documentation: shadcn/ui Scroll Area. See also Differences from shadcn/ui.

API Reference

See the Base UI Scroll Area documentation.