Gathering detailed insights and metrics for @hoppscotch/vue-sonner
Gathering detailed insights and metrics for @hoppscotch/vue-sonner
Gathering detailed insights and metrics for @hoppscotch/vue-sonner
Gathering detailed insights and metrics for @hoppscotch/vue-sonner
npm install @hoppscotch/vue-sonner
Typescript
Module System
Node Version
NPM Version
73.1
Supply Chain
98.9
Quality
82
Maintenance
100
Vulnerability
100
License
Vue (62.33%)
TypeScript (20.73%)
CSS (16.57%)
HTML (0.36%)
Total Downloads
9,906
Last Day
18
Last Week
386
Last Month
1,825
Last Year
9,906
2 Stars
150 Commits
2 Branches
7 Contributors
Latest Version
1.2.3
Package Id
@hoppscotch/vue-sonner@1.2.3
Unpacked Size
254.05 kB
Size
178.52 kB
File Count
12
NPM Version
10.7.0
Node Version
18.20.3
Publised On
25 Jun 2024
Cumulative downloads
Total Downloads
Last day
-84.1%
18
Compared to previous day
Last week
-29%
386
Compared to previous week
Last month
-4.4%
1,825
Compared to previous month
Last year
0%
9,906
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, 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>
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 class: 'my-toast', 5 descriptionClass: 'my-toast-description' 6 }" 7/>
1toast('Event has been created', { 2 style: { 3 background: 'red' 4 }, 5 class: 'my-toast', 6 descriptionClass: 'my-toast-description' 7})
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/>
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>
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.