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