2.0.19 • Published 4 days ago

react-resizable-panels v2.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

react-resizable-panels

React components for resizable panel groups/layouts

import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels";

<PanelGroup autoSaveId="example" direction="horizontal">
  <Panel defaultSize={25}>
    <SourcesExplorer />
  </Panel>
  <PanelResizeHandle />
  <Panel>
    <SourceViewer />
  </Panel>
  <PanelResizeHandle />
  <Panel defaultSize={25}>
    <Console />
  </Panel>
</PanelGroup>;

If you like this project, 🎉 become a sponsor or ☕ buy me a coffee

Props

PanelGroup

proptypedescription
autoSaveId?stringUnique id used to auto-save group arrangement via localStorage
childrenReactNodeArbitrary React element(s)
className?stringClass name to attach to root element
direction"horizontal" \| "vertical"Group orientation
id?stringGroup id; falls back to useId when not provided
onLayout?(sizes: number[]) => voidCalled when group layout changes
storage?PanelGroupStorageCustom storage API; defaults to localStorage 1
style?CSSPropertiesCSS style to attach to root element
tagName?string = "div"HTML element tag name for root element

1: Storage API must define the following synchronous methods:

  • getItem: (name:string) => string
  • setItem: (name: string, value: string) => void

PanelGroup components also expose an imperative API for manual resizing: | method | description | | :---------------------------- | :--------------------------------------------------------------- | | getId(): string | Gets the panel group's ID. | | getLayout(): number[] | Gets the panel group's current layout ([1 - 100, ...]). | | setLayout(layout: number[]) | Resize panel group to the specified layout ([1 - 100, ...]). |

Panel

proptypedescription
childrenReactNodeArbitrary React element(s)
className?stringClass name to attach to root element
collapsedSize?number=0Panel should collapse to this size
collapsible?boolean=falsePanel should collapse when resized beyond its minSize
defaultSize?numberInitial size of panel (numeric value between 1-100)
id?stringPanel id (unique within group); falls back to useId when not provided
maxSize?number = 100Maximum allowable size of panel (numeric value between 1-100); defaults to 100
minSize?number = 10Minimum allowable size of panel (numeric value between 1-100); defaults to 10
onCollapse?() => voidCalled when panel is collapsed
onExpand?() => voidCalled when panel is expanded
onResize?(size: number) => voidCalled when panel is resized; size parameter is a numeric value between 1-100. 1
order?numberOrder of panel within group; required for groups with conditionally rendered panels
style?CSSPropertiesCSS style to attach to root element
tagName?string = "div"HTML element tag name for root element

1: If any Panel has an onResize callback, the order prop should be provided for all Panels.

Panel components also expose an imperative API for manual resizing: | method | description | | :----------------------- | :--------------------------------------------------------------------------------- | | collapse() | If panel is collapsible, collapse it fully. | | expand() | If panel is currently collapsed, expand it to its most recent size. | | getId(): string | Gets the ID of the panel. | | getSize(): number | Gets the current size of the panel as a percentage (1 - 100). | | isCollapsed(): boolean | Returns true if the panel is currently collapsed (size === 0). | | isExpanded(): boolean | Returns true if the panel is currently not collapsed (!isCollapsed()). | | getSize(): number | Returns the most recently committed size of the panel as a percentage (1 - 100). | | resize(size: number) | Resize panel to the specified percentage (1 - 100). |

PanelResizeHandle

proptypedescription
children?ReactNodeCustom drag UI; can be any arbitrary React element(s)
className?stringClass name to attach to root element
hitAreaMargins?{ coarse: number = 15; fine: number = 5; }Allow this much margin when determining resizable handle hit detection
disabled?booleanDisable drag handle
id?stringResize handle id (unique within group); falls back to useId when not provided
onDragging?(isDragging: boolean) => voidCalled when group layout changes
style?CSSPropertiesCSS style to attach to root element
tagName?string = "div"HTML element tag name for root element

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 a 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" />

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>
  );
}

