0.15.0 • Published 5 years ago

@material/react-snackbar v0.15.0

Weekly downloads
2,850
License
MIT
Repository
github
Last release
5 years ago

React Snackbar

A React version of an MDC Snackbar.

Installation

npm install @material/react-snackbar

Usage

Styles

with Sass:

import '@material/react-snackbar/index.scss';

with CSS:

import '@material/react-snackbar/dist/snackbar.css';

Javascript Instantiation

import React from 'react';
import {Snackbar} from '@material/react-snackbar';

class MyApp extends React.Component {
  render() {
    return (
      <Snackbar message="Click Me!" actionText="dismiss" />
    );
  }
}

Props

Prop NameTypeDescription
messageStringMessage to show in the snackbar
reasonStringPassed as argument when snackbar closes programatically
classNameStringClasses to be applied to the root element.
timeoutMsNumberTimeout in milliseconds when to close snackbar.
closeOnEscapeBooleanCloses popup on "Esc" button if true.
actionTextStringText for action button
leadingBooleanShows snackbar on the left if true (or right for rtl languages)
stackedBooleanShows buttons under text if true
onAnnounceFunction() => voidCallback for handling screenreader announce event
onOpeningFunction() => voidCallback for handling event, which happens before opening
onOpenFunction(evt: Event) => voidCallback for handling event, which happens after opening
onClosingFunction() => voidCallback for handling event, which happens before closing
onCloseFunction() => voidCallback for handling event, which happens after closing

Getting snackbar parameters

If you need to get the timeoutMs, closeOnEscape, or open value, then you can use a ref like so:

import React from 'react';
import {Snackbar} from '@material/react-snackbar';
 class MyApp extends React.Component {
  getSnackbarInfo = (snackbar) => {
    if (!snackbar) return;
    console.log(snackbar.getTimeoutMs());
    console.log(snackbar.isOpen());
    console.log(snackbar.getCloseOnEscape());
  }
  render() {
    return (
      <Snackbar
        message="Click Me!"
        actionText="dismiss"
        ref={this.getSnackbarInfo}
      />
    );
  }
}

Sass Mixins

Sass mixins may be available to customize various aspects of the Components. Please refer to the MDC Web repository for more information on what mixins are available, and how to use them.

Advanced Sass Mixins

Usage with Icons

Please see our Best Practices doc when importing or using icon fonts.