Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getContainerSize #621

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Pane.displayName = "Allotment.Pane";
export type AllotmentHandle = {
reset: () => void;
resize: (sizes: number[]) => void;
getContainerSize: () => { width: number; height: number };
};

export type AllotmentProps = {
Expand Down Expand Up @@ -140,7 +141,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
onReset,
onVisibleChange,
onDragStart,
onDragEnd
onDragEnd,
},
ref
) => {
Expand All @@ -150,6 +151,10 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
const splitViewRef = useRef<SplitView | null>(null);
const splitViewViewRef = useRef(new Map<React.Key, HTMLElement>());
const layoutService = useRef<LayoutService>(new LayoutService());
const containerSizeRef = useRef<{ width: number; height: number }>({
height: 0,
width: 0,
});
const views = useRef<PaneView[]>([]);

const [dimensionsInitialized, setDimensionsInitialized] = useState(false);
Expand Down Expand Up @@ -192,6 +197,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
resize: (sizes) => {
splitViewRef.current?.resizeViews(sizes);
},
getContainerSize: () => ({ ...containerSizeRef.current }),
}));

useIsomorphicLayoutEffect(() => {
Expand Down Expand Up @@ -219,43 +225,42 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
proportionalLayout,
...(initializeSizes &&
defaultSizes && {
descriptor: {
size: defaultSizes.reduce((a, b) => a + b, 0),
views: defaultSizes.map((size, index) => {
const props = splitViewPropsRef.current.get(
previousKeys.current[index]
);

const view = new PaneView(layoutService.current, {
element: document.createElement("div"),
minimumSize: props?.minSize ?? minSize,
maximumSize: props?.maxSize ?? maxSize,
priority: props?.priority ?? LayoutPriority.Normal,
...(props?.preferredSize && {
preferredSize: props?.preferredSize,
}),
snap: props?.snap ?? snap,
});

views.current.push(view);
descriptor: {
size: defaultSizes.reduce((a, b) => a + b, 0),
views: defaultSizes.map((size, index) => {
const props = splitViewPropsRef.current.get(
previousKeys.current[index]
);

return {
container: [...splitViewViewRef.current.values()][index],
size: size,
view: view,
};
}),
},
}),
const view = new PaneView(layoutService.current, {
element: document.createElement("div"),
minimumSize: props?.minSize ?? minSize,
maximumSize: props?.maxSize ?? maxSize,
priority: props?.priority ?? LayoutPriority.Normal,
...(props?.preferredSize && {
preferredSize: props?.preferredSize,
}),
snap: props?.snap ?? snap,
});

views.current.push(view);

return {
container: [...splitViewViewRef.current.values()][index],
size: size,
view: view,
};
}),
},
}),
};

splitViewRef.current = new SplitView(
containerRef.current,
options,
onChange,
onDragStart,
onDragEnd,

onDragEnd
);

splitViewRef.current.on("sashDragStart", () => {
Expand Down Expand Up @@ -472,6 +477,8 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
ref: containerRef,
onResize: ({ width, height }) => {
if (width && height) {
containerSizeRef.current.height = height;
containerSizeRef.current.width = width;
splitViewRef.current?.layout(vertical ? height : width);
layoutService.current.setSize(vertical ? height : width);
setDimensionsInitialized(true);
Expand Down