Resizable(可调整大小)
支持键盘操作且具备无障碍性的可调整尺寸面板组及布局。
loading
关于
Resizable 组件基于 react-resizable-panels,该库由 bvaughn 开发。
安装
bash
npx shadcn@latest add resizable1
1. 安装以下依赖:
bash
npm install react-resizable-panels1
2. 将以下代码复制并粘贴到你的项目中。
tsx
"use client"
import { cn } from "cn"
import * as ResizablePrimitive from "react-resizable-panels"
function ResizablePanelGroup({
className,
...props
}: ResizablePrimitive.GroupProps) {
return (
<ResizablePrimitive.Group
data-slot="resizable-panel-group"
className={cn(
"flex h-full w-full aria-[orientation=vertical]:flex-col",
className
)}
{...props}
/>
)
}
function ResizablePanel({ ...props }: ResizablePrimitive.PanelProps) {
return <ResizablePrimitive.Panel data-slot="resizable-panel" {...props} />
}
function ResizableHandle({
withHandle,
className,
...props
}: ResizablePrimitive.SeparatorProps & {
withHandle?: boolean
}) {
return (
<ResizablePrimitive.Separator
data-slot="resizable-handle"
className={cn(
"relative flex w-px items-center justify-center bg-border ring-offset-background after:absolute after:inset-y-0 after:start-1/2 after:w-1 after:-translate-x-1/2 rtl:after:translate-x-1/2 focus-visible:ring-1 focus-visible:ring-ring focus-visible:outline-hidden aria-[orientation=horizontal]:h-px aria-[orientation=horizontal]:w-full aria-[orientation=horizontal]:after:start-0 aria-[orientation=horizontal]:after:h-1 aria-[orientation=horizontal]:after:w-full aria-[orientation=horizontal]:after:translate-x-0 rtl:aria-[orientation=horizontal]:after:-translate-x-0 aria-[orientation=horizontal]:after:-translate-y-1/2 [&[aria-orientation=horizontal]>div]:rotate-90",
className
)}
{...props}
>
{withHandle && (
<div className="z-10 flex h-6 w-1 shrink-0 rounded-lg bg-border" />
)}
</ResizablePrimitive.Separator>
)
}
export { ResizableHandle, ResizablePanel, ResizablePanelGroup }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
3. 更新导入路径,使其与项目配置一致。
用法
tsx
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable"1
2
3
4
5
2
3
4
5
tsx
<ResizablePanelGroup orientation="horizontal">
<ResizablePanel>One</ResizablePanel>
<ResizableHandle />
<ResizablePanel>Two</ResizablePanel>
</ResizablePanelGroup>1
2
3
4
5
2
3
4
5
组件结构
使用以下结构构建 ResizablePanelGroup:
text
ResizablePanelGroup
├── ResizablePanel
├── ResizableHandle
└── ResizablePanel1
2
3
4
2
3
4
垂直
使用 orientation="vertical" 沿垂直方向调整尺寸。
loading
手柄
在 ResizableHandle 上设置 withHandle 属性以显示手柄。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
请参阅 react-resizable-panels 文档。
更新日志
2025-02-02 react-resizable-panels v4
已更新到 react-resizable-panels v4。详情请参阅 v4.0.0 发布说明。
如果直接使用 react-resizable-panels 的基础组件,请留意以下变更:
| v3 | v4 |
|---|---|
PanelGroup | Group |
PanelResizeHandle | Separator |
direction 属性 | orientation 属性 |
defaultSize={50} | defaultSize="50%" |
onLayout | onLayoutChange |
ImperativePanelHandle | PanelImperativeHandle |
Panel 上的 ref 属性 | panelRef 属性 |
data-panel-group-direction | aria-orientation |
shadcn/ui 的封装组件(ResizablePanelGroup、ResizablePanel、 ResizableHandle)保持不变。