Creating a Responsive Navbar in React/Next.js with Material-UI that hide on scroll
Tags: react, nextjs, website, webdev
Published: 2023-11-07 08:45:56

Welcome to our step-by-step guide on creating a responsive navbar using React and Next.js, enriched with the elegance of Material-UI. A well-designed navbar is an essential part of any web application, providing users with easy access to different sections of your website. In this blog post, we will walk you through the process of building a responsive navbar that works seamlessly on both desktop and mobile devices. 

## Please note that:
This navigation bar is a very useful tool for web developers. It has three important features that make it different from other navigation bars. First, it offers a smooth scroll feature that hides the navbar when you scroll down and reveals it with style when you scroll up. Second, it’s super adaptable — it works seamlessly on big screens, small screens, and everything in between. Finally, it lets you showcase your brand with a logo and add a personal touch with your picture, making your website not only functional but also uniquely you. It’s the perfect professional touch for your web project.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndewh2cjvg1qaxtf01xv.png)

## Prerequisites
Before we dive into the code, make sure you have the following tools and libraries installed:

Material-UI: Material-UI is a popular React UI framework that provides a wide range of components to create modern, responsive designs. You can install it with the following command:

```
npm install @mui/material @mui/icons-material
```

## Step 1: Importing Required Components and Libraries
We’ll begin by importing the necessary components and libraries that we’ll use to build our navbar. Here’s a list of the key components we’ll be working with:

```
import React from "react";
import Link from "next/link"; // Import the Link component from Next.js
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import MenuIcon from "@mui/icons-material/Menu";
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Tooltip from "@mui/material/Tooltip";
import { Tabs, Tab, useMediaQuery, useTheme } from "@mui/material";
import InfoIcon from "@mui/icons-material/Info";
import ManageAccountsIcon from "@mui/icons-material/ManageAccounts";
import TimelineIcon from "@mui/icons-material/Timeline";
import AddReactionIcon from "@mui/icons-material/AddReaction";
import ContactMailIcon from "@mui/icons-material/ContactMail";

import {
  List,
  ListItemButton,
  ListItemIcon,
  ListItemText,
  Menu,
} from "@mui/material";

import { Home } from "@mui/icons-material";
import Slide from "@mui/material/Slide"; // Import the Slide component from Material-UI
import useScrollTrigger from "@mui/material/useScrollTrigger"; // Import the useScrollTrigger hook
import CssBaseline from "@mui/material/CssBaseline";
import myProfilePic from "./sufi.webp";
import logo from "./logo.webp";
import Image from "next/image";
```
If you are building it on Nextjs, then don’t forget to import `"use-client"` on the top of your code

## Step 2: Creating a HideOnScroll Component
To achieve a smooth scrolling effect where the navbar disappears when scrolling down and reappears when scrolling up, we’ll create a custom HideOnScroll component. This component uses the Slide component from Material-UI.

```
function HideOnScroll(props) {
  const { children } = props;
  const trigger = useScrollTrigger();

  return (
    <Slide appear={false} direction="down" in={!trigger}>
      {children}
    </Slide>
  );
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87xsmpqrtb3u25wh859w.gif)

## Step 3: Building the Responsive Navbar

```
function ResponsiveAppBar(props) {
  const handleOpenNavMenu = (event) => {
    setAnchorElNav(event.currentTarget);
  };
  const [anchorElNav, setAnchorElNav] = React.useState(null);

  const handleCloseNavMenu = () => {
    setAnchorElNav(null);
  };

  const theme = useTheme();
  console.log(theme);
  const isMatch = useMediaQuery(theme.breakpoints.down("md"));
  console.log(isMatch);

  return (
    <React.Fragment>
      <CssBaseline />
      <HideOnScroll {...props}>
        <AppBar
          className="navbar1"
          style={{
            width: "93.5%",
            right: "3.2%",
            border: "2px solid white",
            borderRadius: "30px",
            background:
              "linear-gradient( 90deg, rgba(78, 78, 246, 0.647) 0%, rgba(247, 90, 216, 0.696) 100% );",
          }}
        >
          <Container maxWidth="xl">
            <Toolbar disableGutters>
              <Typography
                variant="h6"
                noWrap
                component="a"
                href="/"
                sx={{
                  mr: 2,
                  display: { xs: "none", md: "flex" },
                  fontFamily: "monospace",
                  fontWeight: 700,
                  letterSpacing: ".3rem",
                  color: "inherit",
                  textDecoration: "none",
                }}
                className="cursorp"
              >
                <Avatar
                  sx={{ display: { xs: "none", md: "flex" }, mr: 1 }}
                  className="cursorp Tab8 animate__animated animate__backInLeft"
                >
                  <Image
                    src={logo}
                    style={{ width: "100%", height: "auto" }}
                    alt="logo"
                    width={100}
                    height={100}
                    loading="lazy"
                  />
                </Avatar>
              </Typography>

              <Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
                <IconButton
                  size="large"
                  aria-label="account of current user"
                  aria-controls="menu-appbar"
                  aria-haspopup="true"
                  onClick={handleOpenNavMenu}
                  color="inherit"
                >
                  <MenuIcon />
                </IconButton>
              </Box>

              <Typography
                variant="h5"
                noWrap
                component="a"
                href=""
                sx={{
                  mr: 2,
                  display: { xs: "flex", md: "none" },
                  flexGrow: 1,
                  fontFamily: "monospace",
                  fontWeight: 700,
                  letterSpacing: ".3rem",
                  color: "inherit",
                  textDecoration: "none",
                }}
              >
                {" "}
                <Avatar
                  // alt="logo"
                  // src="https://res.cloudinary.com/dtvtphhsc/image/upload/fl_immutable_cache.preserve_transparency.progressive.sprite/v1693672396/logo_1_lk0neo.webp"
                  sx={{ display: { xs: "flex", md: "none" }, mr: 1 }}
                >
                  <Image
                    src={logo}
                    style={{ width: "100%", height: "auto" }}
                    alt="logo"
                    width={100}
                    height={100}
                    loading="lazy"
                  />
                </Avatar>
              </Typography>
              <Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}>
                {isMatch ? (
                  <IconButton
                    size="large"
                    aria-label="account of current user"
                    aria-controls="menu-appbar"
                    aria-haspopup="true"
                    onClick={handleOpenNavMenu}
                    color="inherit"
                  >
                    <MenuIcon />
                  </IconButton>
                ) : (
                  <>
                    <Tabs centered sx={{ margin: "auto" }}>
                      <Tab
                        value="one"
                        label={
                          <p>
                            <Home /> Home
                          </p>
                        }
                        onClick={() => {
                          window.location.href = "/";
                        }}
                        style={{
                          textDecoration: "none",
                          color: "white",
                        }}
                        className="Tab1 animate__animated animate__zoomIn"
                      />

                      <Tab
                        label={
                          <Link
                            href="/about"
                            style={{
                              textDecoration: "none",
                              color: "white",
                              opacity: "1",
                            }}
                          >
                            <p>
                              {" "}
                              <InfoIcon /> About
                            </p>
                          </Link>
                        }
                        className="Tab2 animate__animated animate__zoomIn"
                      >
                        {" "}
                      </Tab>

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/skills"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              {" "}
                              <ManageAccountsIcon /> Skills
                            </Link>
                          </p>
                        }
                        className="Tab3 animate__animated animate__zoomIn"
                      />

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/projects"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              <TimelineIcon /> Projects
                            </Link>
                          </p>
                        }
                        className="Tab3 animate__animated animate__zoomIn"
                      />

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/blogs"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              {" "}
                              <AddReactionIcon /> Blogs
                            </Link>
                          </p>
                        }
                        className="Tab5 animate__animated animate__zoomIn"
                      />
                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/contact"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              <ContactMailIcon /> Contact
                            </Link>
                          </p>
                        }
                        className="Tab6 animate__animated animate__zoomIn"
                      />
                    </Tabs>
                  </>
                )}
              </Box>

              <Box sx={{ flexGrow: 0 }}>
                <Tooltip>
                  <IconButton sx={{ p: 0 }}>
                    <Avatar className="Tab7 animate__animated animate__backInRight">
                      <Image
                        src={myProfilePic}
                        style={{ width: "100%", height: "auto" }}
                        alt="sufi"
                        width={100}
                        height={100}
                        loading="lazy"
                      />
                    </Avatar>
                  </IconButton>
                </Tooltip>

                <Menu
                  id="menu-appbar-avatar"
                  anchorEl={anchorElNav}
                  anchorOrigin={{
                    vertical: "bottom",
                    horizontal: "right",
                  }}
                  keepMounted
                  transformOrigin={{
                    vertical: "top", // Adjust to match the new position
                    horizontal: "right",
                  }}
                  open={Boolean(anchorElNav)}
                  onClose={handleCloseNavMenu}
                  sx={{
                    display: { xs: "block", md: "none" },
                  }}
                >
                  {/* <Drawer
                    open={isDrawerOpen}
                    onClose={() => setIsDrawerOpen(false)}
                  > */}
                  <List className="DrawerList">
                    <ListItemButton>
                      <ListItemIcon>
                        <Home />
                      </ListItemIcon>
                      <ListItemText
                        primary={"Home"}
                        onClick={() => {
                          window.location.href = "/";
                        }}
                      />
                    </ListItemButton>

                    <ListItemButton>
                      <ListItemIcon>
                        <InfoIcon />
                      </ListItemIcon>
                      <Link
                        href="/about"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"About"}
                        />{" "}
                      </Link>
                    </ListItemButton>

                    <ListItemButton>
                      <ListItemIcon>
                        <ManageAccountsIcon />
                      </ListItemIcon>
                      <Link
                        href="/skills"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Skills"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <TimelineIcon />
                      </ListItemIcon>
                      <Link
                        href="/projects"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Projects"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <AddReactionIcon />
                      </ListItemIcon>
                      <Link
                        href="/blogs"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Blogs"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <ContactMailIcon />
                      </ListItemIcon>
                      <Link
                        href="/contact"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Contact"}
                        />
                      </Link>
                    </ListItemButton>
                  </List>
                </Menu>
              </Box>
            </Toolbar>
          </Container>
        </AppBar>
      </HideOnScroll>
    </React.Fragment>
  );
}
export default ResponsiveAppBar;
```

## Putting It All Together: Complete Code for Your Responsive Navbar: Full Code

```
"use client";

