File tree Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Original file line number Diff line number Diff line change 1
1
import { ref , watchEffect } from 'vue'
2
+ import type { ToastT } from './types'
3
+ import { ToastState } from './state'
2
4
3
- export const useIsDocumentHidden = ( ) => {
5
+ export function useIsDocumentHidden ( ) {
4
6
const isDocumentHidden = ref ( false )
5
7
6
8
watchEffect ( ( ) => {
@@ -12,6 +14,36 @@ export const useIsDocumentHidden = () => {
12
14
} )
13
15
14
16
return {
15
- isDocumentHidden
17
+ isDocumentHidden,
16
18
}
17
19
}
20
+
21
+ export function useVueSonner ( ) {
22
+ const activeToasts = ref < ToastT [ ] > ( [ ] )
23
+
24
+ watchEffect ( ( onInvalidate ) => {
25
+ const unsubscribe = ToastState . subscribe ( ( toast ) => {
26
+ if ( 'dismiss' in toast && toast . dismiss ) {
27
+ return activeToasts . value . filter ( ( t ) => t . id !== toast . id ) ;
28
+ }
29
+
30
+ const existingToastIndex = activeToasts . value . findIndex ( ( t ) => t . id === toast . id ) ;
31
+ if ( existingToastIndex !== - 1 ) {
32
+ const updatedToasts = [ ...activeToasts . value ] ;
33
+ updatedToasts [ existingToastIndex ] = { ...updatedToasts [ existingToastIndex ] , ...toast } ;
34
+
35
+ activeToasts . value = updatedToasts
36
+ } else {
37
+ activeToasts . value = [ toast , ...activeToasts . value ]
38
+ }
39
+ } )
40
+
41
+ onInvalidate ( ( ) => {
42
+ unsubscribe ( )
43
+ } )
44
+ } )
45
+
46
+ return {
47
+ activeToasts
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments