Skip to content

Commit

Permalink
feat: add resize method to component instance (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwalley authored Feb 6, 2022
1 parent 904a8e6 commit d25bd91
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ To control the feedback area size of the dragging area between panes you can cal

### Programmatic control

You can use a ref to get access to the Allotment component instance and call its reset method manually:
You can use a ref to get access to the Allotment component instance and call its reset and resize methods manually:

```jsx
const ref = React.useRef(ref);
Expand All @@ -156,6 +156,13 @@ return (
>
Reset
</button>
<button
onClick={() => {
ref.current.resize([100, 200]);
}}
>
Resize
</button>
<Allotment ref={ref}>
<div />
<div />
Expand Down
8 changes: 7 additions & 1 deletion src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export const Pane = forwardRef<HTMLDivElement, PaneProps>(

Pane.displayName = "Allotment.Pane";

export type AllotmentHandle = { reset: () => void };
export type AllotmentHandle = {
reset: () => void;
resize: (sizes: number[]) => void;
};

export type AllotmentProps = {
children: React.ReactNode;
Expand Down Expand Up @@ -101,6 +104,9 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
reset: () => {
splitViewRef.current?.distributeViewSizes();
},
resize: (sizes) => {
splitViewRef.current?.resizeViews(sizes);
},
}));

useLayoutEffect(() => {
Expand Down
21 changes: 21 additions & 0 deletions src/split-view/split-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,27 @@ export class SplitView extends EventEmitter implements Disposable {
this.relayout();
}

public resizeViews(sizes: number[]): void {
for (let index = 0; index < sizes.length; index++) {
const item = this.viewItems[index];
let size = sizes[index];

size = Math.round(size);

size = clamp(
size,
item.minimumSize,
Math.min(item.maximumSize, this.size)
);

item.size = size;
}

this.contentSize = this.viewItems.reduce((r, i) => r + i.size, 0);
this.saveProportions();
this.layout(this.size);
}

/**
* Returns the size of a {@link View view}.
*/
Expand Down
40 changes: 38 additions & 2 deletions stories/allotment.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@ export const Reset: Story<AllotmentProps> = (args) => {
};
Reset.args = {};

export const Resize: Story<AllotmentProps> = (args) => {
const defaultSizes = [60, 40];
const [sizes, setSizes] = useState(defaultSizes);
const ref = useRef<AllotmentHandle>(null!);

return (
<div>
<button
className={styles.button}
type="button"
onClick={() => {
const w = Math.floor(100 * Math.random());

const sizes = [w, 100 - w];
ref.current.resize(sizes);
setSizes(sizes);
}}
>
Resize
</button>
<pre>
<code>{JSON.stringify(sizes)}</code>
</pre>
<div className={styles.container}>
<Allotment ref={ref} defaultSizes={defaultSizes} {...args}>
<Content />
<Content />
</Allotment>
</div>
</div>
);
};
Resize.args = {
minSize: 0,
maxSize: Number.POSITIVE_INFINITY,
};

export const DefaultSize: Story<AllotmentProps> = (args) => {
return (
<div className={styles.container}>
Expand Down Expand Up @@ -239,8 +276,7 @@ export const OnReset: Story = (args) => {
const ref = useRef<AllotmentHandle>(null!);

const handleReset = () => {
ref.current.reset();
alert("reset");
ref.current.resize([100, 200]);
};

return (
Expand Down

0 comments on commit d25bd91

Please sign in to comment.