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

@material/top-app-bar

Package Overview
Dependencies
Maintainers
15
Versions
1670
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@material/top-app-bar

The Material Components for the web top app bar component

  • 14.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
15
Created
Source

Top app bar

The top app bar provides content and actions related to the current screen. It’s used for branding, screen titles, navigation, and actions.

There are two types of top app bar:

  1. Regular top app bar
  2. Contextual action bar

A regular top app bar can transform into a contextual action bar.

Regular app bar: purple background, white text and icons Contextual app bar: black background, white text and icons

Using the top app bar

Installation

npm install @material/top-app-bar

Styles

@use "@material/icon-button";
@use "@material/top-app-bar/mdc-top-app-bar";

@include icon-button.core-styles;

JavaScript Instantiation

import {MDCTopAppBar} from '@material/top-app-bar';

// Instantiation
const topAppBarElement = document.querySelector('.mdc-top-app-bar');
const topAppBar = new MDCTopAppBar(topAppBarElement);

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

Regular top app bar

The top app bar provides content and actions related to the current screen. It’s used for branding, screen titles, navigation, and actions.

Regular top app bar example

<header class="mdc-top-app-bar">
  <div class="mdc-top-app-bar__row">
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
      <button class="material-icons mdc-top-app-bar__navigation-icon mdc-icon-button" aria-label="Open navigation menu">menu</button>
      <span class="mdc-top-app-bar__title">Page title</span>
    </section>
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Favorite">favorite</button>
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Search">search</button>
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Options">more_vert</button>
    </section>
  </div>
</header>
<main class="mdc-top-app-bar--fixed-adjust">
  App content
</main>
Menu icons

Top app bars can contain action items which are placed on the side opposite the navigation icon. You must also attach the mdc-icon-button class to both the mdc-top-app-bar__navigation-icon and the mdc-top-app-bar__action-item elements in order to get the correct styles applied.

For further documentation on icons, please see the mdc-icon-button docs.

Contextual action bar

A top app bar can transform into a contextual action bar to provide contextual actions to selected items. For example, upon user selection of photos from a gallery, the top app bar transforms to a contextual app bar with actions related to the selected photos.

When a top app bar transforms into a contextual action bar, the following changes occur:

  • The bar color changes
  • Navigation icon is replaced with a close icon
  • Top app bar title text converts to contextual action bar text
  • Top app bar actions are replaced with contextual actions
  • Upon closing, the contextual action bar transforms back into a top app bar.

Contextual action bar example

The following example shows a contextual action bar with a contextual title, a close icon, two contextual action icons, and an overflow menu:

<header class="mdc-top-app-bar">
  <div class="mdc-top-app-bar__row">
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
      <button class="material-icons mdc-top-app-bar__navigation-icon mdc-icon-button" aria-label="Close">close</button>
      <span class="mdc-top-app-bar__title">Contextual title</span>
    </section>
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Share">share</button>
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Delete">delete</button>
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Open menu">more_vert</button>
    </section>
  </div>
</header>
<main class="mdc-top-app-bar--fixed-adjust">
  App content
</main>

Other Variants

Short

Short top app bars are top app bars that can collapse to the navigation icon side when scrolled.

<header class="mdc-top-app-bar mdc-top-app-bar--short">
  <div class="mdc-top-app-bar__row">
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
      <button class="material-icons mdc-top-app-bar__navigation-icon mdc-icon-button">menu</button>
      <span class="mdc-top-app-bar__title">Title</span>
    </section>
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
      <button class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Bookmark this page">bookmark</button>
    </section>
  </div>
</header>
<main class="mdc-top-app-bar--short-fixed-adjust">
  App content
</main>

Note: Short top app bars should be used with no more than 1 action item.

Short - always closed

Short top app bars can be configured to always appear collapsed by applying the mdc-top-app-bar--short-collapsed before instantiating the component :

<header class="mdc-top-app-bar mdc-top-app-bar--short mdc-top-app-bar--short-collapsed">
  ...
</header>
<main class="mdc-top-app-bar--short-fixed-adjust">
  App content
</main>

Fixed

Fixed top app bars stay at the top of the page and elevate above the content when scrolled.

