14.0.0 • Published 5 months ago

@material/snackbar v14.0.0

Weekly downloads
44,398
License
MIT
Repository
github
Last release
5 months ago

Snackbars

The MDC Snackbar component is a spec-aligned snackbar/toast component adhering to the Material Design snackbars & toasts requirements. It requires JavaScript to show and hide itself.

Design & API Documentation

Installation

npm install @material/snackbar

Usage

Snackbar DOM

<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>

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>

Using the JS Component

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

Including in code

ES2015
import {MDCSnackbar, MDCSnackbarFoundation} from '@material/snackbar';
CommonJS
const mdcSnackbar = require('mdc-snackbar');
const MDCSnackbar = mdcSnackbar.MDCSnackbar;
const MDCSnackbarFoundation = mdcSnackbar.MDCSnackbarFoundation;
AMD
require(['path/to/mdc-snackbar'], mdcSnackbar => {
  const MDCSnackbar = mdcSnackbar.MDCSnackbar;
  const MDCSnackbarFoundation = mdcSnackbar.MDCSnackbarFoundation;
});
Global
const MDCSnackbar = mdc.snackbar.MDCSnackbar;
const MDCSnackbarFoundation = mdc.snackbar.MDCSnackbarFoundation;

Automatic Instantiation

If you do not care about retaining the component instance for the snackbar, simply call attachTo() and pass it a DOM element.

mdc.snackbar.MDCSnackbar.attachTo(document.querySelector('.mdc-snackbar'));

Manual Instantiation

Snackbars can easily be initialized using their default constructors as well, similar to attachTo.

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

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

Handling events

When snackbar is shown or dismissed, the component will emit a MDCSnackbar:show or MDCSnackbar:hide custom event with no data attached.

Showing a message and action

Once you have obtained an MDCSnackbar instance attached to the DOM, you can use the show method to trigger the display of a message with optional action. The show method takes an object for snackbar data. The table below shows the properties and their usage.

PropertyEffectRemarksType
messageThe text message to display.RequiredString
timeoutThe amount of time in milliseconds to show the snackbar.Optional (default 2750)Integer
actionHandlerThe function to execute when the action is clicked.OptionalFunction
actionTextThe text to display for the action button.Required if actionHandler is setString
multilineWhether to show the snackbar with space for multiple lines of textOptionalBoolean
actionOnBottomWhether to show the action below the multiple lines of textOptional, applies when multiline is trueBoolean

Responding to a Snackbar Action

To respond to a snackbar action, assign a function to the optional actionHandler property in the object that gets passed to the show method. If you choose to set this property, you must also set the actionText property.

<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>
import {MDCSnackbar} from '@material/snackbar';

const snackbar = new MDCSnackbar(document.querySelector('.mdc-snackbar'));
const dataObj = {
  message: messageInput.value,
  actionText: 'Undo',
  actionHandler: function () {
    console.log('my cool function');
  }
};

snackbar.show(dataObj);

Keep snackbar when the action button is pressed

By default the snackbar will be dimissed when the user presses the action button. If you want the snackbar to remain visible until the timeout is reached (regardless of whether the user pressed the action button or not) you can set the dismissesOnAction property to false:

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

Using the Foundation Class

MDC Snackbar ships with an MDCSnackbarFoundation class that external frameworks and libraries can use to integrate the component. As with all foundation classes, an adapter object must be provided. The adapter for snackbars must provide the following functions, with correct signatures:

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

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.

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.1.1

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

6 years ago

0.40.0

6 years ago

0.39.1

6 years ago

0.39.0

6 years ago

0.39.0-0

6 years ago

0.38.0

6 years ago

0.37.1

6 years ago

0.36.0

6 years ago

0.35.0

6 years ago

0.34.0

6 years ago

0.33.0

6 years ago

0.30.0

6 years ago

0.29.0

6 years ago

0.28.0

6 years ago

0.27.0

6 years ago

0.25.0

6 years ago

0.24.0

6 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago