bvaughn/react-resizable-panels
Fork: 143 Star: 3989 (更新于 2024-11-19 15:46:34)
license: MIT
Language: TypeScript .
最后发布版本: 2.0.23 ( 2024-08-08 22:01:11)
react-resizable-panels
React components for resizable panel groups/layouts.
Supported input methods include mouse, touch, and keyboard (via Window Splitter).
If you like this project, 🎉 become a sponsor or ☕ buy me a coffee
FAQ
Can panel sizes be specified in pixels?
No. Pixel-based constraints added significant complexity to the initialization and validation logic and so I've decided not to support them. You may be able to implement a version of this yourself following a pattern like this but it is not officially supported by this library.
How can I fix layout/sizing problems with conditionally rendered panels?
The Panel
API doesn't require id
and order
props because they aren't necessary for static layouts. When panels are conditionally rendered though, it's best to supply these values.
<PanelGroup direction="horizontal">
{renderSideBar && (
<>
<Panel id="sidebar" minSize={25} order={1}>
<Sidebar />
</Panel>
<PanelResizeHandle />
</>
)}
<Panel minSize={25} order={2}>
<Main />
</Panel>
</PanelGroup>
Can I attach a ref to the DOM elements?
No. I think exposing two refs (one for the component's imperative API and one for a DOM element) would be awkward. This library does export several utility methods for accessing the underlying DOM elements though. For example:
import {
getPanelElement,
getPanelGroupElement,
getResizeHandleElement,
Panel,
PanelGroup,
PanelResizeHandle,
} from "react-resizable-panels";
export function Example() {
const refs = useRef();
useEffect(() => {
const groupElement = getPanelGroupElement("group");
const leftPanelElement = getPanelElement("left-panel");
const rightPanelElement = getPanelElement("right-panel");
const resizeHandleElement = getResizeHandleElement("resize-handle");
// If you want to, you can store them in a ref to pass around
refs.current = {
groupElement,
leftPanelElement,
rightPanelElement,
resizeHandleElement,
};
}, []);
return (
<PanelGroup direction="horizontal" id="group">
<Panel id="left-panel">{/* ... */}</Panel>
<PanelResizeHandle id="resize-handle" />
<Panel id="right-panel">{/* ... */}</Panel>
</PanelGroup>
);
}
Why don't I see any resize UI?
This likely means that you haven't applied any CSS to style the resize handles. By default, a resize handle is just an empty DOM element. To add styling, use the className
or style
props:
// Tailwind example
<PanelResizeHandle className="w-2 bg-blue-800" />
Can panel sizes be persistent?
Yes. Panel groups with an autoSaveId
prop will automatically save and restore their layouts on mount.
How can I use persistent layouts with SSR?
By default, this library uses localStorage
to persist layouts. With server rendering, this can cause a flicker when the default layout (rendered on the server) is replaced with the persisted layout (in localStorage
). The way to avoid this flicker is to also persist the layout with a cookie like so:
Server component
import ResizablePanels from "@/app/ResizablePanels";
import { cookies } from "next/headers";
export function ServerComponent() {
const layout = cookies().get("react-resizable-panels:layout");
let defaultLayout;
if (layout) {
defaultLayout = JSON.parse(layout.value);
}
return <ClientComponent defaultLayout={defaultLayout} />;
}
Client component
"use client";
import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels";
export function ClientComponent({
defaultLayout = [33, 67],
}: {
defaultLayout: number[] | undefined;
}) {
const onLayout = (sizes: number[]) => {
document.cookie = `react-resizable-panels:layout=${JSON.stringify(sizes)}`;
};
return (
<PanelGroup direction="horizontal" onLayout={onLayout}>
<Panel defaultSize={defaultLayout[0]}>{/* ... */}</Panel>
<PanelResizeHandle className="w-2 bg-blue-800" />
<Panel defaultSize={defaultLayout[1]}>{/* ... */}</Panel>
</PanelGroup>
);
}
[!NOTE] Be sure to specify a
defaultSize
prop for everyPanel
component to avoid layout flicker.
A demo of this is available here.
How can I set the CSP "nonce"
attribute?
import { setNonce } from "react-resizable-panels";
setNonce("your-nonce-value-here");
How can I disable global cursor styles?
import { disableGlobalCursorStyles } from "react-resizable-panels";
disableGlobalCursorStyles();
最近版本更新:(数据更新于 2024-10-04 13:41:08)
2024-08-08 22:01:11 2.0.23
2024-04-15 23:12:40 2.0.17
2024-03-24 21:20:02 2.0.16
2024-03-24 20:44:46 2.0.15
2024-03-24 20:44:43 2.0.14
2024-03-14 20:14:38 2.0.13
2024-03-14 20:14:28 2.0.12
2024-02-25 05:56:58 2.0.11
2024-02-25 05:56:51 2.0.10
2024-02-15 07:32:21 2.0.9
主题(topics):
group, layout, panel, react, resizable
bvaughn/react-resizable-panels同语言 TypeScript最近更新仓库
2024-11-21 16:14:54 RSSNext/Follow
2024-11-21 14:41:29 continuedev/continue
2024-11-21 00:40:00 immich-app/immich
2024-11-20 19:46:23 CopilotKit/CopilotKit
2024-11-20 18:49:53 awslabs/multi-agent-orchestrator
2024-11-20 14:58:38 x-extends/vxe-table