mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-30 11:47:55 +01:00
chore(Notification): improve types
This commit is contained in:
@@ -42,9 +42,9 @@ import UIcon from '../elements/Icon.vue'
|
|||||||
import UAvatar from '../elements/Avatar.vue'
|
import UAvatar from '../elements/Avatar.vue'
|
||||||
import UButton from '../elements/Button.vue'
|
import UButton from '../elements/Button.vue'
|
||||||
import { useTimer } from '../../composables/useTimer'
|
import { useTimer } from '../../composables/useTimer'
|
||||||
import type { ToastNotificationAction } from '../../types'
|
import type { NotificationAction } from '../../types'
|
||||||
import type { Avatar as AvatarType } from '../../types/avatar'
|
import type { Avatar} from '../../types/avatar'
|
||||||
import type { Button as ButtonType } from '../../types/button'
|
import type { Button } from '../../types/button'
|
||||||
import { classNames } from '../../utils'
|
import { classNames } from '../../utils'
|
||||||
import { useAppConfig } from '#imports'
|
import { useAppConfig } from '#imports'
|
||||||
// TODO: Remove
|
// TODO: Remove
|
||||||
@@ -77,11 +77,11 @@ export default defineComponent({
|
|||||||
default: null
|
default: null
|
||||||
},
|
},
|
||||||
avatar: {
|
avatar: {
|
||||||
type: Object as PropType<Partial<AvatarType>>,
|
type: Object as PropType<Partial<Avatar>>,
|
||||||
default: null
|
default: null
|
||||||
},
|
},
|
||||||
close: {
|
close: {
|
||||||
type: Object as PropType<Partial<ButtonType>>,
|
type: Object as PropType<Partial<Button>>,
|
||||||
default: () => appConfig.ui.notification.default.close
|
default: () => appConfig.ui.notification.default.close
|
||||||
},
|
},
|
||||||
timeout: {
|
timeout: {
|
||||||
@@ -89,7 +89,7 @@ export default defineComponent({
|
|||||||
default: 5000
|
default: 5000
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
type: Array as PropType<ToastNotificationAction[]>,
|
type: Array as PropType<NotificationAction[]>,
|
||||||
default: () => []
|
default: () => []
|
||||||
},
|
},
|
||||||
callback: {
|
callback: {
|
||||||
@@ -162,7 +162,7 @@ export default defineComponent({
|
|||||||
emit('close')
|
emit('close')
|
||||||
}
|
}
|
||||||
|
|
||||||
function onAction (action: ToastNotificationAction) {
|
function onAction (action: NotificationAction) {
|
||||||
if (timer) {
|
if (timer) {
|
||||||
timer.stop()
|
timer.stop()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
import { computed, defineComponent } from 'vue'
|
import { computed, defineComponent } from 'vue'
|
||||||
import type { PropType } from 'vue'
|
import type { PropType } from 'vue'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
import type { ToastNotification } from '../../types'
|
import type { Notification } from '../../types'
|
||||||
import { useToast } from '../../composables/useToast'
|
import { useToast } from '../../composables/useToast'
|
||||||
import UNotification from './Notification.vue'
|
import UNotification from './Notification.vue'
|
||||||
import { useState, useAppConfig } from '#imports'
|
import { useState, useAppConfig } from '#imports'
|
||||||
@@ -44,7 +44,7 @@ export default defineComponent({
|
|||||||
const ui = computed<Partial<typeof appConfig.ui.notifications>>(() => defu({}, props.ui, appConfig.ui.notifications))
|
const ui = computed<Partial<typeof appConfig.ui.notifications>>(() => defu({}, props.ui, appConfig.ui.notifications))
|
||||||
|
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
const notifications = useState<ToastNotification[]>('notifications', () => [])
|
const notifications = useState<Notification[]>('notifications', () => [])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// eslint-disable-next-line vue/no-dupe-keys
|
// eslint-disable-next-line vue/no-dupe-keys
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { useClipboard } from '@vueuse/core'
|
import { useClipboard } from '@vueuse/core'
|
||||||
import type { ToastNotification } from '../types/toast'
|
import type { Notification } from '../types/notification'
|
||||||
import { useToast } from './useToast'
|
import { useToast } from './useToast'
|
||||||
|
|
||||||
export function useCopyToClipboard (options: Partial<ToastNotification> = {}) {
|
export function useCopyToClipboard (options: Partial<Notification> = {}) {
|
||||||
const { copy: copyToClipboard, isSupported } = useClipboard()
|
const { copy: copyToClipboard, isSupported } = useClipboard()
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
import type { ToastNotification } from '../types'
|
import type { Notification } from '../types'
|
||||||
import { useState } from '#imports'
|
import { useState } from '#imports'
|
||||||
|
|
||||||
export function useToast () {
|
export function useToast () {
|
||||||
const notifications = useState<ToastNotification[]>('notifications', () => [])
|
const notifications = useState<Notification[]>('notifications', () => [])
|
||||||
|
|
||||||
function add (notification: Partial<ToastNotification>) {
|
function add (notification: Partial<Notification>) {
|
||||||
const body = {
|
const body = {
|
||||||
id: new Date().getTime().toString(),
|
id: new Date().getTime().toString(),
|
||||||
...notification
|
...notification
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = notifications.value.findIndex((n: ToastNotification) => n.id === body.id)
|
const index = notifications.value.findIndex((n: Notification) => n.id === body.id)
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
notifications.value.push(body as ToastNotification)
|
notifications.value.push(body as Notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
function remove (id: string) {
|
function remove (id: string) {
|
||||||
notifications.value = notifications.value.filter((n: ToastNotification) => n.id !== id)
|
notifications.value = notifications.value.filter((n: Notification) => n.id !== id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
2
src/runtime/types/index.d.ts
vendored
2
src/runtime/types/index.d.ts
vendored
@@ -1,5 +1,5 @@
|
|||||||
export * from './avatar'
|
export * from './avatar'
|
||||||
export * from './clipboard'
|
export * from './clipboard'
|
||||||
export * from './command-palette'
|
export * from './command-palette'
|
||||||
|
export * from './notification'
|
||||||
export * from './popper'
|
export * from './popper'
|
||||||
export * from './toast'
|
|
||||||
|
|||||||
23
src/runtime/types/notification.d.ts
vendored
Normal file
23
src/runtime/types/notification.d.ts
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import type { Avatar } from './avatar'
|
||||||
|
import type { Button } from './button'
|
||||||
|
import appConfig from '#build/app.config'
|
||||||
|
|
||||||
|
export interface NotificationAction extends Partial<Button> {
|
||||||
|
click: Function
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Notification {
|
||||||
|
id: string
|
||||||
|
title: string
|
||||||
|
description: string
|
||||||
|
icon?: string
|
||||||
|
avatar?: Partial<Avatar>
|
||||||
|
close?: Partial<Button>
|
||||||
|
timeout: number
|
||||||
|
actions?: NotificationAction[]
|
||||||
|
click?: Function
|
||||||
|
callback?: Function
|
||||||
|
progressColor?: string
|
||||||
|
progressVariant?: string
|
||||||
|
ui?: Partial<typeof appConfig.ui.notification>
|
||||||
|
}
|
||||||
17
src/runtime/types/toast.d.ts
vendored
17
src/runtime/types/toast.d.ts
vendored
@@ -1,17 +0,0 @@
|
|||||||
import type { Button } from './button'
|
|
||||||
|
|
||||||
export interface ToastNotificationAction extends Partial<Button> {
|
|
||||||
click: Function
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ToastNotification {
|
|
||||||
id: string
|
|
||||||
title: string
|
|
||||||
description: string
|
|
||||||
type: string
|
|
||||||
icon?: string
|
|
||||||
timeout: number
|
|
||||||
actions?: ToastNotificationAction[]
|
|
||||||
click?: Function
|
|
||||||
callback?: Function
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user