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

Stuggling to get positioning to work with useMasonry #118

Open
johtso opened this issue Apr 29, 2022 · 3 comments
Open

Stuggling to get positioning to work with useMasonry #118

johtso opened this issue Apr 29, 2022 · 3 comments

Comments

@johtso
Copy link

johtso commented Apr 29, 2022

I managed to get things working quite quickly with the Masonry component, but after reading in the docs that you could avoid the need for a resize observer by using useMasonry I decided to try and switch to that approach.

Unfortunately it's not quite working right, the first few items are being positioned on top of each other. It's not obvious how to go about diagnosing why that is. I'm hoping there's something obvious that I've overlooked that you might be able to point out!

I put together a small codesandbox: https://codesandbox.io/s/modest-feynman-02o4y9

@jaredLunde
Copy link
Owner

Out of town. Maybe your math is wrong? Works in the recipe: https://codesandbox.io/embed/usemasonry-example-3pcg9?file=/src/index.js&codemirror=1

If you're dynamically changing sizes you still need to recalculate positions when that happens, eg when the window resizes.

@johtso
Copy link
Author

johtso commented Apr 30, 2022

Thanks for the reply! That's what has me confused, as far as I can tell I'm following the same approach as the recipe. My items have fixed aspect ratio, the items in the recipe also resize when the window is resized.

Here's the same code with the only difference being using the Masonry component that works perfectly: https://codesandbox.io/s/pedantic-cohen-bt7el9

@dnish
Copy link

dnish commented Sep 25, 2022

Had the same issue, I've used the same code from the sandbox, the Masonry component was working, when using useMasonry, all items were overlapping. After a while I've checked the code within the Masonry component and notices, that the following fixes the issue:

const positioner = usePositioner({width: width || windowWidth,columnCount:3});

It was important to add width || windowWidth. On the first rendering, width is 0. After adding it, the items were rendered correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants