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 TabList API. The Tabs are used to make navigation easier and more useful. We can easily switch between the views. The TabList contains the list of tabs. The API provides a lot of functionality and we will learn to implement them.
Import TabList API:
import TabList from '@mui/lab/TabList'; // or import { TabList } from '@mui/lab';
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 a component similar to the table row.
- classes (Object): Override the existing styles or add new styles to the component.
- component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
- sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.
Syntax: Create a TabList as follows:
<TabListonChange={handleChange}> <Tab label="Tutorial 1" value="1" /> </TabList>
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
Step 4: Run the project as follows:
npm start
Example 1: In the following example, we have a TabList showing different tabs.
App.js
import "./App.css" ; import * as React from "react" ; import Box from "@mui/material/Box" ; import Tab from "@mui/material/Tab" ; import TabContext from "@mui/lab/TabContext" ; import TabList from "@mui/lab/TabList" ; import TabPanel from "@mui/lab/TabPanel" ; function App() { const [value, setValue] = React.useState( "1" ); const handleChange = (event, newValue) => { setValue(newValue); }; return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> </div> <div style={{ width: "fit-content" , margin: "auto" , }} > <strong>React MUI TabList API</strong> </div> <br /> <div style={{ margin: "auto" , display: "flex" , justifyContent: "space-evenly" , }} > <Box sx={{ width: "100%" , typography: "body1" }}> <TabContext value={value}> <Box sx={{ borderBottom: 1, borderColor: "divider" }}> <TabList onChange={handleChange} > <Tab label= "Tutorial 1" value= "1" /> <Tab label= "Tutorial 2" value= "2" /> <Tab label= "Tutorial 3" value= "3" /> </TabList> </Box> <TabPanel value= "1" >Data Structures</TabPanel> <TabPanel value= "2" >Algorithms</TabPanel> <TabPanel value= "3" >Web Development</TabPanel> </TabContext> </Box> </div> </div> ); } export default App; |
Output:
Example 2: In the following example, have changed the background color of tabs.
App.js
import "./App.css" ; import * as React from "react" ; import Box from "@mui/material/Box" ; import Tab from "@mui/material/Tab" ; import TabContext from "@mui/lab/TabContext" ; import TabList from "@mui/lab/TabList" ; import TabPanel from "@mui/lab/TabPanel" ; function App() { const [value, setValue] = React.useState( "1" ); const handleChange = (event, newValue) => { setValue(newValue); }; return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> </div> <div style={{ width: "fit-content" , margin: "auto" , }} > <strong>React MUI TabList API</strong> </div> <br /> <div style={{ margin: "auto" , display: "flex" , justifyContent: "space-evenly" , }} > <Box sx={{ width: "100%" , typography: "body1" }}> <TabContext value={value}> <Box sx={{ borderBottom: 1, borderColor: "divider" }}> <TabList sx={{ backgroundColor: "lightGreen" , }} onChange={handleChange} > <Tab label= "Tutorial 1" value= "1" /> <Tab label= "Tutorial 2" value= "2" /> <Tab label= "Tutorial 3" value= "3" /> </TabList> </Box> <TabPanel value= "1" >Data Structures</TabPanel> <TabPanel value= "2" >Algorithms</TabPanel> <TabPanel value= "3" >Web Development</TabPanel> </TabContext> </Box> </div> </div> ); } export default App; |
Output:
Reference: https://mui.com/material-ui/api/tab-panel/