@destyler/interact-outside 0.0.7-beta.9

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-PRESENT Elone Hoo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,32 @@
1
+ //#region index.d.ts
2
+ interface InteractOutsideHandlers {
3
+ /**
4
+ * Function called when the pointer is pressed down outside the component
5
+ */
6
+ onPointerDownOutside?: ((event: PointerDownOutsideEvent) => void) | undefined;
7
+ /**
8
+ * Function called when the focus is moved outside the component
9
+ */
10
+ onFocusOutside?: ((event: FocusOutsideEvent) => void) | undefined;
11
+ /**
12
+ * Function called when an interaction happens outside the component
13
+ */
14
+ onInteractOutside?: ((event: InteractOutsideEvent) => void) | undefined;
15
+ }
16
+ interface InteractOutsideOptions extends InteractOutsideHandlers {
17
+ exclude?: ((target: HTMLElement) => boolean) | undefined;
18
+ defer?: boolean | undefined;
19
+ }
20
+ interface EventDetails<T> {
21
+ originalEvent: T;
22
+ contextmenu: boolean;
23
+ focusable: boolean;
24
+ }
25
+ type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
26
+ type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
27
+ type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
28
+ type MaybeElement = HTMLElement | null | undefined;
29
+ type NodeOrFn = MaybeElement | (() => MaybeElement);
30
+ declare function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions): () => void;
31
+ //#endregion
32
+ export { EventDetails, FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, InteractOutsideOptions, MaybeElement, NodeOrFn, PointerDownOutsideEvent, trackInteractOutside };
package/dist/index.js ADDED
@@ -0,0 +1,197 @@
1
+ import { addDomEvent, contains, getDocument, getEventTarget, getNearestOverflowAncestor, getWindow, isContextMenuEvent, isFocusable, isHTMLElement, raf } from "@destyler/dom";
2
+ import { callAll } from "@destyler/utils";
3
+
4
+ //#region src/frame-utils.ts
5
+ function getWindowFrames(win) {
6
+ const frames = {
7
+ each(cb) {
8
+ for (let i = 0; i < win.frames?.length; i += 1) {
9
+ const frame = win.frames[i];
10
+ if (frame) cb(frame);
11
+ }
12
+ },
13
+ addEventListener(event, listener, options) {
14
+ frames.each((frame) => {
15
+ try {
16
+ frame.document.addEventListener(event, listener, options);
17
+ } catch {}
18
+ });
19
+ return () => {
20
+ try {
21
+ frames.removeEventListener(event, listener, options);
22
+ } catch {}
23
+ };
24
+ },
25
+ removeEventListener(event, listener, options) {
26
+ frames.each((frame) => {
27
+ try {
28
+ frame.document.removeEventListener(event, listener, options);
29
+ } catch {}
30
+ });
31
+ }
32
+ };
33
+ return frames;
34
+ }
35
+ function getParentWindow(win) {
36
+ const parent = win.frameElement != null ? win.parent : null;
37
+ return {
38
+ addEventListener: (event, listener, options) => {
39
+ try {
40
+ parent?.addEventListener(event, listener, options);
41
+ } catch {}
42
+ return () => {
43
+ try {
44
+ parent?.removeEventListener(event, listener, options);
45
+ } catch {}
46
+ };
47
+ },
48
+ removeEventListener: (event, listener, options) => {
49
+ try {
50
+ parent?.removeEventListener(event, listener, options);
51
+ } catch {}
52
+ }
53
+ };
54
+ }
55
+
56
+ //#endregion
57
+ //#region index.ts
58
+ const POINTER_OUTSIDE_EVENT = "pointerdown.outside";
59
+ const FOCUS_OUTSIDE_EVENT = "focus.outside";
60
+ function isComposedPathFocusable(composedPath) {
61
+ for (const node of composedPath) if (isHTMLElement(node) && isFocusable(node)) return true;
62
+ return false;
63
+ }
64
+ const isPointerEvent = (event) => "clientY" in event;
65
+ function isEventPointWithin(node, event) {
66
+ if (!isPointerEvent(event) || !node) return false;
67
+ const rect = node.getBoundingClientRect();
68
+ if (rect.width === 0 || rect.height === 0) return false;
69
+ return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
70
+ }
71
+ function isPointInRect(rect, point) {
72
+ return rect.y <= point.y && point.y <= rect.y + rect.height && rect.x <= point.x && point.x <= rect.x + rect.width;
73
+ }
74
+ function isEventWithinScrollbar(event, ancestor) {
75
+ if (!ancestor || !isPointerEvent(event)) return false;
76
+ const isScrollableY = ancestor.scrollHeight > ancestor.clientHeight;
77
+ const onScrollbarY = isScrollableY && event.clientX > ancestor.offsetLeft + ancestor.clientWidth;
78
+ const isScrollableX = ancestor.scrollWidth > ancestor.clientWidth;
79
+ const onScrollbarX = isScrollableX && event.clientY > ancestor.offsetTop + ancestor.clientHeight;
80
+ const rect = {
81
+ x: ancestor.offsetLeft,
82
+ y: ancestor.offsetTop,
83
+ width: ancestor.clientWidth + (isScrollableY ? 16 : 0),
84
+ height: ancestor.clientHeight + (isScrollableX ? 16 : 0)
85
+ };
86
+ const point = {
87
+ x: event.clientX,
88
+ y: event.clientY
89
+ };
90
+ if (!isPointInRect(rect, point)) return false;
91
+ return onScrollbarY || onScrollbarX;
92
+ }
93
+ function trackInteractOutsideImpl(node, options) {
94
+ const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options;
95
+ if (!node) return;
96
+ const doc = getDocument(node);
97
+ const win = getWindow(node);
98
+ const frames = getWindowFrames(win);
99
+ const parentWin = getParentWindow(win);
100
+ function isEventOutside(event) {
101
+ const target = getEventTarget(event);
102
+ if (!isHTMLElement(target)) return false;
103
+ if (!target.isConnected) return false;
104
+ if (contains(node, target)) return false;
105
+ if (isEventPointWithin(node, event)) return false;
106
+ const triggerEl = doc.querySelector(`[aria-controls="${node.id}"]`);
107
+ if (triggerEl) {
108
+ const triggerAncestor = getNearestOverflowAncestor(triggerEl);
109
+ if (isEventWithinScrollbar(event, triggerAncestor)) return false;
110
+ }
111
+ const nodeAncestor = getNearestOverflowAncestor(node);
112
+ if (isEventWithinScrollbar(event, nodeAncestor)) return false;
113
+ return !exclude?.(target);
114
+ }
115
+ const pointerdownCleanups = /* @__PURE__ */ new Set();
116
+ function onPointerDown(event) {
117
+ function handler() {
118
+ const func = defer ? raf : (v) => v();
119
+ const composedPath = event.composedPath?.() ?? [event.target];
120
+ func(() => {
121
+ if (!node || !isEventOutside(event)) return;
122
+ if (onPointerDownOutside || onInteractOutside) {
123
+ const handler$1 = callAll(onPointerDownOutside, onInteractOutside);
124
+ node.addEventListener(POINTER_OUTSIDE_EVENT, handler$1, { once: true });
125
+ }
126
+ fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
127
+ bubbles: false,
128
+ cancelable: true,
129
+ detail: {
130
+ originalEvent: event,
131
+ contextmenu: isContextMenuEvent(event),
132
+ focusable: isComposedPathFocusable(composedPath)
133
+ }
134
+ });
135
+ });
136
+ }
137
+ if (event.pointerType === "touch") {
138
+ pointerdownCleanups.forEach((fn) => fn());
139
+ pointerdownCleanups.add(addDomEvent(doc, "click", handler, { once: true }));
140
+ pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
141
+ pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
142
+ } else handler();
143
+ }
144
+ const cleanups = /* @__PURE__ */ new Set();
145
+ const timer = setTimeout(() => {
146
+ cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
147
+ cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
148
+ cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
149
+ }, 0);
150
+ function onFocusin(event) {
151
+ const func = defer ? raf : (v) => v();
152
+ func(() => {
153
+ if (!node || !isEventOutside(event)) return;
154
+ if (onFocusOutside || onInteractOutside) {
155
+ const handler = callAll(onFocusOutside, onInteractOutside);
156
+ node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
157
+ }
158
+ fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
159
+ bubbles: false,
160
+ cancelable: true,
161
+ detail: {
162
+ originalEvent: event,
163
+ contextmenu: false,
164
+ focusable: isFocusable(getEventTarget(event))
165
+ }
166
+ });
167
+ });
168
+ }
169
+ cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
170
+ cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
171
+ cleanups.add(frames.addEventListener("focusin", onFocusin, true));
172
+ return () => {
173
+ clearTimeout(timer);
174
+ pointerdownCleanups.forEach((fn) => fn());
175
+ cleanups.forEach((fn) => fn());
176
+ };
177
+ }
178
+ function trackInteractOutside(nodeOrFn, options) {
179
+ const { defer } = options;
180
+ const func = defer ? raf : (v) => v();
181
+ const cleanups = [];
182
+ cleanups.push(func(() => {
183
+ const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
184
+ cleanups.push(trackInteractOutsideImpl(node, options));
185
+ }));
186
+ return () => {
187
+ cleanups.forEach((fn) => fn?.());
188
+ };
189
+ }
190
+ function fireCustomEvent(el, type, init) {
191
+ const win = el.ownerDocument.defaultView || window;
192
+ const event = new win.CustomEvent(type, init);
193
+ return el.dispatchEvent(event);
194
+ }
195
+
196
+ //#endregion
197
+ export { trackInteractOutside };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@destyler/interact-outside",
3
+ "type": "module",
4
+ "version": "0.0.7-beta.9",
5
+ "description": "",
6
+ "author": "Elone Hoo <elonehoo@gmail.com>",
7
+ "license": "MIT",
8
+ "exports": {
9
+ ".": "./dist/index.js"
10
+ },
11
+ "main": "dist/index.js",
12
+ "module": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "*.d.ts",
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@destyler/dom": "0.0.7-beta.9",
20
+ "@destyler/utils": "0.0.7-beta.9"
21
+ },
22
+ "scripts": {
23
+ "clear": "rimraf ./dist",
24
+ "clear:node_modules": "rimraf ./node_modules",
25
+ "build": "pnpm run clear && tsdown",
26
+ "watch": "tsdown --watch"
27
+ }
28
+ }