Reputation: 10421
I'm looking for the best way set the page layout for an SPA that consists of a static header (AppBar
) on top and the dynamic page content below the header.
The page content ScreenSizePage
sometimes (but not always) should have a height that exactly matches the remaining height of the screen.
In other words: what is the best way to only make the height of the div
in ScreenSizePage
exactly fit the remaining screen height in a Material-UI app?
index.tsx:
import React from 'react';
import ReactDOM from 'react-dom';
import CssBaseline from '@material-ui/core/CssBaseline';
import {ThemeProvider} from '@material-ui/styles';
import {BrowserRouter} from 'react-router-dom';
import App from './App';
import theme from './theme';
ReactDOM.render(
<ThemeProvider theme={theme}>
<CssBaseline />
<BrowserRouter>
<App />
</BrowserRouter>
</ThemeProvider>,
document.querySelector('#root'),
);
App.tsx:
import React from 'react';
import {Route, Switch} from 'react-router-dom';
import AppBar from './AppBar';
import HomePage from './pages/HomePage';
import {ScreenSizePage} from './pages/ScreenSizePage';
export default function App() {
return (
<div>
<AppBar />
<Switch>
<Route exact={true} path="/" component={HomePage} />
<Route path="/ScreenSizePage" component={ScreenSizePage} />
</Switch>
</div>
);
}
AppBar.tsx:
import React from 'react';
import {createStyles, makeStyles, Theme} 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';
const useStyles = makeStyles((theme: Theme) => createStyles({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
}));
export default function DenseAppBar() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar variant="dense">
{/* ... */}
<Typography variant="h6" color="inherit">
Home
</Typography>
</Toolbar>
</AppBar>
</div>
);
}
HomePage.tsx:
import React from 'react';
export default function HomePage() {
return (
<div>This page can have any height</div>
);
}
ScreenSizePage.tsx:
import React from 'react';
export default function ScreenSizePage() {
return (
<div>This page should use the remaining page height</div>
);
}
Upvotes: 4
Views: 9612
Reputation: 1520
You can use CSS flexbox or "calc" function to achieve this. "flex-grow" will make the content fit the remaining space. Codesandbox below. I have commented out the part with "calc"
https://codesandbox.io/s/material-demo-brz62
Upvotes: 8