mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-19 22:41:42 +01:00
chore: add more composables (#138)
This commit is contained in:
committed by
GitHub
parent
fef93f3198
commit
fd4b608150
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user