React split-pane component.
- React-based: Integrate effortlessly into your existing React-based application.
- Industry standard look and feel: Like VS Code's split view implementation? You're in luck! This component is derived from the same codebase.
- Dynamic: Want to declaratively add and remove panes? We've got you covered.
You can find examples of using the library here.
Allotment is available from npm.
Allotment has react
and react-dom
as peer dependencies.
yarn add react react-dom
yarn add allotment
import React from "react";
import { Allotment } from "allotment";
import "allotment/dist/style.css";
export const App = () => (
<Allotment>
<ComponentA>
<ComponentB>
</Allotment>
);
If you want more control over the behaviour of the individual panes you can use the Allotment.Pane
component. This includes setting the minimum and maximum size of a pane, as well as whether to enable snapping behaviour.
<Allotment>
<Allotment.Pane minSize={200}>
<ComponentA>
</Allotment.Pane>
<Allotment.Pane snap>
<ComponentB>
</Allotment.Pane>
</Allotment>
All properties are optional.
An array of initial sizes of the panes. If the sum of the sizes differs from the size of the container then the panes' sizes will be scaled proportionally.
<Allotment defaultSizes={[100, 200]}>
<div />
<div />
</Allotment>
Maximum size of any pane.
Minimum size of any pane.
Resize each view proportionally when resizing container.
Enable snap to zero for all panes.
Direction to split. If true then the panes will be stacked vertically, otherwise they will be stacked horizontally.
Callback that is fired when the pane sizes change (usually on drag). Recommended to add a debounce function to rate limit the callback. Passed an array of numbers.
Callback that is fired whenever the user double clicks a sash.
Maximum size of this pane. Overrides maxSize
set on parent component.
Minimum size of this pane. Overrides minSize
set on parent component.
Enable snap to zero for this pane. Overrides snap
set on parent component.
Whether the pane should be visible.
Allotment uses CSS variables for styling. You can customize the following default variables.
:root {
--focus-border: #007fd4;
--separator-border: #838383;
}
To control the feedback area size of the dragging area between panes you can call the exported setSashSize
function with desired size in pixels. Set it to a larger value if you feel it's hard to resize the panes using the mouse. On touch devices the feedback area is always set to 20 pixels
You can use a ref to get access to the Allotment component instance and call its reset and resize methods manually:
const ref = React.useRef(ref);
return (
<div>
<button
onClick={() => {
ref.current.reset();
}}
>
Reset
</button>
<button
onClick={() => {
ref.current.resize([100, 200]);
}}
>
Resize
</button>
<Allotment ref={ref}>
<div />
<div />
</Allotment>
</div>
);
The Allotment component takes its width and height from the element which contains it. It does not come with an explicit width or height out of the box. It's easy to end up with a div of height zero by accident. For example, adding allotment to a brand new Create React App project without setting a height on a containing div won't work because the default root div itself has no height.
You should also check that the css has been imported/included, for example at the root of your application:
import "allotment/dist/style.css";
The simplest approach is place your content inside a new div with width and height 100%
and overflow auto
. This div will have the same dimensions as the pane it's inside and if its content overflows the browser will provide scrolling behaviour.
If you get an error when importing allotment in a Next.js project consider not including the module server-side. Allotment currently only works in a browser. It might be possible to produce sensible results server-side in the future so create an issue requesting this if interested.
See these issues for more discussion: Example usage with Next.js and SyntaxError: Cannot use import statement outside a module.