Skip to content

Commit

Permalink
fix: initialize dimensions before first paint
Browse files Browse the repository at this point in the history
The container dimensions are in the `ResizeObserver` callback. It fires
only after the first paint. This means that the dimensions are not
initialized during the first paint, which causes a layout shift.

The dimensions are known before the first paint. We can retrieve the
container width and height by using `getBoundingClientRect` inside
`useLayoutEffect`. This way we can initialize the dimensions correctly
so the panes are already sized the right way during the first render.
  • Loading branch information
Gelio committed Jan 11, 2023
1 parent 5af7d85 commit 3df3fcb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
},
});

useIsomorphicLayoutEffect(() => {
if (!dimensionsInitialized) {
const { height, width } = containerRef.current.getBoundingClientRect();
splitViewRef.current?.layout(vertical ? height : width);
layoutService.current.setSize(vertical ? height : width);
setDimensionsInitialized(true);
}
}, [dimensionsInitialized, vertical]);

useEffect(() => {
if (isIOS) {
setSashSize(20);
Expand Down

0 comments on commit 3df3fcb

Please sign in to comment.