All components also come with an imperative API.
Each component has a handle
prop that you can use to access the component’s API.
NOTE:
ref
is used to get the element for the component, so the imperative API is not available on theref
prop.
The PanelGroup
component has methods for getting the sizes of the panels and setting the sizes of the panels.
import { useRef } from "react";
import {
Panel,
PanelGroup,
PanelGroupHandle,
PanelResizer,
} from "react-window-splitter";
import { Button } from "@/Components/Button";
export function ImperativeGroupExample() {
const panelGroupHandle = useRef<PanelGroupHandle>(null);
return (
<div className="flex flex-col gap-4">
<PanelGroup
orientation="horizontal"
style={{ height: 200 }}
handle={panelGroupHandle}
>
<Panel color="green" min="50px" max="300px" />
<PanelResizer />
<Panel color="red" min="50px" />
</PanelGroup>
<Button
onPress={() => {
panelGroupHandle.current?.setSizes(["100px", "20px", "80%"]);
}}
>
Override sizes
</Button>
</div>
);
}
The Panel
component has methods getting and setting it’s size as well as collapsing and expanding the panel.
import { useRef } from "react";
import {
Panel,
PanelGroup,
PanelHandle,
PanelResizer,
} from "react-window-splitter";
import { Button } from "@/Components/Button";
export function ImperativeGroupExample() {
const panelHandle = useRef<PanelHandle>(null);
return (
<div className="flex flex-col gap-4">
<PanelGroup orientation="horizontal" style={{ height: 200 }}>
<Panel
handle={panelHandle}
color="green"
min="200px"
max="300px"
collapsible
collapsedSize="100px"
/>
<PanelResizer />
<Panel color="red" min="50px" />
</PanelGroup>
<Button
onPress={() => {
panelHandle.current?.collapse();
}}
>
Collapse
</Button>
</div>
);
}