Skip to content

Instantly share code, notes, and snippets.

@hafbau
Last active December 11, 2022 18:14
Show Gist options
  • Save hafbau/1a332cf84455aafc2dcd2716ee14fcc1 to your computer and use it in GitHub Desktop.
Save hafbau/1a332cf84455aafc2dcd2716ee14fcc1 to your computer and use it in GitHub Desktop.
Vertical Tabs Example - React Material UI
import React from 'react';
import Tabs from '@material-ui/core/Tabs'
import Tab from '@material-ui/core/Tab'
import Typography from '@material-ui/core/Typography'
import { withStyles } from '@material-ui/core/withStyles';
class ProfileTabs extends React.PureComponent {
state = { activeIndex: 0 }
handleChange = (_, activeIndex) => this.setState({ activeIndex })
render() {
const { activeIndex } = this.state;
return (
<div
style={{
display: 'flex',
}}
>
<VerticalTabs
value={activeIndex}
onChange={this.handleChange}
>
<MyTab label='item one' />
<MyTab label='item two' />
<MyTab label='item three' />
</VerticalTabs>
{ activeIndex === 0 && <TabContainer>Item One</TabContainer> }
{ activeIndex === 1 && <TabContainer>Item Two</TabContainer> }
{ activeIndex === 2 && <TabContainer>Item Three</TabContainer> }
</div>
)
}
}
const VerticalTabs = withStyles(theme => ({
flexContainer: {
flexDirection: 'column'
},
indicator: {
display: 'none',
}
}))(Tabs)
const MyTab = withStyles(theme => ({
selected: {
color: 'tomato',
borderBottom: '2px solid tomato'
}
}))(Tab);
function TabContainer(props) {
return (
<Typography component="div" style={{ padding: 24 }}>
{props.children}
</Typography>
);
}
export default ProfileTabs;
@tuancaraballo
Copy link

Thank you hafbau, this was exactly what I needed! Cheers

@manuhazen
Copy link

Amazing hafbau! Thanks

@frankieali
Copy link

frankieali commented Jun 24, 2019

Thanks for providing this Gist. I'm test driving it now for my project.

I think your withStyles import is incorrect:
import { withStyles } from '@material-ui/core/withStyles'; should be import { withStyles } from '@material-ui/styles/withStyles';

@frankieali
Copy link

frankieali commented Jul 3, 2019 via email

@dalepres
Copy link

Your article is very helpful to this new-to-react developer. Maybe you can help with one thing: I need a react vertical tab component that takes images rather than text labels. Do you know how to do it? If not, at least your article gives me the idea to make my own react component for the left-hand navigation I need; I'll just have to work out the tie to show the right tab container on click.

@hafbau
Copy link
Author

hafbau commented Aug 26, 2020

Hey @dalepres, I made this gist when material UI didn't have a way to make vertical tabs. They have now incorporated the idea in the library it self. They have an orientation prop that you could set to vertical. See api here: https://material-ui.com/api/tabs/

As regards your question, what you want to achieve is so that you click on the images and the content of the page changes? Is that what you're thinking?

@dalepres
Copy link

hafbau, I did get a Material-UI tabs working with images with some great help on stackoverflow.com. There were several undocumented features and steps required to make it happen. I'm learning that material-ui is a great and maturing package but may not as mature as I had initially thought. That's not an issue and I appreciate all the great work from those who create and support it. I'm looking forward to learning it more thoroughly.

@Gblack-Hub
Copy link

Thank you.

@chombazm
Copy link

Thanks so much. this is what i really needed 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment