Skip to content

Commit 395bd9c

Browse files
njfamirmarashagp
andcommitted
refactor(snackbar): review and enhance
Co-authored-by: arashagp <[email protected]>
1 parent 34ef497 commit 395bd9c

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

packages/snackbar/src/lib/handler.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ const logger = createLogger(`${__package_name__}/handler`);
99

1010
/**
1111
* @property content - Content to be displayed in the snackbar.
12-
* @property {action} - The action button configuration.
12+
* @property [action] - The action button configuration.
1313
* @property action.label - The label for the action button.
1414
* @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.
1816
* @property addCloseButton - Whether to add a close button to the snackbar.
1917
*/
2018
export type SnackbarOptions = {
@@ -28,7 +26,16 @@ export type SnackbarOptions = {
2826
};
2927

3028
/**
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+
* });
3239
*/
3340
export const snackbarActionButtonClickedSignal = new AlwatrTrigger({
3441
name: 'snackbar-action-button-clicked',
@@ -42,14 +49,13 @@ export const snackbarActionButtonClickedSignal = new AlwatrTrigger({
4249
*
4350
* snackbarSignal.notify({
4451
* content: 'This is a snackbar message',
45-
* // The following properties are optional.
4652
* action: {
4753
* label: 'Undo',
4854
* handler: () => {
4955
* console.log('Action button clicked');
5056
* },
5157
* },
52-
* duration: '4s',
58+
* duration: '5s',
5359
* addCloseButton: true,
5460
* });
5561
*/
@@ -65,16 +71,14 @@ let unsubscribeActionButtonHandler: (() => void) | null = null;
6571

6672
/**
6773
* Displays the snackbar with the given options.
74+
*
6875
* @param options - Options for configuring the snackbar.
6976
*/
7077
async function showSnackbar(options: SnackbarOptions): Promise<void> {
7178
logger.logMethodArgs?.('showSnackbar', {options});
7279

73-
// Parse the duration
74-
if (options.duration != null) options.duration = parseDuration(options.duration);
75-
7680
// Set default duration if not provided
77-
options.duration = parseDuration('4s');
81+
options.duration ??= '5s';
7882

7983
const element = document.createElement('snack-bar') as SnackbarComponent;
8084

@@ -89,14 +93,16 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
8993

9094
// Subscribe to the action button click
9195
unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal.subscribe(() => {
96+
logger.logOther?.('Snackbar action button clicked.');
97+
9298
options.action!.handler();
9399

94-
return closeSnackbar_();
100+
return closeSnackbar();
95101
}).unsubscribe;
96102
}
97103

98104
let closed = false;
99-
const closeSnackbar_ = async () => {
105+
const closeSnackbar = async () => {
100106
if (closed === true) return;
101107
logger.logMethodArgs?.('closeSnackbar', {options});
102108

@@ -107,11 +113,11 @@ async function showSnackbar(options: SnackbarOptions): Promise<void> {
107113

108114
// Close the last snackbar if it exists
109115
await closeLastSnackbar?.();
110-
closeLastSnackbar = closeSnackbar_;
116+
closeLastSnackbar = closeSnackbar;
111117
document.body.appendChild(element);
112118

113119
// 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);
116122
}
117123
}

0 commit comments

Comments
 (0)