Gathering detailed insights and metrics for vue-sonner-test
Gathering detailed insights and metrics for vue-sonner-test
Gathering detailed insights and metrics for vue-sonner-test
Gathering detailed insights and metrics for vue-sonner-test
npm install vue-sonner-test
Typescript
Module System
Node Version
NPM Version
72.9
Supply Chain
98.5
Quality
78.5
Maintenance
100
Vulnerability
100
License
Vue (53.58%)
TypeScript (28.18%)
CSS (16.21%)
JavaScript (1.45%)
HTML (0.58%)
Total Downloads
308
Last Day
4
Last Week
5
Last Month
6
Last Year
308
960 Stars
216 Commits
49 Forks
2 Watching
2 Branches
19 Contributors
Minified
Minified + Gzipped
Latest Version
0.0.5
Package Id
vue-sonner-test@0.0.5
Unpacked Size
8.71 MB
Size
8.61 MB
File Count
26
NPM Version
10.1.0
Node Version
20.9.0
Publised On
29 Feb 2024
Cumulative downloads
Total Downloads
Last day
0%
4
Compared to previous day
Last week
0%
5
Compared to previous week
Last month
20%
6
Compared to previous month
Last year
0%
308
Compared to previous year
An opinionated toast component for Vue. It's a Vue port of Sonner
https://user-images.githubusercontent.com/6118824/228208185-be5aefd4-7fa8-4f95-a41c-88a60c0e2800.mp4
Vue Sonner
is an opinionated toast component for Vue. It's customizable, but styled by default. Comes with a swipe to dismiss animation.
To start using the library, install it in your project:
1pnpm install vue-sonner 2or 3yarn add vue-sonner
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>
Define a nuxt plugin
1// plugins/sonner.client.ts 2import { Toaster, toast } from 'vue-sonner' 3 4export default defineNuxtPlugin((nuxtApp) => { 5 nuxtApp.vueApp.component('Toaster', Toaster) 6 7 return { 8 provide: { 9 toast 10 } 11 } 12})
Use Toaster
component and $toast
function anywhere in the Vue SFC
1<!-- app.vue --> 2<template> 3 <div> 4 <NuxtPage /> 5 <Toaster position="top-right" /> 6 <button @click="() => $toast('My first toast')">Render a toast</button> 7 </div> 8</template> 9 10<script setup lang="ts"> 11 // alternatively, you can also use it here 12 const { $toast } = useNuxtApp() 13</script>
Add the build transpile for vue-sonner
1// nuxt.config.ts 2import { defineNuxtConfig } from 'nuxt/config' 3 4export default defineNuxtConfig({ 5 ... 6 build: { 7 transpile: ['vue-sonner'] 8 } 9})
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
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})
Renders a checkmark icon in front of the message.
1toast.success('Event has been created')
Renders an error icon in front of the message.
1toast.error('Event has not been created')
Renders a button.
1toast('Event has been created', { 2 action: { 3 label: 'Undo', 4 onClick: () => console.log('Undo') 5 } 6})
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})
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, makeRaw } 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(makeRaw(CustomDiv)) 14</script>
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>
You can change the theme using the theme
prop. Default theme is light.
1<Toaster theme="dark" />
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" />
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" />
You can style your toasts globally with the toastOptions
prop in the Toaster
component.
1<Toaster 2 :toastOptions="{ 3 style: { background: 'red' }, 4 className: 'my-toast', 5 descriptionClassName: 'my-toast-description' 6 }" 7/>
1toast('Event has been created', { 2 style: { 3 background: 'red' 4 }, 5 className: 'my-toast', 6 descriptionClassName: 'my-toast-description' 7})
Add a close button to all toasts that shows on hover by adding the closeButton
prop.
1<Toaster closeButton />
You can make error and success state more colorful by adding the richColors
prop.
1<Toaster richColors />
Offset from the edges of the screen.
1<Toaster offset="80px" />
To remove a toast programmatically use toast.dismiss(id)
.
1const toastId = toast('Event has been created') 2 3toast.dismiss(toastId)
You can also use the dismiss method without the id to dismiss all toasts.
1toast.dismiss()
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 5// Persisent toast 6toast('Event has been created', { 7 duration: Infinity 8})
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})
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']" />
MIT @xiaoluoboding
No vulnerabilities found.
No security vulnerabilities found.