@@ -4,6 +4,7 @@ import {waitForTimeout} from '@alwatr/wait';
4
4
5
5
import { snackbarActionButtonClickedSignal , snackbarSignal } from './signal.js' ;
6
6
7
+ import type { SnackbarElement } from './element.js' ;
7
8
import type { SnackbarOptions } from './type.js' ;
8
9
9
10
const logger = createLogger ( `${ __package_name__ } /handler` ) ;
@@ -19,18 +20,13 @@ let closeLastSnackbar: (() => Promise<void>) | null = null;
19
20
let unsubscribeActionButtonHandler : ( ( ) => void ) | null = null ;
20
21
21
22
/**
22
- * Displays the snackbar with the given options.
23
+ * Create snackbar element with given options.
23
24
*
24
25
* @param options - Options for configuring the snackbar.
26
+ * @returns The created snackbar element.
25
27
*/
26
- async function showSnackbar ( options : SnackbarOptions ) : Promise < void > {
27
- logger . logMethodArgs ?.( 'showSnackbar' , { options} ) ;
28
-
29
- // Set default duration if not provided
30
- options . duration ??= '5s' ;
31
-
28
+ function createSnackbarElement ( options : SnackbarOptions ) : SnackbarElement {
32
29
const element = document . createElement ( 'snack-bar' ) ;
33
-
34
30
element . setAttribute ( 'content' , options . content ) ;
35
31
36
32
if ( options . addCloseButton === true ) {
@@ -39,19 +35,43 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
39
35
40
36
if ( options . action != null ) {
41
37
element . setAttribute ( 'action-button-label' , options . action . label ) ;
38
+ }
42
39
43
- const actionButtonClickHandler = ( event : { id : string } ) => {
44
- if ( event . id !== options . action ! . id ) return ;
45
- logger . logOther ?.( 'Snackbar action button clicked.' , event ) ;
40
+ return element ;
41
+ }
46
42
47
- return closeSnackbar ( ) ;
48
- } ;
43
+ /**
44
+ * Handle action button click.
45
+ *
46
+ * @param options - Options for configuring the snackbar.
47
+ * @param closeSnackbar - Function to close the snackbar.
48
+ */
49
+ function handleActionButtonClick ( options : SnackbarOptions , closeSnackbar : ( ) => Promise < void > ) : void {
50
+ const actionButtonClickHandler = ( event : { id : string } ) => {
51
+ if ( event . id !== options . action ! . id ) return ;
52
+ logger . logOther ?.( 'Snackbar action button clicked.' , event ) ;
49
53
50
- // Subscribe to the action button click
51
- unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal . subscribe ( actionButtonClickHandler . bind ( null ) , {
52
- once : true ,
53
- } ) . unsubscribe ;
54
- }
54
+ return closeSnackbar ( ) ;
55
+ } ;
56
+
57
+ // Subscribe to the action button click
58
+ unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal . subscribe ( actionButtonClickHandler . bind ( null ) , {
59
+ once : true ,
60
+ } ) . unsubscribe ;
61
+ }
62
+
63
+ /**
64
+ * Displays the snackbar with the given options.
65
+ *
66
+ * @param options - Options for configuring the snackbar.
67
+ */
68
+ async function showSnackbar ( options : SnackbarOptions ) : Promise < void > {
69
+ logger . logMethodArgs ?.( 'showSnackbar' , { options} ) ;
70
+
71
+ // Set default duration if not provided
72
+ options . duration ??= '5s' ;
73
+
74
+ const element = createSnackbarElement ( options ) ;
55
75
56
76
let closed = false ;
57
77
const closeSnackbar = async ( ) => {
@@ -63,6 +83,10 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
63
83
closed = true ;
64
84
} ;
65
85
86
+ if ( options . action != null ) {
87
+ handleActionButtonClick ( options , closeSnackbar ) ;
88
+ }
89
+
66
90
// Close the last snackbar if it exists
67
91
await closeLastSnackbar ?.( ) ;
68
92
closeLastSnackbar = closeSnackbar ;
0 commit comments