<h1>Material Snackbar Example using Alpine.Js</h1>
<button x-data="buttonShowSnackbar()" @click="showAlpineSnackbar('alpine-snack-bar')" class="button">Show Snackbar</button>
<h2>Click on the above button to see snackbar message</h2>
<div x-id="alpine-snack-bar" x-data="{ show : false, message: null }" x-show="show" class="alpine-snackbar-wrapper">
  <div class="alpine-snackbar-content" x-text="message"></div>
</div>
body {
  font-family: "Roboto", sans-serif;
}

.alpine-snackbar-wrapper {
  min-width: 344px;
  max-width: 672px;
  min-height: 48px;
  background-color: #2196f3;
  color: #fff;
  text-align: center;
  margin: auto 8px;
  display: flex;
  align-items: center;
  padding: 0;
  border-radius: 4px;
  position: fixed;
  right: 1%;
  z-index: 1;
  bottom: 30px;
  box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.2),
    0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);
}

.alpine-snackbar-content {
  flex-grow: 1;
  font-size: 0.875em;
  font-weight: 400;
  padding: 14px 16px;
}

.button {
  border: none;
  padding: 14px 16px;
  border-radius: 4px;
  font-size: 1em;
  background-color: #2196f3;
  color: white;
  cursor: pointer;
}
function buttonShowSnackbar() {
  return {
    showAlpineSnackbar(id) {
      this.$component(id).message =
        "Hello from another component using the $component";
      this.$component(id).show = true;
      setTimeout(() => {
        this.$component(id).show = false;
        this.$component(id).message = null;
      }, 2000);
    }
  };
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdn.jsdelivr.net/gh/alpine-collective/alpine-magic-helpers@0.5.x/dist/component.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/alpinejs/2.8.0/alpine.js