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

How to make height dynamic? #9

Open
ziqiaojin opened this issue May 3, 2019 · 3 comments
Open

How to make height dynamic? #9

ziqiaojin opened this issue May 3, 2019 · 3 comments

Comments

@ziqiaojin
Copy link

Hello, thank you for creating mauerwerk.

I am having some trouble making modifications as I am still kind of new to react.
What I am trying to do is fit image contents in the grid cells, and instead of specifying the height of each cells, I want to adapt the height to whatever contents is in the cell.
If you point out where should I be looking at, I would be very grateful.

@ptcampbell
Copy link

ptcampbell commented Jul 16, 2019

If you pass the image height and width (whether from the DOM or otherwise) into your grid item you can determine its ratio.

Then you could set the height of each box to zero and set its padding-bottom to a percentage using this this method.

I haven't tried it but it should work, including responsively without re-render.

Edit: Scratch that, the parent container's height will always be 0 since it is an accumulative height of each item. You'd probably need to set an image's height by fetching its width from the DOM once it has loaded.

@eriksachse
Copy link

I did it the following:
First, measure the width of the container with use-measure
const [refWindow, boundsWindow] = useMeasure();

<div className={styles.container} ref={refWindow}>

Then, in your grid props heights use the images width, height and container width to measure the height of the element. Keep in mind to fracture it with the count of columns.

heights={(d) => {
          const aspect = ((d.height / d.width) * boundsWindow.width) / 3;
          return aspect;
}}

@fukemy
Copy link

fukemy commented Aug 6, 2024

@eriksachse, the height can not calculate for some reason, still not work to me

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

4 participants