Scroll area

A scroll view with Apple’s overlay scrollbars, which fade in while scrolling and never take layout space.

content

Apple: Scroll views

· Base UI: ScrollArea

A list

"use client"

import { ScrollArea } from "@applecn/ui/components/scroll-area"
import { Text } from "@applecn/ui/components/text"

const albums = [
  "Abbey Road",
  "Blue",
  "Kind of Blue",
  "Rumours",
  "Purple Rain",
  "Blonde",
  "Currents",
  "Discovery",
  "Homogenic",
  "Illinois",
  "In Rainbows",
  "Lemonade",
]

export default function ScrollAreaBasic() {
  return (
    <ScrollArea className="h-48 w-full max-w-sm rounded-card bg-card">
      <ul className="flex flex-col p-2">
        {albums.map((album) => (
          <li
            key={album}
            className="flex h-(--sidebar-row-height) items-center rounded-sidebar px-3 text-[length:var(--sidebar-font)] hover:bg-fill-4"
          >
            <Text as="span" variant="body">
              {album}
            </Text>
          </li>
        ))}
      </ul>
    </ScrollArea>
  )
}

Install

From the registry this site publishes, with dependencies resolved by the CLI.

npx shadcn@latest add https://applecn.vercel.app/r/scroll-area.json

Source

packages/ui/src/components/scroll-area.tsx

"use client"

import { ScrollArea as ScrollAreaPrimitive } from "@base-ui/react/scroll-area"
import { cn } from "cn"

/**
 * A scroll view (HIG › Scroll views) with Apple's overlay scrollbars: the content scrolls
 * natively, and a rounded thumb on the tertiary label fades in while scrolling or hovering
 * and out again when idle, never taking layout space.
 */
type ScrollAreaProps = ScrollAreaPrimitive.Root.Props & {
  orientation?: "vertical" | "horizontal" | "both"
}

function ScrollArea({
  className,
  children,
  orientation = "vertical",
  ...props
}: ScrollAreaProps) {
  return (
    <ScrollAreaPrimitive.Root
      data-slot="scroll-area"
      className={cn("relative min-h-0 min-w-0", className)}
      {...props}
    >
      <ScrollAreaPrimitive.Viewport
        data-slot="scroll-area-viewport"
        className="size-full overscroll-contain rounded-[inherit] outline-none focus-visible:ring-4 focus-visible:ring-ring/60"
      >
        {children}
      </ScrollAreaPrimitive.Viewport>
      {orientation !== "horizontal" ? (
        <ScrollAreaScrollbar orientation="vertical" />
      ) : null}
      {orientation !== "vertical" ? (
        <ScrollAreaScrollbar orientation="horizontal" />
      ) : null}
      {orientation === "both" ? (
        <ScrollAreaPrimitive.Corner data-slot="scroll-area-corner" />
      ) : null}
    </ScrollAreaPrimitive.Root>
  )
}

function ScrollAreaScrollbar({
  className,
  orientation = "vertical",
  ...props
}: ScrollAreaPrimitive.Scrollbar.Props) {
  return (
    <ScrollAreaPrimitive.Scrollbar
      data-slot="scroll-area-scrollbar"
      orientation={orientation}
      keepMounted
      className={cn(
        "flex touch-none opacity-0 transition-opacity duration-(--duration-hover) ease-(--ease-standard) select-none data-hovering:opacity-100 data-hovering:duration-(--duration-press) data-scrolling:opacity-100 data-scrolling:duration-(--duration-press)",
        orientation === "vertical" &&
          "absolute inset-y-0.5 end-0.5 w-2 justify-center",
        orientation === "horizontal" &&
          "absolute inset-x-0.5 bottom-0.5 h-2 flex-col justify-center",
        className
      )}
      {...props}
    >
      <ScrollAreaPrimitive.Thumb
        data-slot="scroll-area-thumb"
        className={cn(
          "rounded-full bg-label-3",
          orientation === "vertical" ? "w-1.5" : "h-1.5"
        )}
      />
    </ScrollAreaPrimitive.Scrollbar>
  )
}

export { ScrollArea, ScrollAreaScrollbar }
export type { ScrollAreaProps }