@@ -2,7 +2,15 @@ import * as React from 'react';
22import { useEventCallback , useForceUpdate } from '@fluentui/react-utilities' ;
33import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts' ;
44import { createToaster } from './vanilla' ;
5- import { Toast , ToastListenerMap , ToastPosition , ToasterId , ToasterOptions } from './types' ;
5+ import type {
6+ CommonToastDetail ,
7+ ShowToastEventDetail ,
8+ Toast ,
9+ ToastListenerMap ,
10+ ToastPosition ,
11+ ToasterId ,
12+ ToasterOptions ,
13+ } from './types' ;
614import { ToasterProps } from '../components/Toaster' ;
715import { EVENTS } from './constants' ;
816
@@ -21,54 +29,51 @@ export function useToaster<TElement extends HTMLElement>(options: ToasterProps =
2129 return ;
2230 }
2331
24- const buildToast : ToastListenerMap [ typeof EVENTS . show ] = e => {
25- if ( ! isCorrectToaster ( e . detail . toasterId ) ) {
26- return ;
27- }
32+ const addToastListener = < TType extends keyof ToastListenerMap > (
33+ eventType : TType ,
34+ callback : ToastListenerMap [ TType ] ,
35+ ) => {
36+ const listener : ToastListenerMap [ TType ] = ( e : CustomEvent < CommonToastDetail > ) => {
37+ if ( ! isCorrectToaster ( e . detail . toasterId ) ) {
38+ return ;
39+ }
40+
41+ callback ( e as CustomEvent < ShowToastEventDetail > ) ;
42+ forceUpdate ( ) ;
43+ } ;
44+
45+ targetDocument . addEventListener ( eventType , listener as ( ) => void ) ;
46+ return ( ) => targetDocument . removeEventListener ( eventType , listener as ( ) => void ) ;
47+ } ;
2848
49+ const buildToast : ToastListenerMap [ typeof EVENTS . show ] = e => {
2950 toaster . buildToast ( e . detail , forceUpdate ) ;
30- forceUpdate ( ) ;
3151 } ;
3252
3353 const dismissToast : ToastListenerMap [ typeof EVENTS . dismiss ] = e => {
34- if ( ! isCorrectToaster ( e . detail . toasterId ) ) {
35- return ;
36- }
37-
3854 toaster . dismissToast ( e . detail . toastId ) ;
39- forceUpdate ( ) ;
4055 } ;
4156
4257 const updateToast : ToastListenerMap [ typeof EVENTS . update ] = e => {
43- if ( ! isCorrectToaster ( e . detail . toasterId ) ) {
44- return ;
45- }
46-
4758 toaster . updateToast ( e . detail ) ;
48- forceUpdate ( ) ;
4959 } ;
5060
5161 const dismissAllToasts : ToastListenerMap [ typeof EVENTS . dismissAll ] = e => {
52- if ( ! isCorrectToaster ( e . detail . toasterId ) ) {
53- return ;
54- }
55-
5662 toaster . dismissAllToasts ( ) ;
57- forceUpdate ( ) ;
5863 } ;
5964
60- targetDocument . addEventListener ( EVENTS . show , buildToast as ( ) => void ) ;
61- targetDocument . addEventListener ( EVENTS . dismiss , dismissToast as ( ) => void ) ;
62- targetDocument . addEventListener ( EVENTS . update , updateToast as ( ) => void ) ;
63- targetDocument . addEventListener ( EVENTS . dismissAll , dismissAllToasts as ( ) => void ) ;
65+ const cleanupBuildListener = addToastListener ( EVENTS . show , buildToast ) ;
66+ const cleanupUpdateListener = addToastListener ( EVENTS . update , updateToast ) ;
67+ const cleanupDismissListener = addToastListener ( EVENTS . dismiss , dismissToast ) ;
68+ const cleanupDismissAllListener = addToastListener ( EVENTS . dismissAll , dismissAllToasts ) ;
6469
6570 return ( ) => {
66- targetDocument . removeEventListener ( EVENTS . show , buildToast as ( ) => void ) ;
67- targetDocument . removeEventListener ( EVENTS . dismiss , dismissToast as ( ) => void ) ;
68- targetDocument . removeEventListener ( EVENTS . update , updateToast as ( ) => void ) ;
69- targetDocument . removeEventListener ( EVENTS . dismissAll , dismissAllToasts as ( ) => void ) ;
71+ cleanupBuildListener ( ) ;
72+ cleanupDismissAllListener ( ) ;
73+ cleanupUpdateListener ( ) ;
74+ cleanupDismissListener ( ) ;
7075 } ;
71- } , [ toaster , forceUpdate , isCorrectToaster , targetDocument ] ) ;
76+ } , [ toaster , forceUpdate , targetDocument , isCorrectToaster ] ) ;
7277
7378 const toastsToRender = ( ( ) => {
7479 if ( ! toaster ) {
0 commit comments