@@ -233,15 +233,36 @@ export class ToastStore extends ReactStore<State, {}, typeof selectors> {
233233 }
234234 } ;
235235
236- closeToast = ( toastId : string ) => {
237- const toast = selectors . toast ( this . state , toastId ) ;
238- toast ?. onClose ?.( ) ;
236+ closeToast = ( toastId ?: string ) => {
237+ const closeAll = toastId === undefined ;
238+ const toast = closeAll ? undefined : selectors . toast ( this . state , toastId ) ;
239+ if ( ! closeAll && ! toast ) {
240+ return ;
241+ }
239242
240243 const { limit, toasts } = this . state ;
241244
245+ if ( closeAll ) {
246+ toasts . forEach ( ( item ) => {
247+ item . onClose ?.( ) ;
248+ } ) ;
249+ this . timers . forEach ( ( timer ) => {
250+ timer . timeout ?. clear ( ) ;
251+ } ) ;
252+ this . timers . clear ( ) ;
253+ } else {
254+ toast ?. onClose ?.( ) ;
255+
256+ const timer = this . timers . get ( toastId ) ;
257+ if ( timer ?. timeout ) {
258+ timer . timeout . clear ( ) ;
259+ this . timers . delete ( toastId ) ;
260+ }
261+ }
262+
242263 let activeIndex = 0 ;
243264 const newToasts = toasts . map ( ( item ) => {
244- if ( item . id === toastId ) {
265+ if ( closeAll || item . id === toastId ) {
245266 return { ...item , transitionStatus : 'ending' as const , height : 0 } ;
246267 }
247268 if ( item . transitionStatus === 'ending' ) {
@@ -252,14 +273,14 @@ export class ToastStore extends ReactStore<State, {}, typeof selectors> {
252273 return item . limited !== isLimited ? { ...item , limited : isLimited } : item ;
253274 } ) ;
254275
255- const timer = this . timers . get ( toastId ) ;
256- if ( timer && timer . timeout ) {
257- timer . timeout . clear ( ) ;
258- this . timers . delete ( toastId ) ;
259- }
260-
261276 this . handleFocusManagement ( toastId ) ;
262- this . setToasts ( newToasts ) ;
277+
278+ const updates : Partial < State > = { toasts : newToasts } ;
279+ if ( closeAll || toasts . length === 1 ) {
280+ updates . hovering = false ;
281+ updates . focused = false ;
282+ }
283+ this . update ( updates ) ;
263284 } ;
264285
265286 promiseToast = < Value , Data extends object > (
@@ -381,7 +402,7 @@ export class ToastStore extends ReactStore<State, {}, typeof selectors> {
381402 this . update ( updates ) ;
382403 }
383404
384- private handleFocusManagement ( toastId : string ) {
405+ private handleFocusManagement ( toastId : string | undefined ) {
385406 const activeEl = activeElement ( ownerDocument ( this . state . viewport ) ) ;
386407 if (
387408 ! this . state . viewport ||
@@ -391,6 +412,11 @@ export class ToastStore extends ReactStore<State, {}, typeof selectors> {
391412 return ;
392413 }
393414
415+ if ( toastId === undefined ) {
416+ this . restoreFocusToPrevElement ( ) ;
417+ return ;
418+ }
419+
394420 const toasts = selectors . toasts ( this . state ) ;
395421 const currentIndex = selectors . toastIndex ( this . state , toastId ) ;
396422 let nextToast : ToastObject < any > | null = null ;
0 commit comments