@@ -22,6 +22,18 @@ type ToastArg = ToastConfig & {
22
22
* // second toast: 'Hello (2)'
23
23
*/
24
24
withCount ?: boolean ;
25
+ /**
26
+ * Whether to update the description when updating the toast. Defaults to true.
27
+ * @example
28
+ * // updateDescription: true
29
+ * toast({ title: 'Hello', description: 'Foo' }); // Foo
30
+ * toast({ title: 'Hello', description: 'Bar' }); // Bar
31
+ * @example
32
+ * // updateDescription: false
33
+ * toast({ title: 'Hello', description: 'Foo' }); // Foo
34
+ * toast({ title: 'Hello', description: 'Bar' }); // Foo
35
+ */
36
+ updateDescription ?: boolean ;
25
37
} ;
26
38
27
39
type ToastInternalState = {
@@ -48,6 +60,7 @@ const getGetState = (id: string) => () => $toastMap.get()[id] ?? null;
48
60
/**
49
61
* Creates a toast with the given config. If the toast with the same id already exists, it will be updated.
50
62
* When a toast is updated, its title, description, status and duration will be overwritten by the new config.
63
+ * Use `updateDescription: false` to keep the description when updating.
51
64
* Set duration to `null` to make the toast persistent.
52
65
* @param arg The toast config.
53
66
* @returns An object with methods to get the toast state, close the toast and check if the toast is active
@@ -61,17 +74,20 @@ export const toast = (arg: ToastArg): ToastApi => {
61
74
if ( arg . withCount === undefined ) {
62
75
arg . withCount = true ;
63
76
}
77
+ if ( arg . updateDescription === undefined ) {
78
+ arg . updateDescription = true ;
79
+ }
64
80
let state = $toastMap . get ( ) [ arg . id ] ;
65
81
if ( ! state ) {
66
82
// First time caller, create and set the state
67
- state = { id, config : parseConfig ( id , arg , 1 ) , count : 1 } ;
83
+ state = { id, config : parseConfig ( null , id , arg , 1 ) , count : 1 } ;
68
84
$toastMap . setKey ( id , state ) ;
69
85
// Create the toast
70
86
toastApi ( state . config ) ;
71
87
} else {
72
88
// This toast is already active, update its state
73
89
state . count += 1 ;
74
- state . config = parseConfig ( id , arg , state . count ) ;
90
+ state . config = parseConfig ( state , id , arg , state . count ) ;
75
91
$toastMap . setKey ( id , state ) ;
76
92
// Update the toast itself
77
93
toastApi . update ( id , state . config ) ;
@@ -81,18 +97,26 @@ export const toast = (arg: ToastArg): ToastApi => {
81
97
82
98
/**
83
99
* Give a toast id, arg and current count, returns the parsed toast config (including dynamic title and description)
100
+ * @param state The current state of the toast or null if it doesn't exist
84
101
* @param id The id of the toast
85
102
* @param arg The arg passed to the toast function
86
103
* @param count The current call count of the toast
87
104
* @returns The parsed toast config
88
105
*/
89
- const parseConfig = ( id : string , arg : ToastArg , count : number ) : ToastConfig => {
90
- const title = arg . withCount && count > 1 ? `${ arg . title } (${ count } )` : arg . title ;
106
+ const parseConfig = ( state : ToastInternalState | null , id : string , arg : ToastArg , count : number ) : ToastConfig => {
91
107
const onCloseComplete = ( ) => {
92
108
$toastMap . setKey ( id , undefined ) ;
93
109
if ( arg . onCloseComplete ) {
94
110
arg . onCloseComplete ( ) ;
95
111
}
96
112
} ;
97
- return { ...arg , title, onCloseComplete } ;
113
+ const title = arg . withCount && count > 1 ? `${ arg . title } (${ count } )` : arg . title ;
114
+ const description = ! arg . updateDescription && state ? state . config . description : arg . description ;
115
+ const config : ToastConfig = {
116
+ ...arg ,
117
+ title,
118
+ description,
119
+ onCloseComplete,
120
+ } ;
121
+ return config ;
98
122
} ;
0 commit comments