|
| 1 | +import {AlwatrSignal, AlwatrTrigger} from '@alwatr/flux'; |
| 2 | +import {createLogger} from '@alwatr/logger'; |
| 3 | +import {parseDuration, type Duration} from '@alwatr/parse-duration'; |
| 4 | +import {waitForTimeout} from '@alwatr/wait'; |
| 5 | + |
| 6 | +import type {SnackbarComponent} from './element.js'; |
| 7 | + |
| 8 | +const logger = createLogger(`${__package_name__}/handler`); |
| 9 | + |
| 10 | +/** |
| 11 | + * @property content - Content to be displayed in the snackbar. |
| 12 | + * @property {action} - The action button configuration. |
| 13 | + * @property action.label - The label for the action button. |
| 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. |
| 18 | + * @property addCloseButton - Whether to add a close button to the snackbar. |
| 19 | + */ |
| 20 | +export type SnackbarOptions = { |
| 21 | + content: string; |
| 22 | + action?: { |
| 23 | + label: string; |
| 24 | + handler: () => void; |
| 25 | + }; |
| 26 | + duration?: Duration; |
| 27 | + addCloseButton?: boolean; |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Signal for when the snackbar action button is clicked. |
| 32 | + */ |
| 33 | +export const snackbarActionButtonClickedSignal = new AlwatrTrigger({ |
| 34 | + name: 'snackbar-action-button-clicked', |
| 35 | +}); |
| 36 | + |
| 37 | +/** |
| 38 | + * Signal for displaying the snackbar. |
| 39 | + * |
| 40 | + * @example |
| 41 | + * import {snackbarSignal} from '@nexim/snackbar'; |
| 42 | + * |
| 43 | + * snackbarSignal.notify({ |
| 44 | + * content: 'This is a snackbar message', |
| 45 | + * // The following properties are optional. |
| 46 | + * action: { |
| 47 | + * label: 'Undo', |
| 48 | + * handler: () => { |
| 49 | + * console.log('Action button clicked'); |
| 50 | + * }, |
| 51 | + * }, |
| 52 | + * duration: '4s', |
| 53 | + * addCloseButton: true, |
| 54 | + * }); |
| 55 | + */ |
| 56 | +export const snackbarSignal = new AlwatrSignal<SnackbarOptions>({name: 'snackbar'}); |
| 57 | + |
| 58 | +// Subscribe to the snackbar signal to show the snackbar when the signal is emitted. |
| 59 | +snackbarSignal.subscribe((options) => { |
| 60 | + showSnackbar(options); |
| 61 | +}); |
| 62 | + |
| 63 | +let closeLastSnackbar: (() => Promise<void>) | null = null; |
| 64 | +let unsubscribeActionButtonHandler: (() => void) | null = null; |
| 65 | + |
| 66 | +/** |
| 67 | + * Displays the snackbar with the given options. |
| 68 | + * @param options - Options for configuring the snackbar. |
| 69 | + */ |
| 70 | +async function showSnackbar(options: SnackbarOptions): Promise<void> { |
| 71 | + logger.logMethodArgs?.('showSnackbar', {options}); |
| 72 | + |
| 73 | + // Parse the duration |
| 74 | + if (options.duration != null) options.duration = parseDuration(options.duration); |
| 75 | + |
| 76 | + // Set default duration if not provided |
| 77 | + options.duration = parseDuration('4s'); |
| 78 | + |
| 79 | + const element = document.createElement('snack-bar') as SnackbarComponent; |
| 80 | + |
| 81 | + element.setAttribute('content', options.content); |
| 82 | + |
| 83 | + if (options.addCloseButton === true) { |
| 84 | + element.setAttribute('add-close-button', ''); |
| 85 | + } |
| 86 | + |
| 87 | + if (options.action != null) { |
| 88 | + element.setAttribute('action-button-label', options.action.label); |
| 89 | + |
| 90 | + // Subscribe to the action button click |
| 91 | + unsubscribeActionButtonHandler = snackbarActionButtonClickedSignal.subscribe(() => { |
| 92 | + options.action!.handler(); |
| 93 | + |
| 94 | + return closeSnackbar_(); |
| 95 | + }).unsubscribe; |
| 96 | + } |
| 97 | + |
| 98 | + let closed = false; |
| 99 | + const closeSnackbar_ = async () => { |
| 100 | + if (closed === true) return; |
| 101 | + logger.logMethodArgs?.('closeSnackbar', {options}); |
| 102 | + |
| 103 | + await element.close(); |
| 104 | + unsubscribeActionButtonHandler?.(); |
| 105 | + closed = true; |
| 106 | + }; |
| 107 | + |
| 108 | + // Close the last snackbar if it exists |
| 109 | + await closeLastSnackbar?.(); |
| 110 | + closeLastSnackbar = closeSnackbar_; |
| 111 | + document.body.appendChild(element); |
| 112 | + |
| 113 | + // Set a timeout to close the snackbar if duration is not infinite |
| 114 | + if (options.duration !== -1) { |
| 115 | + waitForTimeout(parseDuration(options.duration)).then(closeSnackbar_); |
| 116 | + } |
| 117 | +} |
0 commit comments