Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@material/tab

Package Overview
Dependencies
Maintainers
15
Versions
1661
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@material/tab

The Material Components for the web tab component

  • 14.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
15
Created
Source

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

  • Material Design guidelines: Tabs
  • Demo

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 material-icons" aria-hidden="true">favorite</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>
  <div class="mdc-tab__focus-ring"></div>
</button>

The mdc-tab__focus-ring element ensures that a focus indicator is displayed in high contrast mode around the active/focused tab.

Styles

@use "@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 material-icons" aria-hidden="true">favorite</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>
  <div class="mdc-tab__focus-ring"></div>
</button>

Tab with Indicator Spanning Only Content

In the example under Basic Usage, the Tab Indicator will span the entire tab. Alternatively, the tab indicator can be set up to span only the content of the tab if it is instead placed within the mdc-tab__content element:

<button class="mdc-tab" role="tab" aria-selected="false" tabindex="-1">
  <span class="mdc-tab__content">
    <span class="mdc-tab__icon material-icons" aria-hidden="true">favorite</span>
    <span class="mdc-tab__text-label">Favorites</span>
    <span class="mdc-tab-indicator">
      <span class="mdc-tab-indicator__content mdc-tab-indicator__content--underline"></span>
    </span>
  </span>
  <span class="mdc-tab__ripple"></span>
  <div class="mdc-tab__focus-ring"></div>
</button>

Tab Icons

We recommend using Material Icons from Google Fonts:

<head>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>

However, you can also use SVG, Font Awesome, or any other icon library you wish.

Style Customization

CSS Classes

CSS ClassDescription
mdc-tabMandatory.
mdc-tab__contentMandatory. Container of tab icon, text label and tab indicator.
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 the text label of 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
text-label-color($color)Customizes the color of the tab text label.
icon-color($color)Customizes the color of the tab icon.
states-color($color)Customizes the base states color, which affects hover/focus states and the press ripple.
ink-color($color)Customizes the text label, icon, and base states color.
active-text-label-color($color)Customizes the color of the active tab's text label.
active-icon-color($color)Customizes the color of the active tab's icon.
active-states-color($color)Customizes the color of the active tab's states.
parent-positioningSets the positioning of the MDCTab's parent element so that MDCTab.computeDimensions() reports the same values in all browsers.
fixed-width($width)Sets the fixed width of the tab. The tab will never be smaller than the given width.
horizontal-padding($padding)Sets the horizontal padding of the tab.
height($height)Sets custom height to tab bar.
text-transform($transform)Sets tab text-transform property

MDCTab Properties and Methods

PropertyValue TypeDescription
idstringValue of the root tab element's id attribute.
activeboolean (read-only)Allows getting the active state of the tab.
focusOnActivateboolean (write-only)Sets whether the tab should focus itself when activated. Defaults to true.
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": {"tabId": string}}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.
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.
setFocusOnActivate(focusOnActivate: boolean) => voidSets whether the tab should focus itself when activated.
activate(previousIndicatorClientRect?: ClientRect) => voidActivates the tab. previousIndicatorClientRect is an optional argument.
deactivate() => voidDeactivates the tab.
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()

Keywords

FAQs

Package last updated on 28 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc