Skip to content

Allow overriding "reason" in closeSnackbar#620

Open
Wersjon wants to merge 1 commit intoiamhosseindhv:masterfrom
Wersjon:allow-for-reason-override
Open

Allow overriding "reason" in closeSnackbar#620
Wersjon wants to merge 1 commit intoiamhosseindhv:masterfrom
Wersjon:allow-for-reason-override

Conversation

@Wersjon
Copy link
Copy Markdown

@Wersjon Wersjon commented Apr 29, 2025

Resolving issue found in: #591

This PR allows you to override closeSnackbar reason, so you can act in onClose depending if user closed the snackbar himself or if it was for any other reason - like changing views and hiding snackbar.
This is useful if you want to be sure that user saw a snackbar, if user closed it himself you can save it to localStorage / DB.
The reason why you would want to close the snackbar while changing views is quite simple - context often matters and displaying same snackbar in different context may have different meaning.

Currently it is impossible to tell if a user intentionally closed a notification by clicking on the "close" button or if "closeSnackbar" was invoked from somewhere in the application for whatever reason. Both will deliver reason "instructed".

This is my first contribution ever, so unsure If I'm doing everything correctly.

Currently it is impossible to tell if a user intentionally closed a notification by clicking on the "close" button or if "closeSnackbar" was invoked from somewhere in the application for whatever reason. Both will deliver reason "instructed".
Comment thread src/types.ts
* is called with no key (user requested all the snackbars to be closed)
*/
onClose?: (event: React.SyntheticEvent<any> | null, reason: CloseReason, key?: SnackbarKey) => void;
onClose?: (event: React.SyntheticEvent<any> | null, reason: CloseReason | string, key?: SnackbarKey) => void;
Copy link
Copy Markdown
Author

@Wersjon Wersjon Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't edit the CloseReason is to think of it as "Official" reasons - something that's defined by the library itself and used internally.
Also, my thinking was that TS would auto-suggest the CloseReasons with intellisense - but also allow for any other string, but TS compiler seems to simplify it to string 🤔 microsoft/TypeScript#29729

Workaround would be to change the type to reason: CloseReason | (string & {}), but felt too hacky to include this in a PR, but it does work:
image

* Close snackbar with the given key
*/
closeSnackbar: ProviderContext['closeSnackbar'] = (key) => {
closeSnackbar: ProviderContext['closeSnackbar'] = (key, reason = 'instructed') => {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason has been set by default to 'instructed' to avoid any possible issues with migrating to next version.
this PR should be a minor change given that existing implementation don't change in behavior

@Wersjon Wersjon changed the title Allow for reason override in closeSnackbar Allow overriding "reason" in closeSnackbar Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants