Friday, November 29, 2024
Google search engine
HomeLanguagesReact MUI ImageList API

React MUI ImageList API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI ImageList API. The ImageList displays a collection of images in a structured grid format. The API provides a lot of functionality and we will learn to implement them.

Import ImageList API

import ImageList from '@mui/material/ImageList';
// or
import { ImageList } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.

  • children (node): It is the content of the component which are ImageListItem elements.
  • classes (Object): Override the existing styles or add new styles to the component.
  • cols(integer): It is used to set the number of columns. The default value is 2.
  • component(elementType): This is used for the root node. 
  • gap(integer): It is used to set the gap between items. The default value is 4.
  • rowHeight(auto/number): It is used to set the height. The default value is auto.
  • sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.
  • variant(masonry/quilted/standard/woven): It is used to set the variant. The default value is standard.

 

CSS Rules:

  • root(.MuiImageList-root): It is the style applied to the root element.
  • standard(.MuiImageList-standard): It is the style applied to the root element if a variant is set to standard.
  • woven(.MuiImageList-woven): It is the style applied to the root element if a variant is set to woven.
  • masonry(.MuiImageList-masonry): It is the style applied to the root element if a variant is set to masonry.
  • quilted(.MuiImageList-quilted): It is the style applied to the root element if a variant is set to quilt.

Syntax: Create an ImageList as follows:

<ImageList sx={{ width: 500, height: 250, margin: "auto" }}
    cols={4} variant="quilted">
  <ImageListItem cols={1} rows={1}>
    <img
 src="#" loading="lazy"/>
  </ImageListItem>
</ImageList>

Installing and Creating React app and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab @mui/icons-material

Project Structure: The project structure should look like the below:

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a standard variant ImageList component

App.js




import "./App.css";
import * as React from "react";
import ImageList from "@mui/material/ImageList";
import ImageListItem from "@mui/material/ImageListItem";
function srcset(image, size, rows = 1, cols = 1) {
    return {
        src: `${image}?w=${size * cols}
      &h=${size * rows}&fit=crop&auto=format`,
        srcSet: `${image}?w=${size * cols}&h=${size * rows
            }&fit=crop&auto=format&dpr=2 2x`,
    };
}
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    neveropen
                </h1>
                <strong>React MUI ImageList API</strong>
            </div>
            <br />
            <ImageList
                sx={{ width: 500, height: 250, margin: "auto" }}
                cols={4}
                variant="quilted"
                rowHeight={121}
            >
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
            </ImageList>
        </div>
    );
}
export default App;


Output

 

Example 2: In the following example, we have a woven variant of ImageList.

App.js




import "./App.css";
import * as React from "react";
import ImageList from "@mui/material/ImageList";
import ImageListItem from "@mui/material/ImageListItem";
function srcset(image, size, rows = 1, cols = 1) {
    return {
        src: `${image}?w=${size * cols}
     &h=${size * rows}&fit=crop&auto=format`,
        srcSet: `${image}?w=${size * cols}&h=${size * rows
            }&fit=crop&auto=format&dpr=2 2x`,
    };
}
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    neveropen
                </h1>
                <strong>React MUI ImageList API</strong>
            </div>
            <br />
            <ImageList
                sx={{ width: 500, height: 450, margin: "auto" }}
                variant="woven"
                cols={3}
                gap={8}
            >
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                            121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
            </ImageList>
        </div>
    );
}
export default App;


Output

 

Reference: https://mui.com/material-ui/api/image-list/

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments