Example of horizontal grid scrolling in material ui v5

Viewed 763

In the previous version of material-ui v4.11.1 they had a Grid List component that in the current version was replaced by Image list. But in Gid List there was an example for Single line Grid list.

https://material-ui.com/components/grid-list/#single-line-grid-list

enter image description here

But in current version material ui v5.0.0-alpha.37

https://next.material-ui.com/components/image-list/

The example for horizontal scrolling is not provided for the Grid or Image List component. And tried adding the modifications from the previous version.

flexWrap: "nowrap",
overflowX: "scroll",

but it doesn't work, I would appreciate if someone could share a simple example.

1 Answers

So i ran into a similar issue and was able to solve this for version 5 of material UI. In your ImageList component, you want to define, within sx, an overflow of auto. Furthermore, the height at which you want your images to appear as on screen you will define with rowHeight, so I placed rowHeight at 200 for myself. Finally, within your ImageListItem, you want to set it, within sx, with a display of flex and a flexDirection of row, doing so allows each item to be placed horizontally. To add spacing in between each image, within the img tag define a marginRight of 1em or whichever number you choose for your liking - remember this goes within your style attribute.

Here is my code example of creating a horizontal scroll of ImageList:

<ImageList sx={{ overflowX: 'auto' }} rowHeight={200}>
          <ImageListItem sx={{display: 'flex', flexDirection: 'row'}}>
            {state.posts[i].images.map(image => {
              return (
              <img
              src={image}
              srcSet={image}
              alt='title'
              loading='lazy'
              style={{paddingRight: '1em'}}
              />
              )
            })}
          </ImageListItem>
        </ImageList>

Here are two images of what it looks like:

First image

second image

Related