Dmitri Chumak
Dmitri Chumak

Reputation: 83

React Material-UI GridList cascading layout

Is it possible to use GridList component of the React Material-UI library in a pinterest like cascading layout?

I use Material-UI Card components as children for the GridList:

    const cards = props.items.map((item) => <Card key={item.id} {...item} />);
    return (
      <GridList cols={3} cellHeight={'auto'}>
        {React.Children.toArray(cards)}
      </GridList>
    );

The result is following: The result image

I want to remove gaps in red circles. Any help will be much appreciated.

Upvotes: 4

Views: 3786

Answers (2)

Lukas Liesis
Lukas Liesis

Reputation: 26423

This layout has a name: masonry, which is coming from the building constructions, because of how bricks are put together :) There is mui component for it:

https://mui.com/components/masonry/#basic-masonry

Copying sample here too:

import * as React from 'react';
import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import Paper from '@mui/material/Paper';
import Masonry from '@mui/lab/Masonry';

const heights = [150, 30, 90, 70, 110, 150, 130, 80, 50, 90, 100, 150, 30, 50, 80];

const Item = styled(Paper)(({ theme }) => ({
  ...theme.typography.body2,
  color: theme.palette.text.secondary,
  border: '1px solid black',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center',
}));

export default function BasicMasonry() {
  return (
    <Box sx={{ width: 500, minHeight: 393 }}>
      <Masonry columns={4} spacing={1}>
        {heights.map((height, index) => (
          <Item key={index} sx={{ height }}>
            {index + 1}
          </Item>
        ))}
      </Masonry>
    </Box>
  );
}

enter image description here

Upvotes: 0

sumeetv
sumeetv

Reputation: 26

The CSS equivalent of you want is to have flex-direction set to be column instead of row, but it doesn't look like the material-ui documentation gives you the option to change the order in which things render.

I would try using a different container object or build something yourself since the Pinterest style layout requires some more complicated logic when determining where the next element to be rendered should go.

Upvotes: 1

Related Questions