pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { useSnackbar } from './useSnackbar';
|
||||
export * from './useSnackbar.types';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export { useSnackbar } from './useSnackbar';
|
||||
export * from './useSnackbar.types';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useSnackbar/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import { UseSnackbarParameters, UseSnackbarReturnValue } from './useSnackbar.types';
|
||||
/**
|
||||
* The basic building block for creating custom snackbar.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Snackbar](https://mui.com/base-ui/react-snackbar/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSnackbar API](https://mui.com/base-ui/react-snackbar/hooks-api/#use-snackbar)
|
||||
*/
|
||||
export declare function useSnackbar(parameters?: UseSnackbarParameters): UseSnackbarReturnValue;
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useEventCallback as useEventCallback, unstable_useTimeout as useTimeout } from '@mui/utils';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
/**
|
||||
* The basic building block for creating custom snackbar.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Snackbar](https://mui.com/base-ui/react-snackbar/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSnackbar API](https://mui.com/base-ui/react-snackbar/hooks-api/#use-snackbar)
|
||||
*/
|
||||
export function useSnackbar(parameters = {}) {
|
||||
const {
|
||||
autoHideDuration = null,
|
||||
disableWindowBlurListener = false,
|
||||
onClose,
|
||||
open,
|
||||
resumeHideDuration
|
||||
} = parameters;
|
||||
const timerAutoHide = useTimeout();
|
||||
React.useEffect(() => {
|
||||
if (!open) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {KeyboardEvent} nativeEvent
|
||||
*/
|
||||
function handleKeyDown(nativeEvent) {
|
||||
if (!nativeEvent.defaultPrevented) {
|
||||
// IE11, Edge (prior to using Blink?) use 'Esc'
|
||||
if (nativeEvent.key === 'Escape' || nativeEvent.key === 'Esc') {
|
||||
// not calling `preventDefault` since we don't know if people may ignore this event e.g. a permanently open snackbar
|
||||
onClose == null || onClose(nativeEvent, 'escapeKeyDown');
|
||||
}
|
||||
}
|
||||
}
|
||||
document.addEventListener('keydown', handleKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener('keydown', handleKeyDown);
|
||||
};
|
||||
}, [open, onClose]);
|
||||
const handleClose = useEventCallback((event, reason) => {
|
||||
onClose == null || onClose(event, reason);
|
||||
});
|
||||
const setAutoHideTimer = useEventCallback(autoHideDurationParam => {
|
||||
if (!onClose || autoHideDurationParam == null) {
|
||||
return;
|
||||
}
|
||||
timerAutoHide.start(autoHideDurationParam, () => {
|
||||
handleClose(null, 'timeout');
|
||||
});
|
||||
});
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
setAutoHideTimer(autoHideDuration);
|
||||
}
|
||||
return timerAutoHide.clear;
|
||||
}, [open, autoHideDuration, setAutoHideTimer, timerAutoHide]);
|
||||
const handleClickAway = event => {
|
||||
onClose == null || onClose(event, 'clickaway');
|
||||
};
|
||||
|
||||
// Pause the timer when the user is interacting with the Snackbar
|
||||
// or when the user hide the window.
|
||||
const handlePause = timerAutoHide.clear;
|
||||
|
||||
// Restart the timer when the user is no longer interacting with the Snackbar
|
||||
// or when the window is shown back.
|
||||
const handleResume = React.useCallback(() => {
|
||||
if (autoHideDuration != null) {
|
||||
setAutoHideTimer(resumeHideDuration != null ? resumeHideDuration : autoHideDuration * 0.5);
|
||||
}
|
||||
}, [autoHideDuration, resumeHideDuration, setAutoHideTimer]);
|
||||
const createHandleBlur = otherHandlers => event => {
|
||||
const onBlurCallback = otherHandlers.onBlur;
|
||||
onBlurCallback == null || onBlurCallback(event);
|
||||
handleResume();
|
||||
};
|
||||
const createHandleFocus = otherHandlers => event => {
|
||||
const onFocusCallback = otherHandlers.onFocus;
|
||||
onFocusCallback == null || onFocusCallback(event);
|
||||
handlePause();
|
||||
};
|
||||
const createMouseEnter = otherHandlers => event => {
|
||||
const onMouseEnterCallback = otherHandlers.onMouseEnter;
|
||||
onMouseEnterCallback == null || onMouseEnterCallback(event);
|
||||
handlePause();
|
||||
};
|
||||
const createMouseLeave = otherHandlers => event => {
|
||||
const onMouseLeaveCallback = otherHandlers.onMouseLeave;
|
||||
onMouseLeaveCallback == null || onMouseLeaveCallback(event);
|
||||
handleResume();
|
||||
};
|
||||
React.useEffect(() => {
|
||||
// TODO: window global should be refactored here
|
||||
if (!disableWindowBlurListener && open) {
|
||||
window.addEventListener('focus', handleResume);
|
||||
window.addEventListener('blur', handlePause);
|
||||
return () => {
|
||||
window.removeEventListener('focus', handleResume);
|
||||
window.removeEventListener('blur', handlePause);
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
}, [disableWindowBlurListener, open, handleResume, handlePause]);
|
||||
const getRootProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = _extends({}, extractEventHandlers(parameters), extractEventHandlers(externalProps));
|
||||
return _extends({
|
||||
// ClickAwayListener adds an `onClick` prop which results in the alert not being announced.
|
||||
// See https://github.com/mui/material-ui/issues/29080
|
||||
role: 'presentation'
|
||||
}, externalProps, externalEventHandlers, {
|
||||
onBlur: createHandleBlur(externalEventHandlers),
|
||||
onFocus: createHandleFocus(externalEventHandlers),
|
||||
onMouseEnter: createMouseEnter(externalEventHandlers),
|
||||
onMouseLeave: createMouseLeave(externalEventHandlers)
|
||||
});
|
||||
};
|
||||
return {
|
||||
getRootProps,
|
||||
onClickAway: handleClickAway
|
||||
};
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/// <reference types="react" />
|
||||
export type SnackbarCloseReason = 'timeout' | 'clickaway' | 'escapeKeyDown';
|
||||
export interface UseSnackbarParameters {
|
||||
/**
|
||||
* The number of milliseconds to wait before automatically calling the
|
||||
* `onClose` function. `onClose` should then set the state of the `open`
|
||||
* prop to hide the Snackbar. This behavior is disabled by default with
|
||||
* the `null` value.
|
||||
* @default null
|
||||
*/
|
||||
autoHideDuration?: number | null;
|
||||
/**
|
||||
* If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
|
||||
* @default false
|
||||
*/
|
||||
disableWindowBlurListener?: boolean;
|
||||
/**
|
||||
* Callback fired when the component requests to be closed.
|
||||
* Typically `onClose` is used to set state in the parent component,
|
||||
* which is used to control the `Snackbar` `open` prop.
|
||||
* The `reason` parameter can optionally be used to control the response to `onClose`,
|
||||
* for example ignoring `clickaway`.
|
||||
*
|
||||
* @param {React.SyntheticEvent<any> | Event} event The event source of the callback.
|
||||
* @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
|
||||
*/
|
||||
onClose?: (event: React.SyntheticEvent<any> | Event | null, reason: SnackbarCloseReason) => void;
|
||||
/**
|
||||
* If `true`, the component is shown.
|
||||
*/
|
||||
open?: boolean;
|
||||
/**
|
||||
* The number of milliseconds to wait before dismissing after user interaction.
|
||||
* If `autoHideDuration` prop isn't specified, it does nothing.
|
||||
* If `autoHideDuration` prop is specified but `resumeHideDuration` isn't,
|
||||
* we default to `autoHideDuration / 2` ms.
|
||||
*/
|
||||
resumeHideDuration?: number;
|
||||
}
|
||||
export type UseSnackbarRootSlotProps<ExternalProps = {}> = ExternalProps & UseSnackbarRootSlotOwnProps;
|
||||
export interface UseSnackbarRootSlotOwnProps {
|
||||
onBlur: React.FocusEventHandler;
|
||||
onFocus: React.FocusEventHandler;
|
||||
onMouseEnter: React.MouseEventHandler;
|
||||
onMouseLeave: React.MouseEventHandler;
|
||||
ref?: React.RefCallback<Element>;
|
||||
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: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSnackbarRootSlotProps<ExternalProps>;
|
||||
/**
|
||||
* Callback fired when a "click away" event is detected.
|
||||
*/
|
||||
onClickAway: (event: React.SyntheticEvent<any> | Event) => void;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in new issue
Block a user