Skip to content

Commit

Permalink
fix: initialize dimensions before first paint (#526)
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 Feb 2, 2023
1 parent a59c774 commit 2706777
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/allotment.tsx
Expand Up @@ -458,6 +458,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 2706777

Please sign in to comment.