UNPKG

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