Open In App

Create a scroll status in ReactJS using Material-UI

Progress bars at the top, showing scroll status are very common nowadays in webpages. Also, ReactJS and Material-UI go pretty well along with each other, React-JS being the most popular JavaScript framework for building UI components and Material-UI a library that provides various useful and reusable react components.

Prerequisites:

Steps to Create the React Application And Installing Module:

Step 1: Create a React application using the following command.



npx create-react-app gfg

Step 2: Move to the project directory.

cd gfg

Step 3: Install the material-ui modules using the following command.



npm install @material-ui/core

Project Structure:

package.json:

"dependencies": {
"@material-ui/core": "^4.12.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Progress Bars in Material-UI are of two types:

  1. Determinate: Progress status is controlled using a variable.
  2. Indeterminate: Progress status is indeterminate.

Approach: We will use the Linear Determinate progress bar for displaying the scroll status of our app. The value prop of ‘LinearProgress’ component determines the value of the progress indicator between 0 to 100.

const [progress, setProgress] = React.useState(0);

Example: Below is the practical implementation of the scroll status using ReactJS




//App.js
import React from 'react';
import StatusBar from './Component/StatusBar';
import CssBaseline from '@material-ui/core/CssBaseline';
import Container from '@material-ui/core/Container';
import Typography from '@material-ui/core/Typography';
 
 
const gfglogo =
 
function App() {
    const paras = [1, 2, 3, 4, 5, 6];
    return (
        <React.Fragment>
            <CssBaseline />
 
            {/* Status bar component */}
            <StatusBar></StatusBar>
 
            <br></br>
            {paras.map(para => (
                <Container maxWidth="xs" key={para}>
                    <div style={{
                        display: 'flex',
                        flexDirection: "column",
                        justifyContent: "center",
                        alignItems: "center",
                        backgroundRepeat: 'none',
                    }}>
                        <Typography component="h1"
                            variant="h2" align="center"
                            color="textPrimary" gutterBottom>
                            Hello World
                        </Typography>
                        <img src={gfglogo} alt=""
                            style=
                            {
                                {
                                    width: "80px",
                                    marginBottom: "10px"
                                }
                            } />
                        <Typography variant="h5" align="center"
                            color="textSecondary"
                            paragraph>
                            Something short and leading about
                            the collection below—its contents,
                            the creator, etc. Make it short and
                            sweet, but not too short.
                        </Typography>
                    </div>
                    <br /><br />
                </Container>
            ))}
        </React.Fragment>
    );
}
 
export default App;




//StatusBar.js
import React from 'react';
import { makeStyles }
    from '@material-ui/core/styles';
import LinearProgress
    from '@material-ui/core/LinearProgress';
 
const useStyles = makeStyles({
    root: {
        position: 'fixed',
        width: '100%',
    },
});
 
 
export default function StatusBar() {
    const classes = useStyles();
    const [progress, setProgress] = React.useState(0);
 
    React.useEffect(() => {
 
        let computeProgress = () => {
            // The scrollTop gives length of window that has been scrolled
            const scrolled =
                document.documentElement.scrollTop;
            // scrollHeight gives total length of the window and
            // The clientHeight gives the length of viewport
            const scrollLength = document.documentElement.scrollHeight -
                document.documentElement.clientHeight;
            const progress =
                `${100 * scrolled / scrollLength}`;
 
            setProgress(progress);
        }
 
        // Adding event listener on mounting
        window.addEventListener("scroll", computeProgress);
 
        // Removing event listener upon unmounting
        return () =>
            window.removeEventListener("scroll", computeProgress);
    });
 
    return (
        <div className={classes.root}>
            <LinearProgress
                variant="determinate"
                value={progress} />
        </div>
    );
}

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000:

Output


Article Tags :