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 | 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 _jsx(Root, _extends({}, rootProps, {
|
89 | children: children
|
90 | }));
|
91 | });
|
92 | process.env.NODE_ENV !== "production" ? Button.propTypes = {
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | |
98 |
|
99 |
|
100 | action: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
|
101 | current: PropTypes.shape({
|
102 | focusVisible: PropTypes.func.isRequired
|
103 | })
|
104 | })]),
|
105 | |
106 |
|
107 |
|
108 | children: PropTypes.node,
|
109 | |
110 |
|
111 |
|
112 | className: PropTypes.string,
|
113 | |
114 |
|
115 |
|
116 |
|
117 | disabled: PropTypes.bool,
|
118 | |
119 |
|
120 |
|
121 |
|
122 | focusableWhenDisabled: PropTypes.bool,
|
123 | |
124 |
|
125 |
|
126 | href: PropTypes.string,
|
127 | |
128 |
|
129 |
|
130 | onFocusVisible: PropTypes.func,
|
131 | |
132 |
|
133 |
|
134 |
|
135 | rootElementName: PropTypes .string,
|
136 | |
137 |
|
138 |
|
139 |
|
140 | slotProps: PropTypes.shape({
|
141 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
142 | }),
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 | slots: PropTypes.shape({
|
149 | root: PropTypes.elementType
|
150 | }),
|
151 | |
152 |
|
153 |
|
154 | to: PropTypes.string
|
155 | } : void 0;
|
156 | export { Button }; |
\ | No newline at end of file |