Snackbar Overview

The Snackbar component displays temporary messages that slide in from the bottom of the screen. These messages provide feedback for user actions and automatically disappear after a set duration (or stay until manually closed when marked as persistent). Snackbars are non-blocking and allow users to continue interacting with the application while the message is displayed.

When to Use

Use snackbars for:

  • Confirming successful actions (save, delete, update)
  • Displaying non-critical error messages
  • Providing brief feedback that doesn't require user action
  • Showing undo actions for reversible operations

Don't use snackbars for:

  • Critical error messages that require immediate attention
  • Long or complex messages
  • Information that users need to reference later
  • Actions that require user input or decision-making
Setup 1. Add the Snackbar Container

First, add the snackbar container to your page (preferably in a root component):

<dap-ds-snackbar></dap-ds-snackbar>
2. Configure TypeScript and ESLint

To use the global showDapSnackbar function without type errors or linting issues, you need to configure both TypeScript and ESLint:

TypeScript Global Declaration

Add the function signature to your global.d.ts file (or create one in your project root):

declare function showDapSnackbar(
  message: string,
  options?: {
    alertType?: 'default' | 'successful' | 'error' | 'information'
    duration?: number
    persistent?: boolean
    actions?: Array<{
      text: string
      func?: () => void
      href?: string
      target?: string
      variant?: string
    }>
    closeButton?: string
    customStyle?: Record<string, string>
  },
): void
ESLint Configuration

Add showDapSnackbar to your ESLint globals to prevent "is not defined" errors:

{
  "root": true,
  "globals": {
    "showDapSnackbar": "readonly"
  },
  "extends": [
    "next/core-web-vitals",
    "eslint:recommended"
  ]
}
3. Use the Function

Now you can use the global showDapSnackbar function throughout your application:

// Basic usage
showDapSnackbar('Your changes have been saved!')

// With options
showDapSnackbar('Failed to save document', {
  alertType: 'error',
  duration: 8000,
  actions: [{ text: 'Retry', func: () => retryAction() }]
})
Examples Message Types

Different alert types provide visual context for the message content:

Message typesSuccess MessageError MessageInfo MessageDefault Message
Static Helper Methods

The component provides convenient static methods for common message types:


import { DapDSSnackbar } from 'dap-design-system'

// Use static methods from anywhere in your React app
DapDSSnackbar.success('Operation completed!')
DapDSSnackbar.error('Something failed!')
DapDSSnackbar.info('Important information')
DapDSSnackbar.closeAll() // Close all messages

// Or create an alias for convenience
const snackbar = DapDSSnackbar
snackbar.success('Much cleaner!')

