Skip to content
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

[useSnackbar] Add explicit return type #36052

Merged
merged 10 commits into from
Mar 6, 2023
17 changes: 16 additions & 1 deletion docs/pages/base/api/use-snackbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@
"ref": { "type": { "name": "React.Ref<any&gt", "description": "React.Ref<any&gt" } },
"resumeHideDuration": { "type": { "name": "number", "description": "number" } }
},
"returnValue": {},
"returnValue": {
"getRootProps": {
"type": {
"name": "<TOther extends Record<string, ((event: any) =&gt void) | undefined&gt = {}&gt(externalProps?: TOther) =&gt UseSnackbarRootSlotProps<TOther&gt",
"description": "<TOther extends Record<string, ((event: any) =&gt void) | undefined&gt = {}&gt(externalProps?: TOther) =&gt UseSnackbarRootSlotProps<TOther&gt"
},
"required": true
},
"onClickAway": {
"type": {
"name": "(event: React.SyntheticEvent<any&gt | Event) =&gt void",
"description": "(event: React.SyntheticEvent<any&gt | Event) =&gt void"
},
"required": true
}
},
"name": "useSnackbar",
"filename": "/packages/mui-base/src/useSnackbar/useSnackbar.ts",
"demos": "<ul><li><a href=\"/base/react-snackbar/#hook\">Unstyled Snackbar</a></li></ul>"
Expand Down
5 changes: 4 additions & 1 deletion docs/translations/api-docs/use-snackbar/use-snackbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"open": "If <code>true</code>, the component is shown.",
"resumeHideDuration": "The number of milliseconds to wait before dismissing after user interaction.\nIf <code>autoHideDuration</code> prop isn't specified, it does nothing.\nIf <code>autoHideDuration</code> prop is specified but <code>resumeHideDuration</code> isn't,\nwe default to <code>autoHideDuration / 2</code> ms."
},
"returnValueDescriptions": {}
"returnValueDescriptions": {
"getRootProps": "Resolver for the root slot's props.",
"onClickAway": "Callback fired when a \"click away\" event is detected."
}
}
12 changes: 7 additions & 5 deletions packages/mui-base/src/useSnackbar/useSnackbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { unstable_useEventCallback as useEventCallback } from '@mui/utils';
import {
UseSnackbarParameters,
SnackbarCloseReason,
UseSnackbarRootSlotProps,
UseSnackbarReturnValue,
} from './useSnackbar.types';
import extractEventHandlers from '../utils/extractEventHandlers';

Expand All @@ -18,7 +18,7 @@ import extractEventHandlers from '../utils/extractEventHandlers';
*
* - [useSnackbar API](https://mui.com/base/api/use-snackbar/)
*/
export default function useSnackbar(parameters: UseSnackbarParameters) {
export default function useSnackbar(parameters: UseSnackbarParameters): UseSnackbarReturnValue {
const {
autoHideDuration = null,
disableWindowBlurListener = false,
Expand Down Expand Up @@ -145,9 +145,11 @@ export default function useSnackbar(parameters: UseSnackbarParameters) {
return undefined;
}, [disableWindowBlurListener, handleResume, open]);

const getRootProps = <TOther extends Record<string, React.EventHandler<any> | undefined> = {}>(
otherHandlers: TOther = {} as TOther,
): UseSnackbarRootSlotProps<TOther> => {
const getRootProps: UseSnackbarReturnValue['getRootProps'] = <
TOther extends Parameters<UseSnackbarReturnValue['getRootProps']>[0],
>(
otherHandlers: TOther,
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
) => {
const propsEventHandlers = extractEventHandlers(parameters) as Partial<UseSnackbarParameters>;
const externalEventHandlers = {
...propsEventHandlers,
Expand Down
15 changes: 15 additions & 0 deletions packages/mui-base/src/useSnackbar/useSnackbar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ export interface UseSnackbarRootSlotOwnProps {
ref?: React.Ref<any>;
role: React.AriaRole;
}

export interface UseSnackbarReturnValue {
/**
* Resolver for the root slot's props.
* @param externalProps props for the root slot
* @returns props that should be spread on the root slot
*/
getRootProps: <TOther extends Record<string, ((event: any) => void) | undefined> = {}>(
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
externalProps?: TOther,
) => UseSnackbarRootSlotProps<TOther>;
/**
* Callback fired when a "click away" event is detected.
*/
onClickAway: (event: React.SyntheticEvent<any> | Event) => void;
}