-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snackbar): command signal and controller
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {commandHandler} from '@alwatr/signal'; | ||
|
||
import type {AlwatrSnackbar} from './element.js'; | ||
import type {SnackbarOptions, SnackbarResponse} from './type.js'; | ||
|
||
const currentSnackbar: AlwatrSnackbar | null = null; | ||
|
||
const _showSnackbar = (options: SnackbarOptions): SnackbarResponse => { | ||
options.duration ??= 5; | ||
// TODO: create new alwatr-snackbar and append to body | ||
alert(options.message); | ||
return {}; | ||
}; | ||
|
||
commandHandler.define<SnackbarOptions, SnackbarResponse>('show-snackbar-command', _showSnackbar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {commandTrigger} from '@alwatr/signal'; | ||
import {MaybePromise} from '@alwatr/type'; | ||
|
||
import type {SnackbarOptions, SnackbarResponse} from './type.js'; | ||
|
||
export const snackbarSignalTrigger = commandTrigger.bind<SnackbarOptions, SnackbarResponse>('show-snackbar-command'); | ||
|
||
/** | ||
* Show snackbar with optional action. | ||
* | ||
* Example: | ||
* | ||
* Simple toast: | ||
* ```ts | ||
* showSnackbar({message: 'Form submitted successfully.'}); | ||
* ``` | ||
* | ||
* With action label: | ||
* ```ts | ||
* const response = await showSnackbar({ | ||
* message: 'Email archived.', | ||
* actionLabel: 'Undo', | ||
* }); | ||
* if (response.actionClicked) { | ||
* // undo... | ||
* } | ||
* ``` | ||
*/ | ||
export function showSnackbar(options: SnackbarOptions): MaybePromise<SnackbarResponse> { | ||
if (!options.actionLabel) { | ||
return snackbarSignalTrigger.requestWithResponse(options); | ||
} | ||
// else | ||
snackbarSignalTrigger.request(options); | ||
return {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export type SnackbarOptions = { | ||
/** | ||
* Snackbar message. | ||
*/ | ||
message: string; | ||
|
||
/** | ||
* Snackbar action button label text. | ||
*/ | ||
actionLabel?: string; | ||
|
||
/** | ||
* Snackbar automatically disappear from the screen after a minimum of 4 seconds, and a maximum of 10 seconds. | ||
* | ||
* If duration is `-1` and `actionLabel` defined, snackbar remain until action button clicked. | ||
* | ||
* @default 5s | ||
*/ | ||
duration?: number; | ||
} | ||
|
||
export type SnackbarResponse = { | ||
actionButton?: true; | ||
} |