1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.default = void 0;
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
10 | var _utils = require("@mui/utils");
|
11 | var _jsxRuntime = require("react/jsx-runtime");
|
12 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
13 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
14 |
|
15 | function mapEventPropToEvent(eventProp) {
|
16 | return eventProp.substring(2).toLowerCase();
|
17 | }
|
18 | function clickedRootScrollbar(event, doc) {
|
19 | return doc.documentElement.clientWidth < event.clientX || doc.documentElement.clientHeight < event.clientY;
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function ClickAwayListener(props) {
|
34 | const {
|
35 | children,
|
36 | disableReactTree = false,
|
37 | mouseEvent = 'onClick',
|
38 | onClickAway,
|
39 | touchEvent = 'onTouchEnd'
|
40 | } = props;
|
41 | const movedRef = React.useRef(false);
|
42 | const nodeRef = React.useRef(null);
|
43 | const activatedRef = React.useRef(false);
|
44 | const syntheticEventRef = React.useRef(false);
|
45 | React.useEffect(() => {
|
46 |
|
47 |
|
48 | setTimeout(() => {
|
49 | activatedRef.current = true;
|
50 | }, 0);
|
51 | return () => {
|
52 | activatedRef.current = false;
|
53 | };
|
54 | }, []);
|
55 | const handleRef = (0, _utils.unstable_useForkRef)(
|
56 |
|
57 | children.ref, nodeRef);
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | const handleClickAway = (0, _utils.unstable_useEventCallback)(event => {
|
66 |
|
67 |
|
68 | const insideReactTree = syntheticEventRef.current;
|
69 | syntheticEventRef.current = false;
|
70 | const doc = (0, _utils.unstable_ownerDocument)(nodeRef.current);
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | if (!activatedRef.current || !nodeRef.current || 'clientX' in event && clickedRootScrollbar(event, doc)) {
|
76 | return;
|
77 | }
|
78 |
|
79 |
|
80 | if (movedRef.current) {
|
81 | movedRef.current = false;
|
82 | return;
|
83 | }
|
84 | let insideDOM;
|
85 |
|
86 |
|
87 | if (event.composedPath) {
|
88 | insideDOM = event.composedPath().indexOf(nodeRef.current) > -1;
|
89 | } else {
|
90 | insideDOM = !doc.documentElement.contains(
|
91 |
|
92 | event.target) || nodeRef.current.contains(
|
93 |
|
94 | event.target);
|
95 | }
|
96 | if (!insideDOM && (disableReactTree || !insideReactTree)) {
|
97 | onClickAway(event);
|
98 | }
|
99 | });
|
100 |
|
101 |
|
102 | const createHandleSynthetic = handlerName => event => {
|
103 | syntheticEventRef.current = true;
|
104 | const childrenPropsHandler = children.props[handlerName];
|
105 | if (childrenPropsHandler) {
|
106 | childrenPropsHandler(event);
|
107 | }
|
108 | };
|
109 | const childrenProps = {
|
110 | ref: handleRef
|
111 | };
|
112 | if (touchEvent !== false) {
|
113 | childrenProps[touchEvent] = createHandleSynthetic(touchEvent);
|
114 | }
|
115 | React.useEffect(() => {
|
116 | if (touchEvent !== false) {
|
117 | const mappedTouchEvent = mapEventPropToEvent(touchEvent);
|
118 | const doc = (0, _utils.unstable_ownerDocument)(nodeRef.current);
|
119 | const handleTouchMove = () => {
|
120 | movedRef.current = true;
|
121 | };
|
122 | doc.addEventListener(mappedTouchEvent, handleClickAway);
|
123 | doc.addEventListener('touchmove', handleTouchMove);
|
124 | return () => {
|
125 | doc.removeEventListener(mappedTouchEvent, handleClickAway);
|
126 | doc.removeEventListener('touchmove', handleTouchMove);
|
127 | };
|
128 | }
|
129 | return undefined;
|
130 | }, [handleClickAway, touchEvent]);
|
131 | if (mouseEvent !== false) {
|
132 | childrenProps[mouseEvent] = createHandleSynthetic(mouseEvent);
|
133 | }
|
134 | React.useEffect(() => {
|
135 | if (mouseEvent !== false) {
|
136 | const mappedMouseEvent = mapEventPropToEvent(mouseEvent);
|
137 | const doc = (0, _utils.unstable_ownerDocument)(nodeRef.current);
|
138 | doc.addEventListener(mappedMouseEvent, handleClickAway);
|
139 | return () => {
|
140 | doc.removeEventListener(mappedMouseEvent, handleClickAway);
|
141 | };
|
142 | }
|
143 | return undefined;
|
144 | }, [handleClickAway, mouseEvent]);
|
145 | return (0, _jsxRuntime.jsx)(React.Fragment, {
|
146 | children: React.cloneElement(children, childrenProps)
|
147 | });
|
148 | }
|
149 | process.env.NODE_ENV !== "production" ? ClickAwayListener.propTypes = {
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | |
155 |
|
156 |
|
157 | children: _utils.elementAcceptingRef.isRequired,
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 | disableReactTree: _propTypes.default.bool,
|
164 | |
165 |
|
166 |
|
167 |
|
168 | mouseEvent: _propTypes.default.oneOf(['onClick', 'onMouseDown', 'onMouseUp', 'onPointerDown', 'onPointerUp', false]),
|
169 | |
170 |
|
171 |
|
172 | onClickAway: _propTypes.default.func.isRequired,
|
173 | |
174 |
|
175 |
|
176 |
|
177 | touchEvent: _propTypes.default.oneOf(['onTouchEnd', 'onTouchStart', false])
|
178 | } : void 0;
|
179 | if (process.env.NODE_ENV !== 'production') {
|
180 |
|
181 | ClickAwayListener['propTypes' + ''] = (0, _utils.exactProp)(ClickAwayListener.propTypes);
|
182 | }
|
183 | var _default = ClickAwayListener;
|
184 | exports.default = _default; |
\ | No newline at end of file |