Categories
Material UI

Material UI — Link and Accessible Tabs

Spread the love

UI is a Material Design library made for React.

It’s a set of React components that have Material Design styles.

In this article, we’ll look at how to add vertical tabs with Material UI.

Vertical Tabs

We can add vertical tabs with the orientation prop set to vertical .

For example, we can write:

import React from "react";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div>
      <Tabs
        orientation="vertical"
        variant="scrollable"
        value={value}
        onChange={handleChange}
      >
        <Tab label="Item One" />
        <Tab label="Item Two" />
        <Tab label="Item Three" />
      </Tabs>
      <TabPanel value={value} index={0}>
        Item One
      </TabPanel>
      <TabPanel value={value} index={1}>
        Item Two
      </TabPanel>
      <TabPanel value={value} index={2}>
        Item Three
      </TabPanel>
    </div>
  );
}

to add tabs with content below it.

The tabs will be vertical since we set orientation to vertical .

Nav Tabs

We can also use nav links for tabs.

For example, we can write:

import React from "react";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Box from "@material-ui/core/Box";
import AppBar from "@material-ui/core/AppBar";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

return <div {...other}>{value === index && <Box p={3}>{children}</Box>}</div>;
}

function LinkTab(props) {
  return (
    <Tab
      component="a"
      onClick={event => {
        event.preventDefault();
      }}
      {...props}
    />
  );
}

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div>
      <AppBar position="static">
        <Tabs variant="fullWidth" value={value} onChange={handleChange}>
          <LinkTab label="Page One" href="/foo" />
          <LinkTab label="Page Two" href="/bar" />
          <LinkTab label="Page Three" href="/baz" />
        </Tabs>
      </AppBar>
      <TabPanel value={value} index={0}>
        Page One
      </TabPanel>
      <TabPanel value={value} index={1}>
        Page Two
      </TabPanel>
      <TabPanel value={value} index={2}>
        Page Three
      </TabPanel>
    </div>
  );
}

to add link tabs.

We have a LinkTab component that renders the Tab with the onClick prop set to a function that calls preventDefault so that we can navigate to the URL in the href prop.

Icon Tabs

Also, we can add icons to tabs.

We pass the icon into the icon prop of Tab to add an icon.

For example, we can write:

import React from "react";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Paper from "@material-ui/core/Paper";
import PhoneIcon from "@material-ui/icons/Phone";
import FavoriteIcon from "@material-ui/icons/Favorite";
import AirplanemodeActiveIcon from "@material-ui/icons/AirplanemodeActive";

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div>
      <Paper square>
        <Tabs
          value={value}
          onChange={handleChange}
          variant="fullWidth"
          indicatorColor="primary"
          textColor="primary"
        >
          <Tab icon={<PhoneIcon />} />
          <Tab icon={<FavoriteIcon />} />
          <Tab icon={<AirplanemodeActiveIcon />} />
        </Tabs>
      </Paper>
    </div>
  );
}

to add icons to our tabs.

We pass the icon component as the value of the icon prop of the Tab .

Keyboard Navigation

We can add the selectionFollowsFocus prop to enable keyboard navigation./

For example, we can write:

import React from "react";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Paper from "@material-ui/core/Paper";
import PhoneIcon from "@material-ui/icons/Phone";
import FavoriteIcon from "@material-ui/icons/Favorite";
import AirplanemodeActiveIcon from "@material-ui/icons/AirplanemodeActive";

export default function App() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div>
      <Paper square>
        <Tabs
          selectionFollowsFocus
          value={value}
          onChange={handleChange}
          variant="fullWidth"
          indicatorColor="primary"
          textColor="primary"
        >
          <Tab icon={<PhoneIcon />} />
          <Tab icon={<FavoriteIcon />} />
          <Tab icon={<AirplanemodeActiveIcon />} />
        </Tabs>
      </Paper>
    </div>
  );
}

to enable keyboard navigation.

If we focus on the tabs, then we can navigate with the keyboard with it.

Conclusion

We can customize our tabs by making them vertical.

Also, we can add tabs that act as links.

And we can enable keyboard navigation.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “Material UI — Link and Accessible Tabs”

For LinkTabs, doesn’t setting component to ‘a’ cause a full page reload, which defeats the purpose of using react? should be using router links instead.

Leave a Reply

Your email address will not be published. Required fields are marked *