@hanzogui/dismissable 3.0.2 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +40 -19
- package/dist/cjs/Dismissable.cjs +266 -170
- package/dist/cjs/Dismissable.native.js +31 -29
- package/dist/cjs/Dismissable.native.js.map +1 -1
- package/dist/cjs/DismissableProps.cjs +7 -5
- package/dist/cjs/DismissableProps.native.js +7 -5
- package/dist/cjs/DismissableProps.native.js.map +1 -1
- package/dist/cjs/index.cjs +7 -5
- package/dist/cjs/index.native.js +8 -6
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/esm/Dismissable.mjs +232 -138
- package/dist/esm/Dismissable.mjs.map +1 -1
- package/dist/esm/Dismissable.native.js +7 -7
- package/dist/esm/Dismissable.native.js.map +1 -1
- package/dist/jsx/Dismissable.mjs +232 -138
- package/dist/jsx/Dismissable.mjs.map +1 -1
- package/dist/jsx/Dismissable.native.js +31 -29
- package/dist/jsx/Dismissable.native.js.map +1 -1
- package/dist/jsx/DismissableProps.native.js +7 -5
- package/dist/jsx/index.native.js +7 -5
- package/package.json +14 -12
- package/types/Dismissable.d.ts +1 -1
- package/types/Dismissable.d.ts.map +1 -0
- package/types/Dismissable.native.d.ts.map +1 -0
- package/types/DismissableProps.d.ts.map +1 -0
- package/types/index.d.ts.map +1 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present, Hanzo AI, Inc.
|
|
4
|
+
|
|
5
|
+
Portions of this software are derived from upstream code originally licensed under
|
|
6
|
+
the MIT License, with the following copyright notices retained per its terms:
|
|
2
7
|
|
|
3
8
|
Copyright (c) 2020 Nate Wienert
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
Copyright (c) 2015-present, Nicolas Gallagher.
|
|
10
|
+
Copyright (c) 2015-present, Facebook, Inc.
|
|
11
|
+
Copyright (c) 2021 Radix
|
|
12
|
+
Copyright (c) 2017 Carmelo Pullara
|
|
13
|
+
Copyright (c) 2018 Framer B.V.
|
|
14
|
+
Copyright (c) 2022 WorkOS
|
|
15
|
+
|
|
16
|
+
All rights reserved.
|
|
17
|
+
|
|
18
|
+
Redistribution and use in source and binary forms, with or without
|
|
19
|
+
modification, are permitted provided that the following conditions are met:
|
|
20
|
+
|
|
21
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
22
|
+
list of conditions and the following disclaimer.
|
|
23
|
+
|
|
24
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
25
|
+
this list of conditions and the following disclaimer in the documentation
|
|
26
|
+
and/or other materials provided with the distribution.
|
|
27
|
+
|
|
28
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
29
|
+
may be used to endorse or promote products derived from this software
|
|
30
|
+
without specific prior written permission.
|
|
31
|
+
|
|
32
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
33
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
34
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
35
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
36
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
37
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
38
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
39
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
40
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
41
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
42
|
+
POSSIBILITY OF SUCH DAMAGE.
|
package/dist/cjs/Dismissable.cjs
CHANGED
|
@@ -2,33 +2,35 @@ var __create = Object.create;
|
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf
|
|
6
|
-
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
7
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
16
|
get: () => from[key],
|
|
16
17
|
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
20
22
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: true
|
|
33
|
+
}), mod);
|
|
32
34
|
var Dismissable_exports = {};
|
|
33
35
|
__export(Dismissable_exports, {
|
|
34
36
|
Dismissable: () => Dismissable,
|
|
@@ -41,43 +43,48 @@ __export(Dismissable_exports, {
|
|
|
41
43
|
useIsInsideDismissable: () => useIsInsideDismissable
|
|
42
44
|
});
|
|
43
45
|
module.exports = __toCommonJS(Dismissable_exports);
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
var import_compose_refs = require("@hanzogui/compose-refs");
|
|
47
|
+
var import_core = require("@hanzogui/core");
|
|
48
|
+
var import_use_escape_keydown = require("@hanzogui/use-escape-keydown");
|
|
49
|
+
var import_use_event = require("@hanzogui/use-event");
|
|
50
|
+
var React = __toESM(require("react"), 1);
|
|
51
|
+
var ReactDOM = __toESM(require("react-dom"), 1);
|
|
52
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
53
|
function dispatchDiscreteCustomEvent(target, event) {
|
|
52
|
-
target
|
|
54
|
+
if (target) ReactDOM.flushSync(() => target.dispatchEvent(event));
|
|
53
55
|
}
|
|
54
|
-
const DISMISSABLE_LAYER_NAME = "Dismissable"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const DISMISSABLE_LAYER_NAME = "Dismissable";
|
|
57
|
+
const CONTEXT_UPDATE = "dismissable.update";
|
|
58
|
+
const POINTER_DOWN_OUTSIDE = "dismissable.pointerDownOutside";
|
|
59
|
+
const FOCUS_OUTSIDE = "dismissable.focusOutside";
|
|
58
60
|
let originalBodyPointerEvents;
|
|
59
|
-
const globalLayers = /* @__PURE__ */new Set()
|
|
60
|
-
|
|
61
|
+
const globalLayers = /* @__PURE__ */new Set();
|
|
62
|
+
const layerChangeListeners = /* @__PURE__ */new Set();
|
|
61
63
|
let layersWithPointerEventsDisabledCount = 0;
|
|
62
64
|
function notifyLayerChange() {
|
|
63
|
-
for (const listener of layerChangeListeners)
|
|
65
|
+
for (const listener of layerChangeListeners) {
|
|
66
|
+
listener();
|
|
67
|
+
}
|
|
64
68
|
}
|
|
65
69
|
function getDismissableLayerCount() {
|
|
66
70
|
return globalLayers.size;
|
|
67
71
|
}
|
|
68
72
|
function debugDismissableLayers() {
|
|
69
73
|
const layers = Array.from(globalLayers);
|
|
70
|
-
|
|
74
|
+
console.log("[Dismissable] Active layers:", layers.length, layers);
|
|
75
|
+
return layers;
|
|
71
76
|
}
|
|
72
77
|
function useHasDismissableLayers() {
|
|
73
78
|
const [count, setCount] = React.useState(() => globalLayers.size);
|
|
74
|
-
|
|
79
|
+
React.useEffect(() => {
|
|
75
80
|
setCount(globalLayers.size);
|
|
76
81
|
const update = () => setCount(globalLayers.size);
|
|
77
|
-
|
|
82
|
+
layerChangeListeners.add(update);
|
|
83
|
+
return () => {
|
|
78
84
|
layerChangeListeners.delete(update);
|
|
79
85
|
};
|
|
80
|
-
}, [])
|
|
86
|
+
}, []);
|
|
87
|
+
return count > 0;
|
|
81
88
|
}
|
|
82
89
|
const DismissableContext = React.createContext({
|
|
83
90
|
layers: /* @__PURE__ */new Set(),
|
|
@@ -85,28 +92,33 @@ const DismissableContext = React.createContext({
|
|
|
85
92
|
branches: /* @__PURE__ */new Set()
|
|
86
93
|
});
|
|
87
94
|
function useIsInsideDismissable(ref) {
|
|
88
|
-
const context = React.useContext(DismissableContext)
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
const context = React.useContext(DismissableContext);
|
|
96
|
+
const [isInside, setIsInside] = React.useState(false);
|
|
97
|
+
React.useEffect(() => {
|
|
91
98
|
const check = () => {
|
|
92
99
|
const el = ref.current;
|
|
93
100
|
if (!el) {
|
|
94
|
-
setIsInside(
|
|
101
|
+
setIsInside(false);
|
|
95
102
|
return;
|
|
96
103
|
}
|
|
97
|
-
for (const layer of context.layers)
|
|
98
|
-
|
|
99
|
-
|
|
104
|
+
for (const layer of context.layers) {
|
|
105
|
+
if (layer.contains(el)) {
|
|
106
|
+
setIsInside(true);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
100
109
|
}
|
|
101
|
-
setIsInside(
|
|
110
|
+
setIsInside(false);
|
|
102
111
|
};
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
check();
|
|
113
|
+
document.addEventListener(CONTEXT_UPDATE, check);
|
|
114
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, check);
|
|
115
|
+
}, [context.layers, ref]);
|
|
116
|
+
return isInside;
|
|
105
117
|
}
|
|
106
118
|
function useDismissableLayersAbove(ref) {
|
|
107
|
-
const context = React.useContext(DismissableContext)
|
|
108
|
-
|
|
109
|
-
|
|
119
|
+
const context = React.useContext(DismissableContext);
|
|
120
|
+
const [count, setCount] = React.useState(0);
|
|
121
|
+
React.useEffect(() => {
|
|
110
122
|
const check = () => {
|
|
111
123
|
const el = ref.current;
|
|
112
124
|
if (!el) {
|
|
@@ -114,157 +126,236 @@ function useDismissableLayersAbove(ref) {
|
|
|
114
126
|
return;
|
|
115
127
|
}
|
|
116
128
|
let above = 0;
|
|
117
|
-
for (const layer of context.layers)
|
|
129
|
+
for (const layer of context.layers) {
|
|
130
|
+
if (layer.contains(el)) {
|
|
131
|
+
above++;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
118
134
|
setCount(above);
|
|
119
135
|
};
|
|
120
|
-
|
|
121
|
-
|
|
136
|
+
check();
|
|
137
|
+
document.addEventListener(CONTEXT_UPDATE, check);
|
|
138
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, check);
|
|
139
|
+
}, [context.layers, ref]);
|
|
140
|
+
return count;
|
|
122
141
|
}
|
|
123
142
|
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
124
143
|
const {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
144
|
+
disableOutsidePointerEvents = false,
|
|
145
|
+
forceUnmount,
|
|
146
|
+
onEscapeKeyDown,
|
|
147
|
+
onPointerDownOutside,
|
|
148
|
+
onFocusOutside,
|
|
149
|
+
onInteractOutside,
|
|
150
|
+
onDismiss,
|
|
151
|
+
asChild,
|
|
152
|
+
children,
|
|
153
|
+
branches: branchesProp,
|
|
154
|
+
...layerProps
|
|
155
|
+
} = props;
|
|
156
|
+
const Comp = asChild ? import_core.Slot : import_core.View;
|
|
157
|
+
const context = React.useContext(DismissableContext);
|
|
158
|
+
const [node, setNode] = React.useState(null);
|
|
159
|
+
const [, force] = React.useState({});
|
|
160
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, node2 => setNode(node2));
|
|
161
|
+
const layers = Array.from(context.layers);
|
|
162
|
+
const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1);
|
|
163
|
+
const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled);
|
|
164
|
+
const index = node ? layers.indexOf(node) : -1;
|
|
165
|
+
const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0;
|
|
166
|
+
const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex;
|
|
167
|
+
const pointerDownOutside = usePointerDownOutside(event => {
|
|
168
|
+
const target = event.target;
|
|
169
|
+
const branches = branchesProp || context.branches;
|
|
170
|
+
const isPointerDownOnBranch = [...branches].some(branch => branch.contains(target));
|
|
171
|
+
if (!isPointerEventsEnabled || isPointerDownOnBranch) return;
|
|
172
|
+
onPointerDownOutside?.(event);
|
|
173
|
+
onInteractOutside?.(event);
|
|
174
|
+
if (!event.defaultPrevented) onDismiss?.();
|
|
175
|
+
});
|
|
176
|
+
const focusOutside = useFocusOutside(event => {
|
|
177
|
+
const target = event.target;
|
|
178
|
+
const branches = branchesProp || context.branches;
|
|
179
|
+
const isFocusInBranch = [...branches].some(branch => branch.contains(target));
|
|
180
|
+
if (isFocusInBranch) return;
|
|
181
|
+
onFocusOutside?.(event);
|
|
182
|
+
onInteractOutside?.(event);
|
|
183
|
+
if (!event.defaultPrevented) onDismiss?.();
|
|
184
|
+
});
|
|
185
|
+
const forceUnmountRef = React.useRef(forceUnmount);
|
|
186
|
+
React.useEffect(() => {
|
|
159
187
|
forceUnmountRef.current = forceUnmount;
|
|
160
|
-
}, [forceUnmount])
|
|
188
|
+
}, [forceUnmount]);
|
|
189
|
+
(0, import_use_escape_keydown.useEscapeKeydown)(event => {
|
|
161
190
|
if (forceUnmountRef.current) return;
|
|
162
191
|
const currentLayers = Array.from(context.layers);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
192
|
+
const currentIndex = node ? currentLayers.indexOf(node) : -1;
|
|
193
|
+
const isHighestLayer = currentIndex === currentLayers.length - 1;
|
|
194
|
+
if (!isHighestLayer) return;
|
|
195
|
+
onEscapeKeyDown?.(event);
|
|
196
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
onDismiss();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
React.useEffect(() => {
|
|
202
|
+
if (!node) return;
|
|
203
|
+
if (forceUnmount) return;
|
|
204
|
+
if (disableOutsidePointerEvents) {
|
|
205
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 0) {
|
|
206
|
+
originalBodyPointerEvents = document.body.style.pointerEvents;
|
|
207
|
+
document.body.style.pointerEvents = "none";
|
|
208
|
+
}
|
|
209
|
+
context.layersWithOutsidePointerEventsDisabled.add(node);
|
|
210
|
+
layersWithPointerEventsDisabledCount++;
|
|
211
|
+
}
|
|
212
|
+
context.layers.add(node);
|
|
213
|
+
globalLayers.add(node);
|
|
214
|
+
if (disableOutsidePointerEvents || layersWithPointerEventsDisabledCount > 0) {
|
|
215
|
+
dispatchUpdate();
|
|
216
|
+
}
|
|
217
|
+
notifyLayerChange();
|
|
218
|
+
return () => {
|
|
219
|
+
if (disableOutsidePointerEvents) {
|
|
220
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 1) {
|
|
221
|
+
document.body.style.pointerEvents = originalBodyPointerEvents;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
167
224
|
};
|
|
168
|
-
}, [node, disableOutsidePointerEvents, forceUnmount, context])
|
|
169
|
-
|
|
225
|
+
}, [node, disableOutsidePointerEvents, forceUnmount, context]);
|
|
226
|
+
React.useEffect(() => {
|
|
227
|
+
if (forceUnmount) return;
|
|
228
|
+
return () => {
|
|
170
229
|
if (!node) return;
|
|
171
230
|
const hadPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.has(node);
|
|
172
|
-
context.layers.delete(node)
|
|
231
|
+
context.layers.delete(node);
|
|
232
|
+
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
233
|
+
globalLayers.delete(node);
|
|
234
|
+
if (layersWithPointerEventsDisabledCount > 0) {
|
|
235
|
+
dispatchUpdate();
|
|
236
|
+
}
|
|
237
|
+
notifyLayerChange();
|
|
238
|
+
if (hadPointerEventsDisabled) {
|
|
239
|
+
layersWithPointerEventsDisabledCount--;
|
|
240
|
+
}
|
|
173
241
|
};
|
|
174
|
-
}, [node, context, forceUnmount])
|
|
242
|
+
}, [node, context, forceUnmount]);
|
|
243
|
+
React.useEffect(() => {
|
|
175
244
|
const handleUpdate = () => {
|
|
176
|
-
layersWithPointerEventsDisabledCount > 0
|
|
245
|
+
if (layersWithPointerEventsDisabledCount > 0) {
|
|
246
|
+
force({});
|
|
247
|
+
}
|
|
177
248
|
};
|
|
178
|
-
|
|
179
|
-
|
|
249
|
+
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
250
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
251
|
+
}, []);
|
|
252
|
+
return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Comp, {
|
|
180
253
|
...layerProps,
|
|
181
254
|
ref: composedRefs,
|
|
182
255
|
...(!asChild && {
|
|
183
256
|
display: "contents"
|
|
184
257
|
}),
|
|
185
258
|
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0,
|
|
186
|
-
onFocusCapture: (0,
|
|
187
|
-
onBlurCapture: (0,
|
|
188
|
-
onPointerDownCapture: (0,
|
|
259
|
+
onFocusCapture: (0, import_core.composeEventHandlers)(props.onFocusCapture, focusOutside.onFocusCapture),
|
|
260
|
+
onBlurCapture: (0, import_core.composeEventHandlers)(props.onBlurCapture, focusOutside.onBlurCapture),
|
|
261
|
+
onPointerDownCapture: (0, import_core.composeEventHandlers)(props.onPointerDownCapture, pointerDownOutside.onPointerDownCapture),
|
|
189
262
|
children
|
|
190
263
|
});
|
|
191
264
|
});
|
|
192
265
|
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
193
|
-
const BRANCH_NAME = "DismissableBranch"
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
266
|
+
const BRANCH_NAME = "DismissableBranch";
|
|
267
|
+
const DismissableBranch = React.forwardRef((props, forwardedRef) => {
|
|
268
|
+
const {
|
|
269
|
+
branches: branchesProp,
|
|
270
|
+
...rest
|
|
271
|
+
} = props;
|
|
272
|
+
const context = React.useContext(DismissableContext);
|
|
273
|
+
const ref = React.useRef(null);
|
|
274
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, ref);
|
|
275
|
+
React.useEffect(() => {
|
|
276
|
+
const node = ref.current;
|
|
277
|
+
if (!(node instanceof HTMLElement)) return;
|
|
278
|
+
const branches = branchesProp || context.branches;
|
|
279
|
+
if (node && branches) {
|
|
280
|
+
branches.add(node);
|
|
281
|
+
return () => {
|
|
207
282
|
branches.delete(node);
|
|
208
283
|
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
284
|
+
}
|
|
285
|
+
}, [branchesProp, context.branches]);
|
|
286
|
+
return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_core.View, {
|
|
287
|
+
asChild: "except-style",
|
|
288
|
+
...rest,
|
|
289
|
+
ref: composedRefs
|
|
214
290
|
});
|
|
291
|
+
});
|
|
215
292
|
DismissableBranch.displayName = BRANCH_NAME;
|
|
216
293
|
function usePointerDownOutside(onPointerDownOutside) {
|
|
217
|
-
const handlePointerDownOutside = (0,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
294
|
+
const handlePointerDownOutside = (0, import_use_event.useEvent)(onPointerDownOutside);
|
|
295
|
+
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
296
|
+
const handleClickRef = React.useRef(() => {});
|
|
297
|
+
React.useEffect(() => {
|
|
221
298
|
const handlePointerDown = event => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
299
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
300
|
+
let handleAndDispatchPointerDownOutsideEvent = function () {
|
|
301
|
+
handleAndDispatchCustomEvent(POINTER_DOWN_OUTSIDE, handlePointerDownOutside, eventDetail, {
|
|
302
|
+
discrete: true
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
const eventDetail = {
|
|
306
|
+
originalEvent: event
|
|
307
|
+
};
|
|
308
|
+
if (event.pointerType === "touch") {
|
|
309
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
310
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent;
|
|
311
|
+
document.addEventListener("click", handleClickRef.current, {
|
|
312
|
+
once: true
|
|
313
|
+
});
|
|
314
|
+
} else {
|
|
315
|
+
handleAndDispatchPointerDownOutsideEvent();
|
|
234
316
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
317
|
+
}
|
|
318
|
+
isPointerInsideReactTreeRef.current = false;
|
|
319
|
+
};
|
|
320
|
+
const timerId = setTimeout(() => {
|
|
321
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
322
|
+
}, 0);
|
|
240
323
|
return () => {
|
|
241
|
-
window.clearTimeout(timerId)
|
|
324
|
+
window.clearTimeout(timerId);
|
|
325
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
326
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
242
327
|
};
|
|
243
|
-
}, [handlePointerDownOutside])
|
|
328
|
+
}, [handlePointerDownOutside]);
|
|
329
|
+
return {
|
|
244
330
|
// ensures we check React component tree (not just DOM tree)
|
|
245
331
|
onPointerDownCapture: () => {
|
|
246
|
-
isPointerInsideReactTreeRef.current =
|
|
332
|
+
isPointerInsideReactTreeRef.current = true;
|
|
247
333
|
}
|
|
248
334
|
};
|
|
249
335
|
}
|
|
250
336
|
function useFocusOutside(onFocusOutside) {
|
|
251
|
-
const handleFocusOutside = (0,
|
|
252
|
-
|
|
253
|
-
|
|
337
|
+
const handleFocusOutside = (0, import_use_event.useEvent)(onFocusOutside);
|
|
338
|
+
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
339
|
+
React.useEffect(() => {
|
|
254
340
|
const handleFocus = event => {
|
|
255
|
-
event.target && !isFocusInsideReactTreeRef.current
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
341
|
+
if (event.target && !isFocusInsideReactTreeRef.current) {
|
|
342
|
+
const eventDetail = {
|
|
343
|
+
originalEvent: event
|
|
344
|
+
};
|
|
345
|
+
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {
|
|
346
|
+
discrete: false
|
|
347
|
+
});
|
|
348
|
+
}
|
|
260
349
|
};
|
|
261
|
-
|
|
262
|
-
|
|
350
|
+
document.addEventListener("focusin", handleFocus);
|
|
351
|
+
return () => document.removeEventListener("focusin", handleFocus);
|
|
352
|
+
}, [handleFocusOutside]);
|
|
353
|
+
return {
|
|
263
354
|
onFocusCapture: () => {
|
|
264
|
-
isFocusInsideReactTreeRef.current =
|
|
355
|
+
isFocusInsideReactTreeRef.current = true;
|
|
265
356
|
},
|
|
266
357
|
onBlurCapture: () => {
|
|
267
|
-
isFocusInsideReactTreeRef.current =
|
|
358
|
+
isFocusInsideReactTreeRef.current = false;
|
|
268
359
|
}
|
|
269
360
|
};
|
|
270
361
|
}
|
|
@@ -275,13 +366,18 @@ function dispatchUpdate() {
|
|
|
275
366
|
function handleAndDispatchCustomEvent(name, handler, detail, {
|
|
276
367
|
discrete
|
|
277
368
|
}) {
|
|
278
|
-
const target = detail.originalEvent.target
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
handler
|
|
285
|
-
once:
|
|
286
|
-
})
|
|
369
|
+
const target = detail.originalEvent.target;
|
|
370
|
+
const event = new CustomEvent(name, {
|
|
371
|
+
bubbles: false,
|
|
372
|
+
cancelable: true,
|
|
373
|
+
detail
|
|
374
|
+
});
|
|
375
|
+
if (handler) target.addEventListener(name, handler, {
|
|
376
|
+
once: true
|
|
377
|
+
});
|
|
378
|
+
if (discrete) {
|
|
379
|
+
dispatchDiscreteCustomEvent(target, event);
|
|
380
|
+
} else {
|
|
381
|
+
target.dispatchEvent(event);
|
|
382
|
+
}
|
|
287
383
|
}
|