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 | import getReactElementRef from '@mui/utils/getReactElementRef';
|
7 |
|
8 |
|
9 | import { jsx as _jsx } from "react/jsx-runtime";
|
10 | function mapEventPropToEvent(eventProp) {
|
11 | return eventProp.substring(2).toLowerCase();
|
12 | }
|
13 | function clickedRootScrollbar(event, doc) {
|
14 | return doc.documentElement.clientWidth < event.clientX || doc.documentElement.clientHeight < event.clientY;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function ClickAwayListener(props) {
|
30 | const {
|
31 | children,
|
32 | disableReactTree = false,
|
33 | mouseEvent = 'onClick',
|
34 | onClickAway,
|
35 | touchEvent = 'onTouchEnd'
|
36 | } = props;
|
37 | const movedRef = React.useRef(false);
|
38 | const nodeRef = React.useRef(null);
|
39 | const activatedRef = React.useRef(false);
|
40 | const syntheticEventRef = React.useRef(false);
|
41 | React.useEffect(() => {
|
42 |
|
43 |
|
44 | setTimeout(() => {
|
45 | activatedRef.current = true;
|
46 | }, 0);
|
47 | return () => {
|
48 | activatedRef.current = false;
|
49 | };
|
50 | }, []);
|
51 | const handleRef = useForkRef(getReactElementRef(children), 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().includes(nodeRef.current);
|
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 |