import React from "react";
import Link from "next/link"; // Import the Link component from Next.js
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import MenuIcon from "@mui/icons-material/Menu";
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Tooltip from "@mui/material/Tooltip";
import { Tabs, Tab, useMediaQuery, useTheme } from "@mui/material";
import InfoIcon from "@mui/icons-material/Info";
import ManageAccountsIcon from "@mui/icons-material/ManageAccounts";
import TimelineIcon from "@mui/icons-material/Timeline";
import AddReactionIcon from "@mui/icons-material/AddReaction";
import ContactMailIcon from "@mui/icons-material/ContactMail";

import {
  List,
  ListItemButton,
  ListItemIcon,
  ListItemText,
  Menu,
} from "@mui/material";

import { Home } from "@mui/icons-material";
import Slide from "@mui/material/Slide"; // Import the Slide component from Material-UI
import useScrollTrigger from "@mui/material/useScrollTrigger"; // Import the useScrollTrigger hook
import CssBaseline from "@mui/material/CssBaseline";
import myProfilePic from "./sufi.webp";
import logo from "./logo.webp";
import Image from "next/image";
function HideOnScroll(props) {
  const { children } = props;
  const trigger = useScrollTrigger();

  return (
    <Slide appear={false} direction="down" in={!trigger}>
      {children}
    </Slide>
  );
}

