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