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

[Masonry] Observe every masonry child to trigger computation when needed #29896

Merged
merged 3 commits into from Jan 14, 2022

Conversation

hbjORbj
Copy link
Member

@hbjORbj hbjORbj commented Nov 25, 2021

Closes #29692
Closes #30453

Before:

  • ResizeObserver API was only observing the masonry container and the first child (which we expected will cause issues in the future). As a result, changes in the dimension of children other than the first do not trigger computation (needed for correct layout of masonry). Code sandbox
  • Why does observing the container not detect changes in dimensions of children? Because masonry container is set to a fixed height after every computation. As a result, except the first mounting, when masonry container doesn't have a fixed height yet, changes in dimensions of children do not get detected.

After:

  • ResizeObserver API no longer observes the container but now it observes every children. Code sandbox

Preview: https://deploy-preview-29896--material-ui.netlify.app/components/masonry/#main-content

@hbjORbj hbjORbj added bug 🐛 Something doesn't work package: lab Specific to @mui/lab component: masonry This is the name of the generic UI component, not the React module! labels Nov 25, 2021
@mui-pr-bot
Copy link

mui-pr-bot commented Nov 25, 2021

Details of bundle changes

@material-ui/lab: parsed: -0.09% 😍, gzip: -0.12% 😍

Generated by 🚫 dangerJS against 1f39589

@hbjORbj hbjORbj changed the title [WIP] [Masonry] Observe every masonry child [Masonry] Observe every masonry child to trigger computation when needed Nov 25, 2021
@siriwatknp
Copy link
Member

Even though it fixes the issue but I am not sure if this is the experience most people want because the masonry item shifted when there are spaces available.

@danilo-leal Do you think this is weird from a designer's perspective?

Screen.Recording.2564-11-26.at.14.14.17.mov

@siriwatknp
Copy link
Member

Also, another demo should be added to the docs. 😊

@hbjORbj
Copy link
Member Author

hbjORbj commented Nov 26, 2021

Even though it fixes the issue but I am not sure if this is the experience most people want because the masonry item shifted when there are spaces available.

@danilo-leal Do you think this is weird from a designer's perspective?

Screen.Recording.2564-11-26.at.14.14.17.mov

That is the property of masonry though. The next item is pushed to the shortest column.

Also, another demo should be added to the docs. 😊

Ah, good call :)

@mnajdova
Copy link
Member

Even though it fixes the issue but I am not sure if this is the experience most people want because the masonry item shifted when there are spaces available.

@danilo-leal Do you think this is weird from a designer's perspective?

That is the property of masonry though. The next item is pushed to the shortest column.

I agree it looks a bit odd :\ @hbjORbj could you link how other Masonry implementations behave in this scenario? Just to do some benchmarking

@hbjORbj
Copy link
Member Author

hbjORbj commented Nov 26, 2021

@mnajdova @siriwatknp

In fact, I think I may be able to come up with a solution so that those masonry items can maintain the original order. I can figure out if computation is running due to a change in masonry item's "height" and then do not set new order for each item in such case. I will update you.

could you link how other Masonry implementations behave in this scenario? Just to do some benchmarking

Sure I will look.

@danilo-leal
Copy link
Contributor

@danilo-leal Do you think this is weird from a designer's perspective?

The behavior is weird to me too but after quickly looking at other Masonries out there, it seems that this is indeed how they behave if the column max height size is filled?! (As Benny mentioned). What made it confusing though was that the cards moved to different columns without any transition/animation whatsoever, so I felt lost.

I know it's a different interaction (drag and drop), but I think we could draw some inspiration from https://muuri.dev/. Check their Grid Demo — how each tile moves smoothly whenever you change some of them from their place. It's just an idea. I haven't interacted with a Masonry with Accordions out on the wild yet to be able to contribute with confidence, so I'm just thinking out loud...

@hbjORbj
Copy link
Member Author

hbjORbj commented Nov 30, 2021

The behavior is weird to me too but after quickly looking at other Masonries out there, it seems that this is indeed how they behave if the column max height size is filled?! (As Benny mentioned). What made it confusing though was that the cards moved to different columns without any transition/animation whatsoever, so I felt lost.

I know it's a different interaction (drag and drop), but I think we could draw some inspiration from https://muuri.dev/. Check their Grid Demo — how each tile moves smoothly whenever you change some of them from their place. It's just an idea. I haven't interacted with a Masonry with Accordions out on the wild yet to be able to contribute with confidence, so I'm just thinking out loud...

@danilo-leal I agree with you. I will definitely check it out!!

resizeObserver.observe(container.firstChild);
}
if (masonryRef.current) {
masonryRef.current.childNodes.forEach((childNode) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we still observe the masonry container?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got it from the PR description, nevermind :)

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, I've tested the new sandbox, looks much better. Before we merge, let's add new demo illustrating this (something similar to the codesandbox could be beneficial).

Also, please add some unit tests, to make sure we don't break this again in the future.

@hbjORbj hbjORbj requested a review from mnajdova January 14, 2022 12:59
@hbjORbj
Copy link
Member Author

hbjORbj commented Jan 14, 2022

I added a demo and a test :)

Since a number of issues need this PR to be merged, as @mnajdova suggested, I will go ahead and merge this once approved, and then look into the animation effect @danilo-leal suggested in a separate PR.

cc @siriwatknp

@mui-bot
Copy link

mui-bot commented Jan 14, 2022

Details of bundle changes

@material-ui/lab: parsed: -0.09% 😍, gzip: -0.12% 😍

Generated by 🚫 dangerJS against 120dc4b

@hbjORbj hbjORbj merged commit 8e4c482 into mui:master Jan 14, 2022
wladimirguerra pushed a commit to wladimirguerra/material-ui that referenced this pull request Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: masonry This is the name of the generic UI component, not the React module! package: lab Specific to @mui/lab
Projects
None yet
6 participants