@@ -9,12 +9,10 @@ const logger = createLogger(`${__package_name__}/handler`);
9
9
10
10
/**
11
11
* @property content - Content to be displayed in the snackbar.
12
- * @property { action } - The action button configuration.
12
+ * @property [ action] - The action button configuration.
13
13
* @property action.label - The label for the action button.
14
14
* @property action.handler - The handler function for the action button.
15
- * @property duration - Duration for which the snackbar is displayed. `-1` for infinite duration.
16
- * Duration for which the snackbar is displayed.
17
- * `-1` for infinite duration.
15
+ * @property duration - Duration for which the snackbar is displayed. `infinite` for infinite duration.
18
16
* @property addCloseButton - Whether to add a close button to the snackbar.
19
17
*/
20
18
export type SnackbarOptions = {
@@ -28,7 +26,16 @@ export type SnackbarOptions = {
28
26
} ;
29
27
30
28
/**
31
- * Signal for when the snackbar action button is clicked.
29
+ * Signal triggered when the snackbar action button is clicked.
30
+ *
31
+ * This signal is used to notify listeners that the action button
32
+ * on the snackbar component has been clicked. It can be used to
33
+ * perform any necessary actions in response to the button click.
34
+ *
35
+ * @example
36
+ * snackbarActionButtonClickedSignal.addListener(() => {
37
+ * console.log('Snackbar action button was clicked!');
38
+ * });
32
39
*/
33
40
export const snackbarActionButtonClickedSignal = new AlwatrTrigger ( {
34
41
name : 'snackbar-action-button-clicked' ,
@@ -42,14 +49,13 @@ export const snackbarActionButtonClickedSignal = new AlwatrTrigger({
42
49
*
43
50
* snackbarSignal.notify({
44
51
* content: 'This is a snackbar message',
45
- * // The following properties are optional.
46
52
* action: {
47
53
* label: 'Undo',
48
54
* handler: () => {
49
55
* console.log('Action button clicked');
50
56
* },
51
57
* },
52
- * duration: '4s ',
58
+ * duration: '5s ',
53
59
* addCloseButton: true,
54
60
* });
55
61
*/
@@ -65,16 +71,14 @@ let unsubscribeActionButtonHandler: (() => void) | null = null;
65
71
66
72
/**
67
73
* Displays the snackbar with the given options.
74
+ *
68
75
* @param options - Options for configuring the snackbar.
69
76
*/
70
77
async function showSnackbar ( options : SnackbarOptions ) : Promise < void > {
71
78
logger . logMethodArgs ?.( 'showSnackbar' , { options} ) ;
72
79
73
- // Parse the duration
74
- if ( options . duration != null ) options . duration = parseDuration ( options . duration ) ;
75
-
76
80
// Set default duration if not provided
77
- options . duration = parseDuration ( '4s' ) ;
81
+ options . duration ??= '5s' ;
78
82
79
83
const element = document . createElement ( 'snack-bar' ) as SnackbarComponent ;
80
84
@@ -89,14 +93,16 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
89
93
90
94
// Subscribe to the action button click
91
95
unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal . subscribe ( ( ) => {
96
+ logger . logOther ?.( 'Snackbar action button clicked.' ) ;
97
+
92
98
options . action ! . handler ( ) ;
93
99
94
- return closeSnackbar_ ( ) ;
100
+ return closeSnackbar ( ) ;
95
101
} ) . unsubscribe ;
96
102
}
97
103
98
104
let closed = false ;
99
- const closeSnackbar_ = async ( ) => {
105
+ const closeSnackbar = async ( ) => {
100
106
if ( closed === true ) return ;
101
107
logger . logMethodArgs ?.( 'closeSnackbar' , { options} ) ;
102
108
@@ -107,11 +113,11 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
107
113
108
114
// Close the last snackbar if it exists
109
115
await closeLastSnackbar ?.( ) ;
110
- closeLastSnackbar = closeSnackbar_ ;
116
+ closeLastSnackbar = closeSnackbar ;
111
117
document . body . appendChild ( element ) ;
112
118
113
119
// Set a timeout to close the snackbar if duration is not infinite
114
- if ( options . duration !== - 1 ) {
115
- waitForTimeout ( parseDuration ( options . duration ) ) . then ( closeSnackbar_ ) ;
120
+ if ( options . duration !== 'infinite' ) {
121
+ waitForTimeout ( parseDuration ( options . duration ) ) . then ( closeSnackbar ) ;
116
122
}
117
123
}
0 commit comments