App Bar
The App Bar displays information and actions relating to the current screen.
The top App Bar provides content and actions related to the current screen. It’s used for branding, screen titles, navigation, and actions.
It can transform into a contextual action bar or be used as a navbar.
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="menu"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" className={classes.title}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
<AppBar position="static">
<Toolbar variant="dense">
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="menu"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit">
Photos
</Typography>
</Toolbar>
</AppBar>
Fixed placement
When you render the app bar position fixed, the dimension of the element doesn't impact the rest of the page. This can cause some part of your content to be invisible, behind the app bar. Here are 3 possible solutions:
- You can use
position="sticky"
instead of fixed. ⚠️ sticky is not supported by IE11. - You can render a second
<Toolbar />
component:
function App() {
return (
<React.Fragment>
<AppBar position="fixed">
<Toolbar>{/* content */}</Toolbar>
</AppBar>
<Toolbar />
</React.Fragment>
);
}
- You can use
theme.mixins.toolbar
CSS:
const useStyles = makeStyles((theme) => ({
offset: theme.mixins.toolbar,
}));
function App() {
const classes = useStyles();
return (
<React.Fragment>
<AppBar position="fixed">
<Toolbar>{/* content */}</Toolbar>
</AppBar>
<div className={classes.offset} />
</React.Fragment>
);
}
Scrolling
You can use the useScrollTrigger()
hook to respond to user scroll actions.
Hide App Bar
The app bar hides on scroll down to leave more space for reading.
Elevate App Bar
The app bar elevates on scroll to communicate that the user is not at the top of the page.
Back to top
A floating action buttons appears on scroll to make it easy to get back to the top of the page.
useScrollTrigger([options]) => trigger
Arguments
options
(Object [optional]):options.disableHysteresis
(Boolean [optional]): Defaults tofalse
. Disable the hysteresis. Ignore the scroll direction when determining thetrigger
value.options.target
(Node [optional]): Defaults towindow
.options.threshold
(Number [optional]): Defaults to100
. Change thetrigger
value when the vertical scroll strictly crosses this threshold (exclusive).
Returns
trigger
: Does the scroll position match the criteria?
Examples
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
function HideOnScroll(props) {
const trigger = useScrollTrigger();
return (
<Slide in={!trigger}>
<div>Hello</div>
</Slide>
);
}