Probably a basic question, but I couldn't find any example in the documentation. Using material-ui-next
beta.30. I have the following:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as mui from 'material-ui';
import 'typeface-roboto';
function App() {
return (
<div>
<mui.Reboot />
<mui.AppBar color="primary" position="fixed">
<mui.Toolbar>
<mui.Typography color="inherit" type="title">
My Title
</mui.Typography>
</mui.Toolbar>
</mui.AppBar>
<mui.Paper>
My Content
</mui.Paper>
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('container')
);
And I want the mui.Paper
content appear beneath the AppBar, and not hidden by it. Is there a component I'm missing somewhere?
Just use position="sticky"
instead of position="fixed"
for your AppBar.
Your content is on screen, but covered up by the AppBar
. You can use theme.mixins.toolbar
to load information about the app bar height and shift your content accordingly:
const styles = theme => ({
// Load app bar information from the theme
toolbar: theme.mixins.toolbar,
});
And then create a div
above your content to shift your content accordingly:
<Paper>
<div className={classes.toolbar} />
MyContent will be shifted downwards by the div above. If you remove
the div, your content will disappear under the app bar.
</Paper>
What's happening here is that theme.mixins.toolbar
is loading information about the AppBar
dimensions into your styles. Then, applying that information to the div
sizes the div
so that it's exactly the right height for shifting your content down the screen.
Here's a full working example:
import React from 'react';
import Paper from 'material-ui/Paper';
import Reboot from 'material-ui/Reboot';
import AppBar from 'material-ui/AppBar';
import Toolbar from 'material-ui/Toolbar';
import Typography from 'material-ui/Typography';
import { withStyles } from 'material-ui/styles';
const styles = (theme) => ({
toolbar: theme.mixins.toolbar,
});
const App = (props) => {
const { classes } = props;
return (
<div>
<Reboot />
<AppBar color="primary" position="fixed">
<Toolbar>
<Typography color="inherit" type="title">
My Title
</Typography>
</Toolbar>
</AppBar>
<Paper>
<div className={classes.toolbar} />
MyContent will be shifted downwards by the div above. If you remove
the div, your content will disappear under the app bar.
</Paper>
</div>
);
}
export default withStyles(styles)(App);
Simply add an empty Toolbar after AppBar:
<AppBar>
<Toolbar>
...toolbar content...
</Toolbar>
</AppBar>
<Toolbar /> // <- does the trick
Source: https://github.com/mui-org/material-ui/issues/16844#issuecomment-517205129
The Elevate App Bar example just adds an empty Toolbar
:
export default function ElevateAppBar(props) {
return (
<React.Fragment>
<CssBaseline />
<ElevationScroll {...props}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to Elevate App Bar</Typography>
</Toolbar>
</AppBar>
</ElevationScroll>
<Toolbar /> // <-- The important part.
<Container>
<Box my={2}>
{[...new Array(12)]
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
</React.Fragment>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With