React components for resizable panel groups/layouts
React Resizable Panels is an essential npm package for developers looking to create flexible, adjustable layouts in their web applications. This node module provides a robust solution for implementing resizable panel groups, making it incredibly useful for creating user interfaces that need to adapt to different screen sizes or user preferences. With React Resizable Panels, developers can effortlessly enable users to adjust the size of panels within their application, enhancing the overall user experience. This functionality is particularly valuable in applications like dashboards, graphical user interfaces, or any complex layout where space management is crucial.
To incorporate React Resizable Panels into your project, you can simply use the command 'npm install react-resizable-panels'. This command integrates the package into your React application, allowing you to leverage resizable panels without the hassle of building the functionality from scratch. Once installed, the module can be easily configured to suit the specific needs of your application, providing a seamless way to make your UI more interactive and responsive. The ease of installation and configuration makes it an attractive choice for developers aiming to enhance their applications with resizable capabilities.
The benefits of using React Resizable Panels extend beyond just user satisfaction. By implementing resizable panels, developers can create more dynamic and adaptable UIs that cater to a wider range of user interactions. This adaptability can lead to better engagement, increased productivity, and a more intuitive interface. Additionally, the React Resizable Panels package is designed to work seamlessly with other React components, ensuring that developers do not have to deal with compatibility issues. This makes it an ideal choice for projects that require robust, flexible UI solutions.
Core dependencies of this npm package and its dev dependencies.
@parcel/config-default, @parcel/core, @parcel/packager-ts, @parcel/transformer-js, @parcel/transformer-react-refresh-wrap, @parcel/transformer-typescript-types, @preconstruct/cli, @babel/preset-typescript, @playwright/test, @types/jest, @types/node, @types/react, @types/react-dom, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, @typescript-eslint/type-utils, eslint, parcel, prettier, process, ts-jest, typescript
A README file for the react-resizable-panels code repository. View Code
React components for resizable panel groups/layouts.
Supported input methods include mouse, touch, and keyboard (via Window Splitter).
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.
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>
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>
);
}
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" />
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:
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} />;
}
"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>
);
}
A demo of this is available here.