Open In App

React MUI Fab 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 Fab API. The FAB or Floating Action Button is used as a button that provides quick navigation to the most accessed area of the website. The API provides a lot of functionality and we will learn to implement them.



Import Fab API

import Fab from '@mui/material/Fab';
// or
import { Fab } 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.

CSS Rules:

Syntax: Create a FAB element as follows:

<Fab color="primary" aria-label="add">
    <AddIcon />
</Fab>

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 enabled Fab and another disabled Fab.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Fab from "@mui/material/Fab";
import AddIcon from "@mui/icons-material/Add";
import EditIcon from "@mui/icons-material/Edit";
import FavoriteIcon from "@mui/icons-material/Favorite";
import NavigationIcon from "@mui/icons-material/Navigation";
import { Html } from "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Fab API</strong>
            </div>
            <br />
            <Box
                sx={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                    width: "50%",
                }}
            >
                <Fab color="primary" 
                    aria-label="add">
                    <AddIcon />
                </Fab>
  
                <Fab disabled aria-label="Html">
                    <Html />
                </Fab>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Example: In the following example, we have extended Fab of different sizes.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Fab from "@mui/material/Fab";
import AddIcon from "@mui/icons-material/Add";
import EditIcon from "@mui/icons-material/Edit";
import FavoriteIcon from "@mui/icons-material/Favorite";
import NavigationIcon from "@mui/icons-material/Navigation";
import { Html } from "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Fab API</strong>
            </div>
            <br />
            <Box
                sx={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                    width: "50%",
                }}
            >
                <Fab variant="extended" size="small" 
                    color="primary" aria-label="add">
                    <NavigationIcon sx={{ mr: 1 }} />
                    Extended
                </Fab>
                <Fab
                    variant="extended"
                    size="medium"
                    color="secondary"
                    aria-label="add"
                >
                    <NavigationIcon sx={{ mr: 1 }} />
                    Extended
                </Fab>
                <Fab variant="extended" color="inherit"
                    aria-label="add">
                    <NavigationIcon sx={{ mr: 1 }} />
                    Extended
                </Fab>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/fab/#main-content


Article Tags :