Socket
Socket
Sign inDemoInstall

@material/tab-bar

Package Overview
Dependencies
16
Maintainers
14
Versions
1651
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @material/tab-bar

The Material Components for the web tab bar component


Version published
Maintainers
14
Created

Readme

Source

Tab Bar

Tabs organize and allow navigation between groups of content that are related and at the same level of hierarchy. The Tab Bar contains the Tab Scroller and Tab components.

Design & API Documentation

  • Material Design guidelines: Tabs
  • Demo

Installation

npm install @material/tab-bar

Basic Usage

HTML Structure

<div class="mdc-tab-bar" role="tablist">
  <div class="mdc-tab-scroller">
    <div class="mdc-tab-scroller__scroll-area">
      <div class="mdc-tab-scroller__scroll-content">
        <button class="mdc-tab mdc-tab--active" role="tab" aria-selected="true" tabindex="0">
          <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>
      </div>
    </div>
  </div>
</div>

Styles

@use "@material/tab-bar/mdc-tab-bar";
@use "@material/tab-scroller/mdc-tab-scroller";
@use "@material/tab-indicator/mdc-tab-indicator";
@use "@material/tab/mdc-tab";

JavaScript Instantiation

import {MDCTabBar} from '@material/tab-bar';

const tabBar = new MDCTabBar(document.querySelector('.mdc-tab-bar'));

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

Variants

MDC Tab Bar does not have any variants; however, its subcomponents do. See the Tab Scroller, Tab, and Tab Indicator documentation for more information.

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-tab-barMandatory.

Sass Mixins

To customize the width of the tab bar, use the following mixin.

MixinDescription
width($width)Customizes the width of the tab bar.
density($density-scale)Sets density scale to default tab bar variant. Use stacked-density mixin for stacked variant. Supported density scales -4, -3, -2, -1 and 0.
stacked-density($density-scale)Sets density scale to stacked tab bar variant. Supported density scales -4, -3, -2, -1 and 0.
tab-scroller-transition($duration-ms, $timing-function)Sets the CSS transition for the tab scrolling animation. This mixin is a proxy to mdc-tab-scroller-transition mixin.

MDCTabBar Properties and Methods

PropertyValue TypeDescription
focusOnActivateboolean (write-only)Sets whether tabs focus themselves when activated. Defaults to true.
useAutomaticActivationboolean (write-only)Sets how tabs activate in response to keyboard interaction. Automatic (true) activates as soon as a tab is focused with arrow keys; manual (false) activates only when the user presses space/enter. The default is automatic (true).
Method SignatureDescription
activateTab(index: number) => voidActivates the tab at the given index.
scrollIntoView(index: number) => voidScrolls the tab at the given index into view.
Event NameEvent Data StructureDescription
MDCTabBar:activated{"detail": {"index": number}}Emitted when a Tab is activated with the index of the activated Tab. Listen for this to update content when a Tab becomes active.

Usage within Web Frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Tab Bar 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.

MDCTabBarAdapter

Method SignatureDescription
scrollTo(scrollX: number) => voidScrolls the Tab Scroller to the given position.
incrementScroll(scrollXIncrement: number) => voidIncrements the Tab Scroller by the given value.
getScrollPosition() => numberReturns the scroll position of the Tab Scroller.
getScrollContentWidth() => numberReturns the width of the Tab Scroller's scroll content element.
getOffsetWidth() => numberReturns the offsetWidth of the root element.
isRTL() => booleanReturns if the text direction is RTL.
setActiveTab(index: number) => voidSets the tab at the given index to be activated.
activateTabAtIndex(index: number, clientRect: ClientRect) => voidActivates the Tab at the given index with the given clientRect.
deactivateTabAtIndex(index) => voidDeactivates the Tab at the given index.
focusTabAtIndex(index: number) => voidFocuses the Tab at the given index.
getTabIndicatorClientRectAtIndex(index: number) => ClientRectReturns the client rect of the Tab at the given index.
getTabDimensionsAtIndex(index) => MDCTabDimensionsReturns the dimensions of the Tab at the given index.
getTabListLength() => numberReturns the number of child Tab components.
getPreviousActiveTabIndex() => numberReturns the index of the previously active Tab.
getFocusedTabIndex() => numberReturns the index of the focused Tab.
getIndexOfTabById(id: string) => numberReturns the index of the given Tab ID.
notifyTabActivated(index: number) => voidEmits the MDCTabBar:activated event.

MDCTabBarFoundation

Method SignatureDescription
activateTab(index: number) => voidActivates the tab at the given index.
setUseAutomaticActivation(useAutomaticActivation: boolean) => voidSets how tabs activate in response to keyboard interaction. Automatic (true) activates as soon as a tab is focused with arrow keys; manual (false) activates only when the user presses space/enter.
handleKeyDown(evt: Event) => voidHandles the logic for the "keydown" event.
handleTabInteraction(evt: Event) => voidHandles the logic for the "MDCTab:interacted" event.
scrollIntoView(index: number) => voidScrolls the Tab at the given index into view.

Keywords

FAQs

Last updated on 03 Aug 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc