477 lines
15 KiB
Plaintext
477 lines
15 KiB
Plaintext
'use client';
|
|
|
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
const _excluded = ["action", "centerRipple", "children", "className", "component", "disabled", "disableRipple", "disableTouchRipple", "focusRipple", "focusVisibleClassName", "LinkComponent", "onBlur", "onClick", "onContextMenu", "onDragLeave", "onFocus", "onFocusVisible", "onKeyDown", "onKeyUp", "onMouseDown", "onMouseLeave", "onMouseUp", "onTouchEnd", "onTouchMove", "onTouchStart", "tabIndex", "TouchRippleProps", "touchRippleRef", "type"];
|
|
import * as React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import clsx from 'clsx';
|
|
import refType from '@mui/utils/refType';
|
|
import elementTypeAcceptingRef from '@mui/utils/elementTypeAcceptingRef';
|
|
import composeClasses from '@mui/utils/composeClasses';
|
|
import styled from '../styles/styled';
|
|
import useThemeProps from '../styles/useThemeProps';
|
|
import useForkRef from '../utils/useForkRef';
|
|
import useEventCallback from '../utils/useEventCallback';
|
|
import useIsFocusVisible from '../utils/useIsFocusVisible';
|
|
import TouchRipple from './TouchRipple';
|
|
import buttonBaseClasses, { getButtonBaseUtilityClass } from './buttonBaseClasses';
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
const useUtilityClasses = ownerState => {
|
|
const {
|
|
disabled,
|
|
focusVisible,
|
|
focusVisibleClassName,
|
|
classes
|
|
} = ownerState;
|
|
const slots = {
|
|
root: ['root', disabled && 'disabled', focusVisible && 'focusVisible']
|
|
};
|
|
const composedClasses = composeClasses(slots, getButtonBaseUtilityClass, classes);
|
|
if (focusVisible && focusVisibleClassName) {
|
|
composedClasses.root += ` ${focusVisibleClassName}`;
|
|
}
|
|
return composedClasses;
|
|
};
|
|
export const ButtonBaseRoot = styled('button', {
|
|
name: 'MuiButtonBase',
|
|
slot: 'Root',
|
|
overridesResolver: (props, styles) => styles.root
|
|
})({
|
|
display: 'inline-flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
position: 'relative',
|
|
boxSizing: 'border-box',
|
|
WebkitTapHighlightColor: 'transparent',
|
|
backgroundColor: 'transparent',
|
|
// Reset default value
|
|
// We disable the focus ring for mouse, touch and keyboard users.
|
|
outline: 0,
|
|
border: 0,
|
|
margin: 0,
|
|
// Remove the margin in Safari
|
|
borderRadius: 0,
|
|
padding: 0,
|
|
// Remove the padding in Firefox
|
|
cursor: 'pointer',
|
|
userSelect: 'none',
|
|
verticalAlign: 'middle',
|
|
MozAppearance: 'none',
|
|
// Reset
|
|
WebkitAppearance: 'none',
|
|
// Reset
|
|
textDecoration: 'none',
|
|
// So we take precedent over the style of a native <a /> element.
|
|
color: 'inherit',
|
|
'&::-moz-focus-inner': {
|
|
borderStyle: 'none' // Remove Firefox dotted outline.
|
|
},
|
|
[`&.${buttonBaseClasses.disabled}`]: {
|
|
pointerEvents: 'none',
|
|
// Disable link interactions
|
|
cursor: 'default'
|
|
},
|
|
'@media print': {
|
|
colorAdjust: 'exact'
|
|
}
|
|
});
|
|
|
|
/**
|
|
* `ButtonBase` contains as few styles as possible.
|
|
* It aims to be a simple building block for creating a button.
|
|
* It contains a load of style reset and some focus/ripple logic.
|
|
*/
|
|
const ButtonBase = /*#__PURE__*/React.forwardRef(function ButtonBase(inProps, ref) {
|
|
const props = useThemeProps({
|
|
props: inProps,
|
|
name: 'MuiButtonBase'
|
|
});
|
|
const {
|
|
action,
|
|
centerRipple = false,
|
|
children,
|
|
className,
|
|
component = 'button',
|
|
disabled = false,
|
|
disableRipple = false,
|
|
disableTouchRipple = false,
|
|
focusRipple = false,
|
|
LinkComponent = 'a',
|
|
onBlur,
|
|
onClick,
|
|
onContextMenu,
|
|
onDragLeave,
|
|
onFocus,
|
|
onFocusVisible,
|
|
onKeyDown,
|
|
onKeyUp,
|
|
onMouseDown,
|
|
onMouseLeave,
|
|
onMouseUp,
|
|
onTouchEnd,
|
|
onTouchMove,
|
|
onTouchStart,
|
|
tabIndex = 0,
|
|
TouchRippleProps,
|
|
touchRippleRef,
|
|
type
|
|
} = props,
|
|
other = _objectWithoutPropertiesLoose(props, _excluded);
|
|
const buttonRef = React.useRef(null);
|
|
const rippleRef = React.useRef(null);
|
|
const handleRippleRef = useForkRef(rippleRef, touchRippleRef);
|
|
const {
|
|
isFocusVisibleRef,
|
|
onFocus: handleFocusVisible,
|
|
onBlur: handleBlurVisible,
|
|
ref: focusVisibleRef
|
|
} = useIsFocusVisible();
|
|
const [focusVisible, setFocusVisible] = React.useState(false);
|
|
if (disabled && focusVisible) {
|
|
setFocusVisible(false);
|
|
}
|
|
React.useImperativeHandle(action, () => ({
|
|
focusVisible: () => {
|
|
setFocusVisible(true);
|
|
buttonRef.current.focus();
|
|
}
|
|
}), []);
|
|
const [mountedState, setMountedState] = React.useState(false);
|
|
React.useEffect(() => {
|
|
setMountedState(true);
|
|
}, []);
|
|
const enableTouchRipple = mountedState && !disableRipple && !disabled;
|
|
React.useEffect(() => {
|
|
if (focusVisible && focusRipple && !disableRipple && mountedState) {
|
|
rippleRef.current.pulsate();
|
|
}
|
|
}, [disableRipple, focusRipple, focusVisible, mountedState]);
|
|
function useRippleHandler(rippleAction, eventCallback, skipRippleAction = disableTouchRipple) {
|
|
return useEventCallback(event => {
|
|
if (eventCallback) {
|
|
eventCallback(event);
|
|
}
|
|
const ignore = skipRippleAction;
|
|
if (!ignore && rippleRef.current) {
|
|
rippleRef.current[rippleAction](event);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
const handleMouseDown = useRippleHandler('start', onMouseDown);
|
|
const handleContextMenu = useRippleHandler('stop', onContextMenu);
|
|
const handleDragLeave = useRippleHandler('stop', onDragLeave);
|
|
const handleMouseUp = useRippleHandler('stop', onMouseUp);
|
|
const handleMouseLeave = useRippleHandler('stop', event => {
|
|
if (focusVisible) {
|
|
event.preventDefault();
|
|
}
|
|
if (onMouseLeave) {
|
|
onMouseLeave(event);
|
|
}
|
|
});
|
|
const handleTouchStart = useRippleHandler('start', onTouchStart);
|
|
const handleTouchEnd = useRippleHandler('stop', onTouchEnd);
|
|
const handleTouchMove = useRippleHandler('stop', onTouchMove);
|
|
const handleBlur = useRippleHandler('stop', event => {
|
|
handleBlurVisible(event);
|
|
if (isFocusVisibleRef.current === false) {
|
|
setFocusVisible(false);
|
|
}
|
|
if (onBlur) {
|
|
onBlur(event);
|
|
}
|
|
}, false);
|
|
const handleFocus = useEventCallback(event => {
|
|
// Fix for https://github.com/facebook/react/issues/7769
|
|
if (!buttonRef.current) {
|
|
buttonRef.current = event.currentTarget;
|
|
}
|
|
handleFocusVisible(event);
|
|
if (isFocusVisibleRef.current === true) {
|
|
setFocusVisible(true);
|
|
if (onFocusVisible) {
|
|
onFocusVisible(event);
|
|
}
|
|
}
|
|
if (onFocus) {
|
|
onFocus(event);
|
|
}
|
|
});
|
|
const isNonNativeButton = () => {
|
|
const button = buttonRef.current;
|
|
return component && component !== 'button' && !(button.tagName === 'A' && button.href);
|
|
};
|
|
|
|
/**
|
|
* IE11 shim for https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat
|
|
*/
|
|
const keydownRef = React.useRef(false);
|
|
const handleKeyDown = useEventCallback(event => {
|
|
// Check if key is already down to avoid repeats being counted as multiple activations
|
|
if (focusRipple && !keydownRef.current && focusVisible && rippleRef.current && event.key === ' ') {
|
|
keydownRef.current = true;
|
|
rippleRef.current.stop(event, () => {
|
|
rippleRef.current.start(event);
|
|
});
|
|
}
|
|
if (event.target === event.currentTarget && isNonNativeButton() && event.key === ' ') {
|
|
event.preventDefault();
|
|
}
|
|
if (onKeyDown) {
|
|
onKeyDown(event);
|
|
}
|
|
|
|
// Keyboard accessibility for non interactive elements
|
|
if (event.target === event.currentTarget && isNonNativeButton() && event.key === 'Enter' && !disabled) {
|
|
event.preventDefault();
|
|
if (onClick) {
|
|
onClick(event);
|
|
}
|
|
}
|
|
});
|
|
const handleKeyUp = useEventCallback(event => {
|
|
// calling preventDefault in keyUp on a <button> will not dispatch a click event if Space is pressed
|
|
// https://codesandbox.io/p/sandbox/button-keyup-preventdefault-dn7f0
|
|
if (focusRipple && event.key === ' ' && rippleRef.current && focusVisible && !event.defaultPrevented) {
|
|
keydownRef.current = false;
|
|
rippleRef.current.stop(event, () => {
|
|
rippleRef.current.pulsate(event);
|
|
});
|
|
}
|
|
if (onKeyUp) {
|
|
onKeyUp(event);
|
|
}
|
|
|
|
// Keyboard accessibility for non interactive elements
|
|
if (onClick && event.target === event.currentTarget && isNonNativeButton() && event.key === ' ' && !event.defaultPrevented) {
|
|
onClick(event);
|
|
}
|
|
});
|
|
let ComponentProp = component;
|
|
if (ComponentProp === 'button' && (other.href || other.to)) {
|
|
ComponentProp = LinkComponent;
|
|
}
|
|
const buttonProps = {};
|
|
if (ComponentProp === 'button') {
|
|
buttonProps.type = type === undefined ? 'button' : type;
|
|
buttonProps.disabled = disabled;
|
|
} else {
|
|
if (!other.href && !other.to) {
|
|
buttonProps.role = 'button';
|
|
}
|
|
if (disabled) {
|
|
buttonProps['aria-disabled'] = disabled;
|
|
}
|
|
}
|
|
const handleRef = useForkRef(ref, focusVisibleRef, buttonRef);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
React.useEffect(() => {
|
|
if (enableTouchRipple && !rippleRef.current) {
|
|
console.error(['MUI: The `component` prop provided to ButtonBase is invalid.', 'Please make sure the children prop is rendered in this custom component.'].join('\n'));
|
|
}
|
|
}, [enableTouchRipple]);
|
|
}
|
|
const ownerState = _extends({}, props, {
|
|
centerRipple,
|
|
component,
|
|
disabled,
|
|
disableRipple,
|
|
disableTouchRipple,
|
|
focusRipple,
|
|
tabIndex,
|
|
focusVisible
|
|
});
|
|
const classes = useUtilityClasses(ownerState);
|
|
return /*#__PURE__*/_jsxs(ButtonBaseRoot, _extends({
|
|
as: ComponentProp,
|
|
className: clsx(classes.root, className),
|
|
ownerState: ownerState,
|
|
onBlur: handleBlur,
|
|
onClick: onClick,
|
|
onContextMenu: handleContextMenu,
|
|
onFocus: handleFocus,
|
|
onKeyDown: handleKeyDown,
|
|
onKeyUp: handleKeyUp,
|
|
onMouseDown: handleMouseDown,
|
|
onMouseLeave: handleMouseLeave,
|
|
onMouseUp: handleMouseUp,
|
|
onDragLeave: handleDragLeave,
|
|
onTouchEnd: handleTouchEnd,
|
|
onTouchMove: handleTouchMove,
|
|
onTouchStart: handleTouchStart,
|
|
ref: handleRef,
|
|
tabIndex: disabled ? -1 : tabIndex,
|
|
type: type
|
|
}, buttonProps, other, {
|
|
children: [children, enableTouchRipple ?
|
|
/*#__PURE__*/
|
|
/* TouchRipple is only needed client-side, x2 boost on the server. */
|
|
_jsx(TouchRipple, _extends({
|
|
ref: handleRippleRef,
|
|
center: centerRipple
|
|
}, TouchRippleProps)) : null]
|
|
}));
|
|
});
|
|
process.env.NODE_ENV !== "production" ? ButtonBase.propTypes /* remove-proptypes */ = {
|
|
// ┌────────────────────────────── Warning ──────────────────────────────┐
|
|
// │ These PropTypes are generated from the TypeScript type definitions. │
|
|
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
|
|
// └─────────────────────────────────────────────────────────────────────┘
|
|
/**
|
|
* A ref for imperative actions.
|
|
* It currently only supports `focusVisible()` action.
|
|
*/
|
|
action: refType,
|
|
/**
|
|
* If `true`, the ripples are centered.
|
|
* They won't start at the cursor interaction position.
|
|
* @default false
|
|
*/
|
|
centerRipple: PropTypes.bool,
|
|
/**
|
|
* The content of the component.
|
|
*/
|
|
children: PropTypes.node,
|
|
/**
|
|
* Override or extend the styles applied to the component.
|
|
*/
|
|
classes: PropTypes.object,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: PropTypes.string,
|
|
/**
|
|
* The component used for the root node.
|
|
* Either a string to use a HTML element or a component.
|
|
*/
|
|
component: elementTypeAcceptingRef,
|
|
/**
|
|
* If `true`, the component is disabled.
|
|
* @default false
|
|
*/
|
|
disabled: PropTypes.bool,
|
|
/**
|
|
* If `true`, the ripple effect is disabled.
|
|
*
|
|
* ⚠️ Without a ripple there is no styling for :focus-visible by default. Be sure
|
|
* to highlight the element by applying separate styles with the `.Mui-focusVisible` class.
|
|
* @default false
|
|
*/
|
|
disableRipple: PropTypes.bool,
|
|
/**
|
|
* If `true`, the touch ripple effect is disabled.
|
|
* @default false
|
|
*/
|
|
disableTouchRipple: PropTypes.bool,
|
|
/**
|
|
* If `true`, the base button will have a keyboard focus ripple.
|
|
* @default false
|
|
*/
|
|
focusRipple: PropTypes.bool,
|
|
/**
|
|
* This prop can help identify which element has keyboard focus.
|
|
* The class name will be applied when the element gains the focus through keyboard interaction.
|
|
* It's a polyfill for the [CSS :focus-visible selector](https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo).
|
|
* The rationale for using this feature [is explained here](https://github.com/WICG/focus-visible/blob/HEAD/explainer.md).
|
|
* A [polyfill can be used](https://github.com/WICG/focus-visible) to apply a `focus-visible` class to other components
|
|
* if needed.
|
|
*/
|
|
focusVisibleClassName: PropTypes.string,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
href: PropTypes /* @typescript-to-proptypes-ignore */.any,
|
|
/**
|
|
* The component used to render a link when the `href` prop is provided.
|
|
* @default 'a'
|
|
*/
|
|
LinkComponent: PropTypes.elementType,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onBlur: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onClick: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onContextMenu: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onDragLeave: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onFocus: PropTypes.func,
|
|
/**
|
|
* Callback fired when the component is focused with a keyboard.
|
|
* We trigger a `onFocus` callback too.
|
|
*/
|
|
onFocusVisible: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onKeyDown: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onKeyUp: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseDown: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseLeave: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseUp: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchEnd: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchMove: PropTypes.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchStart: PropTypes.func,
|
|
/**
|
|
* The system prop that allows defining system overrides as well as additional CSS styles.
|
|
*/
|
|
sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
|
/**
|
|
* @default 0
|
|
*/
|
|
tabIndex: PropTypes.number,
|
|
/**
|
|
* Props applied to the `TouchRipple` element.
|
|
*/
|
|
TouchRippleProps: PropTypes.object,
|
|
/**
|
|
* A ref that points to the `TouchRipple` element.
|
|
*/
|
|
touchRippleRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
|
|
current: PropTypes.shape({
|
|
pulsate: PropTypes.func.isRequired,
|
|
start: PropTypes.func.isRequired,
|
|
stop: PropTypes.func.isRequired
|
|
})
|
|
})]),
|
|
/**
|
|
* @ignore
|
|
*/
|
|
type: PropTypes.oneOfType([PropTypes.oneOf(['button', 'reset', 'submit']), PropTypes.string])
|
|
} : void 0;
|
|
export default ButtonBase; |