Skip to content

Allow overriding "reason" in closeSnackbar #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Wersjon
Copy link

@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".
* @param {string|number|undefined} key key of a Snackbar. key will be `undefined` if closeSnackbar
* 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
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

@@ -255,14 +255,14 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
/**
* Close snackbar with the given key
*/
closeSnackbar: ProviderContext['closeSnackbar'] = (key) => {
closeSnackbar: ProviderContext['closeSnackbar'] = (key, reason = 'instructed') => {
Copy link
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.

1 participant