npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-resizable-panels

v2.0.18

Published

React components for resizable panel groups/layouts

Downloads

951,615

Readme

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

| prop | type | description | | :----------- | :--------------------------- | :--------------------------------------------------------------- | | autoSaveId | ?string | Unique id used to auto-save group arrangement via localStorage | | children | ReactNode | Arbitrary React element(s) | | className | ?string | Class name to attach to root element | | direction | "horizontal" \| "vertical" | Group orientation | | id | ?string | Group id; falls back to useId when not provided | | onLayout | ?(sizes: number[]) => void | Called when group layout changes | | storage | ?PanelGroupStorage | Custom storage API; defaults to localStorage 1 | | style | ?CSSProperties | CSS 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

| prop | type | description | | :-------------- | :------------------------ | :-------------------------------------------------------------------------------------------- | | children | ReactNode | Arbitrary React element(s) | | className | ?string | Class name to attach to root element | | collapsedSize | ?number=0 | Panel should collapse to this size | | collapsible | ?boolean=false | Panel should collapse when resized beyond its minSize | | defaultSize | ?number | Initial size of panel (numeric value between 1-100) | | id | ?string | Panel id (unique within group); falls back to useId when not provided | | maxSize | ?number = 100 | Maximum allowable size of panel (numeric value between 1-100); defaults to 100 | | minSize | ?number = 10 | Minimum allowable size of panel (numeric value between 1-100); defaults to 10 | | onCollapse | ?() => void | Called when panel is collapsed | | onExpand | ?() => void | Called when panel is expanded | | onResize | ?(size: number) => void | Called when panel is resized; size parameter is a numeric value between 1-100. 1 | | order | ?number | Order of panel within group; required for groups with conditionally rendered panels | | style | ?CSSProperties | CSS 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

| prop | type | description | | :--------------- | :-------------------------------------------- | :------------------------------------------------------------------------------ | | children | ?ReactNode | Custom drag UI; can be any arbitrary React element(s) | | className | ?string | Class name to attach to root element | | hitAreaMargins | ?{ coarse: number = 15; fine: number = 5; } | Allow this much margin when determining resizable handle hit detection | | disabled | ?boolean | Disable drag handle | | id | ?string | Resize handle id (unique within group); falls back to useId when not provided | | onDragging | ?(isDragging: boolean) => void | Called when group layout changes | | style | ?CSSProperties | CSS 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.