mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 06:51:46 +01:00
feat: module improvements
This commit is contained in:
53
src/runtime/plugins/toast.client.ts
Normal file
53
src/runtime/plugins/toast.client.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import { Ref } from 'vue'
|
||||
import { ToastNotification, ToastPlugin } from '../types'
|
||||
import { defineNuxtPlugin, useState } from '#app'
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
const notifications: Ref<ToastNotification[]> = useState('notifications', () => [])
|
||||
|
||||
function addNotification (notification: Partial<ToastNotification>) {
|
||||
const body = {
|
||||
id: nanoid(),
|
||||
...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)
|
||||
}
|
||||
|
||||
nuxtApp.provide('toast', {
|
||||
addNotification,
|
||||
removeNotification,
|
||||
success ({ title, description }: { title?: string, description?: string } = {}) {
|
||||
addNotification({
|
||||
type: 'success',
|
||||
title,
|
||||
description,
|
||||
timeout: 4000
|
||||
})
|
||||
},
|
||||
error ({ title = 'An error occurred!', description }: { title?: string, description?: string } = {}) {
|
||||
addNotification({
|
||||
type: 'error',
|
||||
title,
|
||||
description,
|
||||
timeout: 4000
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
declare module '#app' {
|
||||
interface NuxtApp {
|
||||
$toast: ToastPlugin
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user