3
3
import React from 'react' ;
4
4
import ReactDOM from 'react-dom' ;
5
5
6
- import DOMPurify from " dompurify" ;
6
+ import DOMPurify from ' dompurify' ;
7
7
import { getAsset , Loader } from './assets' ;
8
8
import { useIsDocumentHidden } from './hooks' ;
9
9
import { toast , ToastState } from './state' ;
10
10
import './styles.css' ;
11
- import type { ExternalToast , HeightT , ToasterProps , ToastProps , ToastT , ToastToDismiss } from './types' ;
11
+ import {
12
+ isAction ,
13
+ type ExternalToast ,
14
+ type HeightT ,
15
+ type ToasterProps ,
16
+ type ToastProps ,
17
+ type ToastT ,
18
+ type ToastToDismiss ,
19
+ } from './types' ;
12
20
13
21
// Visible toasts amount
14
22
const VISIBLE_TOASTS_AMOUNT = 3 ;
@@ -56,7 +64,7 @@ const Toast = (props: ToastProps) => {
56
64
descriptionClassName = '' ,
57
65
duration : durationFromToaster ,
58
66
position,
59
- gap = GAP ,
67
+ gap,
60
68
loadingIcon : loadingIconProp ,
61
69
expandByDefault,
62
70
classNames,
@@ -375,16 +383,16 @@ const Toast = (props: ToastProps) => {
375
383
< >
376
384
{ toastType || toast . icon || toast . promise ? (
377
385
< div data-icon = "" className = { cn ( classNames ?. icon ) } >
378
- { toast . promise || ( toast . type === 'loading' && ! toast . icon )
379
- ? toast . icon || getLoadingIcon ( )
380
- : null }
386
+ { toast . promise || ( toast . type === 'loading' && ! toast . icon ) ? toast . icon || getLoadingIcon ( ) : null }
381
387
{ toast . type !== 'loading' ? toast . icon || icons ?. [ toastType ] || getAsset ( toastType ) : null }
382
388
</ div >
383
389
) : null }
384
390
385
391
< div data-content = "" className = { cn ( classNames ?. content ) } >
386
- < div data-title = "" className = { cn ( classNames ?. title , toast ?. classNames ?. title ) }
387
- dangerouslySetInnerHTML = { sanitizeHTML ( toast . title as string ) }
392
+ < div
393
+ data-title = ""
394
+ className = { cn ( classNames ?. title , toast ?. classNames ?. title ) }
395
+ dangerouslySetInnerHTML = { sanitizeHTML ( toast . title as string ) }
388
396
> </ div >
389
397
{ toast . description ? (
390
398
< div
@@ -399,29 +407,35 @@ const Toast = (props: ToastProps) => {
399
407
> </ div >
400
408
) : null }
401
409
</ div >
402
- { toast . cancel ? (
410
+ { React . isValidElement ( toast . cancel ) ? (
411
+ toast . cancel
412
+ ) : toast . cancel && isAction ( toast . cancel ) ? (
403
413
< button
404
414
data-button
405
415
data-cancel
406
416
style = { toast . cancelButtonStyle || cancelButtonStyle }
407
417
onClick = { ( event ) => {
418
+ // We need to check twice because typescript
419
+ if ( ! isAction ( toast . cancel ) ) return ;
408
420
if ( ! dismissible ) return ;
409
421
deleteToast ( ) ;
410
- if ( toast . cancel ?. onClick ) {
411
- toast . cancel . onClick ( event ) ;
412
- }
422
+ toast . cancel . onClick ( event ) ;
413
423
} }
414
424
className = { cn ( classNames ?. cancelButton , toast ?. classNames ?. cancelButton ) }
415
425
>
416
426
{ toast . cancel . label }
417
427
</ button >
418
428
) : null }
419
- { toast . action ? (
429
+ { React . isValidElement ( toast . action ) ? (
430
+ toast . action
431
+ ) : toast . action && isAction ( toast . action ) ? (
420
432
< button
421
433
data-button = ""
422
434
style = { toast . actionButtonStyle || actionButtonStyle }
423
435
onClick = { ( event ) => {
424
- toast . action ?. onClick ( event ) ;
436
+ // We need to check twice because typescript
437
+ if ( ! isAction ( toast . action ) ) return ;
438
+ toast . action . onClick ( event ) ;
425
439
if ( event . defaultPrevented ) return ;
426
440
deleteToast ( ) ;
427
441
} }
@@ -465,7 +479,7 @@ const Toaster = (props: ToasterProps) => {
465
479
visibleToasts = VISIBLE_TOASTS_AMOUNT ,
466
480
toastOptions,
467
481
dir = getDocumentDirection ( ) ,
468
- gap,
482
+ gap = GAP ,
469
483
loadingIcon,
470
484
icons,
471
485
containerAriaLabel = 'Notifications' ,
@@ -703,4 +717,3 @@ const Toaster = (props: ToasterProps) => {
703
717
) ;
704
718
} ;
705
719
export { toast , Toaster , type ExternalToast , type ToastT } ;
706
-
0 commit comments