Tabs make it easy to explore and switch between different views.
npm install @rmwc/tabsTabs make it easy to explore and switch between different views.
- Module @rmwc/tabs
- Import styles:
- Using CSS Loader
- import '@rmwc/tabs/styles';
- Or include stylesheets
- '@material/tab-bar/dist/mdc.tab-bar.css'
- '@material/tab/dist/mdc.tab.css'
- '@material/tab-scroller/dist/mdc.tab-scroller.css'
- '@material/tab-indicator/dist/mdc.tab-indicator.css'
- '@material/ripple/dist/mdc.ripple.css'
- '@rmwc/icon/icon.css'
- MDC Docs: https://material.io/develop/web/components/tabs/tab-bar/
Tabs can be either controlled or uncontrolled just like inputs. Use the activeTabIndex and onActivate callback for controlled components.
``jsx`
`jsx
function Example() {
const [activeTab, setActiveTab] = React.useState(0);
return (
onActivate={(evt) => setActiveTab(evt.detail.index)}
>
);
}
`
`jsx`
`jsx`
`jsx`
`jsx`
`jsx`
`jsx`
{/ Tabs automatically scroll with lots of content. /}
`jsx`
material-components-web has some light support for using icons as indicators (it's buried in their docs but there are no working examples or demos). Support has been added to RMWC, but your mileage may vary since it will require quite a bit of manual positioning and styling. By default, the icons appear full size at the center of the tab, effectively making them overlay images.
`jsx`
`jsx
<>
{function IconIndicatorExample() {
const style = {
transformOrigin: 'center center',
transform: 'translateY(1rem) scale(0.45)'
};
return (
iconIndicator={{
icon: 'star',
style: style
}}
/>
iconIndicator={{
icon: 'favorite',
style: style
}}
/>
iconIndicator={{
icon: 'mood',
style: style
}}
/>
);
}}
>
`
| Name | Type | Description |
|------|------|-------------|
| activeTabIndex | number | The index of the active tab. |foundationRef
| | Ref | Advanced: A reference to the MDCFoundation. |indicatorTransition
| | "slide" \| "fade" | Specifies whether the indicator should slide or fade. Defaults to slide. |onActivate
| | (evt: TabBarOnActivateEventT) => void | Callback when the active tab changes. Receives event as an argument with event.target.value set to the activeTabIndex. evt.detail = { index: number; } |
| Name | Type | Description |
|------|------|-------------|
| children | ReactNode | The label for the tab, passed as children. |focusOnActivate
| | boolean | Focuses the tab when activated. Defaults to true. |foundationRef
| | Ref | Advanced: A reference to the MDCFoundation. |icon
| | IconPropT | The icon to use for the tab. |iconIndicator
| | IconPropT | Optionally use a custom icon for the active indicator, instead of the underline. |label
| | any | A label for the tab. |minWidth
| | boolean | Indicates that the tab should shrink in size to be as narrow as possible without causing text to wrap. |onInteraction
| | (evt: TabOnInteractionEventT) => void | Fires when a tab has been interacted with. This is captures both keyboard and click events. evt.detail = { tabId: string } |restrictIndicator
| | boolean | Restricts the indicator to the content |stacked
| | boolean` | Stacks the icon on top of the text label |