Skip to Content
文档

滚动区域

用于创建带有自定义样式滚动条的可滚动区域

SourceStorybookRecipeArk

用法

¥Usage

import { ScrollArea } from "@chakra-ui/react"
<ScrollArea.Root>
  <ScrollArea.Viewport>
    <ScrollArea.Content />
  </ScrollArea.Viewport>
  <ScrollArea.Scrollbar>
    <ScrollArea.Thumb />
  </ScrollArea.Scrollbar>
  <ScrollArea.Corner />
</ScrollArea.Root>

示例

¥Examples

变量

¥Variants

使用 variant 属性更改滚动条的可见性行为。值可以是 hover(默认)或 always

¥Use the variant prop to change the scrollbar visibility behavior. Values can be either hover (default) or always.

variant="hover"

variant="always"

尺寸

¥Sizes

使用 size 属性更改滚动区域的大小。这会影响滚动条的粗细和内容填充。

¥Use the size prop to change the size of the scroll area. This affects the scrollbar thickness and content padding.

size="xs"

size="sm"

size="md"

size="lg"

水平滚动

¥Horizontal Scrolling

当内容水平溢出时,滚动区域会自动支持水平滚动。

¥The scroll area automatically supports horizontal scrolling when content overflows horizontally.

双向

¥Both Directions

当内容在两个方向上溢出时,两个滚动条都会显示。

¥When content overflows in both directions, both scrollbars will appear.

info

你可以渲染 ScrollArea.Corner 组件,使其显示一个角指示器,填充两个滚动条的交叉点,从而实现无缝且富有样式的外观。

¥You can render the ScrollArea.Corner component to show a corner indicator to fill the intersection of the two scrollbars for a seamless, styled appearance.

滚动阴影

¥Scroll Shadow

通过使用 mask-image 在边缘显示滚动阴影,在内容可滚动时添加视觉反馈。

¥Add visual feedback when content is scrollable by implementing scroll shadows that appear at the edges using mask-image.

缩略图样式

¥Thumb Styling

使用不同的样式和颜色自​​定义滚动条缩略图的外观。

¥Customize the appearance of the scrollbar thumb with different styles and colors.

贴在底部

¥Stick to Bottom

实现类似聊天的行为,使新内容自动滚动到底部,但允许手动滚动。

¥Implement chat-like behavior where new content automatically scrolls to the bottom, but allows manual scrolling.

本示例使用 use-stick-to-bottom 组件实现滚动固定。

虚拟化

¥Virtualization

使用 @tanstack/react-virtual 仅渲染可见项目,高效处理大型数据集。

¥Handle large datasets efficiently by rendering only visible items using @tanstack/react-virtual.

商店

¥Store

将滚动区域与外部状态管理和编程控制结合使用。

¥Use the scroll area with external state management and programmatic control.

滚动到侧边

¥Scroll to Side

通过滚动到不同的侧面和方向,以编程方式浏览内容。

¥Programmatically navigate through content by scrolling to different sides and directions.

滚动到位置

¥Scroll to Position

使用流畅的动画跳转到可滚动区域内的特定位置或项目。

¥Jump to specific positions or items within the scrollable area with smooth animations.

RTL 支持

¥RTL Support

滚动区域组件完全支持从右到左 (RTL) 的语言,例如阿拉伯语和希伯来语。

¥The scroll area component fully supports Right-to-Left (RTL) languages like Arabic and Hebrew.

带菜单

¥With Menu

将滚动区域与其他组件(例如菜单)结合使用,以处理溢出内容。此示例演示如何创建包含多个项目的可滚动菜单。

¥Combine scroll area with other components like menus to handle overflowing content. This example shows how to create a scrollable menu with many items.

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

variant 'hover'
'hover' | 'always'

The variant of the component

size 'md'
'xs' | 'sm' | 'md' | 'lg'

The size of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

ids
Partial<{ root: string; viewport: string; content: string; scrollbar: string; thumb: string }>

The ids of the scroll area elements

视口

¥Viewport

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

内容

¥Content

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

滚动条

¥Scrollbar

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
orientation
Orientation

缩略图

¥Thumb

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

角落

¥Corner

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Previous

Next

分隔符