An elegant notification system with smooth animations, glassmorphic design, and comprehensive customization options. Perfect for displaying temporary messages to users.
- ✅ Multiple toast types (success, error, warning, info)
- ✅ Smooth slide-in/out animations with spring physics
- ✅ Glassmorphic design with backdrop blur
- ✅ Auto-dismiss with visual progress bar
- ✅ Position control (6 positions)
- ✅ Stack multiple toasts
- ✅ Programmatic API
- ✅ Customizable duration
- ✅ Manual close button
- ✅ Responsive & mobile-friendly
- ✅ Dark mode support
- ✅ Accessibility features (ARIA attributes)
<!-- Add toast container to your page -->
<ui-toast id="toaster" position="top-right"></ui-toast>// Get reference to toast container
const toaster = document.getElementById('toaster');
// Show a success toast
toaster.success('Success!', 'Your changes have been saved.');
// Show an error toast
toaster.error('Error occurred!', 'Please try again later.');
// Show a warning toast
toaster.warning('Warning', 'Low disk space detected.');
// Show an info toast
toaster.info('Information', 'New updates available.');const toaster = document.getElementById('toaster');
// Advanced usage with configuration
const toastId = toaster.show({
title: 'Upload complete',
description: 'Your file has been uploaded successfully.',
type: 'success', // 'success' | 'error' | 'warning' | 'info'
duration: 5000, // milliseconds (0 = persistent)
closable: true // show close button
});
// Dismiss specific toast
toaster.dismiss(toastId);
// Dismiss all toasts
toaster.dismissAll();Shows a toast notification and returns a unique toast ID.
Parameters:
interface ToastConfig {
title: string; // Toast title (required)
description?: string; // Optional description text
type?: ToastType; // 'success' | 'error' | 'warning' | 'info' (default: 'info')
duration?: number; // Auto-dismiss time in ms, 0 = no auto-dismiss (default: 5000)
closable?: boolean; // Show close button (default: true)
}Returns: Toast ID string for manual dismissal
Convenience method to show a success toast.
toaster.success('Saved!', 'Document saved successfully.');Convenience method to show an error toast.
toaster.error('Failed', 'Unable to connect to server.');Convenience method to show a warning toast.
toaster.warning('Warning', 'Low storage space.');Convenience method to show an info toast.
toaster.info('Update', 'New version available.');Dismisses a specific toast by ID.
const id = toaster.info('Loading...');
// Later...
toaster.dismiss(id);Dismisses all active toasts.
toaster.dismissAll();| Attribute | Type | Default | Description |
|---|---|---|---|
position |
string | "top-right" |
Toast position: top-right, top-left, bottom-right, bottom-left, top-center, bottom-center |
<!-- Top right corner -->
<ui-toast position="top-right"></ui-toast>
<!-- Top center -->
<ui-toast position="top-center"></ui-toast>
<!-- Bottom left corner -->
<ui-toast position="bottom-left"></ui-toast>// Change position dynamically
toaster.setAttribute('position', 'bottom-center');Fired when a toast is shown.
toaster.addEventListener('toast-show', (event) => {
console.log('Toast shown:', event.detail);
// detail: { id, title, description, type, duration, closable }
});Fired when a toast is dismissed.
toaster.addEventListener('toast-dismiss', (event) => {
console.log('Toast dismissed:', event.detail);
// detail: { id }
});toaster.success('Task completed!');toaster.error(
'Connection failed',
'Unable to reach the server. Please check your internet connection.'
);// Quick notification (2 seconds)
toaster.info('Quick message', null, 2000);
// Long notification (10 seconds)
toaster.warning('Important message', 'Please read carefully.', 10000);
// Persistent (must be closed manually)
toaster.info('Persistent message', 'This stays until you close it.', 0);// Show loading toast
const loadingId = toaster.info('Loading...', 'Please wait', 0);
// Simulate async operation
setTimeout(() => {
// Dismiss loading toast
toaster.dismiss(loadingId);
// Show success toast
toaster.success('Complete!', 'Operation finished successfully.');
}, 3000);// Show multiple toasts with stagger
setTimeout(() => toaster.success('Step 1', 'First action completed'), 0);
setTimeout(() => toaster.success('Step 2', 'Second action completed'), 200);
setTimeout(() => toaster.success('Step 3', 'Third action completed'), 400);function validateForm(form) {
const errors = [];
if (!form.email.value) {
errors.push('Email is required');
}
if (!form.password.value) {
errors.push('Password is required');
}
if (errors.length > 0) {
toaster.error(
'Validation failed',
errors.join('. '),
6000
);
return false;
}
toaster.success('Form submitted!', 'Your data has been saved.');
return true;
}// Monitor online/offline status
window.addEventListener('offline', () => {
toaster.error(
'Connection lost',
'You are currently offline. Changes will be saved when connection is restored.',
0 // persistent
);
});
window.addEventListener('online', () => {
toaster.success('Back online', 'Connection restored.');
});The toast component uses CSS custom properties that can be customized:
ui-toast {
--toast-success: #10b981;
--toast-error: #ef4444;
--toast-warning: #f59e0b;
--toast-info: #3b82f6;
--toast-bg: rgba(255, 255, 255, 0.95);
--toast-blur: blur(12px);
--toast-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
--toast-border: rgba(255, 255, 255, 0.3);
--toast-text: #1e293b;
--toast-radius: 14px;
--toast-icon-size: 20px;
}The toast component includes proper ARIA attributes:
role="alert"for screen readersaria-live="polite"for non-intrusive announcementsaria-labelon close button- Keyboard accessible close buttons
- Modern browsers with Web Components support
- Requires CSS backdrop-filter for glassmorphic effect (gracefully degrades)
- Mobile responsive (stacks properly on small screens)
- Don't overuse: Limit to important notifications
- Keep it brief: Short titles and descriptions
- Appropriate duration: 3-5 seconds for reading
- Right position: Consider your UI layout
- Meaningful types: Use appropriate toast types for context
- Respect user: Don't show too many toasts at once
- Persistent for actions: Use duration=0 for important messages requiring acknowledgment
import { UIToast, ToastConfig, ToastType, ToastPosition } from './components/toast';
const toaster = document.querySelector('ui-toast') as UIToast;
const config: ToastConfig = {
title: 'Success',
description: 'Operation completed',
type: 'success',
duration: 5000,
closable: true
};
const toastId: string = toaster.show(config);Part of the UI Component Library.