14.0.0 • Published 4 months ago

@material/tab v14.0.0

Weekly downloads
42,222
License
MIT
Repository
github
Last release
4 months ago

Tab

Tabs organize and allow navigation between groups of content that are related and at the same level of hierarchy. Each Tab governs the visibility of one group of content.

Design & API Documentation

Installation

npm install @material/tab

Basic Usage

HTML Structure

<button class="mdc-tab" role="tab" aria-selected="false" tabindex="-1">
  <span class="mdc-tab__content">
    <span class="mdc-tab__icon">heart</span>
    <span class="mdc-tab__text-label">Favorites</span>
  </span>
  <span class="mdc-tab-indicator">
    <span class="mdc-tab-indicator__content mdc-tab-indicator__content--underline"></span>
  </span>
  <span class="mdc-tab__ripple"></span>
</button>

Styles

@import "@material/tab/mdc-tab";

JavaScript Instantiation

import {MDCTab} from '@material/tab';

const tab = new MDCTab(document.querySelector('.mdc-tab'));

See Importing the JS component for more information on how to import JavaScript.

Variants

Active Tab

NOTE: Don't forget to add the mdc-tab-indicator--active class to the mdc-tab-indicator subcomponent.

<button class="mdc-tab mdc-tab--active" role="tab" aria-selected="true">
  <span class="mdc-tab__content">
    <span class="mdc-tab__icon">heart</span>
    <span class="mdc-tab__text-label">Favorites</span>
  </span>
  <span class="mdc-tab-indicator mdc-tab-indicator--active">
    <span class="mdc-tab-indicator__content mdc-tab-indicator__content--underline"></span>
  </span>
  <span class="mdc-tab__ripple"></span>
</button>

Style Customization

CSS Classes

CSS ClassDescription
mdc-tabMandatory.
mdc-tab__contentMandatory. Indicates the text label of the tab.
mdc-tab__rippleMandatory. Denotes the ripple surface for the tab.
mdc-tab--activeOptional. Indicates that the tab is active.
mdc-tab--stackedOptional. Indicates that the tab icon and label should flow vertically instead of horizontally.
mdc-tab--min-widthOptional. Indicates that the tab should shrink in size to be as narrow as possible without causing text to wrap.
mdc-tab__text-labelOptional. Indicates an icon in the tab.
mdc-tab__iconOptional. Indicates a leading icon in the tab.

Sass Mixins

To customize the colors of any part of the tab, use the following mixins.

MixinDescription
mdc-tab-text-label-color($color)Customizes the color of the tab text label.
mdc-tab-icon-color($color)Customizes the color of the tab icon.
mdc-tab-parent-positioningSets the positioning of the MDCTab's parent element so that MDCTab.computeDimensions() reports the same values in all browsers.
mdc-tab-fixed-width($width)Sets the fixed width of the tab. The tab will never be smaller than the given width.

MDCTab Properties and Methods

PropertyValue TypeDescription
activebooleanAllows getting the active state of the tab.
Method SignatureDescription
activate(previousIndicatorClientRect: ClientRect=) => voidActivates the indicator. previousIndicatorClientRect is an optional argument.
deactivate() => voidDeactivates the indicator.
focus() => voidFocuses the tab.
computeIndicatorClientRect() => ClientRectReturns the bounding client rect of the indicator.
computeDimensions() => MDCTabDimensionsReturns the dimensions of the Tab.
Event NameEvent Data StructureDescription
MDCTab:interacted{"detail": {"tab": MDCTab}}Emitted when the Tab is interacted with, regardless of its active state. Used by parent components to know which Tab to activate.

Usage within Web Frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Tab for your framework. Depending on your needs, you can use the Simple Approach: Wrapping MDC Web Vanilla Components, or the Advanced Approach: Using Foundations and Adapters. Please follow the instructions here.

MDCTabAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the root element.
removeClass(className: string) => voidRemoves a class from the root element.
hasClass(className: string) => booleanReturns true if the root element contains the given class.
setAttr(attr: string, value: string) => voidSets the given attribute on the root element to the given value.
activateIndicator(previousIndicatorClientRect: ClientRect=) => voidActivates the tab indicator subcomponent. previousIndicatorClientRect is an optional argument.
deactivateIndicator() => voidDeactivates the tab indicator subcomponent.
computeIndicatorClientRect() => ClientRectReturns the tab indicator subcomponent's content bounding client rect.
getOffsetLeft() => numberReturns the offsetLeft value of the root element.
getOffsetWidth() => numberReturns the offsetWidth value of the root element.
getContentOffsetLeft() => numberReturns the offsetLeft value of the content element.
getContentOffsetWidth() => numberReturns the offsetWidth value of the content element.
notifyInteracted() => voidEmits the MDCTab:interacted event.
focus() => voidApplies focus to the root element.

MDCTabFoundation

Method SignatureDescription
handleClick() => voidHandles the logic for the "click" event.
isActive() => booleanReturns whether the tab is active.
activate(previousIndicatorClientRect: ClientRect=) => voidActivates the tab. previousIndicatorClientRect is an optional argument.
deactivate() => voidDeactivates the tab.
computeIndicatorClientRect() => ClientRectReturns the tab indicator subcomponent's content bounding client rect.
computeDimensions() => MDCTabDimensionsReturns the dimensions of the tab.

MDCTabFoundation Event Handlers

When wrapping the Tab component, it is necessary to register the following event handler. For an example of this, see the MDCTab component's initialSyncWithDOM method.

EventElementFoundation Handler
clickRoot elementhandleClick()
14.0.0

2 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.0.0

3 years ago

10.0.0

3 years ago

9.0.0

3 years ago

8.0.0

3 years ago

7.0.0

4 years ago

6.0.0

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.0.0

4 years ago

3.2.0

5 years ago

4.0.0-canary.1

5 years ago

4.0.0-canary.0

5 years ago

4.0.0-alpha.0

5 years ago

3.1.0

5 years ago

3.1.0-alpha.0

5 years ago

3.0.0

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-1

5 years ago

1.0.0-0

5 years ago

0.44.1

5 years ago

0.44.0

5 years ago

0.43.0

5 years ago

0.42.0

5 years ago

0.41.0

5 years ago

0.40.1

5 years ago

0.40.0

6 years ago

0.39.3

6 years ago

0.39.2

6 years ago

0.39.1

6 years ago

0.39.0

6 years ago

0.39.0-0

6 years ago

0.38.1

6 years ago

0.38.0

6 years ago