Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have full-height Tabs inside of a Toolbar using material-ui?

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

like image 787
Mathieu Marques Avatar asked Feb 22 '18 13:02

Mathieu Marques


People also ask

How do you use tabs in material UI?

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.

What is the height of appbar in material UI?

mui-appbar container that automatically adjusts its height based on the viewport dimensions: 48px (phone landscape) 56px (phone portrait) 64px (default)

How do I change the width of a tab in Materialui?

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.


1 Answers

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.

like image 126
Ken Gregory Avatar answered Oct 13 '22 07:10

Ken Gregory