@mui/core
Version:
Unstyled React components with which to implement custom design systems.
144 lines (129 loc) • 4.04 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["className", "component", "components", "componentsProps", "children", "disabled", "action", "onBlur", "onClick", "onFocus", "onFocusVisible", "onKeyDown", "onKeyUp", "onMouseLeave"];
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_useForkRef as useForkRef } from '@mui/utils';
import composeClasses from '../composeClasses';
import { getButtonUnstyledUtilityClass } from './buttonUnstyledClasses';
import useButton from './useButton';
import appendOwnerState from '../utils/appendOwnerState';
import { jsx as _jsx } from "react/jsx-runtime";
const useUtilityClasses = ownerState => {
const {
active,
disabled,
focusVisible
} = ownerState;
const slots = {
root: ['root', disabled && 'disabled', focusVisible && 'focusVisible', active && 'active']
};
return composeClasses(slots, getButtonUnstyledUtilityClass, {});
};
/**
* The foundation for building custom-styled buttons.
*
* Demos:
*
* - [Buttons](https://mui.com/components/buttons/)
*
* API:
*
* - [ButtonUnstyled API](https://mui.com/api/button-unstyled/)
*/
const ButtonUnstyled = /*#__PURE__*/React.forwardRef(function ButtonUnstyled(props, ref) {
const {
className,
component,
components = {},
componentsProps = {},
children,
action
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const buttonRef = React.useRef();
const handleRef = useForkRef(buttonRef, ref);
const {
active,
focusVisible,
setFocusVisible,
getRootProps
} = useButton(_extends({}, props, {
ref: handleRef
}));
React.useImperativeHandle(action, () => ({
focusVisible: () => {
setFocusVisible(true);
buttonRef.current.focus();
}
}), [setFocusVisible]);
const ownerState = _extends({}, props, {
active,
focusVisible
});
const ButtonRoot = component ?? components.Root ?? 'button';
const buttonRootProps = appendOwnerState(ButtonRoot, _extends({}, other, componentsProps.root), ownerState);
const classes = useUtilityClasses(ownerState);
return /*#__PURE__*/_jsx(ButtonRoot, _extends({}, getRootProps(), buttonRootProps, {
className: clsx(classes.root, className, buttonRootProps.className),
children: children
}));
});
process.env.NODE_ENV !== "production" ? ButtonUnstyled.propTypes
/* remove-proptypes */
= {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired
})
})]),
/**
* @ignore
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the Root slot.
* Either a string to use a HTML element or a component.
* This is equivalent to `components.Root`. If both are provided, the `component` is used.
* @default 'button'
*/
component: PropTypes.elementType,
/**
* The components used for each slot inside the Button.
* Either a string to use a HTML element or a component.
* @default {}
*/
components: PropTypes.shape({
Root: PropTypes.elementType
}),
/**
* @ignore
*/
componentsProps: PropTypes.object,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* @ignore
*/
onFocusVisible: PropTypes.func
} : void 0;
export default ButtonUnstyled;