Line data Source code
1 16 : <script setup lang="ts">
2 : import { storeToRefs } from 'pinia'
3 : import { ToastProvider } from 'reka-ui'
4 : import { computed, onBeforeUnmount } from 'vue'
5 : import { useI18n } from 'vue-i18n'
6 :
7 : import { cn } from '@comfyorg/tailwind-utils'
8 :
9 : import Button from '@comfyorg/design-system/button/Button'
10 : import ToastClose from '@comfyorg/design-system/toast/ToastClose'
11 : import ToastDescription from '@comfyorg/design-system/toast/ToastDescription'
12 : import ToastRoot from '@comfyorg/design-system/toast/ToastRoot'
13 : import ToastTitle from '@comfyorg/design-system/toast/ToastTitle'
14 : import ToastViewport from '@comfyorg/design-system/toast/ToastViewport'
15 : import { useToast } from '@/components/ui/toast/toastStore'
16 : import type { Toast } from '@/components/ui/toast/toastStore'
17 : import type { ToastId } from '@/types/toastId'
18 :
19 2355 : const { t } = useI18n()
20 :
21 2355 : const toast = useToast()
22 2355 : const { held, toasts } = storeToRefs(toast)
23 :
24 2355 : onBeforeUnmount(toast.dismissAll)
25 :
26 2355 : const shownToasts = computed(() => (held.value ? [] : toasts.value))
27 :
28 2355 : const isAssertive = (item: Toast) =>
29 : item.kind === 'error' || item.kind === 'warning'
30 2355 : const politeToasts = computed(() =>
31 : toasts.value.filter((item) => !isAssertive(item))
32 : )
33 2355 : const assertiveToasts = computed(() => toasts.value.filter(isAssertive))
34 :
35 198 : function announcement(item: Toast) {
36 198 : return [
37 : item.title,
38 : item.description,
39 : ...item.actions.map((action) => action.label)
40 : ]
41 : .filter(Boolean)
42 : .join('. ')
43 : }
44 :
45 2355 : let escapeToastId: ToastId | undefined
46 :
47 1 : function preserveToastOnEscape(event: KeyboardEvent, id: ToastId) {
48 1 : if (!event.defaultPrevented) escapeToastId = id
49 : }
50 :
51 21 : function updateToastOpen(id: ToastId, open: boolean) {
52 21 : if (open) return
53 21 : if (escapeToastId === id) {
54 1 : escapeToastId = undefined
55 1 : return
56 : }
57 20 : toast.dismiss(id)
58 : }
59 :
60 2355 : const icons = {
61 : success: 'icon-[lucide--circle-check] text-success',
62 : error: 'icon-[lucide--circle-x] text-destructive',
63 : info: 'icon-[lucide--info] text-primary',
64 : warning: 'icon-[lucide--triangle-alert] text-warning',
65 : loading: 'icon-[lucide--loader-circle] motion-safe:animate-spin text-primary'
66 : } as const
67 : </script>
68 :
69 : <template>
70 : <div class="sr-only">
71 : <div
72 : role="status"
73 : aria-live="polite"
74 : aria-atomic="false"
75 : :aria-label="t('toastMessages.notificationsLabel')"
76 : >
77 123 : <p v-for="item in politeToasts" :key="item.id">
78 : {{ announcement(item) }}
79 : </p>
80 : </div>
81 : <div
82 : role="alert"
83 : aria-live="assertive"
84 : aria-atomic="false"
85 : :aria-label="t('toastMessages.notificationsLabel')"
86 : >
87 75 : <p v-for="item in assertiveToasts" :key="item.id">
88 : {{ announcement(item) }}
89 : </p>
90 : </div>
91 : </div>
92 : <ToastProvider
93 : v-if="shownToasts.length"
94 : :label="t('toastMessages.notificationsLabel')"
95 : disable-swipe
96 : >
97 199 : <ToastRoot
98 : v-for="item in shownToasts"
99 : :key="item.instance"
100 : :open="true"
101 : :duration="item.duration"
102 : data-testid="toast"
103 : :data-toast-kind="item.kind"
104 : @escape-key-down="preserveToastOnEscape($event, item.id)"
105 : @update:open="updateToastOpen(item.id, $event)"
106 : >
107 : <i
108 : :class="cn(icons[item.kind], 'col-start-1 row-start-1 size-5')"
109 : aria-hidden="true"
110 : />
111 : <ToastTitle class="col-start-2 row-start-1">{{ item.title }}</ToastTitle>
112 : <ToastClose
113 : v-if="item.closable"
114 : :aria-label="t('g.close')"
115 : class="col-start-3 row-start-1"
116 : data-testid="toast-close"
117 : @click="toast.close(item.id)"
118 : />
119 : <ToastDescription v-if="item.description" class="col-start-2 row-start-2">
120 : {{ item.description }}
121 : </ToastDescription>
122 : <div
123 : class="col-span-2 col-start-2 row-start-3 mt-3 flex flex-wrap justify-end gap-2 empty:hidden"
124 : >
125 79 : <Button
126 : v-for="(action, index) in item.actions"
127 : :key="`${action.label}-${index}`"
128 : size="md"
129 : variant="secondary"
130 : @click="action.onClick()"
131 : >
132 : {{ action.label }}
133 : </Button>
134 : </div>
135 : </ToastRoot>
136 : <ToastViewport
137 : :label="
138 : (hotkey) => t('toastMessages.notificationsViewportLabel', { hotkey })
139 : "
140 : data-testid="toast-viewport"
141 : />
142 : </ToastProvider>
143 : </template>
|