Skip to content

Commit 5084f9c

Browse files
committed
fix(snackbar): handler for close button
1 parent 993e741 commit 5084f9c

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

packages/snackbar/src/lib/element.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {LightDomMixin, LoggerMixin} from '@nexim/element';
33
import {html, LitElement, nothing, type PropertyValues, type TemplateResult} from 'lit';
44
import {customElement, property} from 'lit/decorators.js';
55

6-
import {snackbarActionButtonClickedSignal} from './signal.js';
6+
import {snackbarActionButtonClickedSignal, snackbarCloseButtonClickedSignal} from './signal.js';
77
import {waitForNextFrame} from './utils.js';
88

99
declare global {
@@ -46,6 +46,8 @@ export class SnackbarElement extends LightDomMixin(LoggerMixin(LitElement)) {
4646
/**
4747
* Close the snackbar and remove it from the DOM.
4848
* Waits for the closing animation to end before removing the element.
49+
*
50+
* @internal
4951
*/
5052
async close(): Promise<void> {
5153
this.logger_.logMethod?.('close');
@@ -56,6 +58,16 @@ export class SnackbarElement extends LightDomMixin(LoggerMixin(LitElement)) {
5658
this.remove();
5759
}
5860

61+
/**
62+
* Handle the close button click event.
63+
* Sends a signal when the action button is clicked.
64+
*/
65+
private closeButtonClickHandler__(): void {
66+
this.logger_.logMethod?.('closeButtonClickHandler__');
67+
68+
snackbarCloseButtonClickedSignal.notify();
69+
}
70+
5971
/**
6072
* Handle the action button click event.
6173
* Sends a signal when the action button is clicked.
@@ -101,7 +113,7 @@ export class SnackbarElement extends LightDomMixin(LoggerMixin(LitElement)) {
101113
this.logger_.logMethod?.('renderCloseButton__');
102114

103115
return html`
104-
<button class="close-button" @click=${this.close.bind(this)}>
116+
<button class="close-button" @click=${this.closeButtonClickHandler__.bind(this)}>
105117
<span class="alwatr-icon-font">close</span>
106118
</button>
107119
`;

packages/snackbar/src/lib/handler.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createLogger} from '@alwatr/logger';
22
import {parseDuration} from '@alwatr/parse-duration';
33
import {waitForTimeout} from '@alwatr/wait';
44

5-
import {snackbarActionButtonClickedSignal, snackbarSignal} from './signal.js';
5+
import {snackbarActionButtonClickedSignal, snackbarCloseButtonClickedSignal, snackbarSignal} from './signal.js';
66

77
import type {SnackbarElement} from './element.js';
88
import type {SnackbarActionHandler, SnackbarOptions} from './type.js';
@@ -19,6 +19,11 @@ let closeLastSnackbar: (() => Promise<void>) | null = null;
1919
*/
2020
let unsubscribeActionButtonHandler: (() => void) | null = null;
2121

22+
/**
23+
* Store the function to unsubscribe the close button handler after close or action button clicked.
24+
*/
25+
let unsubscribeCloseButtonHandler: (() => void) | null = null;
26+
2227
/**
2328
* Create snackbar element with given options.
2429
*
@@ -47,16 +52,20 @@ function createSnackbarElement(options: SnackbarOptions): SnackbarElement {
4752
* @param handler - Handler to be called when the action button is clicked.
4853
*/
4954
function handleActionButtonClick(closeSnackbar: () => Promise<void>, handler?: SnackbarActionHandler): void {
55+
logger.logMethod?.('handleActionButtonClick');
56+
5057
const actionButtonClickHandler = async () => {
5158
logger.logOther?.('Snackbar action button clicked.');
5259

5360
await handler?.();
5461
return closeSnackbar();
5562
};
5663

57-
// Subscribe to the action button click
64+
/**
65+
* Store the function to unsubscribe the action button handler after close or action button clicked.
66+
*/
5867
unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal.subscribe(actionButtonClickHandler.bind(null), {
59-
once: true,
68+
// once: true, // not work!
6069
}).unsubscribe;
6170
}
6271

@@ -80,13 +89,19 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
8089

8190
await element.close();
8291
unsubscribeActionButtonHandler?.();
92+
unsubscribeCloseButtonHandler?.();
93+
8394
closed = true;
8495
};
8596

8697
if (options.action != null) {
8798
handleActionButtonClick(closeSnackbar, options.action.handler);
8899
}
89100

101+
if (options.addCloseButton === true) {
102+
unsubscribeCloseButtonHandler = snackbarCloseButtonClickedSignal.subscribe(closeSnackbar.bind(null)).unsubscribe;
103+
}
104+
90105
// Close the last snackbar if it exists
91106
await closeLastSnackbar?.();
92107
closeLastSnackbar = closeSnackbar;

packages/snackbar/src/lib/signal.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import type {SnackbarOptions} from './type.js';
66
* Signal triggered when the snackbar action button is clicked to close snackbar.
77
*/
88
export const snackbarActionButtonClickedSignal = /* @__PURE__ */ new AlwatrTrigger({
9-
name: 'snackbar-action-button-clicked',
9+
name: 'snackbar-action-button-click',
10+
});
11+
12+
/**
13+
* Signal triggered when the snackbar close button is clicked to close snackbar.
14+
*/
15+
export const snackbarCloseButtonClickedSignal = /* @__PURE__ */ new AlwatrTrigger({
16+
name: 'snackbar-close-button-click',
1017
});
1118

1219
/**

0 commit comments

Comments
 (0)