function ResponsiveAppBar(props) {
  const handleOpenNavMenu = (event) => {
    setAnchorElNav(event.currentTarget);
  };
  const [anchorElNav, setAnchorElNav] = React.useState(null);

  const handleCloseNavMenu = () => {
    setAnchorElNav(null);
  };

  const theme = useTheme();
  console.log(theme);
  const isMatch = useMediaQuery(theme.breakpoints.down("md"));
  console.log(isMatch);

  return (
    <React.Fragment>
      <CssBaseline />
      <HideOnScroll {...props}>
        <AppBar
          className="navbar1"
          style={{
            width: "93.5%",
            right: "3.2%",
            border: "2px solid white",
            borderRadius: "30px",
            background:
              "linear-gradient( 90deg, rgba(78, 78, 246, 0.647) 0%, rgba(247, 90, 216, 0.696) 100% );",
          }}
        >
          <Container maxWidth="xl">
            <Toolbar disableGutters>
              <Typography
                variant="h6"
                noWrap
                component="a"
                href="/"
                sx={{
                  mr: 2,
                  display: { xs: "none", md: "flex" },
                  fontFamily: "monospace",
                  fontWeight: 700,
                  letterSpacing: ".3rem",
                  color: "inherit",
                  textDecoration: "none",
                }}
                className="cursorp"
              >
                <Avatar
                  sx={{ display: { xs: "none", md: "flex" }, mr: 1 }}
                  className="cursorp Tab8 animate__animated animate__backInLeft"
                >
                  <Image
                    src={logo}
                    style={{ width: "100%", height: "auto" }}
                    alt="logo"
                    width={100}
                    height={100}
                    loading="lazy"
                  />
                </Avatar>
              </Typography>

              <Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
                <IconButton
                  size="large"
                  aria-label="account of current user"
                  aria-controls="menu-appbar"
                  aria-haspopup="true"
                  onClick={handleOpenNavMenu}
                  color="inherit"
                >
                  <MenuIcon />
                </IconButton>
              </Box>

              <Typography
                variant="h5"
                noWrap
                component="a"
                href=""
                sx={{
                  mr: 2,
                  display: { xs: "flex", md: "none" },
                  flexGrow: 1,
                  fontFamily: "monospace",
                  fontWeight: 700,
                  letterSpacing: ".3rem",
                  color: "inherit",
                  textDecoration: "none",
                }}
              >
                {" "}
                <Avatar
                  // alt="logo"
                  // src="https://res.cloudinary.com/dtvtphhsc/image/upload/fl_immutable_cache.preserve_transparency.progressive.sprite/v1693672396/logo_1_lk0neo.webp"
                  sx={{ display: { xs: "flex", md: "none" }, mr: 1 }}
                >
                  <Image
                    src={logo}
                    style={{ width: "100%", height: "auto" }}
                    alt="logo"
                    width={100}
                    height={100}
                    loading="lazy"
                  />
                </Avatar>
              </Typography>
              <Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}>
                {isMatch ? (
                  <IconButton
                    size="large"
                    aria-label="account of current user"
                    aria-controls="menu-appbar"
                    aria-haspopup="true"
                    onClick={handleOpenNavMenu}
                    color="inherit"
                  >
                    <MenuIcon />
                  </IconButton>
                ) : (
                  <>
                    <Tabs centered sx={{ margin: "auto" }}>
                      <Tab
                        value="one"
                        label={
                          <p>
                            <Home /> Home
                          </p>
                        }
                        onClick={() => {
                          window.location.href = "/";
                        }}
                        style={{
                          textDecoration: "none",
                          color: "white",
                        }}
                        className="Tab1 animate__animated animate__zoomIn"
                      />

                      <Tab
                        label={
                          <Link
                            href="/about"
                            style={{
                              textDecoration: "none",
                              color: "white",
                              opacity: "1",
                            }}
                          >
                            <p>
                              {" "}
                              <InfoIcon /> About
                            </p>
                          </Link>
                        }
                        className="Tab2 animate__animated animate__zoomIn"
                      >
                        {" "}
                      </Tab>

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/skills"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              {" "}
                              <ManageAccountsIcon /> Skills
                            </Link>
                          </p>
                        }
                        className="Tab3 animate__animated animate__zoomIn"
                      />

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/projects"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              <TimelineIcon /> Projects
                            </Link>
                          </p>
                        }
                        className="Tab3 animate__animated animate__zoomIn"
                      />

                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/blogs"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              {" "}
                              <AddReactionIcon /> Blogs
                            </Link>
                          </p>
                        }
                        className="Tab5 animate__animated animate__zoomIn"
                      />
                      <Tab
                        value="three"
                        label={
                          <p>
                            <Link
                              href="/contact"
                              style={{
                                textDecoration: "none",
                                color: "white",
                              }}
                            >
                              <ContactMailIcon /> Contact
                            </Link>
                          </p>
                        }
                        className="Tab6 animate__animated animate__zoomIn"
                      />
                    </Tabs>
                  </>
                )}
              </Box>

              <Box sx={{ flexGrow: 0 }}>
                <Tooltip>
                  <IconButton sx={{ p: 0 }}>
                    <Avatar className="Tab7 animate__animated animate__backInRight">
                      <Image
                        src={myProfilePic}
                        style={{ width: "100%", height: "auto" }}
                        alt="sufi"
                        width={100}
                        height={100}
                        loading="lazy"
                      />
                    </Avatar>
                  </IconButton>
                </Tooltip>

                <Menu
                  id="menu-appbar-avatar"
                  anchorEl={anchorElNav}
                  anchorOrigin={{
                    vertical: "bottom",
                    horizontal: "right",
                  }}
                  keepMounted
                  transformOrigin={{
                    vertical: "top", // Adjust to match the new position
                    horizontal: "right",
                  }}
                  open={Boolean(anchorElNav)}
                  onClose={handleCloseNavMenu}
                  sx={{
                    display: { xs: "block", md: "none" },
                  }}
                >
                  {/* <Drawer
                    open={isDrawerOpen}
                    onClose={() => setIsDrawerOpen(false)}
                  > */}
                  <List className="DrawerList">
                    <ListItemButton>
                      <ListItemIcon>
                        <Home />
                      </ListItemIcon>
                      <ListItemText
                        primary={"Home"}
                        onClick={() => {
                          window.location.href = "/";
                        }}
                      />
                    </ListItemButton>

                    <ListItemButton>
                      <ListItemIcon>
                        <InfoIcon />
                      </ListItemIcon>
                      <Link
                        href="/about"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"About"}
                        />{" "}
                      </Link>
                    </ListItemButton>

                    <ListItemButton>
                      <ListItemIcon>
                        <ManageAccountsIcon />
                      </ListItemIcon>
                      <Link
                        href="/skills"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Skills"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <TimelineIcon />
                      </ListItemIcon>
                      <Link
                        href="/projects"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Projects"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <AddReactionIcon />
                      </ListItemIcon>
                      <Link
                        href="/blogs"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Blogs"}
                        />
                      </Link>
                    </ListItemButton>
                    <ListItemButton>
                      <ListItemIcon>
                        <ContactMailIcon />
                      </ListItemIcon>
                      <Link
                        href="/contact"
                        style={{ textDecoration: "none", color: "white" }}
                      >
                        <ListItemText
                          onClick={handleCloseNavMenu}
                          primary={"Contact"}
                        />
                      </Link>
                    </ListItemButton>
                  </List>
                </Menu>
              </Box>
            </Toolbar>
          </Container>
        </AppBar>
      </HideOnScroll>
    </React.Fragment>
  );
}
export default ResponsiveAppBar;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikb4fn8efs6tb9ux88os.gif)

## Conclusion
With the ResponsiveAppBar component in place, you now have a responsive navbar for your React or Next.js project that's both functional and visually appealing. Feel free to enhance and customize it further to meet your project's specific needs.

In our next blog post, we’ll explore how to connect these navigation tabs to different sections of your website. Stay tuned for more exciting web development tutorials!

Please give heart 💖  if u find it useful! Happy coding! 🚀

My wesite [https://sufianmustafa.com/]