<header class="mdc-top-app-bar mdc-top-app-bar--fixed">
  ...
</header>
<main class="mdc-top-app-bar--fixed-adjust">
  App content
</main>

Prominent

The prominent top app bar is taller.

<header class="mdc-top-app-bar mdc-top-app-bar--prominent">
  ...
</header>
<main class="mdc-top-app-bar--prominent-fixed-adjust">
  App content
</main>

Dense

The dense top app bar is shorter.

<header class="mdc-top-app-bar mdc-top-app-bar--dense">
  ...
</header>
<main class="mdc-top-app-bar--dense-fixed-adjust">
  App content
</main>

Style customization

CSS classes

ClassDescription
mdc-top-app-barMandatory.
mdc-top-app-bar--fixedClass used to style the top app bar as a fixed top app bar.
mdc-top-app-bar--fixed-adjustClass used to style the content below the top app bar to prevent the top app bar from covering it.
mdc-top-app-bar--prominentClass used to style the top app bar as a prominent top app bar.
mdc-top-app-bar--prominent-fixed-adjustClass used to style the content below the prominent top app bar to prevent the top app bar from covering it.
mdc-top-app-bar--denseClass used to style the top app bar as a dense top app bar.
mdc-top-app-bar--dense-fixed-adjustClass used to style the content below the dense top app bar to prevent the top app bar from covering it.
mdc-top-app-bar--dense-prominent-fixed-adjustClass used to style the content below the top app bar when styled as both dense and prominent, to prevent the top app bar from covering it.
mdc-top-app-bar--shortClass used to style the top app bar as a short top app bar.
mdc-top-app-bar--short-collapsedClass used to indicate the short top app bar is collapsed.
mdc-top-app-bar--short-fixed-adjustClass used to style the content below the short top app bar to prevent the top app bar from covering it.

Sass mixins

MixinDescription
ink-color($color)Sets the ink color of the top app bar.
icon-ink-color($color)Sets the ink color of the top app bar icons.
fill-color($color)Sets the fill color of the top app bar.
fill-color-accessible($color)Sets the fill color of the top app bar and automatically sets a high-contrast ink color.
short-shape-radius($radius, $rtl-reflexive)Sets the rounded shape to short top app bar variant (when it is collapsed) with given radius size. Set $rtl-reflexive to true to flip radius values in RTL context, defaults to true.

MDCTopAppBar properties and methods

Method SignatureDescription
setScrollTarget(target: element) => voidSets scroll target to different DOM node (default is window).

Events

Event NameEvent Data StructureDescription
MDCTopAppBar:navNoneEmits when the navigation icon is clicked.

Usage within web frameworks

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

MDCTopAppBarAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the root element of the component.
removeClass(className: string) => voidRemoves a class from the root element of the component.
hasClass(className: string) => booleanChecks if the root element of the component has the given className.
setStyle(property: string, value: string) => voidSets the specified CSS property to the given value on the root element.
getTopAppBarHeight() => numberGets the height in px of the top app bar.
getViewportScrollY() => numberGets the number of pixels that the content of body is scrolled from the top of the page.
getTotalActionItems() => numberGets the number of action items in the top app bar.
notifyNavigationIconClicked() => voidEmits a custom event MDCTopAppBar:nav when the navigation icon is clicked.

Foundations

MDCTopAppBarBaseFoundation, MDCTopAppBarFoundation, MDCFixedTopAppBarFoundation and MDCShortTopAppBarFoundation

All foundations provide the following methods:

Method SignatureDescription
handleTargetScroll() => voidHandles scroll event on specified scrollTarget (defaults to window).
handleWindowResize() => voidHandles resize event on window.
handleNavigationClick() => voidHandles click event on navigation icon.
MDCShortTopAppBarFoundation

In addition to the methods above, the short variant provides the following public methods and properties:

Method SignatureDescription
setAlwaysCollapsed(value: boolean) => voidWhen value is true, sets the short top app bar to always be collapsed.
getAlwaysCollapsed() => booleanGets if the short top app bar is in the "always collapsed" state.
PropertyValue TypeDescription
isCollapsedboolean (read-only)Indicates whether the short top app bar is in the collapsed state.

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