UNPKG

4.81 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 const {
38 action,
39 children,
40 focusableWhenDisabled = false,
41 slotProps = {},
42 slots = {},
43 rootElementName: rootElementNameProp = 'button'
44 } = props,
45 other = _objectWithoutPropertiesLoose(props, _excluded);
46 const buttonRef = React.useRef();
47 let rootElementName = rootElementNameProp;
48 if (typeof slots.root === 'string') {
49 rootElementName = slots.root;
50 } else if (other.href || other.to) {
51 rootElementName = 'a';
52 }
53 const {
54 active,
55 focusVisible,
56 setFocusVisible,
57 getRootProps
58 } = useButton(_extends({}, props, {
59 focusableWhenDisabled,
60 rootElementName
61 }));
62 React.useImperativeHandle(action, () => ({
63 focusVisible: () => {
64 setFocusVisible(true);
65 buttonRef.current.focus();
66 }
67 }), [setFocusVisible]);
68 const ownerState = _extends({}, props, {
69 active,
70 focusableWhenDisabled,
71 focusVisible
72 });
73 const classes = useUtilityClasses(ownerState);
74 const defaultElement = other.href || other.to ? 'a' : 'button';
75 const Root = slots.root ?? defaultElement;
76 const rootProps = useSlotProps({
77 elementType: Root,
78 getSlotProps: getRootProps,
79 externalForwardedProps: other,
80 externalSlotProps: slotProps.root,
81 additionalProps: {
82 ref: forwardedRef
83 },
84 ownerState,
85 className: classes.root
86 });
87 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
88 children: children
89 }));
90});
91process.env.NODE_ENV !== "production" ? Button.propTypes /* remove-proptypes */ = {
92 // ┌────────────────────────────── Warning ──────────────────────────────┐
93 // │ These PropTypes are generated from the TypeScript type definitions. │
94 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
95 // └─────────────────────────────────────────────────────────────────────┘
96 /**
97 * A ref for imperative actions. It currently only supports `focusVisible()` action.
98 */
99 action: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
100 current: PropTypes.shape({
101 focusVisible: PropTypes.func.isRequired
102 })
103 })]),
104 /**
105 * @ignore
106 */
107 children: PropTypes.node,
108 /**
109 * @ignore
110 */
111 className: PropTypes.string,
112 /**
113 * If `true`, the component is disabled.
114 * @default false
115 */
116 disabled: PropTypes.bool,
117 /**
118 * If `true`, allows a disabled button to receive focus.
119 * @default false
120 */
121 focusableWhenDisabled: PropTypes.bool,
122 /**
123 * @ignore
124 */
125 href: PropTypes.string,
126 /**
127 * @ignore
128 */
129 onFocusVisible: PropTypes.func,
130 /**
131 * The HTML element that is ultimately rendered, for example 'button' or 'a'
132 * @default 'button'
133 */
134 rootElementName: PropTypes /* @typescript-to-proptypes-ignore */.string,
135 /**
136 * The props used for each slot inside the Button.
137 * @default {}
138 */
139 slotProps: PropTypes.shape({
140 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
141 }),
142 /**
143 * The components used for each slot inside the Button.
144 * Either a string to use a HTML element or a component.
145 * @default {}
146 */
147 slots: PropTypes.shape({
148 root: PropTypes.elementType
149 }),
150 /**
151 * @ignore
152 */
153 to: PropTypes.string
154} : void 0;
155export { Button };
\No newline at end of file