Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/SnackbarProvider/SnackbarProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

// call individual snackbar onClose callback passed through options parameter
const toBeClosed = this.state.snacks.find((item) => item.id === key);
if (isDefined(key) && toBeClosed && toBeClosed.onClose) {
toBeClosed.onClose(null, 'instructed', key);
toBeClosed.onClose(null, reason, key);
}

this.handleCloseSnack(null, 'instructed', key);
this.handleCloseSnack(null, reason, key);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ export interface SharedProps<V extends VariantType = VariantType> extends Partia
* @param {object} event The event source of the callback
* @param {string} reason Can be:`"timeout"` (`autoHideDuration` expired) or: `"maxsnack"`
* (snackbar was closed because `maxSnack` has reached) or: `"instructed"` (snackbar was
* closed programmatically)
* closed programmatically) or: any string if closeSnackbar had reason parameter
* @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
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

}

/**
Expand Down Expand Up @@ -405,7 +405,7 @@ interface EnqueueSnackbar {

export interface ProviderContext {
enqueueSnackbar: EnqueueSnackbar;
closeSnackbar: (key?: SnackbarKey) => void;
closeSnackbar: (key?: SnackbarKey, reason?: string) => void;
}

export declare class SnackbarProvider extends React.Component<SnackbarProviderProps> {
Expand Down