diff --git a/src/allotment.tsx b/src/allotment.tsx index 5b82b266..afc5d9ef 100644 --- a/src/allotment.tsx +++ b/src/allotment.tsx @@ -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 = { @@ -140,7 +141,7 @@ const Allotment = forwardRef( onReset, onVisibleChange, onDragStart, - onDragEnd + onDragEnd, }, ref ) => { @@ -150,6 +151,10 @@ const Allotment = forwardRef( const splitViewRef = useRef(null); const splitViewViewRef = useRef(new Map()); const layoutService = useRef(new LayoutService()); + const containerSizeRef = useRef<{ width: number; height: number }>({ + height: 0, + width: 0, + }); const views = useRef([]); const [dimensionsInitialized, setDimensionsInitialized] = useState(false); @@ -192,6 +197,7 @@ const Allotment = forwardRef( resize: (sizes) => { splitViewRef.current?.resizeViews(sizes); }, + getContainerSize: () => ({ ...containerSizeRef.current }), })); useIsomorphicLayoutEffect(() => { @@ -219,34 +225,34 @@ const Allotment = forwardRef( 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( @@ -254,8 +260,7 @@ const Allotment = forwardRef( options, onChange, onDragStart, - onDragEnd, - + onDragEnd ); splitViewRef.current.on("sashDragStart", () => { @@ -472,6 +477,8 @@ const Allotment = forwardRef( 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);