react-resizable-panels
v4.24.1
Published
<img src="https://react-resizable-panels.vercel.app/og.png" alt="react-resizable-panels logo" width="400" height="210" />
Readme
react-resizable-panels: React components for resizable layouts: panel groups, grids, and workspaces of tabsets that users can rearrange by dragging tabs.
Support
If you like this project there are several ways to support it:
Installation
Begin by installing the library from NPM:
npm install react-resizable-panelsTypeScript types
TypeScript definitions are included within the published dist folder
FAQs
Frequently asked questions can be found here.
Documentation
Documentation for this project is available at react-resizable-panels.vercel.app.
Components
Cell
A Cell occupies one or more tracks (columns and rows) within a Grid. Cells are resized along with the tracks they occupy.
Cell elements always include the following attributes:
<div data-cell data-testid="cell-id-prop" id="cell-id-prop">ℹ️ Test id can be used to narrow selection when unit testing.
⚠️ Cell elements must be direct DOM children of their parent Grid elements.
Required props
Optional props
Grid
A Grid arranges resizable Cells in two dimensions. Columns can be resized horizontally and rows can be resized vertically; dragging the point where a column boundary and a row boundary intersect resizes both.
Size constraints (e.g. min/max size, collapsible) are specified per track (column or row), using the same format as Panel props.
Cells can span multiple columns and/or rows. Track boundaries cannot be resized alongside of a cell that spans across them.
Grid elements always include the following attributes:
<div data-grid data-testid="grid-id-prop" id="grid-id-prop">ℹ️ Test id can be used to narrow selection when unit testing.
Required props
Optional props
Gridline
Resizes the columns (or rows) of a Grid.
For example:
// Rendered between columns 0 and 1 and spans all rows
<Gridline type="column" column={1} />// Rendered between rows 0 and 1 and spans all columns
<Gridline type="row" row={1} />// Rendered between columns 0 and 1 alongside of row 2 only
<Gridline type="column" column={1} row={2} rowSpan={1} />A gridline should not be rendered alongside of a Cell that spans across its boundary (this includes disabled gridlines; in development builds, an error will be logged to the console); render multiple gridlines along the same boundary instead (e.g. before and after the spanning Cell).
ℹ️ Once a boundary contains a gridline, it can only be resized using gridlines; the parts of the boundary that aren't alongside of a gridline can't be dragged.
Where column and row gridlines intersect, dragging resizes both axes.
Gridlines are not required but they are recommended as they improve keyboard accessibility.
Gridline elements always include the following attributes:
<div aria-orientation="vertical" data-separator role="separator">ℹ️ Column gridlines are vertical (aria-orientation="vertical") and row gridlines are horizontal (aria-orientation="horizontal").
ℹ️ In addition to the attributes shown above, gridlines also render all required WAI-ARIA properties.
⚠️ Gridline elements must be direct DOM children of their parent Grid elements.
ℹ️ Gridlines rendered by another component (rather than directly within the Grid) aren't detected until they mount, so server-rendered layouts may shift slightly during hydration.
Required props
Optional props
Group
A Group wraps a set of resizable Panel components. Group content can be resized horizontally or vertically.
Group elements always include the following attributes:
<div data-group data-testid="group-id-prop" id="group-id-prop">ℹ️ Test id can be used to narrow selection when unit testing.
Required props
None
Optional props
Panel
A Panel wraps resizable content and can be configured with min/max size constraints and collapsible behavior.
Panel size props can be in the following formats:
- Percentage of the parent Group (0..100)
- Pixels
- Relative font units (em, rem)
- Viewport relative units (vh, vw)
ℹ️ Numeric values are assumed to be pixels. Strings without explicit units are assumed to be percentages (0%..100%). Percentages may also be specified as strings ending with "%" (e.g. "33%") Pixels may also be specified as strings ending with the unit "px". Other units should be specified as strings ending with their CSS property units (e.g. 1rem, 50vh)
Panel elements always include the following attributes:
<div data-panel data-testid="panel-id-prop" id="panel-id-prop">ℹ️ Test id can be used to narrow selection when unit testing.
⚠️ Panel elements must be direct DOM children of their parent Group elements.
Required props
None
Optional props
Separator
Separators are not required but they are recommended as they improve keyboard accessibility.
⚠️ Separator elements must be direct DOM children of their parent Group elements.
Separator elements always include the following attributes:
<div data-separator data-testid="separator-id-prop" id="separator-id-prop" role="separator">ℹ️ Test id can be used to narrow selection when unit testing.
ℹ️ In addition to the attributes shown above, separator also renders all required WAI-ARIA properties.
Required props
None
Optional props
TabCloseButton
Closes the tab it is rendered within; render it within the element returned by the Workspace's renderTab prop:
renderTab={(tab, props) => (
<div {...props}>
{tab.title}
<TabCloseButton>×</TabCloseButton>
</div>
)}The button renders nothing if closing is disabled (by the tab's disableClose flag or the Workspace's disabled prop).
Its content defaults to "×" (e.g. it can be replaced with an icon),
and its default aria-label is "Close" followed by the tab's title (or id).
Its children, className, style, and title can also be functions of its state (see TabCloseButtonState):
<TabCloseButton title={({ tabTitle }) => `Close ${tabTitle}`} />To keep the tab open (e.g. to confirm unsaved changes first), call event.preventDefault() in an onClick handler:
<TabCloseButton
onClick={(event) => {
if (hasUnsavedChanges && !confirm("Discard unsaved changes?")) {
event.preventDefault();
}
}}
/>ℹ️ This button is not focusable by default (tabIndex={-1}), since tabs shouldn't contain focusable content;
keyboard users can close the focused tab with Delete or Backspace.
Required props
None
Optional props
TabsetCollapseButton
Collapses (or expands) the tabset it is rendered within; render it within the content returned by the Workspace's renderTabsetToolbar prop.
Its children, className, style, and title can be functions of its state (see TabsetCollapseButtonState), e.g. to show a different icon while the tabset is collapsed:
renderTabsetToolbar={() => (
<TabsetCollapseButton title={({ isCollapsed }) => (isCollapsed ? "Expand" : "Collapse")}>
{({ isCollapsed }) => (isCollapsed ? "▸" : "▾")}
</TabsetCollapseButton>
)}The button renders nothing if the tabset can't be collapsed (see ParentTabsetState.isCollapsible), or while it's maximized.
While the tabset is collapsed only because it doesn't fit (see ParentTabsetState.isCollapsedToFit), it can't be expanded, so the button has aria-disabled="true".
Its aria-expanded attribute reflects whether the tabset is expanded, and its aria-controls attribute refers to the selected tab's panel;
its default aria-label ("Collapse tabset") doesn't change, since aria-expanded describes the state.
To keep the tabset as it is, call event.preventDefault() in an onClick handler.
ℹ️ Clicking a tab of a collapsed tabset also expands it (unless the workspace is disabled, which doesn't restrict this button).
Required props
None
Optional props
TabsetDragHandle
Drags the tabset it is rendered within; render it within the content returned by the Workspace's renderTabsetToolbar prop:
renderTabsetToolbar={() => (
<TabsetDragHandle>⠿</TabsetDragHandle>
)}By default, a tabset is dragged by the empty space in its header;
once its header contains a drag handle, only the handle drags it.
Any element with a data-tabset-drag-handle attribute is a drag handle;
this component also sets touch-action: none, so that it can be dragged by touch.
The handle has a data-disabled attribute when the tabset can't be dragged (see useParentTabsetState), e.g. to hide it.
Its children, className, style, and title can also be functions of its state (see TabsetDragHandleState):
<TabsetDragHandle title={({ isDisabled }) => (isDisabled ? undefined : "Drag tabset")}>
⠿
</TabsetDragHandle>Required props
None
Optional props
TabsetMaximizeButton
Maximizes (or restores) the tabset it is rendered within; render it within the content returned by the Workspace's renderTabsetToolbar prop.
Its children, className, style, and title can be functions of its state (see TabsetMaximizeButtonState), e.g. to show a different icon while the tabset is maximized:
renderTabsetToolbar={() => (
<TabsetMaximizeButton title={({ isMaximized }) => (isMaximized ? "Restore" : "Maximize")}>
{({ isMaximized }) => (isMaximized ? "Restore" : "Maximize")}
</TabsetMaximizeButton>
)}The button renders nothing for the only tabset in a workspace (see ParentTabsetState.isRoot) and for floating tabsets, which can't be maximized.
Its aria-pressed attribute reflects whether the tabset is maximized;
its default aria-label ("Maximize tabset") doesn't change, since aria-pressed describes the state.
To keep the tabset as it is, call event.preventDefault() in an onClick handler.
Required props
None
Optional props
Workspace
A Workspace arranges tabsets that users can rearrange by dragging tabs: between tabsets, within a tabset's list of tabs, or onto the edge of a tabset to split it. Tabsets can be resized and maximized.
The layout is described by a serializable model (see WorkspaceModel).
Workspaces are uncontrolled: provide an initial model with the defaultModel prop, save changes from the onModelChanged callback,
and make changes using the imperative API (see WorkspaceImperativeHandle).
ℹ️ Tab content is not remounted when a tab is moved, and its browser state (e.g. iframes, playing media, and focus) is preserved where possible;
browsers that don't support moveBefore (e.g. Safari) reset some of that state (e.g. iframes are reloaded).
Workspace elements always include the following attributes:
<div data-workspace data-testid="workspace-id-prop" id="workspace-id-prop">ℹ️ Test id can be used to narrow selection when unit testing.
Required props
Optional props
Imperative APIs
GridImperativeHandle
Imperative Grid API.
ℹ️ The useGridRef and useGridCallbackRef hooks are exported for convenience in TypeScript projects.
Methods
GridTrackImperativeHandle
Imperative API for an individual Grid track (column or row); returned by GridImperativeHandle.getTrackById and GridImperativeHandle.getTrackByIndex.
ℹ️ Sizes are relative to the space available to the track's axis (e.g. the width of the Grid, minus gaps and gridlines, for a column).