// Legacy component method (still works)
const component = document.querySelector('dap-ds-snackbar')
component.addMessage('Hello', { alertType: 'success' })`}

Actions & Interactions

Snackbars can include action buttons for quick user interactions:

With action buttonsUndo ActionLink Action
Multiple actionsMultiple Actions
Duration & Positioning

Control message timing and screen position. Set persistent: true to disable auto-dismiss entirely — the message stays until the user closes it manually (via the close button or the Escape key) or closeAll() is called.

Custom durationShort DurationLong Duration
Persistent messagesPersistent Message
Close button controlNo Close ButtonWith Close Button
Container Configuration

Configure the snackbar container behavior:

Container limitsTest Max Items (2)
Global controlsShow Multiple
Real-World Patterns Form Feedback
Send MessageSimulate Error
Data Operations
Document ManagementSave DraftDelete
System Notifications
System StatusUpdate AvailableConnection RestoredBackup Complete
Custom Styling

The snackbar component supports extensive customization through CSS custom properties and parts. This section demonstrates practical styling techniques and advanced customization patterns.

Quick Customization with Inline Styles

Use the customStyle option to apply custom CSS properties directly to individual snackbar messages:

Gradient StyleElevated StyleNeon Glow
Configuration Global Methods

Static methods for programmatic control:

// Show messages with specific types
DapDSSnackbar.success('Success message')
DapDSSnackbar.error('Error message') 
DapDSSnackbar.info('Information message')
DapDSSnackbar.default('Default message')

// Show message with custom styling
showDapSnackbar('Styled message', {
  alertType: 'successful',
  customStyle: {
    '--dds-snackbar-background': 'var(--dds-brand-50)',
    '--dds-snackbar-border': '2px solid var(--dds-brand-300)',
    'box-shadow': '0 8px 24px rgba(0, 0, 0, 0.1)'
  },
  duration: 5000,
  actions: [{ text: 'Action', func: () => console.log('clicked') }]
})

// Close all messages
DapDSSnackbar.closeAll()
Accessibility Requirements
  • Screen reader support: Messages are announced when announceMessages is enabled
  • Keyboard navigation: Escape key closes the most recent message
  • Focus management: Action buttons are properly focusable
  • ARIA attributes: Container has appropriate role, aria-live, and aria-label attributes
Keyboard Navigation
  • Escape: Closes the most recent snackbar message
  • Tab: Navigates to action buttons within messages
  • Enter/Space: Activates action buttons
Screen Reader Support
  • Container uses aria-live="polite" for message announcements
  • Individual messages are announced when announceMessages is true
  • Action buttons have proper accessible names
  • Alert types provide semantic context
Accessibility examples:Descriptive SuccessError with Context
Importing
import { DapDSSnackbar } from 'dap-design-system'
Importing React
import { DapDSSnackbarReact } from 'dap-design-system/react'
Tree-Shakeable Imports

For optimal bundle sizes, use the tree-shakeable import syntax:

import { DapDSSnackbar } from 'dap-design-system/components'
Attributes
PropertyTypeDefaultDescription
maxItemsnumber4The maximum number of snackbar messages at a given time.
position'bottom-left', 'bottom-right' , 'bottom-center' , 'top-right' , 'top-center''bottom-right'The position of the snackbar
durationnumber6000The duration of the snackbar message in milliseconds
announceMessagesbooleantrueWhether the snackbar should announce new messages to screen readers
Slots

No slots available.

Events

No custom events available.

CSS Parts
Part NameDescription
hostThe host element
messageThe message part of the snackbar
message-baseThe base part of the message
message-textThe text part of the message
message-closebuttonThe close button part of the message
message-close-iconThe close icon part of the message
message-actionsThe actions part of the message
How to Use CSS Parts

You can style CSS parts using the ::part() pseudo-element selector:

/* Target a specific part */
.my-custom-dap-ds-snackbar::part(host) {
  /* Your custom styles */
}

/* Target multiple parts */
.my-custom-dap-ds-snackbar::part(host),
.my-custom-dap-ds-snackbar::part(message) {
  /* Shared styles */
}

Example usage:

<dap-ds-snackbar class="my-custom-dap-ds-snackbar">
  Snackbar
</dap-ds-snackbar>
.my-custom-dap-ds-snackbar::part(host) {
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

CSS parts allow you to style internal elements of the component while maintaining encapsulation. Learn more in our styling guide.

CSS Custom Properties
Property NameDescription
--dds-snackbar-spacingSpacing used for positioning the snackbar (default: var(--dds-spacing-400))
--dds-snackbar-z-indexZ-index of the snackbar container (default: 10000)
--dds-snackbar-pointer-eventsPointer events behavior for the snackbar (default: auto)
How to Use CSS Custom Properties

CSS custom properties (CSS variables) can be set directly on the component or in your stylesheet:

Method 1: Inline styles (Quick customization)

<dap-ds-snackbar
  style="--dds-snackbar-spacing: value; --dds-snackbar-z-index: value;">
  Snackbar
</dap-ds-snackbar>

Method 2: CSS classes (Reusable styles)

.my-custom-dap-ds-snackbar {
  --dds-snackbar-spacing: value;
  --dds-snackbar-z-index: value;
  --dds-snackbar-pointer-events: value;
}
<dap-ds-snackbar class="my-custom-dap-ds-snackbar">
  Snackbar
</dap-ds-snackbar>

Method 3: Global theme customization

/* Apply to all instances */
dap-ds-snackbar {
  --dds-snackbar-spacing: value;
  --dds-snackbar-z-index: value;
}

CSS custom properties inherit through the Shadow DOM, making them perfect for theming. Changes apply immediately without rebuilding.

Components Snackbar message <dap-ds-snackbar-message/> Attributes
PropertyTypeDefaultDescription
actionsActionType[], undefined[]The actions of the snackbar item.
messagestring''The message of the snackbar item.
closeButtonstring'true'Whether the snackbar has a close button.
alertTypeAlertType'default'The message type of the snackbar item.
customStyleRecord<string,string> , undefinedThe custom style of the snackbar item.
positionSnackbarPosition'bottom-right'The position of the snackbar container for directional animations.
Slots
NameDescription
defaultThe content of the snackbar.
Events
Event NameDescriptionType
dds-closeFires when the snackbar closes.{id: string }
CSS Parts
Part NameDescription
baseMain snackbar container.
textThe text part of the snackbar.
closebuttonThe close button part of the component.
iconThe icon part of the close button.
CSS Custom Properties
Property NameDescription
--dds-snackbar-widthWidth of the snackbar (default: clamp(var(--dds-spacing-6000), 20vw, var(--dds-containers-xsmall)))
--dds-snackbar-gapGap between snackbar elements (default: var(--dds-spacing-400))
--dds-snackbar-paddingPadding inside the snackbar (default: var(--dds-spacing-400))
--dds-snackbar-margin-bottomBottom margin of the snackbar (default: var(--dds-spacing-400))
--dds-snackbar-transitionTransition property for the snackbar (default: transform var(--dds-transition-slow) var(--dds-easing-ease-out)allow-discrete, opacity var(--dds-transition-slow) var(--dds-easing-ease-out) allow-discrete, margin var(--dds-transition-slow) var(--dds-easing-ease-out) allow-discrete, box-shadow var(--dds-transition-slow) var(--dds-easing-ease-out) allow-discrete)
--dds-snackbar-border-radiusBorder radius of the snackbar (default: var(--dds-radius-base))
--dds-snackbar-font-sizeFont size used in the snackbar (default: var(--dds-font-sm))
--dds-snackbar-backgroundBackground color of the snackbar (default: var(--dds-snackbar-error-background))
--dds-snackbar-borderBorder color of the snackbar (default: var(--dds-snackbar-error-border))
--dds-snackbar-textText color of the snackbar (default: var(--dds-snackbar-variant-text))
--dds-snackbar-iconIcon color of the snackbar (default: var(--dds-snackbar-variant-icon))
--dds-snackbar-linkLink color of the snackbar (default: var(--dds-snackbar-variant-link))
--dds-snackbar-information-backgroundBackground color for information variant (default: var(--dds-background-informative-medium))
--dds-snackbar-information-borderBorder color for information variant (default: var(--dds-border-width-base) solidvar(--dds-border-informative-subtle))
--dds-snackbar-information-iconIcon color for information variant (default: var(--dds-icon-informative-subtle))
--dds-snackbar-success-backgroundBackground color for success variant (default: var(--dds-background-positive-medium))
--dds-snackbar-success-borderBorder color for success variant (default: var(--dds-border-width-base) solidvar(--dds-border-positive-subtle))
--dds-snackbar-success-iconIcon color for success variant (default: var(--dds-icon-positive-subtle))
--dds-snackbar-error-backgroundBackground color for error variant (default: var(--dds-background-negative-medium))
--dds-snackbar-error-borderBorder color for error variant (default: var(--dds-border-width-base) solidvar(--dds-border-negative-subtle))
--dds-snackbar-error-iconIcon color for error variant (default: var(--dds-icon-negative-subtle))
--dds-snackbar-variant-textText color for information, success, and error variants (default: var(--dds-text-neutral-base))
--dds-snackbar-variant-iconIcon color for information, success, and error variants (default: var(--dds-text-neutral-base))
--dds-snackbar-variant-linkLink color for information, success, and error variants (default: var(--dds-text-neutral-base))
--dds-snackbar-animation-durationDuration of entrance animations (default: 0.3s)
--dds-snackbar-animation-timingTiming function for animations (default: ease-out)