Open In App

React MUI Toolbar API

Improve
Improve
Like Article
Like
Save
Share
Report

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 are going to discuss the React MUI Toolbar API. The Toolbar displays basic actions and information related to the current screen. The Toolbar is also known as Appbar in some applications. The API provides a lot of functionality and we are going to learn to implement them.

Import Toolbar API

import Toolbar from '@mui/material/Toolbar';
// or
import { Toolbar } 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: It is a component similar to the table row.
  • classes: This overrides the existing styles or adds new styles to the component.
  • sx: The system prop allows defining system overrides as well as additional CSS styles. 
  • component: It specifies the component to be used as the root node.
  • disableGutters(true/false): If set to true, the gutter padding is removed.
  • variant(dense/regular/string): It specifies the variant of the toolbar that items should be regularly packed or densely packed.

Syntax: Create the Toolbar component as follows:

<Toolbar>
  <IconButton>
    <MenuIcon />
  </IconButton>
  <Typography variant="h6" 
      component="div" sx={{ flexGrow: 1 }}>
    GeeksforGeeks
  </Typography>
  <Button color="inherit">Login</Button>
</Toolbar>

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 basic toolbar.

App.js




import "./App.css";
import * as React from "react";
import IconButton from "@mui/material/IconButton";
import MenuIcon from "@mui/icons-material/Menu";
import { Button, Typography, Toolbar, Box, AppBar } from "@mui/material";
function App() {
  return (
    <div className="App">
      <Box sx={{ flexGrow: 1 }}>
        <AppBar position="static" color="success">
          <Toolbar>
            <IconButton
              size="large"
              edge="start"
              color="inherit"
              aria-label="menu"
              sx={{ mr: 2 }}
            >
              <MenuIcon />
            </IconButton>
            <Typography variant="h6" 
                component="div" sx={{ flexGrow: 1 }}>
              GeeksforGeeks
            </Typography>
            <Button color="inherit">Login</Button>
          </Toolbar>
        </AppBar>
      </Box>
      <div
        className="head"
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <h1
          style={{
            color: "green",
          }}
        >
          GeeksforGeeks
        </h1>
        <strong>React MUI Toolbar API</strong>
      </div>
    </div>
  );
}
  
export default App;


Output:

 

Example 2: In the following example, we have disabled gutter paddings.

App.js




import "./App.css";
import * as React from "react";
import IconButton from "@mui/material/IconButton";
import MenuIcon from "@mui/icons-material/Menu";
import { Button, Typography, Toolbar, Box, AppBar } from "@mui/material";
function App() {
  return (
    <div className="App">
      <Box sx={{ flexGrow: 1 }}>
        <AppBar position="static" color="success">
          <Toolbar disableGutters="true">
            <IconButton
              size="large"
              edge="start"
              color="inherit"
              aria-label="menu"
              sx={{ mr: 2 }}
            >
              <MenuIcon />
            </IconButton>
            <Typography variant="h6" component="div" 
                sx={{ flexGrow: 1 }}>
              GeeksforGeeks
            </Typography>
            <Button color="inherit">Login</Button>
          </Toolbar>
        </AppBar>
      </Box>
      <div
        className="head"
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <h1
          style={{
            color: "green",
          }}
        >
          GeeksforGeeks
        </h1>
        <strong>React MUI Toolbar API</strong>
      </div>
    </div>
  );
}
  
export default App;


Output:

 

Reference: https://mui.com/material-ui/api/toolbar/



Last Updated : 10 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads