Installations
npm install vue-sonner
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.10.0
NPM Version
10.2.3
Score
98.8
Supply Chain
99.5
Quality
83.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
Vue (53.58%)
TypeScript (28.18%)
CSS (16.21%)
JavaScript (1.45%)
HTML (0.58%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
1,650,611
Last Day
8,367
Last Week
39,023
Last Month
177,290
Last Year
1,595,272
GitHub Statistics
985 Stars
216 Commits
50 Forks
2 Watching
2 Branches
19 Contributors
Bundle Size
37.94 kB
Minified
9.79 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.3.0
Package Id
vue-sonner@1.3.0
Unpacked Size
267.60 kB
Size
181.86 kB
File Count
21
NPM Version
10.2.3
Node Version
20.10.0
Publised On
21 Nov 2024
Total Downloads
Cumulative downloads
Total Downloads
1,650,611
Last day
-4.6%
8,367
Compared to previous day
Last week
-14.5%
39,023
Compared to previous week
Last month
-4.2%
177,290
Compared to previous month
Last year
2,782.7%
1,595,272
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
24
Sonner for Vue
An opinionated toast component for Vue. It's a Vue port of Sonner
Preview
https://user-images.githubusercontent.com/6118824/228208185-be5aefd4-7fa8-4f95-a41c-88a60c0e2800.mp4
Introduction
Vue Sonner
is an opinionated toast component for Vue. It's customizable, but styled by default. Comes with a swipe to dismiss animation.
Table of Contents
TOC
Installation
To start using the library, install it in your project:
1pnpm install vue-sonner 2or 3yarn add vue-sonner
Test
To run the test you need two separate CLI window :
Launching the test
To launch the test, you need to go in the test directory
1cd ./test
and launch the following command
1cd ./test 2pnpm test:e2e --ui
Build and watch for change in order to fix the test
This command will build the vue-sonner library in lib mode, and add a watch so every time you modify the code of the library, you will have a new bundle and can run the test again.
1pnpm build:dev
Usage
For Vue 3
1<!-- App.vue --> 2<template> 3 <Toaster /> 4 <button @click="() => toast('My first toast')">Render a toast</button> 5</template> 6 7<script lang="ts" setup> 8 import { Toaster, toast } from 'vue-sonner' 9</script>
For Nuxt 3
Use vue-sonner/nuxt
module
1// nuxt.config.ts
2export default defineNuxtConfig({
3 ...
4 modules: ['vue-sonner/nuxt']
5})
Use Toaster
component and $toast
function anywhere in the Vue SFC
1<!-- app.vue --> 2<template> 3 <div> 4 <Toaster position="top-right" /> 5 <button @click="() => $toast('My first toast')">Render a toast</button> 6 </div> 7</template> 8 9<script setup lang="ts"> 10 const { $toast } = useNuxtApp() 11</script>
CDN Link
EMS version
1https://cdn.jsdelivr.net/npm/vue-sonner/+esm
UMD version
1https://www.unpkg.com/vue-sonner@0.3.1/lib/vue-sonner.umd.cjs
Types
Default
Most basic toast. You can customize it (and any other type) by passing an options object as the second argument.
1toast('Event has been created')
With custom description:
1toast('Event has been created', { 2 description: 'Monday, January 3rd at 6:00pm' 3})
Success
Renders a checkmark icon in front of the message.
1toast.success('Event has been created')
Error
Renders an error icon in front of the message.
1toast.error('Event has not been created')
Action
Renders a button.
1toast('Event has been created', { 2 action: { 3 label: 'Undo', 4 onClick: () => console.log('Undo') 5 } 6})
Promise
Starts in a loading state and will update automatically after the promise resolves or fails.
You can pass a function to the success/error messages to incorporate the result/error of the promise.
1toast.promise(() => new Promise((resolve) => setTimeout(resolve, 2000)), { 2 loading: 'Loading', 3 success: (data: any) => 'Success', 4 error: (data: any) => 'Error' 5})
Custom Component
You can pass a Vue Component as the first argument instead of a string to render custom Component while maintaining default styling. You can use the headless version below for a custom, unstyled toast.
1<script lang="ts" setup> 2 import { defineComponent, h, markRaw } from 'vue' 3 4 const CustomDiv = defineComponent({ 5 setup() { 6 return () => 7 h('div', { 8 innerHTML: 'A custom toast with unstyling' 9 }) 10 } 11 }) 12 13 toast(markRaw(CustomDiv)) 14</script>
Customization
Headless
You can use toast.custom
to render an unstyled toast with custom jsx while maintaining the functionality.
1<script lang="ts" setup> 2import { markRaw } from 'vue' 3 4import HeadlessToast from './HeadlessToast.vue' 5 6toast.custom(markRaw(HeadlessToast), { duration: 999999 }) 7</script>
Theme
You can change the theme using the theme
prop. Default theme is light.
1<Toaster theme="dark" />
Position
You can change the position through the position
prop on the <Toaster />
component. Default is top-right
.
1<!-- Available positions --> 2<!-- top-left, top-center, top-right, bottom-left, bottom-center, bottom-right --> 3 4<Toaster position="top-center" />
Expanded
Toasts can also be expanded by default through the expand
prop. You can also change the amount of visible toasts which is 3 by default.
1<Toaster expand :visibleToasts="9" />
Styling for all toasts
You can style your toasts globally with the toastOptions
prop in the Toaster
component.
1<Toaster 2 :toastOptions="{ 3 style: { background: 'red' }, 4 class: 'my-toast', 5 descriptionClass: 'my-toast-description' 6 }" 7/>
Styling for individual toast
1toast('Event has been created', { 2 style: { 3 background: 'red' 4 }, 5 class: 'my-toast', 6 descriptionClass: 'my-toast-description' 7})
Tailwind CSS
The preferred way to style the toasts with tailwind is by using the unstyled
prop. That will give you an unstyled toast which you can then style with tailwind.
1<Toaster 2 :toastOptions="{ 3 unstyled: true, 4 classes: { 5 toast: 'bg-blue-400', 6 title: 'text-red-400', 7 description: 'text-red-400', 8 actionButton: 'bg-zinc-400', 9 cancelButton: 'bg-orange-400', 10 closeButton: 'bg-lime-400' 11 } 12 }" 13/>
You can do the same when calling toast()
.
1toast('Hello World', { 2 unstyled: true, 3 classes: { 4 toast: 'bg-blue-400', 5 title: 'text-red-400 text-2xl', 6 description: 'text-red-400', 7 actionButton: 'bg-zinc-400', 8 cancelButton: 'bg-orange-400', 9 closeButton: 'bg-lime-400' 10 } 11})
Styling per toast type is also possible.
1<Toaster 2 :toastOptions="{ 3 unstyled: true, 4 classes: { 5 error: 'bg-red-400', 6 success: 'text-green-400', 7 warning: 'text-yellow-400', 8 info: 'bg-blue-400' 9 } 10 }" 11/>
Changing Icon
You can change the default icons using slots:
1<Toaster> 2 <template #loading-icon> 3 <LoadingIcon /> 4 </template> 5 <template #success-icon> 6 <SuccessIcon /> 7 </template> 8 <template #error-icon> 9 <ErrorIcon /> 10 </template> 11 <template #info-icon> 12 <InfoIcon /> 13 </template> 14 <template #warning-icon> 15 <WarningIcon /> 16 </template> 17</Toaster>
Close button
Add a close button to all toasts that shows on hover by adding the closeButton
prop.
1<Toaster closeButton />
Rich colors
You can make error and success state more colorful by adding the richColors
prop.
1<Toaster richColors />
Custom offset
Offset from the edges of the screen.
1<Toaster offset="80px" />
On Close Callback
You can pass onDismiss
and onAutoClose
callbacks. onDismiss
gets fired when either the close button gets clicked or the toast is swiped. onAutoClose
fires when the toast disappears automatically after it's timeout (duration
prop).
1toast('Event has been created', { 2 onDismiss: (t) => console.log(`Toast with id ${t.id} has been dismissed`), 3 onAutoClose: (t) => 4 console.log(`Toast with id ${t.id} has been closed automatically`) 5})
Persisting toasts
You can change the duration of each toast by using the duration property, or change the duration of all toasts like this:
1<Toaster :duration="10000" />
1toast('Event has been created', { 2 duration: 10000 3}) 4 5If you want a toast to stay on screen forever, you can set the duration to `Infinity`. 6 7// Persisent toast 8toast('Event has been created', { 9 duration: Infinity 10})
Dismissing toasts programmatically
To remove a toast programmatically use toast.dismiss(id)
.
1const toastId = toast('Event has been created') 2 3toast.dismiss(toastId)
You can also dismiss all toasts at once by calling toast.dismiss()
without an id.
1toast.dismiss()
Keyboard focus
You can focus on the toast area by pressing ⌥/alt + T. You can override it by providing an array of event.code values for each key.
1<Toaster hotkey="['KeyC']" />
Inspiration
- sonner - An opinionated toast component for React.
License
MIT @xiaoluoboding
No vulnerabilities found.
No security vulnerabilities found.
Other packages similar to vue-sonner
vuetify-sonner
Stackable toast component for Vuetify.
@hoppscotch/vue-sonner
[![NPM][npmBadge]][npmUrl] [![Minzip Package][bundlePhobiaBadge]][bundlePhobiaUrl] [![NPM Download][npmDtBadge]][npmDtUrl]
vue-sonner-test
[![NPM][npmBadge]][npmUrl] [![Minzip Package][bundlePhobiaBadge]][bundlePhobiaUrl] [![NPM Download][npmDtBadge]][npmDtUrl]
@neoncoder/vuetify-sonner
Stackable toast component for Vuetify.