A demo of this is available here.

@everything-registry/sub-chunk-2607@million23/nextjsfusillotest-reactt-uijotai-devtoolslogo-shadcn-reactmajor-mongo@aznadmin/ui@chat2db/ui@camped-ui/resizable@briks/ui@asharbabar/design@astroapps/schemas-editor@commonalityco/feature-constraints@commonalityco/feature-graph@commonalityco/ui-constraints@cloudmix-dev/react@coursebuilder/ui@cqcl/quantinuum-ui@cookbookdev/docsbot@cwrc/leafwriter@codefast/ui@douglasneuroinformatics/libui@dcorp/web-ui@deskree/ion-editor@dinui/react@evostacktec/components@edge-ui/react@graphdl/ui@g4mbit/ui@fwoosh/app@flatjs/nextra@itsrakesh/ui@italics/italics@jidebiao/componentsxbeshuimrdxtr-sherlocknexai-ai-support-chat-bubblenexai.jsmtdb1mrdxtrmightyappsbuildermuse-uimx-nextmtxuimtxuilibmtxuilib-oldmtxuilibv2polar-auth-complexiaranaerapid-next-saasreact-embed-devtoolssemaphorselenium-idereact-semaphorsdlb_configuration_visualizerreact-salmonscwshadcn-reactshadcn-ui-lab-dsshadcn-v4-labdsshadcdnshadcdncdnsvg-to-component-previewsweepaisolomon-ai-frontend-platformvertc-design-system@igloo-be/ra-blocks-inputxbesh-uixplorer-uivisio-cms@adhese/ui@1stmmd/js-navigator@agni-ui/react@antribute/ui@jansora/ui@lshay/ui@kanaries/graphic-walker@libsqlstudio/gui@intavia/ui@lifespikes/ui@lightdotso/ui@ma-liang/components@marimo-team/frontend@marimo-team/frontend-wasm@marimo-team/islands@ikonintegration/ui@pytsx/flow-board@pytsx/ui@rsc-parser/core@rise4fun/docusaurus-theme-side-editor@redocly/replay@nihilncunia/editor@partnerplatform/design@polkadex/ux@ocleva/react@pgkit/admin@playground-cms/cms@mui/toolpad@mukunth2211/uilibrary@mvdlei/ui
2.0.19

4 days ago

2.0.18

15 days ago

2.0.17

18 days ago

2.0.15

1 month ago

2.0.16

1 month ago

2.0.14

1 month ago

2.0.13

2 months ago

2.0.12

2 months ago

2.0.11

2 months ago

2.0.10

2 months ago

2.0.9

3 months ago

2.0.8

3 months ago

2.0.8-rc.1

3 months ago

2.0.7

3 months ago

2.0.5

3 months ago

2.0.6

3 months ago

2.0.4

3 months ago

2.0.3

3 months ago

2.0.2

3 months ago

2.0.1

3 months ago

2.0.0

3 months ago

1.0.10

3 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago

1.0.0-rc.3

5 months ago

1.0.0-rc.4

5 months ago

1.0.0-rc.2

5 months ago

1.0.0-rc.1

5 months ago

0.0.62

5 months ago

0.0.63

5 months ago

0.0.60

6 months ago

0.0.61

5 months ago

0.0.59

6 months ago

0.0.56

6 months ago

0.0.57

6 months ago

0.0.58

6 months ago

0.0.46

11 months ago

0.0.47

11 months ago

0.0.51

11 months ago

0.0.52

10 months ago

0.0.53

10 months ago

0.0.54

10 months ago

0.0.55

9 months ago

0.0.50

11 months ago

0.0.48

11 months ago

0.0.49

11 months ago

0.0.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.43

12 months ago

0.0.44

12 months ago

0.0.45

12 months ago

0.0.37

1 year ago

0.0.38

1 year ago

0.0.39

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.36

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago