Skip to content

Commit

Permalink
feat: dynamic minSize and maxSize (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwalley authored Apr 10, 2022
1 parent 3be2144 commit d764ebe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function isPane(item: React.ReactNode): item is typeof Pane {

function isPaneProps(props: AllotmentProps | PaneProps): props is PaneProps {
return (
(props as PaneProps).minSize !== undefined ||
(props as PaneProps).maxSize !== undefined ||
(props as PaneProps).preferredSize !== undefined ||
(props as PaneProps).visible !== undefined
);
Expand Down Expand Up @@ -327,9 +329,34 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
const index = keys.findIndex((key) => key === updateKey);

if (props && isPaneProps(props)) {
if (props.preferredSize !== undefined) {
if (
props.preferredSize !== undefined &&
views.current[index].preferredSize !== props.preferredSize
) {
views.current[index].preferredSize = props.preferredSize;
}

let sizeChanged = false;

if (
props.minSize !== undefined &&
views.current[index].minimumSize !== props.minSize
) {
views.current[index].minimumSize = props.minSize;
sizeChanged = true;
}

if (
props.maxSize !== undefined &&
views.current[index].maximumSize !== props.maxSize
) {
views.current[index].maximumSize = props.maxSize;
sizeChanged = true;
}

if (sizeChanged) {
splitViewRef.current?.layout();
}
}
}

Expand Down
35 changes: 34 additions & 1 deletion src/split-view/split-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ export interface View {
setVisible?(visible: boolean): void;
}

export interface PaneViewOptions {
element: HTMLElement;
minimumSize?: number;
maximumSize?: number;
snap?: boolean;
}

export class PaneView implements View {
public minimumSize: number = 0;
public maximumSize: number = Number.POSITIVE_INFINITY;

readonly element: HTMLElement;
readonly snap: boolean;

constructor(options: PaneViewOptions) {
this.element = options.element;

this.minimumSize =
typeof options.minimumSize === "number" ? options.minimumSize : 30;

this.maximumSize =
typeof options.maximumSize === "number"
? options.maximumSize
: Number.POSITIVE_INFINITY;

this.snap = typeof options.snap === "boolean" ? options.snap : false;
}

layout(size: number): void {
//console.log(size);
}
}

type ViewItemSize = number | { cachedVisibleSize: number };

abstract class ViewItem {
Expand Down Expand Up @@ -537,7 +570,7 @@ export class SplitView extends EventEmitter implements Disposable {
*
* @param size The entire size of the {@link SplitView}.
*/
public layout(size: number): void {
public layout(size: number = this.size): void {
const previousSize = Math.max(this.size, this.contentSize);
this.size = size;

Expand Down

0 comments on commit d764ebe

Please sign in to comment.