New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@material/snackbar

Package Overview
Dependencies
Maintainers
13
Versions
1684
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@material/snackbar

The Material Components for the web snackbar component

  • 0.42.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
13
Created
Source

Snackbars

Snackbars provide brief messages about app processes at the bottom of the screen.

Design & API Documentation

  • Material Design guidelines: Snackbars & toasts
  • Demo

Installation

npm install @material/snackbar

Basic Usage

HTML Structure

<div class="mdc-snackbar"
     aria-live="assertive"
     aria-atomic="true"
     aria-hidden="true">
  <div class="mdc-snackbar__text"></div>
  <div class="mdc-snackbar__action-wrapper">
    <button type="button" class="mdc-snackbar__action-button"></button>
  </div>
</div>

Styles

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

JavaScript Instantiation

MDC Snackbar ships with a Component / Foundation combo which provides the API for showing snackbar messages with optional action.

import {MDCSnackbar} from '@material/snackbar';

const snackbar = new MDCSnackbar(document.querySelector('.mdc-snackbar'));

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

Variants

Start Aligned Snackbars (tablet and desktop only)

MDC Snackbar can be start aligned (including in RTL contexts). To create a start-aligned snackbar, add the mdc-snackbar--align-start modifier class to the root element.

<div class="mdc-snackbar mdc-snackbar--align-start"
     aria-live="assertive"
     aria-atomic="true"
     aria-hidden="true">
  <div class="mdc-snackbar__text"></div>
  <div class="mdc-snackbar__action-wrapper">
    <button type="button" class="mdc-snackbar__action-button"></button>
  </div>
</div>

Additional Information

Avoiding Flash-Of-Unstyled-Content (FOUC)

If you are loading the mdc-snackbar CSS asynchronously, you may experience a brief flash-of-unstyled-content (FOUC) due to the snackbar's translate transition running once the CSS loads. To avoid this temporary FOUC, you can add the following simple style before the mdc-snackbar CSS is loaded:

.mdc-snackbar { transform: translateY(100%); }

This will move the snackbar offscreen until the CSS is fully loaded and avoids a translate transition upon load.

Style Customization

CSS Classes

CSS ClassDescription
mdc-snackbarMandatory. Container for the snackbar elements.
mdc-snackbar__action-wrapperMandatory. Wraps the action button.
mdc-snackbar__action-buttonMandatory. The action button.
mdc-snackbar__textMandtory. The next of the snackbar.
mdc-snackbar--align-startOptional. Class to align snackbar to start, ltr dependent.
mdc-snackbar--action-on-bottomOptional on the mdc-snackbar element. Moves action to bottom of snackbar. Can be applied in js.
mdc-snackbar--multilineOptional on the mdc-snackbar element. Makes the snackbar multiple lines. Can be applied in js.

MDCSnackbar Properties and Methods

PropertyValue TypeDescription
dismissesOnActionbooleanWhether the snackbar dismisses when the action is clicked, or if it waits for the timeout anyway. Defaults to true.
Method SignatureDescription
show(data: DataObject=) => voidDisplays the snackbar. data populates the snackbar and sets options (see below).

DataObject Properties

PropertyTypeDescription
messagestringMandatory. The text message to display.
timeoutnumberThe amount of time in milliseconds to show the snackbar. Defaults to 2750.
actionHandlerfunctionThe function to execute when the action is clicked.
actionTextstringMandatory if actionHandler is set. The text to display for the action button.
multilinebooleanWhether to show the snackbar with space for multiple lines of text.
actionOnBottombooleanWhether to show the action below the multiple lines of text (only applicable when multiline is true).

Events

Event Nameevent.detailDescription
MDCSnackbar:hide{}Emitted when the Snackbar is hidden.
MDCSnackbar:show{}Emitted when the Snackbar is shown.

Usage Within Frameworks

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

MDCSnackbarAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the root element.
removeClass(className: string) => voidRemoves a class from the root element.
setAriaHidden() => voidSets aria-hidden="true" on the root element.
unsetAriaHidden() => voidRemoves the aria-hidden attribute from the root element.
setActionAriaHidden() => voidSets aria-hidden="true" on the action element.
unsetActionAriaHidden() => voidRemoves the aria-hidden attribute from the action element.
setActionText(actionText: string) => voidSet the text content of the action element.
setMessageText(message: string) => voidSet the text content of the message element.
setFocus() => voidSets focus on the action button.
isFocused() => booleanDetects focus on the action button.
visibilityIsHidden() => booleanReturns document.hidden property.
registerBlurHandler(handler: EventListener) => voidRegisters an event handler to be called when a blur event is triggered on the action button.
deregisterBlurHandler(handler: EventListener) => voidDeregisters a blur event handler from the actionButton.
registerVisibilityChangeHandler(handler: EventListener) => voidRegisters an event handler to be called when a 'visibilitychange' event occurs.
deregisterVisibilityChangeHandler(handler: EventListener) => voidDeregisters an event handler to be called when a 'visibilitychange' event occurs.
registerCapturedInteractionHandler(evtType: string, handler: EventListener) => voidRegisters an event handler to be called when the given event type is triggered on the body.
deregisterCapturedInteractionHandler(evtType: string, handler: EventListener) => voidDeregisters an event handler from the body.
registerActionClickHandler(handler: EventListener) => voidRegisters an event handler to be called when a click event is triggered on the action element.
deregisterActionClickHandler(handler: EventListener) => voidDeregisters an event handler from a click event on the action element. This will only be called with handlers that have previously been passed to registerActionClickHandler calls.
registerTransitionEndHandler(handler: EventListener) => voidRegisters an event handler to be called when an transitionend event is triggered on the root element. Note that you must account for vendor prefixes in order for this to work correctly.
deregisterTransitionEndHandler(handler: EventListener) => voidDeregisters an event handler from an transitionend event listener. This will only be called with handlers that have previously been passed to registerTransitionEndHandler calls.
notifyShow() => voidDispatches an event notifying listeners that the snackbar has been shown.
notifyHide() => voidDispatches an event notifying listeners that the snackbar has been hidden.

Keywords

FAQs

Package last updated on 04 Dec 2018

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