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. Snackbars are non-blocking and allow users to continue interacting with the application while the message is displayed.
✅ 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
First, add the snackbar container to your page (preferably in a root component):
<dap-ds-snackbar></dap-ds-snackbar>
Then use the global showDapSnackbar
function or static methods to display messages:
// Basic usage
showDapSnackbar('Your changes have been saved!')
// With options
showDapSnackbar('Failed to save document', {
alertType: 'error',
duration: 8000,
actions: [{ text: 'Retry', func: () => retryAction() }]
})
Different alert types provide visual context for the message content:
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' })`}
Snackbars can include action buttons for quick user interactions:
Control message timing and screen position:
Configure the snackbar container behavior:
The snackbar component supports extensive customization through CSS custom properties and parts. This section demonstrates practical styling techniques and advanced customization patterns.
Use the customStyle
option to apply custom CSS properties directly to individual snackbar messages:
Create unique entrance and exit animations:
/* Slide and scale entrance */
.slide-scale-snackbar {
--dds-snackbar-animation-duration: 0.6s;
--dds-snackbar-animation-timing: cubic-bezier(0.34, 1.56, 0.64, 1);
}
.slide-scale-snackbar::part(host) {
animation: slide-scale-in var(--dds-snackbar-animation-duration) var(--dds-snackbar-animation-timing);
}
@keyframes slide-scale-in {
0% {
transform: translateY(100%) scale(0.8);
opacity: 0;
}
100% {
transform: translateY(0) scale(1);
opacity: 1;
}
}
/* Bounce entrance */
.bounce-snackbar {
--dds-snackbar-animation-duration: 0.8s;
--dds-snackbar-animation-timing: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.bounce-snackbar::part(host) {
animation: bounce-in var(--dds-snackbar-animation-duration) var(--dds-snackbar-animation-timing);
}
@keyframes bounce-in {
0% {
transform: translateY(100%) scale(0.3);
opacity: 0;
}
50% {
transform: translateY(-20px) scale(1.05);
}
70% {
transform: translateY(10px) scale(0.95);
}
100% {
transform: translateY(0) scale(1);
opacity: 1;
}
}
/* Rotate and fade entrance */
.rotate-fade-snackbar {
--dds-snackbar-animation-duration: 0.5s;
--dds-snackbar-animation-timing: ease-out;
}
.rotate-fade-snackbar::part(host) {
animation: rotate-fade-in var(--dds-snackbar-animation-duration) var(--dds-snackbar-animation-timing);
}
@keyframes rotate-fade-in {
0% {
transform: translateY(50px) rotate(-10deg);
opacity: 0;
}
100% {
transform: translateY(0) rotate(0deg);
opacity: 1;
}
}
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()
- 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
, andaria-label
attributes
- Escape: Closes the most recent snackbar message
- Tab: Navigates to action buttons within messages
- Enter/Space: Activates action buttons
- 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
import { DapDSSnackbar } from 'dap-design-system/dist/dds'
import { DapDSSnackbarReact } from 'dap-design-system/dist/react'
Property | Type | Default | Description |
---|---|---|---|
maxItems | number | 4 | The 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 |
duration | number | 6000 | The duration of the snackbar message in milliseconds |
announceMessages | boolean | true | Whether the snackbar should announce new messages to screen readers |
No slots available.
No custom events available.
Part Name | Description |
---|---|
host | The host element |
Property Name | Description |
---|---|
--dds-snackbar-spacing | Spacing used for positioning the snackbar (default: var(--dds-spacing-400)) |
--dds-snackbar-z-index | Z-index of the snackbar container (default: 10000) |
--dds-snackbar-pointer-events | Pointer events behavior for the snackbar (default: auto) |
--dds-snackbar-animation-duration | Duration of entrance animations (default: 0.3s) |
--dds-snackbar-animation-timing | Timing function for animations (default: ease-out) |
Property | Type | Default | Description |
---|---|---|---|
actions | ActionType[] , undefined | [] | The actions of the snackbar item. |
message | string | '' | The message of the snackbar item. |
closeButton | string | 'true' | Whether the snackbar has a close button. |
alertType | AlertType | 'default' | The message type of the snackbar item. |
customStyle | Record<string,string> , undefined | The custom style of the snackbar item. | |
position | SnackbarPosition | 'bottom-right' | The position of the snackbar container for directional animations. |
Name | Description |
---|---|
default | The content of the snackbar. |
Event Name | Description | Type |
---|---|---|
dds-close | Fires when the snackbar closes. | {id: string } |
Part Name | Description |
---|---|
base | Main snackbar container. |
text | The text part of the snackbar. |
closebutton | The close button part of the component. |
icon | The icon part of the close button. |
Property Name | Description |
---|---|
--dds-snackbar-width | Width of the snackbar. |
--dds-snackbar-gap | Gap between snackbar elements. |
--dds-snackbar-padding | Padding inside the snackbar. |
--dds-snackbar-margin-bottom | Bottom margin of the snackbar. |
--dds-snackbar-transition | Transition property for the snackbar. |
--dds-snackbar-border-width | Border width of the snackbar. |
--dds-snackbar-border-radius | Border radius of the snackbar. |
--dds-snackbar-font-size | Font size used in the snackbar. |
--dds-snackbar-background | Background color of the snackbar. |
--dds-snackbar-border | Border color of the snackbar. |
--dds-snackbar-text | Text color of the snackbar. |
--dds-snackbar-icon | Icon color of the snackbar. |
--dds-snackbar-link | Link color of the snackbar. |
--dds-snackbar-information-background | Background color for information variant. |
--dds-snackbar-information-border | Border color for information variant. |
--dds-snackbar-information-icon | Icon color for information variant. |
--dds-snackbar-success-background | Background color for success variant. |
--dds-snackbar-success-border | Border color for success variant. |
--dds-snackbar-success-icon | Icon color for success variant. |
--dds-snackbar-error-background | Background color for error variant. |
--dds-snackbar-error-border | Border color for error variant. |
--dds-snackbar-error-icon | Icon color for error variant. |
--dds-snackbar-variant-text | Text color for information, success, and error variants. |
--dds-snackbar-variant-icon | Icon color for information, success, and error variants. |
--dds-snackbar-variant-link | Link color for information, success, and error variants. |