UNPKG

4.61 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import composeClasses from '../composeClasses';
6import { getButtonUtilityClass } from './buttonClasses';
7import useButton from '../useButton';
8import { useSlotProps } from '../utils';
9import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
10import { jsx as _jsx } from "react/jsx-runtime";
11var useUtilityClasses = function useUtilityClasses(ownerState) {
12 var active = ownerState.active,
13 disabled = ownerState.disabled,
14 focusVisible = ownerState.focusVisible;
15 var slots = {
16 root: ['root', disabled && 'disabled', focusVisible && 'focusVisible', active && 'active']
17 };
18 return composeClasses(slots, useClassNamesOverride(getButtonUtilityClass));
19};
20/**
21 * The foundation for building custom-styled buttons.
22 *
23 * Demos:
24 *
25 * - [Button](https://mui.com/base/react-button/)
26 *
27 * API:
28 *
29 * - [Button API](https://mui.com/base/react-button/components-api/#button)
30 */
31var Button = /*#__PURE__*/React.forwardRef(function Button(props, forwardedRef) {
32 var _slots$root;
33 var action = props.action,
34 children = props.children,
35 disabled = props.disabled,
36 _props$focusableWhenD = props.focusableWhenDisabled,
37 focusableWhenDisabled = _props$focusableWhenD === void 0 ? false : _props$focusableWhenD,
38 onFocusVisible = props.onFocusVisible,
39 _props$slotProps = props.slotProps,
40 slotProps = _props$slotProps === void 0 ? {} : _props$slotProps,
41 _props$slots = props.slots,
42 slots = _props$slots === void 0 ? {} : _props$slots,
43 other = _objectWithoutProperties(props, ["action", "children", "disabled", "focusableWhenDisabled", "onFocusVisible", "slotProps", "slots"]);
44 var buttonRef = React.useRef();
45 var _useButton = useButton(_extends({}, props, {
46 focusableWhenDisabled: focusableWhenDisabled
47 })),
48 active = _useButton.active,
49 focusVisible = _useButton.focusVisible,
50 setFocusVisible = _useButton.setFocusVisible,
51 getRootProps = _useButton.getRootProps;
52 React.useImperativeHandle(action, function () {
53 return {
54 focusVisible: function focusVisible() {
55 setFocusVisible(true);
56 buttonRef.current.focus();
57 }
58 };
59 }, [setFocusVisible]);
60 var ownerState = _extends({}, props, {
61 active: active,
62 focusableWhenDisabled: focusableWhenDisabled,
63 focusVisible: focusVisible
64 });
65 var classes = useUtilityClasses(ownerState);
66 var defaultElement = other.href || other.to ? 'a' : 'button';
67 var Root = (_slots$root = slots.root) != null ? _slots$root : defaultElement;
68 var rootProps = useSlotProps({
69 elementType: Root,
70 getSlotProps: getRootProps,
71 externalForwardedProps: other,
72 externalSlotProps: slotProps.root,
73 additionalProps: {
74 ref: forwardedRef
75 },
76 ownerState: ownerState,
77 className: classes.root
78 });
79 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
80 children: children
81 }));
82});
83process.env.NODE_ENV !== "production" ? Button.propTypes /* remove-proptypes */ = {
84 // ----------------------------- Warning --------------------------------
85 // | These PropTypes are generated from the TypeScript type definitions |
86 // | To update them edit TypeScript types and run "yarn proptypes" |
87 // ----------------------------------------------------------------------
88 /**
89 * A ref for imperative actions. It currently only supports `focusVisible()` action.
90 */
91 action: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
92 current: PropTypes.shape({
93 focusVisible: PropTypes.func.isRequired
94 })
95 })]),
96 /**
97 * @ignore
98 */
99 children: PropTypes.node,
100 /**
101 * If `true`, the component is disabled.
102 * @default false
103 */
104 disabled: PropTypes.bool,
105 /**
106 * If `true`, allows a disabled button to receive focus.
107 * @default false
108 */
109 focusableWhenDisabled: PropTypes.bool,
110 /**
111 * @ignore
112 */
113 href: PropTypes.string,
114 /**
115 * @ignore
116 */
117 onFocusVisible: PropTypes.func,
118 /**
119 * The props used for each slot inside the Button.
120 * @default {}
121 */
122 slotProps: PropTypes.shape({
123 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
124 }),
125 /**
126 * The components used for each slot inside the Button.
127 * Either a string to use a HTML element or a component.
128 * @default {}
129 */
130 slots: PropTypes.shape({
131 root: PropTypes.elementType
132 }),
133 /**
134 * @ignore
135 */
136 to: PropTypes.string
137} : void 0;
138export default Button;
\No newline at end of file