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 ImageListItem API. The ImageList displays a collection of images in a structured grid format. ImageListItem contains a single image. The API provides a lot of functionality and we will learn to implement them.
Import ImageListItem API:
import ImageListItem from '@mui/material/ImageListItem'; // or import { ImageListItem } 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 is an image element.
- classes (Object): Override the existing styles or add new styles to the component.
- cols(integer): It is used to set the width of the item in the number of grid columns. The default value is 1.
- component(elementType): This is used for the root node.
- rows(integer): It is used to set the height of the item in the number of grid rows. The default value is 1.
- sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.
CSS Rules:
- root(.MuiImageListItem-root): It is the style applied to the root element.
- img(.MuiImageListItem-img): It is the style applied to an img element to ensure it covers the item.
- standard(.MuiImageListItem-standard): It is the style applied to the root element if a variant is set to standard.
- woven(.MuiImageListItem-woven): It is the style applied to the root element if a variant is set to woven.
- masonry(.MuiImageListItem-masonry): It is the style applied to the root element if a variant is set to masonry.
- quilted(.MuiImageListItem-quilted): It is the style applied to the root element if a variant is set to quilt.
Syntax: Create an ImageListItem as follows:
<ImageListItem cols={2} rows={1}> <img src="#" loading="lazy"/> </ImageListItem>
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 npm install @emotion/styled @mui/lab @mui/icons-material
Project Structure: The project should look like the below:
Step 4: Run the project as follows:
npm start
Example 1: In the following example, we have an ImageListItem inside 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 ImageListItem API</strong> </div> <br /> <ImageList variant= "quilted" sx={{ width: 500, height: 250, margin: "auto" }} cols={4} 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 ImageListItem with different cols and rows size.
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 ImageListItem API</strong> </div> <br /> <ImageList variant= "quilted" sx={{ width: 500, height: 450, margin: "auto" }} cols={4} 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={2} rows={1}> <img {...srcset( 121, 2, 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-item/