1

I'm using Material Design's Snackbar and VueJS and I'd like to be able to detect when the snackbar has closed.

Snackbar has a built in property called isOpen, so I'm able to use return this.snackbar.isOpen; as a boolean. However, there isn't a similar isClosed property, and when the snackbar does close, isOpen remains true.

It seems that isOpen is not reactive to closing, just to opening. But I want something to trigger when the snackbar is closes after having been open. Would appreciate any advice

I init my snackbar like so:

this.snackbar = new MDCSnackbar(document.querySelectorAll(".mdc-snackbar"));
James Stewart
  • 869
  • 12
  • 33

2 Answers2

3

I solved it:

const snackbarElement = document.querySelector(".mdc-snackbar");
this.snackbar = new MDCSnackbar(snackbarElement);

snackbarElement.addEventListener("MDCSnackbar:closed", (event) => {

Previously, I was trying to add the eventListener to the snackbar itself

James Stewart
  • 869
  • 12
  • 33
0

You should be able to listen to the MDCSnackbar:closed event, documented in the events section of the documentation.

Since you tagged your question with vue.js, I assume you are integrating MDC into a Vue app. In which case I would suggest you have a look at vue-mdc-adapter. They have more Vue-specific documentation for a snackbar as well. Your snackbar might then be something like:

<mdc-snackbar v-model="snack" @hide="handleHide"></mdc-snackbar>

@hide – notify listeners that the snackbar has been hidden.

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34