Docs
Shadcn

Scroll Area 滚动区域

为溢出内容提供一致的自定义滚动区域。

Preview
01 · 推荐歌曲03:20
02 · 推荐歌曲03:21
03 · 推荐歌曲03:22
04 · 推荐歌曲03:23
05 · 推荐歌曲03:24
06 · 推荐歌曲03:25
07 · 推荐歌曲03:26
08 · 推荐歌曲03:27
Code
import * as React from "react"

import { ScrollArea } from "@scopify/ui/shadcn/components/scroll-area"
import { Separator } from "@scopify/ui/shadcn/components/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 leading-none font-medium">Tags</h4>
        {tags.map((tag) => (
          <React.Fragment key={tag}>
            <div className="text-sm">{tag}</div>
            <Separator className="my-2" />
          </React.Fragment>
        ))}
      </div>
    </ScrollArea>
  )
}

Scopify 说明

本页按 shadcn/ui 官方 Radix UI 组件文档的章节与示例完整整理。示例预览使用 Scopify 当前的 @scopify/ui/shadcn 实现;导入路径、主题 Token 和行为差异以本仓库源码为准。

Installation

npx shadcn@latest add scroll-area

Usage

import { ScrollArea, ScrollBar } from "@scopify/ui/shadcn/components/scroll-area"
<ScrollArea className="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.

Preview
01 · 推荐歌曲03:20
02 · 推荐歌曲03:21
03 · 推荐歌曲03:22
04 · 推荐歌曲03:23
05 · 推荐歌曲03:24
06 · 推荐歌曲03:25
07 · 推荐歌曲03:26
08 · 推荐歌曲03:27
Code
import * as React from "react"
import Image from "next/image"

import { ScrollArea, ScrollBar } from "@scopify/ui/shadcn/components/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 className="w-96 rounded-md border whitespace-nowrap">
      <div className="flex w-max space-x-4 p-4">
        {works.map((artwork) => (
          <figure key={artwork.artist} className="shrink-0">
            <div className="overflow-hidden rounded-md">
              <Image
                src={artwork.art}
                alt={`Photo by ${artwork.artist}`}
                className="aspect-[3/4] h-fit w-fit object-cover"
                width={300}
                height={400}
              />
            </div>
            <figcaption className="pt-2 text-xs text-muted-foreground">
              Photo by{" "}
              <span className="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.

Preview
01 · 推荐歌曲03:20
02 · 推荐歌曲03:21
03 · 推荐歌曲03:22
04 · 推荐歌曲03:23
05 · 推荐歌曲03:24
06 · 推荐歌曲03:25
07 · 推荐歌曲03:26
08 · 推荐歌曲03:27
Code
"use client"

import * as React from "react"

import {
  useTranslation,
  type Translations,
} from "@/components/language-selector"
import { ScrollArea } from "@/styles/radix-nova/ui-rtl/scroll-area"
import { Separator } from "@/styles/radix-nova/ui-rtl/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 className="h-72 w-48 rounded-md border" dir={dir}>
      <div className="p-4">
        <h4 className="mb-4 text-sm leading-none font-medium">{t.tags}</h4>
        {tags.map((tag) => (
          <React.Fragment key={tag}>
            <div className="text-sm">{tag}</div>
            <Separator className="my-2" />
          </React.Fragment>
        ))}
      </div>
    </ScrollArea>
  )
}

API Reference

See the Radix UI Scroll Area documentation.

本页内容