10

How do I use MDCSnackbarFoundation ? with MDCSnackbar

This documentation is not clear enough to get an idea. https://github.com/material-components/material-components-web/tree/master/packages/mdc-snackbar#using-the-foundation-class

This is my code and I need to bind MDCSnackbarFoundation for it.

logger = new MDCSnackbar($selector[0]);

Thanks

benvc
  • 14,448
  • 4
  • 33
  • 54
Dhanan
  • 207
  • 4
  • 12

1 Answers1

1

At first you have to have a Node.js server. And then you have to install a package snackbar for Node.js like follows:

npm install @material/snackbar

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: 'The text message to display.',
    actionText: 'Take action',
    //The function to execute when the action is clicked.
    actionHandler: function()
    {
        console.log('my cool function');
    }
};
​
snackbar.show(dataObj);

For further information:

Before you start with Node.js I would recommend to read one from two books:

Bharata
  • 13,509
  • 6
  • 36
  • 50
  • @benvc, would you like to check it befor you think. You can see it in [your example in your comment above](https://plnkr.co/edit/b4v160c186ErrPG5vNza?p=preview) – your example does not work. – Bharata Jul 30 '18 at 09:52