Skip to content

Commit

Permalink
[Masonry] Observe the first child of masonry rather than window
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Oct 20, 2021
1 parent 55affcb commit 8463cd2
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/mui-lab/src/Masonry/Masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,12 @@ const Masonry = React.forwardRef(function Masonry(inProps, ref) {
const resizeObserver = new ResizeObserver(handleResize);

const container = masonryRef.current;
// only the masonry container and its first child are observed for resizing;
// this might cause unforeseen problems in some use cases;
resizeObserver.observe(container);
// Observing window in addition to masonry container is more stable because
// masonry container's dimensions may not change even if user resizes the window;
// this causes a problem for responsive `columns`/`spacing`;
// e,g, CSS configures `width`/`margin` of each item to expect 5 columns
// whereas computation of `order` for each item and `height` for masonry is still for 4 columns
window.addEventListener('resize', handleResize);
resizeObserver.observe(container?.firstChild);
return () => {
resizeObserver.disconnect();
window.removeEventListener('resize', handleResize);
};
}, [columns, spacing]);

Expand Down

0 comments on commit 8463cd2

Please sign in to comment.