523 lines
16 KiB
JavaScript
523 lines
16 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.styles = void 0;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var React = _interopRequireWildcard(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var ReactDOM = _interopRequireWildcard(require("react-dom"));
|
|
|
|
var _clsx = _interopRequireDefault(require("clsx"));
|
|
|
|
var _utils = require("@material-ui/utils");
|
|
|
|
var _useForkRef = _interopRequireDefault(require("../utils/useForkRef"));
|
|
|
|
var _useEventCallback = _interopRequireDefault(require("../utils/useEventCallback"));
|
|
|
|
var _deprecatedPropType = _interopRequireDefault(require("../utils/deprecatedPropType"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var _useIsFocusVisible2 = _interopRequireDefault(require("../utils/useIsFocusVisible"));
|
|
|
|
var _TouchRipple = _interopRequireDefault(require("./TouchRipple"));
|
|
|
|
var styles = {
|
|
/* Styles applied to the root element. */
|
|
root: {
|
|
display: 'inline-flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
position: 'relative',
|
|
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',
|
|
'-moz-appearance': 'none',
|
|
// Reset
|
|
'-webkit-appearance': '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.
|
|
|
|
},
|
|
'&$disabled': {
|
|
pointerEvents: 'none',
|
|
// Disable link interactions
|
|
cursor: 'default'
|
|
},
|
|
'@media print': {
|
|
colorAdjust: 'exact'
|
|
}
|
|
},
|
|
|
|
/* Pseudo-class applied to the root element if `disabled={true}`. */
|
|
disabled: {},
|
|
|
|
/* Pseudo-class applied to the root element if keyboard focused. */
|
|
focusVisible: {}
|
|
};
|
|
/**
|
|
* `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.
|
|
*/
|
|
|
|
exports.styles = styles;
|
|
var ButtonBase = /*#__PURE__*/React.forwardRef(function ButtonBase(props, ref) {
|
|
var action = props.action,
|
|
buttonRefProp = props.buttonRef,
|
|
_props$centerRipple = props.centerRipple,
|
|
centerRipple = _props$centerRipple === void 0 ? false : _props$centerRipple,
|
|
children = props.children,
|
|
classes = props.classes,
|
|
className = props.className,
|
|
_props$component = props.component,
|
|
component = _props$component === void 0 ? 'button' : _props$component,
|
|
_props$disabled = props.disabled,
|
|
disabled = _props$disabled === void 0 ? false : _props$disabled,
|
|
_props$disableRipple = props.disableRipple,
|
|
disableRipple = _props$disableRipple === void 0 ? false : _props$disableRipple,
|
|
_props$disableTouchRi = props.disableTouchRipple,
|
|
disableTouchRipple = _props$disableTouchRi === void 0 ? false : _props$disableTouchRi,
|
|
_props$focusRipple = props.focusRipple,
|
|
focusRipple = _props$focusRipple === void 0 ? false : _props$focusRipple,
|
|
focusVisibleClassName = props.focusVisibleClassName,
|
|
onBlur = props.onBlur,
|
|
onClick = props.onClick,
|
|
onFocus = props.onFocus,
|
|
onFocusVisible = props.onFocusVisible,
|
|
onKeyDown = props.onKeyDown,
|
|
onKeyUp = props.onKeyUp,
|
|
onMouseDown = props.onMouseDown,
|
|
onMouseLeave = props.onMouseLeave,
|
|
onMouseUp = props.onMouseUp,
|
|
onTouchEnd = props.onTouchEnd,
|
|
onTouchMove = props.onTouchMove,
|
|
onTouchStart = props.onTouchStart,
|
|
onDragLeave = props.onDragLeave,
|
|
_props$tabIndex = props.tabIndex,
|
|
tabIndex = _props$tabIndex === void 0 ? 0 : _props$tabIndex,
|
|
TouchRippleProps = props.TouchRippleProps,
|
|
_props$type = props.type,
|
|
type = _props$type === void 0 ? 'button' : _props$type,
|
|
other = (0, _objectWithoutProperties2.default)(props, ["action", "buttonRef", "centerRipple", "children", "classes", "className", "component", "disabled", "disableRipple", "disableTouchRipple", "focusRipple", "focusVisibleClassName", "onBlur", "onClick", "onFocus", "onFocusVisible", "onKeyDown", "onKeyUp", "onMouseDown", "onMouseLeave", "onMouseUp", "onTouchEnd", "onTouchMove", "onTouchStart", "onDragLeave", "tabIndex", "TouchRippleProps", "type"]);
|
|
var buttonRef = React.useRef(null);
|
|
|
|
function getButtonNode() {
|
|
// #StrictMode ready
|
|
return ReactDOM.findDOMNode(buttonRef.current);
|
|
}
|
|
|
|
var rippleRef = React.useRef(null);
|
|
|
|
var _React$useState = React.useState(false),
|
|
focusVisible = _React$useState[0],
|
|
setFocusVisible = _React$useState[1];
|
|
|
|
if (disabled && focusVisible) {
|
|
setFocusVisible(false);
|
|
}
|
|
|
|
var _useIsFocusVisible = (0, _useIsFocusVisible2.default)(),
|
|
isFocusVisible = _useIsFocusVisible.isFocusVisible,
|
|
onBlurVisible = _useIsFocusVisible.onBlurVisible,
|
|
focusVisibleRef = _useIsFocusVisible.ref;
|
|
|
|
React.useImperativeHandle(action, function () {
|
|
return {
|
|
focusVisible: function focusVisible() {
|
|
setFocusVisible(true);
|
|
buttonRef.current.focus();
|
|
}
|
|
};
|
|
}, []);
|
|
React.useEffect(function () {
|
|
if (focusVisible && focusRipple && !disableRipple) {
|
|
rippleRef.current.pulsate();
|
|
}
|
|
}, [disableRipple, focusRipple, focusVisible]);
|
|
|
|
function useRippleHandler(rippleAction, eventCallback) {
|
|
var skipRippleAction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : disableTouchRipple;
|
|
return (0, _useEventCallback.default)(function (event) {
|
|
if (eventCallback) {
|
|
eventCallback(event);
|
|
}
|
|
|
|
var ignore = skipRippleAction;
|
|
|
|
if (!ignore && rippleRef.current) {
|
|
rippleRef.current[rippleAction](event);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
var handleMouseDown = useRippleHandler('start', onMouseDown);
|
|
var handleDragLeave = useRippleHandler('stop', onDragLeave);
|
|
var handleMouseUp = useRippleHandler('stop', onMouseUp);
|
|
var handleMouseLeave = useRippleHandler('stop', function (event) {
|
|
if (focusVisible) {
|
|
event.preventDefault();
|
|
}
|
|
|
|
if (onMouseLeave) {
|
|
onMouseLeave(event);
|
|
}
|
|
});
|
|
var handleTouchStart = useRippleHandler('start', onTouchStart);
|
|
var handleTouchEnd = useRippleHandler('stop', onTouchEnd);
|
|
var handleTouchMove = useRippleHandler('stop', onTouchMove);
|
|
var handleBlur = useRippleHandler('stop', function (event) {
|
|
if (focusVisible) {
|
|
onBlurVisible(event);
|
|
setFocusVisible(false);
|
|
}
|
|
|
|
if (onBlur) {
|
|
onBlur(event);
|
|
}
|
|
}, false);
|
|
var handleFocus = (0, _useEventCallback.default)(function (event) {
|
|
// Fix for https://github.com/facebook/react/issues/7769
|
|
if (!buttonRef.current) {
|
|
buttonRef.current = event.currentTarget;
|
|
}
|
|
|
|
if (isFocusVisible(event)) {
|
|
setFocusVisible(true);
|
|
|
|
if (onFocusVisible) {
|
|
onFocusVisible(event);
|
|
}
|
|
}
|
|
|
|
if (onFocus) {
|
|
onFocus(event);
|
|
}
|
|
});
|
|
|
|
var isNonNativeButton = function isNonNativeButton() {
|
|
var button = getButtonNode();
|
|
return component && component !== 'button' && !(button.tagName === 'A' && button.href);
|
|
};
|
|
/**
|
|
* IE 11 shim for https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat
|
|
*/
|
|
|
|
|
|
var keydownRef = React.useRef(false);
|
|
var handleKeyDown = (0, _useEventCallback.default)(function (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;
|
|
event.persist();
|
|
rippleRef.current.stop(event, function () {
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
var handleKeyUp = (0, _useEventCallback.default)(function (event) {
|
|
// calling preventDefault in keyUp on a <button> will not dispatch a click event if Space is pressed
|
|
// https://codesandbox.io/s/button-keyup-preventdefault-dn7f0
|
|
if (focusRipple && event.key === ' ' && rippleRef.current && focusVisible && !event.defaultPrevented) {
|
|
keydownRef.current = false;
|
|
event.persist();
|
|
rippleRef.current.stop(event, function () {
|
|
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);
|
|
}
|
|
});
|
|
var ComponentProp = component;
|
|
|
|
if (ComponentProp === 'button' && other.href) {
|
|
ComponentProp = 'a';
|
|
}
|
|
|
|
var buttonProps = {};
|
|
|
|
if (ComponentProp === 'button') {
|
|
buttonProps.type = type;
|
|
buttonProps.disabled = disabled;
|
|
} else {
|
|
if (ComponentProp !== 'a' || !other.href) {
|
|
buttonProps.role = 'button';
|
|
}
|
|
|
|
buttonProps['aria-disabled'] = disabled;
|
|
}
|
|
|
|
var handleUserRef = (0, _useForkRef.default)(buttonRefProp, ref);
|
|
var handleOwnRef = (0, _useForkRef.default)(focusVisibleRef, buttonRef);
|
|
var handleRef = (0, _useForkRef.default)(handleUserRef, handleOwnRef);
|
|
|
|
var _React$useState2 = React.useState(false),
|
|
mountedState = _React$useState2[0],
|
|
setMountedState = _React$useState2[1];
|
|
|
|
React.useEffect(function () {
|
|
setMountedState(true);
|
|
}, []);
|
|
var enableTouchRipple = mountedState && !disableRipple && !disabled;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
React.useEffect(function () {
|
|
if (enableTouchRipple && !rippleRef.current) {
|
|
console.error(['Material-UI: The `component` prop provided to ButtonBase is invalid.', 'Please make sure the children prop is rendered in this custom component.'].join('\n'));
|
|
}
|
|
}, [enableTouchRipple]);
|
|
}
|
|
|
|
return /*#__PURE__*/React.createElement(ComponentProp, (0, _extends2.default)({
|
|
className: (0, _clsx.default)(classes.root, className, focusVisible && [classes.focusVisible, focusVisibleClassName], disabled && classes.disabled),
|
|
onBlur: handleBlur,
|
|
onClick: onClick,
|
|
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
|
|
}, buttonProps, other), children, enableTouchRipple ?
|
|
/*#__PURE__*/
|
|
|
|
/* TouchRipple is only needed client-side, x2 boost on the server. */
|
|
React.createElement(_TouchRipple.default, (0, _extends2.default)({
|
|
ref: rippleRef,
|
|
center: centerRipple
|
|
}, TouchRippleProps)) : null);
|
|
});
|
|
process.env.NODE_ENV !== "production" ? ButtonBase.propTypes = {
|
|
// ----------------------------- Warning --------------------------------
|
|
// | These PropTypes are generated from the TypeScript type definitions |
|
|
// | To update them edit the d.ts file and run "yarn proptypes" |
|
|
// ----------------------------------------------------------------------
|
|
|
|
/**
|
|
* A ref for imperative actions.
|
|
* It currently only supports `focusVisible()` action.
|
|
*/
|
|
action: _utils.refType,
|
|
|
|
/**
|
|
* @ignore
|
|
*
|
|
* Use that prop to pass a ref to the native button component.
|
|
* @deprecated Use `ref` instead.
|
|
*/
|
|
buttonRef: (0, _deprecatedPropType.default)(_utils.refType, 'Use `ref` instead.'),
|
|
|
|
/**
|
|
* If `true`, the ripples will be centered.
|
|
* They won't start at the cursor interaction position.
|
|
*/
|
|
centerRipple: _propTypes.default.bool,
|
|
|
|
/**
|
|
* The content of the component.
|
|
*/
|
|
children: _propTypes.default.node,
|
|
|
|
/**
|
|
* Override or extend the styles applied to the component.
|
|
* See [CSS API](#css) below for more details.
|
|
*/
|
|
classes: _propTypes.default.object,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
|
|
/**
|
|
* The component used for the root node.
|
|
* Either a string to use a HTML element or a component.
|
|
*/
|
|
component: _utils.elementTypeAcceptingRef,
|
|
|
|
/**
|
|
* If `true`, the base button will be disabled.
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the ripple effect will be 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 `focusVisibleClassName`.
|
|
*/
|
|
disableRipple: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the touch ripple effect will be disabled.
|
|
*/
|
|
disableTouchRipple: _propTypes.default.bool,
|
|
|
|
/**
|
|
* If `true`, the base button will have a keyboard focus ripple.
|
|
*/
|
|
focusRipple: _propTypes.default.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/master/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.default.string,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
href: _propTypes.default.string,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onBlur: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onClick: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onDragLeave: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onFocus: _propTypes.default.func,
|
|
|
|
/**
|
|
* Callback fired when the component is focused with a keyboard.
|
|
* We trigger a `onFocus` callback too.
|
|
*/
|
|
onFocusVisible: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onKeyDown: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onKeyUp: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseDown: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseLeave: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onMouseUp: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchEnd: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchMove: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onTouchStart: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
|
|
|
/**
|
|
* Props applied to the `TouchRipple` element.
|
|
*/
|
|
TouchRippleProps: _propTypes.default.object,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
type: _propTypes.default.oneOfType([_propTypes.default.oneOf(['button', 'reset', 'submit']), _propTypes.default.string])
|
|
} : void 0;
|
|
|
|
var _default = (0, _withStyles.default)(styles, {
|
|
name: 'MuiButtonBase'
|
|
})(ButtonBase);
|
|
|
|
exports.default = _default; |