1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
5 | const _excluded = ["action", "children", "disabled", "focusableWhenDisabled", "onFocusVisible", "slotProps", "slots", "rootElementName"];
|
6 | import * as React from 'react';
|
7 | import PropTypes from 'prop-types';
|
8 | import { unstable_composeClasses as composeClasses } from '../composeClasses';
|
9 | import { getButtonUtilityClass } from './buttonClasses';
|
10 | import { useButton } from '../useButton';
|
11 | import { useSlotProps } from '../utils';
|
12 | import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
|
13 | import { jsx as _jsx } from "react/jsx-runtime";
|
14 | const 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 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const Button = 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 _jsx(Root, _extends({}, rootProps, {
|
88 | children: children
|
89 | }));
|
90 | });
|
91 | process.env.NODE_ENV !== "production" ? Button.propTypes = {
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | |
97 |
|
98 |
|
99 | action: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
|
100 | current: PropTypes.shape({
|
101 | focusVisible: PropTypes.func.isRequired
|
102 | })
|
103 | })]),
|
104 | |
105 |
|
106 |
|
107 | children: PropTypes.node,
|
108 | |
109 |
|
110 |
|
111 | className: PropTypes.string,
|
112 | |
113 |
|
114 |
|
115 |
|
116 | disabled: PropTypes.bool,
|
117 | |
118 |
|
119 |
|
120 |
|
121 | focusableWhenDisabled: PropTypes.bool,
|
122 | |
123 |
|
124 |
|
125 | href: PropTypes.string,
|
126 | |
127 |
|
128 |
|
129 | onFocusVisible: PropTypes.func,
|
130 | |
131 |
|
132 |
|
133 |
|
134 | rootElementName: PropTypes .string,
|
135 | |
136 |
|
137 |
|
138 |
|
139 | slotProps: PropTypes.shape({
|
140 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
141 | }),
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | slots: PropTypes.shape({
|
148 | root: PropTypes.elementType
|
149 | }),
|
150 | |
151 |
|
152 |
|
153 | to: PropTypes.string
|
154 | } : void 0;
|
155 | export { Button }; |
\ | No newline at end of file |