GitHubopen_in_new

showSnackbar

showSnackbar() is the programmatic entry point for transient notifications. It renders a snackbar into the shared stack owned by the application's OverlayHost, waits for it to close, and resolves with the outcome.

Prerequisite

Render one OverlayHost near the application root before using this function. The host is only required for programmatic overlays; declarative Snackbar instances do not need it. See OverlayHost for Vue and Nuxt setup.

Basic Usage

ts
import { showSnackbar } from '@chillet/m3-vue';

await showSnackbar({
  message: 'Saved',
});

Configure Defaults

Set application-level defaults when installing M3VuePlugin. These defaults apply to future showSnackbar() calls. Refs and getters are supported, and options passed to an individual call always take precedence.

ts
import { M3VuePlugin } from '@chillet/m3-vue';

app.use(M3VuePlugin, {
  snackbarDefaults: {
    dismissible: false,
    duration: 6_000,
    position: 'bottom-end',
  },
});

Component Subtree Defaults

Call provideSnackbarDefaults() in an ancestor and capture the nearest defaults with useSnackbar() in descendant components. The returned function can be used later in event handlers. The globally imported showSnackbar() keeps using application-level defaults because it has no component injection context.

vue
<!-- NotificationArea.vue -->
<script setup lang="ts">
import { provideSnackbarDefaults } from '@chillet/m3-vue';

provideSnackbarDefaults({
  dismissible: false,
  position: 'top-end',
});
</script>

<template>
  <slot />
</template>
vue
<!-- DescendantComponent.vue -->
<script setup lang="ts">
import { useSnackbar } from '@chillet/m3-vue';

const showSnackbar = useSnackbar();

function notify() {
  void showSnackbar({ message: 'Saved' });
}
</script>

The precedence order is built-in defaults, application plugin defaults, the nearest subtree defaults, and finally the options passed to the individual call.

Return Value

The promise resolves when the snackbar closes.

  • Action click resolves to action.value, or 'action' by default.
  • Dismiss button resolves to dismissValue, or 'dismiss' by default.
  • Auto timeout resolves to timeoutValue, or 'timeout' by default.

The promise rejects if the function is called outside a browser environment, if OverlayHost is not mounted, or if action.onClick throws or rejects.

Options

The tables below are generated from source types and stay in sync with API changes.

ShowSnackbarOptions

FieldTypeRequiredDescriptionDefault
messagestring | undefinedNoSupporting text displayed inside the snackbar.-
actionSnackbarAction | undefinedNoOptional action button.-
durationnumber | null | undefinedNoMilliseconds before the snackbar closes automatically. Use `null` to disable auto close.-
timeoutValuestring | undefinedNoValue returned when the snackbar closes automatically.-
dismissValuestring | undefinedNoValue returned when the dismiss button closes the snackbar.-
positionSnackbarPosition | undefinedNoWhere the snackbar stack is placed in the viewport.-
actionOnNewLineboolean | undefinedNoWhether the action should be placed on a separate line.false
dismissibleboolean | undefinedNoWhether to show a dismiss button.false
dismissButtonLabelstring | undefinedNoAccessible label for the dismiss button."Dismiss notification"

ShowSnackbarDefaults

FieldTypeRequiredDescriptionDefault
durationnumber | null | undefinedNoMilliseconds before the snackbar closes automatically. Use `null` to disable auto close.-
timeoutValuestring | undefinedNoValue returned when the snackbar closes automatically.-
dismissValuestring | undefinedNoValue returned when the dismiss button closes the snackbar.-
positionSnackbarPosition | undefinedNoWhere the snackbar stack is placed in the viewport.-
actionOnNewLineboolean | undefinedNoWhether the action should be placed on a separate line.false
dismissibleboolean | undefinedNoWhether to show a dismiss button.false
dismissButtonLabelstring | undefinedNoAccessible label for the dismiss button."Dismiss notification"

SnackbarAction

FieldTypeRequiredDescriptionDefault
labelstringYesLabel shown in the action area.-
valuestring | undefinedNoValue returned when the action closes the snackbar.-
disabledboolean | undefinedNoWhether the action button is disabled.-
autofocusboolean | undefinedNoWhether the action button should autofocus when rendered.-
closeOnClickboolean | undefinedNoWhether clicking the action should close the snackbar.-
onClick(() => void | boolean | Promise<void | boolean>) | undefinedNoOptional callback invoked before the snackbar closes. Return `false` to keep it open.-

Behavior Notes

  • Programmatic snackbars include a dismiss button by default. Set dismissible: false to hide it.
  • Multiple snackbars can be shown at the same time.
  • Stacks are separated by position, so top and bottom placements do not interfere with each other.
  • Snackbar actions inherit application-level provides and CSS custom properties from OverlayHost.
  • Hovering or focusing the snackbar pauses the auto-close timer.
  • Leaving the snackbar resumes the remaining time instead of restarting the full duration.

For manually rendered snackbars, see Snackbar.