@bgunnarsson/toaster
v1.1.1
Published
Toast message javascript library
Maintainers
Readme
@bgunnarsson/toaster
Toaster = Ristavél in Icelandic.
The easiest toast library ever. Nothing you don't need, everything you do need.
I was fed up with notification libraries that were either too complex or simply didn't do the job well. So, I set out to create this package—to make something simple, effective, and easy to use.
@bgunnarsson/Toaster, the lightweight, dependency-free JavaScript library that gives you full control over your notifications—crispy on the outside, customizable on the inside. Whether you're looking to pop up simple text alerts or serve up rich HTML content, This package got your back, no unnecessary fluff, and no burnt toast.
Designed with developers in mind, this package lets you take the wheel. No enforced styles, no bloated features, just a clean, flexible solution that slides perfectly into any project. Want to position your toast in the top right? Bottom left? Sprinkle it with custom classes and your own CSS? Go for it. This package gives you the freedom to create toast notifications that look and behave exactly how you want them to.
With sensible defaults for quick setup, this package is the perfect sidekick—whether you’re in a hurry or have time to fine-tune every last detail. Simple, powerful, and fully customizable—because who doesn't love a perfectly timed toast?
Features
- Minimal and customizable: No forced styles; bring your own CSS for total control over the appearance.
- Flexible positioning: Toasts can appear at
top left,top right,bottom left, orbottom rightof the screen, with additional support for customizable offsets. - HTML content support: Display both simple text notifications or rich HTML content inside toasts.
- Pause on hover: Auto-dismiss timers can pause while the user hovers over a toast.
- Persistent toasts: Keep toasts visible indefinitely with the
persistoption. - Lightweight and dependency-free: No external libraries required.
- Custom events: Listen for custom events like
toaster:addedandtoaster:removed.
Installation
Install the @bgunnarsson/toaster library using the package manager of your choice.
pnpm i @bgunnarsson/toaster
yarn @bgunnarsson/toaster
npm i @bgunnarsson/toasterSizes
| Original | gzip |
|-----------------|-------------|
| 3 kb | 348 B |
Documentation
Looking for React, its context or a Zustand slice? Click here
Options
ToasterOptions
| Option | Type | Default |
|-----------------|-----------|------------------|
| position | string | bottom right |
| offset | object | { x: 0, y: 0 } |
| customClass | string | |
| stack | boolean | true |
| dismissLabel | string | Click to dismiss |
With stack on, toasts pile up as a deck: the newest in front, and up to two older ones peeking out behind it, above it for bottom positions and below it for top positions. Set stack: false to list them one after another instead.
dismissLabel is the title of a clickable toast, for a page in another language. The toaster is a polite role="status" region, so screen readers read each toast as it arrives. With transitions turned off, by prefers-reduced-motion or the page's own CSS, a closed toast is removed straight away instead of after it slides out.
Importing
This package is ESM only and ships its own TypeScript declarations.
import Toaster from '@bgunnarsson/toaster'Basic Usage
const toaster = new Toaster({ duration: 5000 });
toaster.toast({
content: '<p>Welcome to Toaster!</p>'
})createToaster
The toaster's functions, without the toaster: it is made the first time a toast is shown, so this can be called at module level, in a store, or during a server render.
import { createToaster } from '@bgunnarsson/toaster'
const { toast, dismiss, clearAllToasts } = createToaster({ position: 'bottom right' })Initialization
Initialize the Toaster class with custom options for positioning, duration, and more.
const toaster = new Toaster({
position: 'bottom right', // Position the toasts at the bottom-right corner.
offset: { x: 20, y: 20 }, // Offset the toast from the bottom-right by 20px.
customClass: 'my-toaster', // Add a custom class to the toasts.
}).toast()
| Option | Type | Default |
|-----------------|-----------|------------------|
| content | string | |
| persist | boolean | false |
| duration | number | 3000 |
| pause | boolean | true |
| clickable | boolean | true |
Creating a Toast
Use the toast() method to create and display a toast notification.
toaster.toast({
content: '<p>Hello, this is a toast!</p>', // Display HTML content.
duration: 3000, // Auto-dismiss the toast after 3 seconds.
clickable: true, // Disable the clickability.
})Manually Dismissing a Toast
You can dismiss the current toast programmatically by calling the dismiss() method.
toaster.dismiss()Persistent Toast
Toasts can be made persistent by setting the persist option to true. Persistent toasts stay on screen until manually dismissed.
toaster.toast({
content: 'Persistent toast message!',
persist: true, // Toast will stay until dismissed manually.
})Events
The library dispatches custom events when a toast is added or removed:
toaster:addedFired when a new toast is added.toaster:removedFired when a toast is removed from the DOM.
document.addEventListener('toaster:added', (event) => {
console.log('Toast added:', event.detail);
})
document.addEventListener('toaster:removed', (event) => {
console.log('Toast removed:', event.detail);
})Framework/Library Support
At the time of writing (18.10.2024), this package has been tested for use in:
- Rustikus
- TypeScript
- JavaScript
- React -
@bgunnarsson/toaster/react - Astro
- Svelte
- Vue 3
Upcoming tests:
- Lit
- Preact
- Qwuik
- Angular
- Alpine.js
