chore: add more composables (#138)

This commit is contained in:
Sylvain Marroufin
2023-03-09 11:42:22 +01:00
committed by GitHub
parent fef93f3198
commit fd4b608150
11 changed files with 221 additions and 71 deletions

View File

@@ -1,33 +0,0 @@
import { useClipboard } from '@vueuse/core'
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxtApp) => {
const { copy: copyToClipboard, isSupported } = useClipboard()
function copy (text: string, success: { title?: string, description?: string } = {}, failure: { title?: string, description?: string } = {}) {
if (!isSupported) {
return
}
copyToClipboard(text).then(() => {
if (!success.title && !success.description) {
return
}
nuxtApp.$toast.success(success)
}, function (e) {
nuxtApp.$toast.error({
...failure,
description: failure.description || e.message
})
})
}
return {
provide: {
clipboard: {
copy
}
}
}
})

View File

@@ -1,45 +0,0 @@
import { defineNuxtPlugin, useState } from '#app'
import type { ToastNotification } from '../types'
export default defineNuxtPlugin(() => {
const notifications = useState<ToastNotification[]>('notifications', () => [])
function addNotification (notification: Partial<ToastNotification>) {
const body = {
id: new Date().getTime().toString(),
...notification
}
const index = notifications.value.findIndex((n: ToastNotification) => n.id === body.id)
if (index === -1) {
notifications.value.push(body as ToastNotification)
}
return body
}
function removeNotification (id: string) {
notifications.value = notifications.value.filter((n: ToastNotification) => n.id !== id)
}
return {
provide: {
toast: {
addNotification,
removeNotification,
success (notification: Partial<ToastNotification> = {}) {
return addNotification({ type: 'success', ...notification })
},
info (notification: Partial<ToastNotification> = {}) {
return addNotification({ type: 'info', ...notification })
},
warning (notification: Partial<ToastNotification> = {}) {
return addNotification({ type: 'warning', ...notification })
},
error (notification: Partial<ToastNotification>) {
return addNotification({ type: 'error', title: 'An error occurred!', ...notification })
}
}
}
}
})