Allow overriding "reason" in closeSnackbar#620
Allow overriding "reason" in closeSnackbar#620Wersjon wants to merge 1 commit intoiamhosseindhv:masterfrom
Conversation
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".
| * 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; |
There was a problem hiding this comment.
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:

| * Close snackbar with the given key | ||
| */ | ||
| closeSnackbar: ProviderContext['closeSnackbar'] = (key) => { | ||
| closeSnackbar: ProviderContext['closeSnackbar'] = (key, reason = 'instructed') => { |
There was a problem hiding this comment.
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
Resolving issue found in: #591
This PR allows you to override closeSnackbar reason, so you can act in
onClosedepending 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.