How to autohide AppBar when scrolling using Reactjs Material-UI - reactjs

How can I autohide my Reactjs <AppBar/> component when scrolling as shown in Fig.1?
Fig.1. <AppBar/> autohides when scrolling
I am using Material-UI and my code is as follows.
MyAppBar.js
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Toolbar from '#material-ui/core/Toolbar';
import Typography from '#material-ui/core/Typography';
import Button from '#material-ui/core/Button';
import IconButton from '#material-ui/core/IconButton';
import MenuIcon from '#material-ui/icons/Menu';
const styles = {
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
function ButtonAppBar(props) {
const { classes } = props;
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
ButtonAppBar.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ButtonAppBar);

You can use Slide in conjuction with useScrollTrigger
The gist here is, useScrollTrigger will (by default) return true when you scroll down and the window vertical scrollbar position hits a certain threshold (100 pixels from origin by default) - when you scroll back up it returns false. Hence, these are the reasons why we negate it on the in prop of Slide
export default function HideAppBar() {
const trigger = useScrollTrigger();
return (
<>
<Slide appear={false} direction="down" in={!trigger}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll Down to Hide App Bar</Typography>
</Toolbar>
</AppBar>
</Slide>
...
</>
);
}
You can use the link I gave at the top of this answer pertaining to useScrollTrigger to customize the options such as threshold
Reference: https://material-ui.com/components/app-bar/#hide-app-bar

Related

App Bar not filling the top of the page MUI MATERIAL

I am using MUI MATERIAL.
I got an
It has a padding on the top,right,left of the page.
I want the App Bar to be at the very top of the page.
I tried using classes but it does not work.
components/AppBar/index.js
import * as React from "react";
import AppBar from "#mui/material/AppBar";
import Box from "#mui/material/Box";
import Toolbar from "#mui/material/Toolbar";
import Typography from "#mui/material/Typography";
import Button from "#mui/material/Button";
import IconButton from "#mui/material/IconButton";
import MenuIcon from "#mui/icons-material/Menu";
import { makeStyles } from "#mui/styles";
const useStyles = makeStyles({
root: {
backgroundColor: "#130f40",
margin: 0
}
});
export default function ButtonAppBar() {
const classes = useStyles();
return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="static" className={classes.root}>
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</Box>
);
}
You can add CssBaseline at the top level of your app to get rid of the space around the AppBar component. It will apply style rules from normalize.css which includes margin:0 on the body element which is the reason for this gap.
import * as React from 'react';
import CssBaseline from '#mui/material/CssBaseline';
export default function MyApp() {
return (
<React.Fragment>
<CssBaseline />
{/* The rest of your application */}
</React.Fragment>
);
}

Material-UI v5: makeStyles's not being imported

When I am trying to import "makeStyles" using :
import { makeStyles } from '#mui/styles';
it's not working. The page becomes blank when I run.
But It's working when I change the code and use V4:
import { makeStyles } from "#material-ui/core/styles";
I've installed alll the dependencies that is required. Why It's not working using V5 of material-ui?
Here is my code (styles.js):
import { makeStyles } from "#mui/material";
export default makeStyles((theme) => ({
title: {
display: "none",
[theme.breakpoints.up("sm")]: {
display: "block",
},
},
}));
Header.jsx:
import React from "react";
import { AppBar, Toolbar, Typography, InputBase, Box } from "#mui/material";
import SearchIcon from "#mui/icons-material/Search";
import useStyles from "./styles";
const Header = () => {
const classes = useStyles();
return (
<AppBar position="static">
<Toolbar>
<Typography variant="h5" className={classes.title}>
Travel Advsior
</Typography>
<Box display="flex">
<Typography variant="h6" className={classes.title}>
Explore new places
</Typography>
<div>
<div>
<SearchIcon />
</div>
<InputBase placeholder="Search..." />
</div>
</Box>
</Toolbar>
</AppBar>
);
};
export default Header;

TypeError: Cannot read properties of undefined (reading 'up') MUI

In my Header component my custom styling are coming from style.js where I've used makeStyles from #mui/styles. But In there while setting up the [theme.breakpoints.up("sm")] I'm getting the error above. I tried wrapping the index.js with themeProvider and passed createTheme as prop. but it is giving me another error. I tried looking up the docs but couldn't figure it out.
styles.js
import { makeStyles } from "#mui/styles";
import { alpha } from "#mui/material/styles";
export default makeStyles((theme) => ({
title: {
display: "none",
[theme.breakpoints.up("sm")]: {
display: "block",
},
},
...
})
)
Header component
import React from "react";
import useStyles from "./styles";
import { AppBar, Toolbar, Typography, InputBase, Box } from "#mui/material";
import { Autocomplete } from "#react-google-maps/api";
import { SearchIcon } from "#mui/icons-material/Search";
const Header = () => {
const classes = useStyles();
return (
<AppBar position="static">
<Toolbar className={classes.toolbar}>
<Typography variant="h5" className={classes.title}>
Travel Companion
</Typography>
<Box display="flex">
<Typography variant="h6" className={classes.title}>
Explore new places
</Typography>
<Autocomplete>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
classes={{ root: classes.inputRoot, input: classes.inputInput }}
placeholder="Search"
/>
</div>
</Autocomplete>
</Box>
</Toolbar>
</AppBar>
);
};
export default Header;

Place Material-ui icon inside circular progress indicator

I would like to use a circular progress indicator from Material-UI in the header of my app.
But I just dont know how to fit nicely a download icon from Material Icons inside so that progress bar will move around the icon. Here is what I have now:
and I want to achieve this:
I tried to place icon with absolute positioning, but probably there is a better idea
import React from "react";
import ReactDOM from "react-dom";
import CircularProgress from "#material-ui/core/CircularProgress";
import Button from "#material-ui/core/Button";
import AppBar from '#material-ui/core/AppBar';
import Toolbar from '#material-ui/core/Toolbar';
import IconButton from '#material-ui/core/IconButton';
import VerticalAlignBottomIcon from '#material-ui/icons/VerticalAlignBottom';
function CircularStatic() {
const [completed, setCompleted] = React.useState(0);
React.useEffect(() => {
function progress() {
setCompleted((prevCompleted) =>
prevCompleted >= 100 ? 0 : prevCompleted + 10
);
}
const timer = setInterval(progress, 1000);
return () => {
clearInterval(timer);
};
}, []);
return (
<div>
<Button variant="contained" color="primary">
<CircularProgress variant="static" value={completed} color="inherit">
</CircularProgress>
</Button>
</div>
);
}
function ButtonAppBar() {
return (
<AppBar position="static" style={{ backgroundColor: 'teal' }}>
<Toolbar>
<IconButton edge="start" color="inherit">
<VerticalAlignBottomIcon />
<CircularStatic/>
</IconButton>
</Toolbar>
</AppBar>
);
}
ReactDOM.render(<ButtonAppBar />, document.getElementById("root"));
I customized a little, the Material-UI documentation example here
function CircularProgressWithContent(props) {
return (
<Box position="relative" display="inline-flex">
<CircularProgress />
<Box
top={0}
left={0}
bottom={0}
right={0}
position="absolute"
display="flex"
alignItems="center"
justifyContent="center"
>
<Typography variant="caption" component="div" color="textSecondary">
{props.content}
</Typography>
</Box>
</Box>
);
}
And to use it :
<CircularProgressWithLabel content={<LockOutlinedIcon />} />
It's perfectible but it works well for your use case.

Material-UI not applying color theme to a button as it should be

I'm trying to follow this documentation. I am trying to get Login button to turn green, but it seems to not inherit any theme styling with the given code. Not sure what I'm doing wrong.
I have the following code:
import React from "react";
import {
createMuiTheme,
withStyles,
makeStyles
} from "#material-ui/core/styles";
import { ThemeProvider } from "#material-ui/styles";
import green from "#material-ui/core/colors/green";
import AppBar from "#material-ui/core/AppBar";
import Toolbar from "#material-ui/core/Toolbar";
import Typography from "#material-ui/core/Typography";
import Button from "#material-ui/core/Button";
import IconButton from "#material-ui/core/IconButton";
import MenuIcon from "#material-ui/icons/Menu";
import { Link } from "react-router-dom";
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1
},
menuButton: {
marginRight: theme.spacing(2)
},
title: {
flexGrow: 1
}
}));
const theme = createMuiTheme({
palette: {
primary: green
}
});
export default function ButtonAppBar() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="Menu"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" className={classes.title}>
Hello
</Typography>
<ThemeProvider theme={theme}>
<Link to="/login">
<Button color="secondary">Login</Button>
</Link>
</ThemeProvider>
</Toolbar>
</AppBar>
</div>
);
}
If I were to paste in all the code from the documentation, then I would see the three buttons of each color. But my variation of the code doesn't work.
I am assuming you mean this button?
<Link to="/login">
<Button color="secondary">Login</Button>
</Link>
if so you need to set it to Primary not secondary. You need it wrapped in themeProvider too. If you look at the custom code in that doc you posted you should see this
<ThemeProvider theme={theme}>
<Button variant="contained" color="primary" className={classes.margin}>
Theme Provider
</Button>
</ThemeProvider>
It should match what you use in the createMuiTheme:
const theme = createMuiTheme({
palette: {
primary: green
}
});
You are putting the Button's color="secondary". While creating the palette, you are defining the color green for the primary attribute. I tried it in this sandbox to change the Button's color prop to color="primary" and it changes to green.

Resources