Resizable
用指针和键盘调整、收起并保存面板布局。
用一组可调整大小的面板,让人们拖动它们之间的句柄或使用键盘,调整布局中各窗格的大小,比如编辑器旁边的文件列表。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel defaultSize={35} minSize={20}>
<h3>Files</h3>
<ul>
<li>page.tsx</li>
<li>layout.tsx</li>
<li>globals.css</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize files" />
<ResizablePanel defaultSize={65} minSize={40}>
<Resizable orientation="vertical">
<ResizablePanel defaultSize={60} minSize={30}>
<h3>page.tsx</h3>
<span data-slot="hint">Saved just now</span>
</ResizablePanel>
<ResizableHandle aria-label="Resize editor" />
<ResizablePanel defaultSize={40} minSize={30}>
<h3>Terminal</h3>
<span data-slot="hint">Ready on port 3000</span>
</ResizablePanel>
</Resizable>
</ResizablePanel>
</Resizable>
)
}
export { Example }安装
npm install @ui-sdk/solid @ui-sdk/core组合
用下面的组合构建一个 Resizable:
Resizable
├── ResizablePanel
├── ResizableHandle
└── ResizablePanel在你想调整大小的面板之间放一个句柄,并用 aria-label 给它命名,比如“Resize files”。这一组会把面板排成一行,所以要给它一个尺寸,每个面板的大小是各面板共享空间的百分比。一组面板可以放在一个面板里面,比如终端上方的编辑器。
用法
导入各个部件并把它们组合起来:
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel defaultSize={30}>Files</ResizablePanel>
<ResizableHandle aria-label="Resize files" />
<ResizablePanel>Editor</ResizablePanel>
</Resizable>
)
}
export { Example }没有 defaultSize 的面板占剩下的空间,这里是 70%。
自己构建
@ui-sdk/solid/resizable 自带一个现成的 Resizable。如果你想用自己的名字构建一个,可以从 @ui-sdk/core 里的 resizable 原语(primitive)开始:
import { resizable } from "@ui-sdk/core/resizable"
import { createParts } from "@ui-sdk/solid/primitive"
const {
Root: SplitView,
Panel: SplitViewPane,
Handle: SplitViewDivider,
} = createParts(resizable, { root: "div", panel: "div", handle: "div" })
export { SplitView, SplitViewDivider, SplitViewPane }<SplitView>
<SplitViewPane defaultSize={30}>Files</SplitViewPane>
<SplitViewDivider aria-label="Resize files" />
<SplitViewPane>Editor</SplitViewPane>
</SplitView>每个部件都保留其现成对应部件的行为、状态和数据属性:根部件负责排列面板,每个窗格把大小写入 --resizable-panel-size,分隔部件是可聚焦的 separator。更多内容见渲染原语和自定义部件。
垂直
设置 orientation="vertical" 把面板堆叠起来,并给这一组一个高度。ArrowUp 和 ArrowDown 移动句柄。每个部件都会得到带有这一组方向的 data-orientation,所以垂直组的句柄是 [data-slot="resizable-handle"][data-orientation="vertical"]。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable orientation="vertical">
<ResizablePanel defaultSize={45} minSize={25}>
<h3>Inbox</h3>
<span data-slot="hint">3 unread messages</span>
</ResizablePanel>
<ResizableHandle aria-label="Resize inbox" />
<ResizablePanel defaultSize={55} minSize={25}>
<h3>Quarterly report</h3>
<span data-slot="hint">From Hollis Grant</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }句柄
把任何内容放进句柄,比如一个表明它可以拖动的握柄。句柄本身可以保持成一条细线:即使不加任何样式,它的命中区域也至少有 10 像素宽,以它为中心,触摸时为 24 像素。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel defaultSize={35} minSize={20}>
<h3>Channels</h3>
<ul>
<li>general</li>
<li>design</li>
<li>releases</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize channels">
<div />
</ResizableHandle>
<ResizablePanel defaultSize={65} minSize={20}>
<h3>general</h3>
<span data-slot="hint">Kofi Mensah: The release notes are ready.</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }可收起
collapsible 让面板越过它的 minSize 一直收起到 collapsedSize(默认 0):可以拖过中点到达它的收起尺寸,也可以在它的句柄上按 Enter 或双击,或者通过控制器。往回拖过同一个位置会以最小尺寸打开它,按 Enter 则恢复它原来的尺寸。收起的面板会得到 data-collapsed,尺寸为 0 时,它的内容会离开 Tab 顺序和无障碍树。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel
defaultSize={30}
minSize={25}
collapsible
collapsedSize="48px"
>
<ul>
<li data-slot="row">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z" />
</svg>
<span>Files</span>
</li>
<li data-slot="row">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="m21 21-4.34-4.34" />
<circle cx="11" cy="11" r="8" />
</svg>
<span>Search</span>
</li>
<li data-slot="row">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915" />
<circle cx="12" cy="12" r="3" />
</svg>
<span>Settings</span>
</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize sidebar">
<div />
</ResizableHandle>
<ResizablePanel minSize={40}>
<h3>README.md</h3>
<span data-slot="hint">Edited by Inès Moreau</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }像素尺寸
尺寸是作为数字的百分比,或者带 %、px 或 rem 的字符串。这一组调整大小时,像素和 rem 的限制仍然成立,所以 minSize="160px" 的导航永远不会变得更窄,由它引起的变更会报告 resize 原因。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel defaultSize="200px" minSize="160px" maxSize="320px">
<h3>Docs</h3>
<ul>
<li>Getting Started</li>
<li>Components</li>
<li>Guides</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize navigation">
<div />
</ResizableHandle>
<ResizablePanel minSize={30}>
<h3>Getting Started</h3>
<span data-slot="hint">Install the packages</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }默认布局
defaultLayout 按顺序一次设置每个面板的大小。否则,没有 defaultSize 的面板会平均分配剩下的空间。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable defaultLayout={[25, 35, 40]}>
<ResizablePanel minSize={15}>
<h3>Mail</h3>
<ul>
<li>Inbox</li>
<li>Drafts</li>
<li>Sent</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize folders" />
<ResizablePanel minSize={15}>
<h3>Inbox</h3>
<ul>
<li>Hollis Grant</li>
<li>Inès Moreau</li>
<li>Kofi Mensah</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize inbox" />
<ResizablePanel minSize={15}>
<h3>Quarterly report</h3>
<span data-slot="hint">From Hollis Grant</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }受控
传入 layout 和 onLayoutChange 来控制这一组。布局是按顺序排列的每个面板一个百分比,加起来是 100。每次变更都带有原因,比如 drag、step-key 或 collapse,而 details.cancel() 会保留当前的布局。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
import { createSignal } from "solid-js"
import { useLog } from "./log"
const format = (layout: number[]) => layout.map(Math.round).join(" / ")
function Example() {
const log = useLog()
const [layout, setLayout] = createSignal([30, 70])
return (
<div data-slot="stack">
<div data-slot="controls">
<button type="button" onClick={() => setLayout([50, 50])}>
Split Evenly
</button>
<button type="button" onClick={() => setLayout([30, 70])}>
Reset
</button>
</div>
<Resizable
layout={layout()}
onLayoutChange={(next, details) => {
if (format(next) !== format(layout())) {
log(`${format(next)} (${details.reason})`)
}
setLayout(next)
}}
>
<ResizablePanel minSize={20}>
<h3>Files</h3>
<ul>
<li>page.tsx</li>
<li>layout.tsx</li>
<li>globals.css</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize files" />
<ResizablePanel minSize={20}>
<h3>page.tsx</h3>
<span data-slot="hint">Saved just now</span>
</ResizablePanel>
</Resizable>
</div>
)
}
export { Example }完成后保存
onLayoutChange 在拖动的每一帧运行。onLayoutCommit 只运行一次,在改变布局的交互结束时:指针抬起、按键松开,或者面板收起。在那里保存。拖动期间按 Escape 会把面板放回开始的位置,不会提交任何内容。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
import { useLog } from "./log"
function Example() {
const log = useLog()
return (
<Resizable
onLayoutCommit={(layout, details) =>
log(`Saved ${layout.map(Math.round).join(" / ")} (${details.reason})`)
}
>
<ResizablePanel defaultSize={40} minSize={20}>
<h3>Files</h3>
<ul>
<li>page.tsx</li>
<li>layout.tsx</li>
<li>globals.css</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize files" />
<ResizablePanel minSize={20}>
<h3>page.tsx</h3>
<span data-slot="hint">Saved just now</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }持久化
autoSaveId 在交互完成时把布局保存到 localStorage,并在这一组于浏览器中挂载时恢复它。服务端标记从默认尺寸开始。每一组面板都保留自己的布局,所以一个时有时无的面板也能取回它的大小。给面板一个 id,这样即使页面别处发生变化,保存的布局也能保留下来。
这和 Preferences 是同一个思路,后者在多次访问之间保存一项选择,比如主题。Resizable 则在它的 autoSaveId 下保存整个布局。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable autoSaveId="docs-persistence">
<ResizablePanel id="docs-sidebar" defaultSize={30} minSize={20}>
<h3>Outline</h3>
<ul>
<li>Introduction</li>
<li>Installation</li>
<li>Usage</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize outline">
<div />
</ResizableHandle>
<ResizablePanel id="docs-content" minSize={30}>
<h3>Introduction</h3>
<span data-slot="hint">Last edited by Wren Takahashi</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }存储
storage 接受任何带有 getItem 和 setItem 的对象,比如 sessionStorage 或 cookie。那是浏览器的存储形状,而不是 Preferences 接受的存储辅助函数,比如 createLocalStorage;那些辅助函数各自只保存一个值。像选择偏好那样,在记住选择里决定用 local storage、session storage 还是 cookie。
这个示例把布局保存在 sessionStorage 里,作用于这个标签页的会话:
import {
Resizable,
ResizableHandle,
ResizablePanel,
type ResizableStorage,
} from "@ui-sdk/solid/resizable"
const session: ResizableStorage = {
getItem: (key) => sessionStorage.getItem(key),
setItem: (key, value) => sessionStorage.setItem(key, value),
}
function Example() {
return (
<Resizable autoSaveId="docs-storage" storage={session}>
<ResizablePanel id="session-chats" defaultSize={35} minSize={20}>
<h3>Messages</h3>
<ul>
<li>Inès Moreau</li>
<li>Kofi Mensah</li>
<li>Wren Takahashi</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize messages" />
<ResizablePanel id="session-thread" minSize={20}>
<h3>Inès Moreau</h3>
<span data-slot="hint">Can we move the review to Friday?</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }服务器无法读取 localStorage,所以它会渲染每个面板的 defaultSize,保存的布局在页面水合后才到达。要在首次绘制时就渲染保存的布局,就把它放进 cookie,并把各面板的大小作为各自的 defaultSize 传回去。这里服务器把请求的 Cookie 头传给应用,就像在服务器上读取 cookie那样,应用从中读取保存的尺寸。它导入 resizableStorageKey(它给 cookie 命名):
import { resizableStorageKey } from "@ui-sdk/solid/resizable"
import { Editor, readCookie } from "./editor"
function App(props: { cookie: string | null }) {
const saved = JSON.parse(
readCookie(props.cookie ?? "", resizableStorageKey("editor")) ?? "{}"
)
const [sidebar = 25, main = 75] = saved["sidebar,main"] ?? []
return <Editor sidebar={sidebar} main={main} />
}
export { App }import {
Resizable,
ResizableHandle,
ResizablePanel,
type ResizableStorage,
} from "@ui-sdk/solid/resizable"
const readCookie = (cookie: string, key: string) => {
const value = cookie.match(new RegExp(`(?:^|; )${key}=([^;]*)`))?.[1]
return value === undefined ? null : decodeURIComponent(value)
}
const cookieStorage: ResizableStorage = {
getItem: (key) => readCookie(document.cookie, key),
setItem: (key, value) => {
document.cookie = `${key}=${encodeURIComponent(value)}; path=/; max-age=31536000`
},
}
function Editor(props: { sidebar: number; main: number }) {
return (
<Resizable autoSaveId="editor" storage={cookieStorage}>
<ResizablePanel id="sidebar" defaultSize={props.sidebar} />
<ResizableHandle aria-label="Resize sidebar" />
<ResizablePanel id="main" defaultSize={props.main} />
</Resizable>
)
}
export { Editor, readCookie }控制器
来自 createResizableController() 的控制器可以在任何地方按 id 或索引收起、展开和调整面板大小,并用 getLayout() 读取布局。它做出的变更会报告 imperative 原因。
import {
createResizableController,
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
import { useLog } from "./log"
function Example() {
const log = useLog()
const editor = createResizableController()
return (
<div data-slot="stack">
<div data-slot="controls">
<button
type="button"
onClick={() => editor.collapse("controller-sidebar")}
>
Collapse
</button>
<button
type="button"
onClick={() => editor.expand("controller-sidebar")}
>
Expand
</button>
<button
type="button"
onClick={() => editor.resize("controller-sidebar", "240px")}
>
240px
</button>
<button
type="button"
onClick={() =>
log(
`Layout ${editor.getLayout().map(Math.round).join(" / ")}${editor.isCollapsed("controller-sidebar") ? ", collapsed" : ""}`
)
}
>
Read
</button>
</div>
<Resizable controller={editor}>
<ResizablePanel
id="controller-sidebar"
defaultSize={30}
minSize={20}
collapsible
>
<h3>Files</h3>
<ul>
<li>page.tsx</li>
<li>layout.tsx</li>
<li>globals.css</li>
</ul>
</ResizablePanel>
<ResizableHandle aria-label="Resize files" />
<ResizablePanel minSize={30}>
<h3>page.tsx</h3>
<span data-slot="hint">Saved just now</span>
</ResizablePanel>
</Resizable>
</div>
)
}
export { Example }步长
step 设置方向键移动句柄的距离,默认 1%;largeStep 设置 Shift 加方向键移动的距离,默认 10%。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable step={5} largeStep={25}>
<ResizablePanel defaultSize={50} minSize={20}>
<h3>Original</h3>
<span data-slot="hint">v1.2.0</span>
</ResizablePanel>
<ResizableHandle aria-label="Resize original" />
<ResizablePanel defaultSize={50} minSize={20}>
<h3>Changes</h3>
<span data-slot="hint">v1.3.0</span>
</ResizablePanel>
</Resizable>
)
}
export { Example }禁用
在句柄上设置 disabled 会把它固定在原处。设置在组上会固定每个句柄。禁用的句柄会离开 Tab 顺序,它们旁边的面板保持原有大小。用禁用的 <fieldset> 包住这一组,也会固定每个句柄。
import { Checkbox, CheckboxIndicator } from "@ui-sdk/solid/checkbox"
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
import { createSignal } from "solid-js"
function Example() {
const [locked, setLocked] = createSignal(false)
return (
<div data-slot="stack">
<div data-slot="controls">
<label data-slot="label">
<Checkbox
checked={locked()}
onCheckedChange={(next) => setLocked(next === true)}
>
<CheckboxIndicator>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M20 6 9 17l-5-5" />
</svg>
</CheckboxIndicator>
</Checkbox>
Lock layout
</label>
</div>
<Resizable disabled={locked()}>
<ResizablePanel minSize={15}>
<h3>Pinned</h3>
<span data-slot="hint">2 notes</span>
</ResizablePanel>
<ResizableHandle disabled aria-label="Resize pinned" />
<ResizablePanel minSize={15}>
<h3>Inbox</h3>
<span data-slot="hint">3 unread</span>
</ResizablePanel>
<ResizableHandle aria-label="Resize inbox" />
<ResizablePanel minSize={15}>
<h3>Message</h3>
<span data-slot="hint">From Kofi Mensah</span>
</ResizablePanel>
</Resizable>
</div>
)
}
export { Example }自定义渲染
render、class 和 children 可以是部件状态的函数,比如面板的 size 和它是否 collapsed,或者句柄是否 dragging 或 hovering。渲染约定见部件与组合。
import {
Resizable,
ResizableHandle,
ResizablePanel,
} from "@ui-sdk/solid/resizable"
function Example() {
return (
<Resizable>
<ResizablePanel defaultSize={35} minSize={20} collapsible>
{(state) => (
<>
<h3>Files</h3>
<span data-slot="hint">
{state.collapsed ? "Collapsed" : `${Math.round(state.size)}%`}
</span>
</>
)}
</ResizablePanel>
<ResizableHandle
aria-label="Resize files"
render={(props) => <div {...props} />}
/>
<ResizablePanel minSize={30}>
{(state) => (
<>
<h3>Editor</h3>
<span data-slot="hint">{`${Math.round(state.size)}%`}</span>
</>
)}
</ResizablePanel>
</Resizable>
)
}
export { Example }原语会把拖动中的尺寸直接写入面板样式。在渲染函数里读取 size 或 layout,也会在拖动的每一帧更新该部件。
样式
除了所需的布局,部件没有样式:这一组是 flex 行或列,面板以 flex basis 取得自己的大小,句柄不收缩,并采用 touch-action: none。给这一组一个尺寸,给句柄宽度或高度以及背景,并用 data-hovering、data-dragging 和 :focus-visible 设置样式。句柄被悬停或拖动时,它的光标会显示在整个页面上,并在到达限制时指向它唯一能走的方向。
每个部件都有一个 data-slot,比如 resizable-handle,它的状态属性列在数据属性下。
ResizablePanel
| 变量 | 内容 |
|---|---|
--resizable-panel-size | The panel's share of the group, as a percentage. Written on every frame of a drag, without a render. |
数据属性
Resizable
[data-slot="resizable"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalThe group's direction, on every part. A handle's own aria-orientation is the other one, since it runs across the group. |
data-disabled | Present when the group, or this handle, is disabled. |
data-dragging | Present while a handle of the group is being dragged. |
ResizablePanel
[data-slot="resizable-panel"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalThe group's direction, on every part. A handle's own aria-orientation is the other one, since it runs across the group. |
data-collapsed | Present while a collapsible panel is collapsed. |
ResizableHandle
[data-slot="resizable-handle"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalThe group's direction, on every part. A handle's own aria-orientation is the other one, since it runs across the group. |
data-disabled | Present when the group, or this handle, is disabled. |
data-dragging | Present while this handle is being dragged. |
data-hovering | Present while the pointer is over the handle or within its hit area, which reaches past the handle itself. |
API 参考
属性和回调与下面相同,都作为 Solid props。
Resizable
layout?number[]- Each panel's size in percent, in panel order, like
[25, 75]. Pass it to control the group. defaultLayout?number[]- The layout at first, when uncontrolled. Panels' own
defaultSizeis what a server renders, so prefer that with server rendering. onLayoutChange?((layout: number[], details: ResizableLayoutChangeDetails) => void)- Runs while the layout changes: on every frame of a drag and every key.
details.cancel()keeps the layout. onLayoutCommit?((layout: number[], details: ResizableLayoutCommitDetails) => void)- Runs once an interaction that changed the layout ends: the pointer lifts, the key comes up, or a panel collapses. For saving.
orientation?"horizontal" | "vertical"- The direction panels are laid out in. Horizontal panels sit side by side, vertical ones stack.
disabled?boolean- Stops every handle: no drags, no keys, and handles leave the tab order.
step?number- How far an arrow key moves a handle, in percent.
largeStep?number- How far Shift with an arrow key moves a handle, in percent.
autoSaveId?string- Saves the layout when an interaction completes and restores it on the next visit, under this key. Give panels an
idso it survives changes elsewhere on the page. storage?ResizableStorage- Where
autoSaveIdsaves: anything withgetItemandsetItem, like a cookie store a server can read. controller?object- Resizes the group from anywhere. Create one with
createResizableController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
ResizablePanel
id?string- Identifies the panel for the controller,
aria-controlsand saved layouts. It's the element's id. Generated when left out. defaultSize?ResizableSize- Where the panel starts, in percent or with a unit, like
30or"240px". Without one, panels share what's left evenly. A server renders this size. minSize?ResizableSize- The smallest the panel gets while open.
maxSize?ResizableSize- The largest the panel gets.
collapsible?boolean- Lets the panel collapse below its minimum to
collapsedSize: by dragging past half the way there, with Enter or a double-click on its handle, or through the controller. collapsedSize?ResizableSize- The size a collapsible panel collapses to. At 0, its content leaves the tab order and the accessibility tree.
ResizableHandle
disabled?boolean- Stops this handle: no drags and no keys, and it leaves the tab order.
状态
渲染函数、基于状态的类名和槽位会收到什么。
orientation"horizontal" | "vertical"- The direction panels are laid out in.
disabledboolean- Whether the group is disabled.
draggingboolean- Whether a handle is being dragged.
layoutnumber[]- Each panel's size in percent. Reading it renders on every frame of a drag.
面板:
orientation"horizontal" | "vertical"- The direction panels are laid out in.
sizenumber- The panel's size in percent. Reading it renders on every frame of a drag.
collapsedboolean- Whether the panel is down at its
collapsedSize, below its minimum. collapsibleboolean- Whether the panel can collapse.
句柄:
orientation"horizontal" | "vertical"- The direction panels are laid out in. The separator itself runs across it.
disabledboolean- Whether the handle can't move.
draggingboolean- Whether the handle is being dragged.
hoveringboolean- Whether the pointer is over the handle or within its hit area.
原因
details.reason 说明布局为什么改变。
dragPointerEvent- A slider thumb or a resize handle was dragged, or an item was dragged to a new place in a list.
step-keyKeyboardEvent- Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
collapseMouseEvent | KeyboardEvent- Enter or a double-click on a resize handle collapsed the panel beside it, or restored it.
resizenone- The group changed size, so panels with sizes in pixels moved to stay within their bounds.
imperativenone- A controller or
apicall.
控制器
用 createResizableController() 创建一个,并把它作为 controller 传入。
写方法接受可选的 { reason, event },像拖动一样运行变更回调,并在没有任何改变或变更被取消时返回 false。
getLayout()() => number[]- Each panel's size in percent, in order.
setLayout(layout, options?)(layout: number[], options?: ResizableChangeOptions | undefined) => boolean- Sets every panel's size at once, in percent. It's fitted to the panels' bounds and adds up to 100. Returns whether anything changed.
collapse(panel, options?)(panel: string | number, options?: ResizableChangeOptions | undefined) => boolean- Collapses a collapsible panel, by its id or its index. Returns whether it collapsed.
expand(panel, options?)(panel: string | number, options?: ResizableChangeOptions | undefined) => boolean- Expands a collapsed panel to the size it had before it collapsed, or its minimum. Returns whether it expanded.
resize(panel, size, options?)(panel: string | number, size: ResizableSize, options?: ResizableChangeOptions | undefined) => boolean- Resizes one panel, by its id or its index, like
resize("sidebar", "240px"). Its neighbours give or take the difference. isCollapsed(panel)(panel: string | number) => boolean- Whether a panel is collapsed.
事件
回调先收到新布局,再收到详情。
| 属性 | 所属 | 收到 |
|---|---|---|
onLayoutChange | Resizable | (layout, details): 新布局,以及原因 |
onLayoutCommit | Resizable | (layout, details): 交互结束时的布局 |
无障碍
句柄遵循 WAI-ARIA 窗口分隔条模式。每个句柄都是一个可聚焦的 separator,它的 aria-valuenow 是它前面那个面板的大小,aria-valuemin 和 aria-valuemax 是各面板允许的范围,aria-controls 指向那个面板,aria-orientation 则横跨这一组的排列方向。用 aria-label 给每个句柄命名。
键盘
聚焦一个句柄,沿这一组的轴线按方向键,让它移动 step;按住 Shift 则移动 largeStep。Home 和 End 会把它移到各面板允许的最远处,Enter 收起或恢复它旁边的可收起面板。到达最小值的面板会把剩余的移动量传给下一个面板。禁用的句柄会离开 Tab 顺序。
经过测试的行为
下面每一行都是一个自动化测试。
- Starts at each panel's default size, and panels without one share the rest.
- ArrowRight grows the panel before the handle by one step, and ArrowLeft shrinks it.
- Shift with an arrow key moves by the large step.
- Arrows across the group's axis do nothing.
- ArrowDown and ArrowUp move a vertical group's handle.
- Home collapses a collapsible panel, and End grows it to its maximum.
- Home stops at the minimum of a panel that can't collapse.
- Enter collapses the panel before the handle, and restores its size.
- Enter collapses the panel after the handle when only that one can collapse.
- One step past a collapsible panel's minimum collapses it.
- A panel at its minimum passes the rest on to the next one.
- End stops where the panels' bounds meet.
- Keys pressed without waiting all land.
- Keys complete the change once they come up.
- Step and large step set how far keys move.
- A canceled change keeps the layout.
- A controlled group follows its layout.
- A controlled group that ignores a change completes nothing. 仅在 React 和 Vue 运行。
- A disabled handle stops a move from the next one, so the panels past it keep their sizes.
- DefaultLayout sets where the panels start.
- A disabled group's handles leave the tab order and ignore drags.
- Dragging a handle moves it with the pointer.
- A drag reports its moves with the drag reason and completes once.
- Dragging past a disabled handle moves nothing beyond it.
- Dragging a vertical group's handle moves it up and down.
- Dragging past half a collapsible panel's minimum collapses it, and back opens it.
- A drag stops at the panels' bounds.
- Escape during a drag puts the panels back where they started.
- A double-click on a handle collapses its panel, and another restores it.
- A press just beside the thin handle drags it.
- A press farther from the handle doesn't drag.
- Where two handles meet, one drag moves both.
- A drag shows the axis cursor over the whole page until it ends.
- Collapses and expands a panel, with the imperative reason.
- Resizes a panel to a size in pixels.
- A panel with a minimum in pixels keeps it as the group shrinks.
- Panels keep their share as the group grows.
- A layer above a handle keeps its presses.
- A press beside a handle doesn't click what's under it.
- Only the main button drags.
- An iframe under the pointer doesn't stop a drag.
- At a limit, the cursor points the only way a handle can go.
- A saved layout is there on the first paint.
- A touch drag that starts beside the handle, in its touch hit area, resizes without scrolling.
运行于 React、Vue 和 web components。