I am trying to have a fixed header where on the right side should be tabs. The <Toolbar />
component is responsible for the responsiveness of the block but doesn't allow for stretched tabs so easily.
https://codesandbox.io/s/jv8v6vwqpv
The Material UI Tabs Component We can use the Tabs component from Material UI to create a group of tabs. It has a value prop that sets the currently selected tab using its zero-based index. The Tab component creates each tab. Its label prop sets the tab title.
mui-appbar container that automatically adjusts its height based on the viewport dimensions: 48px (phone landscape) 56px (phone portrait) 64px (default)
If you want tabs of fixed width, you need to override the root css class passed to the Tab component, where you have to override both the minWidth and width attributes.
The problem is that the Toolbar responsively changes its height and the Tabs and Tab component do not (Tabs sets a min-height
of 48px in its root
class, Tab sets a height in its root
class).
Fortunately, the behavior Toolbar
uses is available in a theme mixin, so you can create classes that also use this logic:
const styles = theme => ({
fullHeight: {
...theme.mixins.toolbar,
},
});
This will create a class that has the same responsive height logic used in the Toolbar
component. Using withStyles, you can make this class accessible to your component:
import { withStyles } from "material-ui/styles";
// ...
export default withStyles(styles)(Header);
This will add a classes
prop, which is an object containing a string attribute for each class defined in the object provided to withStyles
. You can apply this to the Tabs
component and each Tab
component by overriding their root classes to ensure that they fill the AppBar
:
render() {
const { classes } = this.props;
return (
<AppBar>
<Toolbar>
<Grid container alignItems="center" justify="space-between">
<Grid item>
<Typography color="inherit" variant="title">
BackTube
</Typography>
</Grid>
<Grid item>
<Tabs classes={{ root: classes.fullHeight }} onChange={this.changeTab} value={this.state.currentTab}>
<Tab classes={{ root: classes.fullHeight }} icon={<Magnify />} value="search" />
<Tab classes={{ root: classes.fullHeight }} icon={<FormatListBulleted />} value="lists" />
<Tab classes={{ root: classes.fullHeight }} icon={<Settings />} value="settings" />
</Tabs>
</Grid>
</Grid>
</Toolbar>
</AppBar>
);
Here is a modified version of your codesandbox with the changes above.
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