Components

滚动区域

¥Scroll Area

增强原生滚动功能,实现自定义跨浏览器样式。

import * as React from "react";
import { ScrollArea } from "radix-ui";
import "./styles.css";
const TAGS = Array.from({ length: 50 }).map(
(_, i, a) => `v1.2.0-beta.${a.length - i}`,
);
const ScrollAreaDemo = () => (
<ScrollArea.Root className="ScrollAreaRoot">
<ScrollArea.Viewport className="ScrollAreaViewport">
<div style={{ padding: "15px 20px" }}>
<div className="Text">Tags</div>
{TAGS.map((tag) => (
<div className="Tag" key={tag}>
{tag}
</div>
))}
</div>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar className="ScrollAreaScrollbar" orientation="vertical" >
<ScrollArea.Thumb className="ScrollAreaThumb" />
</ScrollArea.Scrollbar>
<ScrollArea.Scrollbar className="ScrollAreaScrollbar" orientation="horizontal" >
<ScrollArea.Thumb className="ScrollAreaThumb" />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className="ScrollAreaCorner" />
</ScrollArea.Root>
);
export default ScrollAreaDemo;

Features

    Scrollbar sits on top of the scrollable content, taking up no space.

    Scrolling is native; no underlying position movements via CSS transformations.

    Shims pointer behaviors only when interacting with the controls, so keyboard controls are unaffected.

    Supports Right to Left direction.

安装

¥Installation

从命令行安装组件。

¥Install the component from your command line.

npm install @radix-ui/react-scroll-area

结构

¥Anatomy

导入所有部分并将它们组合在一起。

¥Import all parts and piece them together.

import { ScrollArea } from "radix-ui";
export default () => (
<ScrollArea.Root>
<ScrollArea.Viewport />
<ScrollArea.Scrollbar orientation="horizontal">
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Scrollbar orientation="vertical">
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Corner />
</ScrollArea.Root>
);

API 参考

¥API Reference

¥Root

包含滚动区域的所有部分。

¥Contains all the parts of a scroll area.

PropTypeDefault
asChild
boolean
false
type
enum
"hover"
scrollHideDelay
number
600
dir
enum
No default value
nonce
string
No default value

视口

¥Viewport

滚动区域的视口区域。

¥The viewport area of the scroll area.

PropTypeDefault
asChild
boolean
false

滚动条

¥Scrollbar

垂直滚动条。添加第二个带有 orientation 属性的 Scrollbar,以启用水平滚动。

¥The vertical scrollbar. Add a second Scrollbar with an orientation prop to enable horizontal scrolling.

PropTypeDefault
asChild
boolean
false
forceMount
boolean
No default value
orientation
enum
vertical
Data attributeValues
[data-state]"visible" | "hidden"
[data-orientation]"vertical" | "horizontal"

缩略图

¥Thumb

ScrollArea.Scrollbar 中使用的滑块。

¥The thumb to be used in ScrollArea.Scrollbar.

PropTypeDefault
asChild
boolean
false
Data attributeValues
[data-state]"visible" | "hidden"

角点

¥Corner

垂直和水平滚动条相交的角。

¥The corner where both vertical and horizontal scrollbars meet.

PropTypeDefault
asChild
boolean
false

可访问性

¥Accessibility

在大多数情况下,最好依赖原生滚动并使用 CSS 中提供的自定义选项。如果这还不够,ScrollArea 还提供了额外的可定制性,同时保留了浏览器原生的滚动行为(以及键盘滚动等辅助功能)。

¥In most cases, it's best to rely on native scrolling and work with the customization options available in CSS. When that isn't enough, ScrollArea provides additional customizability while maintaining the browser's native scroll behavior (as well as accessibility features, like keyboard scrolling).

键盘交互

¥Keyboard Interactions

由于该组件依赖于原生滚动,因此默认支持通过键盘滚动。特定键盘交互在不同平台之间可能有所不同,因此我们不会在此处指定它们,也不会添加特定的事件监听器来处理通过按键事件进行的滚动。

¥Scrolling via keyboard is supported by default because the component relies on native scrolling. Specific keyboard interactions may differ between platforms, so we do not specify them here or add specific event listeners to handle scrolling via key events.