Skip to content

Commit

Permalink
feat: add onVisibleChange callback (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwalley authored Apr 4, 2022
1 parent b4f3f42 commit 39f9bec
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Callback that is fired when the pane sizes change (usually on drag). Recommended

Callback that is fired whenever the user double clicks a sash.

### onVisibleChange

Callback that is fired whenever the user changes the visibility of a pane by snapping. Note that this will only be called if the new value is different from the current `visible` prop on the Pane.

## Allotment.Pane props

### maxSize
Expand Down
26 changes: 23 additions & 3 deletions src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export type AllotmentProps = {
onChange?: (sizes: number[]) => void;
/** Callback on reset */
onReset?: () => void;
/** Callback on visibility change */
onVisibleChange?: (index: number, visible: boolean) => void;
} & CommonProps;

const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
Expand All @@ -91,14 +93,13 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
vertical = false,
onChange,
onReset,
onVisibleChange,
},
ref
) => {
const containerRef = useRef<HTMLDivElement>(null!);
const previousKeys = useRef<string[]>([]);
const splitViewPropsRef = useRef(
new Map<React.Key, AllotmentProps | PaneProps>()
);
const splitViewPropsRef = useRef(new Map<React.Key, PaneProps>());
const splitViewRef = useRef<SplitView | null>(null);
const splitViewViewRef = useRef(new Map<React.Key, HTMLElement>());

Expand Down Expand Up @@ -176,6 +177,25 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
onChange
);

splitViewRef.current.on("sashchange", (index: number) => {
if (onVisibleChange && splitViewRef.current) {
const keys = childrenArray.map((child) => child.key as string);

for (let index = 0; index < keys.length; index++) {
const props = splitViewPropsRef.current.get(keys[index]);

if (props?.visible !== undefined) {
if (props.visible !== splitViewRef.current.isViewVisible(index)) {
onVisibleChange(
index,
splitViewRef.current.isViewVisible(index)
);
}
}
}
}
});

splitViewRef.current.on("sashreset", (_index: number) => {
if (onReset) {
onReset();
Expand Down
6 changes: 4 additions & 2 deletions src/split-view/split-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ export class SplitView extends EventEmitter implements Disposable {
this.onSashChange(sashEventMapper(event))
);

sash.on("end", this.onSashEnd);
sash.on("end", () =>
this.onSashEnd(this.sashItems.findIndex((item) => item.sash === sash))
);

sash.on("reset", () => {
const index = this.sashItems.findIndex((item) => item.sash === sash);
Expand Down Expand Up @@ -782,7 +784,7 @@ export class SplitView extends EventEmitter implements Disposable {
}

private onSashEnd = (index: number): void => {
// TODO: this._onDidSashChange.fire(index);
this.emit("sashchange", index);
this.saveProportions();

for (const item of this.viewItems) {
Expand Down
8 changes: 7 additions & 1 deletion stories/allotment.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ export const Visible: Story<AllotmentProps> = (args) => {
{visible ? "Hide" : "Show"}
</button>
<div className={styles.container}>
<Allotment {...args}>
<Allotment
{...args}
snap
onVisibleChange={(_index, value) => {
setVisible(value);
}}
>
<Content />
<Allotment.Pane visible={visible}>
<Content />
Expand Down
19 changes: 10 additions & 9 deletions website/docs/allotment.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ title: Allotment

## Props

| Name | Type | Default | Description |
| -------------------- | ---------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultSizes` | `number[]` | | 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. |
| `maxSize` | `number` | | Maximum size of any pane. |
| `minSize` | `number` | | Minimum size of any pane. |
| `proportionalLayout` | `boolean` | `true` | Resize each view proportionally when resizing container |
| `snap` | `boolean` | `false` | Enable snap to zero for all panes. |
| `vertical` | `boolean` | `false` | Direction to split. If true then the panes will be stacked vertically, otherwise they will be stacked horizontally. |
| `onReset` | `func` | | Callback that is fired whenever the user double clicks a sash |
| Name | Type | Default | Description |
| -------------------- | ---------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultSizes` | `number[]` | | 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. |
| `maxSize` | `number` | | Maximum size of any pane. |
| `minSize` | `number` | | Minimum size of any pane. |
| `proportionalLayout` | `boolean` | `true` | Resize each view proportionally when resizing container |
| `snap` | `boolean` | `false` | Enable snap to zero for all panes. |
| `vertical` | `boolean` | `false` | Direction to split. If true then the panes will be stacked vertically, otherwise they will be stacked horizontally. |
| `onReset` | `func` | | Callback that is fired whenever the user double clicks a sash |
| `onVisibleChange` | `func` | | Callback that is fired whenever the user changes the visibility of a pane by snapping. Note that this will only be called if the new value is different from the current `visible` prop on the Pane. |

0 comments on commit 39f9bec

Please sign in to comment.