Skip to content

Commit fbb04f2

Browse files
committed
refactor(snackbar): pass function for action handler
1 parent fa5250e commit fbb04f2

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

packages/snackbar/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ import {snackbarSignal} from '@nexim/snackbar';
3434

3535
snackbarSignal.notify({
3636
content: 'This is a snackbar message',
37-
// The following properties are optional.
3837
action: {
3938
label: 'Undo',
40-
signalId: 'undo-handler',
39+
handler: () => {
40+
console.log('Action button clicked');
41+
},
4142
},
4243
duration: '5s',
4344
addCloseButton: true,

packages/snackbar/src/lib/handler.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {waitForTimeout} from '@alwatr/wait';
55
import {snackbarActionButtonClickedSignal, snackbarSignal} from './signal.js';
66

77
import type {SnackbarElement} from './element.js';
8-
import type {SnackbarOptions} from './type.js';
8+
import type {SnackbarActionHandler, SnackbarOptions} from './type.js';
99

1010
const logger = createLogger(`${__package_name__}/handler`);
1111

@@ -43,13 +43,14 @@ function createSnackbarElement(options: SnackbarOptions): SnackbarElement {
4343
/**
4444
* Handle action button click.
4545
*
46-
* @param options - Options for configuring the snackbar.
4746
* @param closeSnackbar - Function to close the snackbar.
47+
* @param handler - Handler to be called when the action button is clicked.
4848
*/
49-
function handleActionButtonClick(closeSnackbar: () => Promise<void>): void {
50-
const actionButtonClickHandler = () => {
49+
function handleActionButtonClick(closeSnackbar: () => Promise<void>, handler?: SnackbarActionHandler): void {
50+
const actionButtonClickHandler = async () => {
5151
logger.logOther?.('Snackbar action button clicked.');
5252

53+
await handler?.();
5354
return closeSnackbar();
5455
};
5556

@@ -83,7 +84,7 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
8384
};
8485

8586
if (options.action != null) {
86-
handleActionButtonClick(closeSnackbar);
87+
handleActionButtonClick(closeSnackbar, options.action.handler);
8788
}
8889

8990
// Close the last snackbar if it exists

packages/snackbar/src/lib/type.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
import type {Duration} from '@alwatr/parse-duration';
22

33
/**
4-
* @property content - Content to be displayed in the snackbar.
5-
* @property [action] - The action button configuration.
6-
* @property action.label - The label for the action button.
7-
* @property action.signalId - The signal ID to be emitted when the action button is clicked.
8-
* @property duration - Duration for which the snackbar is displayed. `infinite` for infinite duration.
9-
* @property addCloseButton - Whether to add a close button to the snackbar.
4+
* Snackbar action handler.
5+
*/
6+
export type SnackbarActionHandler = () => Promise<void> | void;
7+
8+
/**
9+
* Snackbar config.
1010
*/
1111
export type SnackbarOptions = {
12+
/**
13+
* Content to be displayed in the snackbar.
14+
*/
1215
content: string;
16+
17+
/**
18+
* The action button configuration.
19+
*/
1320
action?: {
14-
signalId: string;
21+
/**
22+
* The signal ID to be emitted when the action button is clicked.
23+
*/
24+
handler: SnackbarActionHandler;
25+
26+
/**
27+
* The label for the action button.
28+
*/
1529
label: string;
1630
};
31+
32+
/**
33+
* Duration for which the snackbar is displayed. `infinite` for infinite duration.
34+
*/
1735
duration?: Duration | 'infinite';
36+
37+
/**
38+
* Whether to add a close button to the snackbar.
39+
*/
1840
addCloseButton?: boolean;
1941
};

0 commit comments

Comments
 (0)