How to hide mui appbar on scroll in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To hide mui appbar on scroll in React js, you can use this <HideOnScroll threshold={400}> component. It will hide mui appbar on scroll in react js.

Today, I am going to show you, How to hide mui appbar on scroll in react js.

Installation

Install the following packages to use mui AppBar in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

Table of contents

  • Install MUI and create a new React app.
  • Import Material-UI AppBar.
  • Customizing the AppBar.

Step 1: Install MUI and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import Material-UI AppBar.

After installing MUI, you have to import your React component. To do this, add the following line to the top of your component file.

import React from 'react';
import { AppBar, Toolbar, Typography } from '@mui/material';

Step 3: Customizing the AppBar.

You can use the AppBar component in your react js. For example, The AppBar component is used to create a top app bar that contains navigation. You can use this code <HideOnScroll threshold={400}> then it will hide mui appbar on scroll in react js.

<HideOnScroll threshold={400}>
        <AppBar>
          <Toolbar>
            <Box mr={2} clone>
              <IconButton edge="start" color="inherit" aria-label="Open drawer">
                <MenuIcon />
              </IconButton>
            </Box>
            <Typography variant="h6" color="inherit" noWrap>
              Scrolling
            </Typography>
          </Toolbar>
        </AppBar>
      </HideOnScroll>

MUI material hide mui appbar on scroll example.

The below code is an example, to hide mui appbar on scroll, you need to import AppBar Component. You can use this code <HideOnScroll threshold={400}> then hide mui appbar on scroll in react js.

App.js

import React from "react";
import {
  AppBar,
  CssBaseline,
  Container,
  Toolbar,
  IconButton,
  Typography
} from "@mui/material";
import MenuIcon from "@mui/material/Menu";
import Box from "@mui/material/Box";
import HideOnScroll from "./HideOnScroll";

export default function Demo() {
  return (
    <React.Fragment>
      <CssBaseline />
      <HideOnScroll threshold={400}>
        <AppBar>
          <Toolbar>
            <Box mr={2} clone>
              <IconButton edge="start" color="inherit" aria-label="Open drawer">
                <MenuIcon />
              </IconButton>
            </Box>
            <Typography variant="h6" color="inherit" noWrap>
              Scrolling
            </Typography>
          </Toolbar>
        </AppBar>
      </HideOnScroll>
      <Toolbar />
      <Container>
        <Box my={2}>
          Lorem Ipsum is simply dummy text of the printing and typesetting
          industry. Lorem Ipsum has been the industry's standard dummy text ever
          since the 1500s, when an unknown printer took a galley of type and
          scrambled it to make a type specimen book. It has survived not only
          five centuries, but also the leap into electronic typesetting,
          remaining essentially unchanged. It was popularised in the 1960s with
          the release of Letraset sheets containing Lorem Ipsum passages, and
          more recently with desktop publishing software like Aldus PageMaker
          including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of
          the printing and typesetting industry. Lorem Ipsum has been the
          industry's standard dummy text ever since the 1500s, when an unknown
          printer took a galley of type and scrambled it to make a type specimen
          book. It has survived not only five centuries, but also the leap into
          electronic typesetting, remaining essentially unchanged. It was
          popularised in the 1960s with the release of Letraset sheets
          containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem
          Ipsum. Lorem Ipsum is simply dummy text of the printing and
          typesetting industry. Lorem Ipsum has been the industry's standard
          dummy text ever since the 1500s, when an unknown printer took a galley
          of type and scrambled it to make a type specimen book. It has survived
          not only five centuries, but also the leap into electronic
          typesetting, remaining essentially unchanged. It was popularised in
          the 1960s with the release of Letraset sheets containing Lorem Ipsum
          passages, and more recently with desktop publishing software like
          Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is
          simply dummy text of the printing and typesetting industry. Lorem
          Ipsum has been the industry's standard dummy text ever since the
          1500s, when an unknown printer took a galley of type and scrambled it
          to make a type specimen book. It has survived not only five centuries,
          but also the leap into electronic typesetting, remaining essentially
          unchanged. It was popularised in the 1960s with the release of
          Letraset sheets containing Lorem Ipsum passages, and more recently
          with desktop publishing software like Aldus PageMaker including
          versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the
          printing and typesetting industry. Lorem Ipsum has been the industry's
          standard dummy text ever since the 1500s, when an unknown printer took
          a galley of type and scrambled it to make a type specimen book.
        </Box>
      </Container>
    </React.Fragment>
  );
}

HideOnScroll.js

import React from "react";
import { Slide } from "@mui/material";

function getScrollY(scroller) {
  return scroller.pageYOffset !== undefined
    ? scroller.pageYOffset
    : scroller.scrollTop !== undefined
    ? scroller.scrollTop
    : (document.documentElement || document.body.parentNode || document.body)
        .scrollTop;
}

const useHideOnScroll = options => {
  const { threshold, scroller } = options;

  const scrollRef = React.useRef();
  const [hide, setHide] = React.useState(false);

  const handleScroll = React.useCallback(() => {
    const scrollY = getScrollY(scroller || window);
    const prevScrollY = scrollRef.current;
    scrollRef.current = scrollY;

    setHide(
      scrollY < prevScrollY
        ? false
        : scrollY > prevScrollY &&
          scrollY > (threshold != null ? threshold : 100)
        ? true
        : false
    );
  }, [scroller, threshold]);

  React.useEffect(() => {
    window.addEventListener("scroll", handleScroll);
    return () => {
      window.removeEventListener("scroll", handleScroll);
    };
  }, [handleScroll]);

  return hide;
};

export default function HideOnScroll(props) {
  const { children, threshold, scroller, ...other } = props;

  const hide = useHideOnScroll({ threshold, scroller });

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

In the above code example, I have used the @mui/material component and hide mui appbar on scroll in react js.

Check the output of the above code example.

React, Mui, appbar, on, scroll

Here, we are provided code sandbox links for the above program hide mui appbar on scroll in react js. Then you can use whenever you want and do the changes as per your requirements.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts