@zag-js/interact-outside 1.46.0 → 1.48.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/dist/frame-utils.d.mts +8 -10
- package/dist/frame-utils.d.ts +8 -10
- package/dist/frame-utils.js +52 -82
- package/dist/frame-utils.mjs +47 -56
- package/dist/index.d.mts +35 -37
- package/dist/index.d.ts +35 -37
- package/dist/index.js +144 -185
- package/dist/index.mjs +140 -170
- package/package.json +5 -5
package/dist/frame-utils.d.mts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
declare function getWindowFrames(win: Window): {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export declare function getWindowFrames(win: Window): {
|
|
2
|
+
each(cb: (win: Window) => void): void;
|
|
3
|
+
addEventListener(event: string, listener: any, options?: any): () => void;
|
|
4
|
+
removeEventListener(event: string, listener: any, options?: any): void;
|
|
5
5
|
};
|
|
6
|
-
declare function getParentWindow(win: Window): {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { getParentWindow, getWindowFrames };
|
|
6
|
+
export declare function getParentWindow(win: Window): {
|
|
7
|
+
addEventListener: (event: string, listener: any, options?: any) => () => void;
|
|
8
|
+
removeEventListener: (event: string, listener: any, options?: any) => void;
|
|
9
|
+
};
|
package/dist/frame-utils.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
declare function getWindowFrames(win: Window): {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export declare function getWindowFrames(win: Window): {
|
|
2
|
+
each(cb: (win: Window) => void): void;
|
|
3
|
+
addEventListener(event: string, listener: any, options?: any): () => void;
|
|
4
|
+
removeEventListener(event: string, listener: any, options?: any): void;
|
|
5
5
|
};
|
|
6
|
-
declare function getParentWindow(win: Window): {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { getParentWindow, getWindowFrames };
|
|
6
|
+
export declare function getParentWindow(win: Window): {
|
|
7
|
+
addEventListener: (event: string, listener: any, options?: any) => () => void;
|
|
8
|
+
removeEventListener: (event: string, listener: any, options?: any) => void;
|
|
9
|
+
};
|
package/dist/frame-utils.js
CHANGED
|
@@ -1,87 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/frame-utils.ts
|
|
21
|
-
var frame_utils_exports = {};
|
|
22
|
-
__export(frame_utils_exports, {
|
|
23
|
-
getParentWindow: () => getParentWindow,
|
|
24
|
-
getWindowFrames: () => getWindowFrames
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
25
4
|
});
|
|
26
|
-
|
|
5
|
+
// src/frame-utils.ts
|
|
27
6
|
function getWindowFrames(win) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
return frames;
|
|
7
|
+
const frames = {
|
|
8
|
+
each(cb) {
|
|
9
|
+
for (let i = 0; i < win.frames?.length; i += 1) {
|
|
10
|
+
const frame = win.frames[i];
|
|
11
|
+
if (frame) cb(frame);
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
addEventListener(event, listener, options) {
|
|
15
|
+
frames.each((frame) => {
|
|
16
|
+
try {
|
|
17
|
+
frame.document.addEventListener(event, listener, options);
|
|
18
|
+
} catch {}
|
|
19
|
+
});
|
|
20
|
+
return () => {
|
|
21
|
+
try {
|
|
22
|
+
frames.removeEventListener(event, listener, options);
|
|
23
|
+
} catch {}
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
removeEventListener(event, listener, options) {
|
|
27
|
+
frames.each((frame) => {
|
|
28
|
+
try {
|
|
29
|
+
frame.document.removeEventListener(event, listener, options);
|
|
30
|
+
} catch {}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return frames;
|
|
59
35
|
}
|
|
60
36
|
function getParentWindow(win) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
};
|
|
37
|
+
const parent = win.frameElement != null ? win.parent.document : null;
|
|
38
|
+
return {
|
|
39
|
+
addEventListener: (event, listener, options) => {
|
|
40
|
+
try {
|
|
41
|
+
parent?.addEventListener(event, listener, options);
|
|
42
|
+
} catch {}
|
|
43
|
+
return () => {
|
|
44
|
+
try {
|
|
45
|
+
parent?.removeEventListener(event, listener, options);
|
|
46
|
+
} catch {}
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
removeEventListener: (event, listener, options) => {
|
|
50
|
+
try {
|
|
51
|
+
parent?.removeEventListener(event, listener, options);
|
|
52
|
+
} catch {}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
82
55
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
getParentWindow,
|
|
86
|
-
getWindowFrames
|
|
87
|
-
});
|
|
56
|
+
exports.getParentWindow = getParentWindow;
|
|
57
|
+
exports.getWindowFrames = getWindowFrames;
|
package/dist/frame-utils.mjs
CHANGED
|
@@ -1,61 +1,52 @@
|
|
|
1
1
|
// src/frame-utils.ts
|
|
2
2
|
function getWindowFrames(win) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
return frames;
|
|
3
|
+
const frames = {
|
|
4
|
+
each(cb) {
|
|
5
|
+
for (let i = 0; i < win.frames?.length; i += 1) {
|
|
6
|
+
const frame = win.frames[i];
|
|
7
|
+
if (frame) cb(frame);
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
addEventListener(event, listener, options) {
|
|
11
|
+
frames.each((frame) => {
|
|
12
|
+
try {
|
|
13
|
+
frame.document.addEventListener(event, listener, options);
|
|
14
|
+
} catch {}
|
|
15
|
+
});
|
|
16
|
+
return () => {
|
|
17
|
+
try {
|
|
18
|
+
frames.removeEventListener(event, listener, options);
|
|
19
|
+
} catch {}
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
removeEventListener(event, listener, options) {
|
|
23
|
+
frames.each((frame) => {
|
|
24
|
+
try {
|
|
25
|
+
frame.document.removeEventListener(event, listener, options);
|
|
26
|
+
} catch {}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return frames;
|
|
34
31
|
}
|
|
35
32
|
function getParentWindow(win) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
33
|
+
const parent = win.frameElement != null ? win.parent.document : null;
|
|
34
|
+
return {
|
|
35
|
+
addEventListener: (event, listener, options) => {
|
|
36
|
+
try {
|
|
37
|
+
parent?.addEventListener(event, listener, options);
|
|
38
|
+
} catch {}
|
|
39
|
+
return () => {
|
|
40
|
+
try {
|
|
41
|
+
parent?.removeEventListener(event, listener, options);
|
|
42
|
+
} catch {}
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
removeEventListener: (event, listener, options) => {
|
|
46
|
+
try {
|
|
47
|
+
parent?.removeEventListener(event, listener, options);
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
57
51
|
}
|
|
58
|
-
export {
|
|
59
|
-
getParentWindow,
|
|
60
|
-
getWindowFrames
|
|
61
|
-
};
|
|
52
|
+
export { getParentWindow, getWindowFrames };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
interface InteractOutsideHandlers {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
export interface InteractOutsideHandlers {
|
|
2
|
+
/**
|
|
3
|
+
* Function called when the pointer is pressed down outside the component
|
|
4
|
+
*/
|
|
5
|
+
onPointerDownOutside?: ((event: PointerDownOutsideEvent) => void) | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Function called when the focus is moved outside the component
|
|
8
|
+
*/
|
|
9
|
+
onFocusOutside?: ((event: FocusOutsideEvent) => void) | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Function called when an interaction happens outside the component
|
|
12
|
+
*/
|
|
13
|
+
onInteractOutside?: ((event: InteractOutsideEvent) => void) | undefined;
|
|
14
14
|
}
|
|
15
|
-
interface InteractOutsideOptions extends InteractOutsideHandlers {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
export interface InteractOutsideOptions extends InteractOutsideHandlers {
|
|
16
|
+
exclude?: ((target: HTMLElement) => boolean) | undefined;
|
|
17
|
+
defer?: boolean | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Default: `true`. If `true`, the interact outside detection will recursively include elements
|
|
20
|
+
* that are controlled by elements within the trap via `aria-controls`. Only elements
|
|
21
|
+
* with `aria-expanded="true"` and interactive container roles (menu, listbox, dialog, etc.)
|
|
22
|
+
* will be included. This allows nested menus, select dropdowns, and other portalled
|
|
23
|
+
* content to remain within the interaction boundary.
|
|
24
|
+
*/
|
|
25
|
+
followControlledElements?: boolean | undefined;
|
|
26
26
|
}
|
|
27
|
-
interface EventDetails<T> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
export interface EventDetails<T> {
|
|
28
|
+
originalEvent: T;
|
|
29
|
+
contextmenu: boolean;
|
|
30
|
+
focusable: boolean;
|
|
31
|
+
target: EventTarget;
|
|
32
32
|
}
|
|
33
|
-
type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
|
|
34
|
-
type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
|
|
35
|
-
type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
|
|
36
|
-
type MaybeElement = HTMLElement | null | undefined;
|
|
37
|
-
type NodeOrFn = MaybeElement | (() => MaybeElement);
|
|
38
|
-
declare function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions): () => void;
|
|
39
|
-
|
|
40
|
-
export { type EventDetails, type FocusOutsideEvent, type InteractOutsideEvent, type InteractOutsideHandlers, type InteractOutsideOptions, type MaybeElement, type NodeOrFn, type PointerDownOutsideEvent, trackInteractOutside };
|
|
33
|
+
export type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
|
|
34
|
+
export type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
|
|
35
|
+
export type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
|
|
36
|
+
export type MaybeElement = HTMLElement | null | undefined;
|
|
37
|
+
export type NodeOrFn = MaybeElement | (() => MaybeElement);
|
|
38
|
+
export declare function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions): () => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
interface InteractOutsideHandlers {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
export interface InteractOutsideHandlers {
|
|
2
|
+
/**
|
|
3
|
+
* Function called when the pointer is pressed down outside the component
|
|
4
|
+
*/
|
|
5
|
+
onPointerDownOutside?: ((event: PointerDownOutsideEvent) => void) | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Function called when the focus is moved outside the component
|
|
8
|
+
*/
|
|
9
|
+
onFocusOutside?: ((event: FocusOutsideEvent) => void) | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Function called when an interaction happens outside the component
|
|
12
|
+
*/
|
|
13
|
+
onInteractOutside?: ((event: InteractOutsideEvent) => void) | undefined;
|
|
14
14
|
}
|
|
15
|
-
interface InteractOutsideOptions extends InteractOutsideHandlers {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
export interface InteractOutsideOptions extends InteractOutsideHandlers {
|
|
16
|
+
exclude?: ((target: HTMLElement) => boolean) | undefined;
|
|
17
|
+
defer?: boolean | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Default: `true`. If `true`, the interact outside detection will recursively include elements
|
|
20
|
+
* that are controlled by elements within the trap via `aria-controls`. Only elements
|
|
21
|
+
* with `aria-expanded="true"` and interactive container roles (menu, listbox, dialog, etc.)
|
|
22
|
+
* will be included. This allows nested menus, select dropdowns, and other portalled
|
|
23
|
+
* content to remain within the interaction boundary.
|
|
24
|
+
*/
|
|
25
|
+
followControlledElements?: boolean | undefined;
|
|
26
26
|
}
|
|
27
|
-
interface EventDetails<T> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
export interface EventDetails<T> {
|
|
28
|
+
originalEvent: T;
|
|
29
|
+
contextmenu: boolean;
|
|
30
|
+
focusable: boolean;
|
|
31
|
+
target: EventTarget;
|
|
32
32
|
}
|
|
33
|
-
type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
|
|
34
|
-
type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
|
|
35
|
-
type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
|
|
36
|
-
type MaybeElement = HTMLElement | null | undefined;
|
|
37
|
-
type NodeOrFn = MaybeElement | (() => MaybeElement);
|
|
38
|
-
declare function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions): () => void;
|
|
39
|
-
|
|
40
|
-
export { type EventDetails, type FocusOutsideEvent, type InteractOutsideEvent, type InteractOutsideHandlers, type InteractOutsideOptions, type MaybeElement, type NodeOrFn, type PointerDownOutsideEvent, trackInteractOutside };
|
|
33
|
+
export type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
|
|
34
|
+
export type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
|
|
35
|
+
export type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
|
|
36
|
+
export type MaybeElement = HTMLElement | null | undefined;
|
|
37
|
+
export type NodeOrFn = MaybeElement | (() => MaybeElement);
|
|
38
|
+
export declare function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions): () => void;
|
package/dist/index.js
CHANGED
|
@@ -1,200 +1,159 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
trackInteractOutside: () => trackInteractOutside
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
24
4
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var FOCUS_OUTSIDE_EVENT = "focus.outside";
|
|
5
|
+
const require_frame_utils = require("./frame-utils.js");
|
|
6
|
+
let _zag_js_dom_query = require("@zag-js/dom-query");
|
|
7
|
+
let _zag_js_utils = require("@zag-js/utils");
|
|
8
|
+
// src/index.ts
|
|
9
|
+
const POINTER_OUTSIDE_EVENT = "pointerdown.outside";
|
|
10
|
+
const FOCUS_OUTSIDE_EVENT = "focus.outside";
|
|
32
11
|
function isComposedPathFocusable(composedPath) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return false;
|
|
12
|
+
for (const node of composedPath) if ((0, _zag_js_dom_query.isHTMLElement)(node) && (0, _zag_js_dom_query.isFocusable)(node)) return true;
|
|
13
|
+
return false;
|
|
37
14
|
}
|
|
38
|
-
|
|
15
|
+
const isPointerEvent = (event) => "clientY" in event;
|
|
39
16
|
function isEventPointWithin(node, event) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
17
|
+
if (!isPointerEvent(event) || !node) return false;
|
|
18
|
+
const rect = node.getBoundingClientRect();
|
|
19
|
+
if (rect.width === 0 || rect.height === 0) return false;
|
|
20
|
+
return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
|
|
44
21
|
}
|
|
45
22
|
function isPointInRect(rect, point) {
|
|
46
|
-
|
|
23
|
+
return rect.y <= point.y && point.y <= rect.y + rect.height && rect.x <= point.x && point.x <= rect.x + rect.width;
|
|
47
24
|
}
|
|
48
25
|
function isEventWithinScrollbar(event, ancestor) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (!isPointInRect(rect, point)) return false;
|
|
65
|
-
return onScrollbarY || onScrollbarX;
|
|
26
|
+
if (!ancestor || !isPointerEvent(event)) return false;
|
|
27
|
+
const isScrollableY = ancestor.scrollHeight > ancestor.clientHeight;
|
|
28
|
+
const onScrollbarY = isScrollableY && event.clientX > ancestor.offsetLeft + ancestor.clientWidth;
|
|
29
|
+
const isScrollableX = ancestor.scrollWidth > ancestor.clientWidth;
|
|
30
|
+
const onScrollbarX = isScrollableX && event.clientY > ancestor.offsetTop + ancestor.clientHeight;
|
|
31
|
+
if (!isPointInRect({
|
|
32
|
+
x: ancestor.offsetLeft,
|
|
33
|
+
y: ancestor.offsetTop,
|
|
34
|
+
width: ancestor.clientWidth + (isScrollableY ? 16 : 0),
|
|
35
|
+
height: ancestor.clientHeight + (isScrollableX ? 16 : 0)
|
|
36
|
+
}, {
|
|
37
|
+
x: event.clientX,
|
|
38
|
+
y: event.clientY
|
|
39
|
+
})) return false;
|
|
40
|
+
return onScrollbarY || onScrollbarX;
|
|
66
41
|
}
|
|
67
42
|
function trackInteractOutsideImpl(node, options) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (!(0, import_dom_query.isTouchDevice)()) {
|
|
168
|
-
cleanups.add((0, import_dom_query.addDomEvent)(doc, "focusin", onFocusin, true));
|
|
169
|
-
cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
|
|
170
|
-
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
171
|
-
}
|
|
172
|
-
return () => {
|
|
173
|
-
clearTimeout(timer);
|
|
174
|
-
pointerdownCleanups.forEach((fn) => fn());
|
|
175
|
-
cleanups.forEach((fn) => fn());
|
|
176
|
-
};
|
|
43
|
+
const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer, followControlledElements = true } = options;
|
|
44
|
+
if (!node) return;
|
|
45
|
+
const doc = (0, _zag_js_dom_query.getDocument)(node);
|
|
46
|
+
const win = (0, _zag_js_dom_query.getWindow)(node);
|
|
47
|
+
const frames = require_frame_utils.getWindowFrames(win);
|
|
48
|
+
const parentWin = require_frame_utils.getParentWindow(win);
|
|
49
|
+
function isEventOutside(event, target) {
|
|
50
|
+
if (!(0, _zag_js_dom_query.isHTMLElement)(target)) return false;
|
|
51
|
+
if (!target.isConnected) return false;
|
|
52
|
+
if ((0, _zag_js_dom_query.contains)(node, target)) return false;
|
|
53
|
+
if (isEventPointWithin(node, event)) return false;
|
|
54
|
+
if (followControlledElements && (0, _zag_js_dom_query.isControlledElement)(node, target)) return false;
|
|
55
|
+
const triggerEl = doc.querySelector(`[aria-controls="${node.id}"]`);
|
|
56
|
+
if (triggerEl) {
|
|
57
|
+
if (isEventWithinScrollbar(event, (0, _zag_js_dom_query.getNearestOverflowAncestor)(triggerEl))) return false;
|
|
58
|
+
}
|
|
59
|
+
if (isEventWithinScrollbar(event, (0, _zag_js_dom_query.getNearestOverflowAncestor)(node))) return false;
|
|
60
|
+
return !exclude?.(target);
|
|
61
|
+
}
|
|
62
|
+
const pointerdownCleanups = /* @__PURE__ */ new Set();
|
|
63
|
+
const isInShadowRoot = (0, _zag_js_dom_query.isShadowRoot)(node?.getRootNode());
|
|
64
|
+
let isPointerDown = false;
|
|
65
|
+
function onPointerDown(event) {
|
|
66
|
+
isPointerDown = true;
|
|
67
|
+
const onPointerUp = () => {
|
|
68
|
+
isPointerDown = false;
|
|
69
|
+
};
|
|
70
|
+
doc.addEventListener("pointerup", onPointerUp, { once: true });
|
|
71
|
+
win.addEventListener("pointerup", onPointerUp, { once: true });
|
|
72
|
+
function handler(clickEvent) {
|
|
73
|
+
const func = defer && !(0, _zag_js_dom_query.isTouchDevice)() ? _zag_js_dom_query.raf : (v) => v();
|
|
74
|
+
const evt = clickEvent ?? event;
|
|
75
|
+
const composedPath = evt?.composedPath?.() ?? [evt?.target];
|
|
76
|
+
func(() => {
|
|
77
|
+
const target = isInShadowRoot ? composedPath[0] : (0, _zag_js_dom_query.getEventTarget)(event);
|
|
78
|
+
if (!node || !isEventOutside(event, target)) return;
|
|
79
|
+
if (onPointerDownOutside || onInteractOutside) {
|
|
80
|
+
const handler = (0, _zag_js_utils.callAll)(onPointerDownOutside, onInteractOutside);
|
|
81
|
+
node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true });
|
|
82
|
+
}
|
|
83
|
+
fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
|
|
84
|
+
bubbles: false,
|
|
85
|
+
cancelable: true,
|
|
86
|
+
detail: {
|
|
87
|
+
originalEvent: evt,
|
|
88
|
+
contextmenu: (0, _zag_js_dom_query.isContextMenuEvent)(evt),
|
|
89
|
+
focusable: isComposedPathFocusable(composedPath),
|
|
90
|
+
target
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (event.pointerType === "touch") {
|
|
96
|
+
pointerdownCleanups.forEach((fn) => fn());
|
|
97
|
+
pointerdownCleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "click", handler, { once: true }));
|
|
98
|
+
pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
|
|
99
|
+
pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
|
|
100
|
+
} else handler();
|
|
101
|
+
}
|
|
102
|
+
const cleanups = /* @__PURE__ */ new Set();
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
cleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "pointerdown", onPointerDown, true));
|
|
105
|
+
cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
|
|
106
|
+
cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
|
|
107
|
+
}, 0);
|
|
108
|
+
const focusFrame = _zag_js_dom_query.AnimationFrame.create();
|
|
109
|
+
function onFocusin(event) {
|
|
110
|
+
if (isPointerDown) return;
|
|
111
|
+
(defer ? (fn) => focusFrame.request(fn) : (fn) => fn())(() => {
|
|
112
|
+
const composedPath = event?.composedPath?.() ?? [event?.target];
|
|
113
|
+
const target = isInShadowRoot ? composedPath[0] : (0, _zag_js_dom_query.getEventTarget)(event);
|
|
114
|
+
if (!node || !isEventOutside(event, target)) return;
|
|
115
|
+
if (onFocusOutside || onInteractOutside) {
|
|
116
|
+
const handler = (0, _zag_js_utils.callAll)(onFocusOutside, onInteractOutside);
|
|
117
|
+
node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
|
|
118
|
+
}
|
|
119
|
+
fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
|
|
120
|
+
bubbles: false,
|
|
121
|
+
cancelable: true,
|
|
122
|
+
detail: {
|
|
123
|
+
originalEvent: event,
|
|
124
|
+
contextmenu: false,
|
|
125
|
+
focusable: (0, _zag_js_dom_query.isFocusable)(target),
|
|
126
|
+
target
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (!(0, _zag_js_dom_query.isTouchDevice)()) {
|
|
132
|
+
cleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "focusin", onFocusin, true));
|
|
133
|
+
cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
|
|
134
|
+
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
135
|
+
}
|
|
136
|
+
return () => {
|
|
137
|
+
clearTimeout(timer);
|
|
138
|
+
focusFrame.cancel();
|
|
139
|
+
pointerdownCleanups.forEach((fn) => fn());
|
|
140
|
+
cleanups.forEach((fn) => fn());
|
|
141
|
+
};
|
|
177
142
|
}
|
|
178
143
|
function trackInteractOutside(nodeOrFn, options) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
cleanups.forEach((fn) => fn?.());
|
|
190
|
-
};
|
|
144
|
+
const { defer } = options;
|
|
145
|
+
const func = defer ? _zag_js_dom_query.raf : (v) => v();
|
|
146
|
+
const cleanups = [];
|
|
147
|
+
cleanups.push(func(() => {
|
|
148
|
+
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
|
|
149
|
+
cleanups.push(trackInteractOutsideImpl(node, options));
|
|
150
|
+
}));
|
|
151
|
+
return () => {
|
|
152
|
+
cleanups.forEach((fn) => fn?.());
|
|
153
|
+
};
|
|
191
154
|
}
|
|
192
155
|
function fireCustomEvent(el, type, init) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return el.dispatchEvent(event);
|
|
156
|
+
const event = new (el.ownerDocument.defaultView || window).CustomEvent(type, init);
|
|
157
|
+
return el.dispatchEvent(event);
|
|
196
158
|
}
|
|
197
|
-
|
|
198
|
-
0 && (module.exports = {
|
|
199
|
-
trackInteractOutside
|
|
200
|
-
});
|
|
159
|
+
exports.trackInteractOutside = trackInteractOutside;
|
package/dist/index.mjs
CHANGED
|
@@ -1,185 +1,155 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { addDomEvent, isContextMenuEvent, isShadowRoot, isTouchDevice } from "@zag-js/dom-query";
|
|
3
|
-
import {
|
|
4
|
-
contains,
|
|
5
|
-
getDocument,
|
|
6
|
-
getEventTarget,
|
|
7
|
-
getNearestOverflowAncestor,
|
|
8
|
-
getWindow,
|
|
9
|
-
isControlledElement,
|
|
10
|
-
isFocusable,
|
|
11
|
-
isHTMLElement,
|
|
12
|
-
raf
|
|
13
|
-
} from "@zag-js/dom-query";
|
|
14
|
-
import { callAll } from "@zag-js/utils";
|
|
15
1
|
import { getParentWindow, getWindowFrames } from "./frame-utils.mjs";
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
import { AnimationFrame, addDomEvent, contains, getDocument, getEventTarget, getNearestOverflowAncestor, getWindow, isContextMenuEvent, isControlledElement, isFocusable, isHTMLElement, isShadowRoot, isTouchDevice, raf } from "@zag-js/dom-query";
|
|
3
|
+
import { callAll } from "@zag-js/utils";
|
|
4
|
+
// src/index.ts
|
|
5
|
+
const POINTER_OUTSIDE_EVENT = "pointerdown.outside";
|
|
6
|
+
const FOCUS_OUTSIDE_EVENT = "focus.outside";
|
|
18
7
|
function isComposedPathFocusable(composedPath) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
8
|
+
for (const node of composedPath) if (isHTMLElement(node) && isFocusable(node)) return true;
|
|
9
|
+
return false;
|
|
23
10
|
}
|
|
24
|
-
|
|
11
|
+
const isPointerEvent = (event) => "clientY" in event;
|
|
25
12
|
function isEventPointWithin(node, event) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
13
|
+
if (!isPointerEvent(event) || !node) return false;
|
|
14
|
+
const rect = node.getBoundingClientRect();
|
|
15
|
+
if (rect.width === 0 || rect.height === 0) return false;
|
|
16
|
+
return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
|
|
30
17
|
}
|
|
31
18
|
function isPointInRect(rect, point) {
|
|
32
|
-
|
|
19
|
+
return rect.y <= point.y && point.y <= rect.y + rect.height && rect.x <= point.x && point.x <= rect.x + rect.width;
|
|
33
20
|
}
|
|
34
21
|
function isEventWithinScrollbar(event, ancestor) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!isPointInRect(rect, point)) return false;
|
|
51
|
-
return onScrollbarY || onScrollbarX;
|
|
22
|
+
if (!ancestor || !isPointerEvent(event)) return false;
|
|
23
|
+
const isScrollableY = ancestor.scrollHeight > ancestor.clientHeight;
|
|
24
|
+
const onScrollbarY = isScrollableY && event.clientX > ancestor.offsetLeft + ancestor.clientWidth;
|
|
25
|
+
const isScrollableX = ancestor.scrollWidth > ancestor.clientWidth;
|
|
26
|
+
const onScrollbarX = isScrollableX && event.clientY > ancestor.offsetTop + ancestor.clientHeight;
|
|
27
|
+
if (!isPointInRect({
|
|
28
|
+
x: ancestor.offsetLeft,
|
|
29
|
+
y: ancestor.offsetTop,
|
|
30
|
+
width: ancestor.clientWidth + (isScrollableY ? 16 : 0),
|
|
31
|
+
height: ancestor.clientHeight + (isScrollableX ? 16 : 0)
|
|
32
|
+
}, {
|
|
33
|
+
x: event.clientX,
|
|
34
|
+
y: event.clientY
|
|
35
|
+
})) return false;
|
|
36
|
+
return onScrollbarY || onScrollbarX;
|
|
52
37
|
}
|
|
53
38
|
function trackInteractOutsideImpl(node, options) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
if (!isTouchDevice()) {
|
|
154
|
-
cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
|
|
155
|
-
cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
|
|
156
|
-
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
157
|
-
}
|
|
158
|
-
return () => {
|
|
159
|
-
clearTimeout(timer);
|
|
160
|
-
pointerdownCleanups.forEach((fn) => fn());
|
|
161
|
-
cleanups.forEach((fn) => fn());
|
|
162
|
-
};
|
|
39
|
+
const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer, followControlledElements = true } = options;
|
|
40
|
+
if (!node) return;
|
|
41
|
+
const doc = getDocument(node);
|
|
42
|
+
const win = getWindow(node);
|
|
43
|
+
const frames = getWindowFrames(win);
|
|
44
|
+
const parentWin = getParentWindow(win);
|
|
45
|
+
function isEventOutside(event, target) {
|
|
46
|
+
if (!isHTMLElement(target)) return false;
|
|
47
|
+
if (!target.isConnected) return false;
|
|
48
|
+
if (contains(node, target)) return false;
|
|
49
|
+
if (isEventPointWithin(node, event)) return false;
|
|
50
|
+
if (followControlledElements && isControlledElement(node, target)) return false;
|
|
51
|
+
const triggerEl = doc.querySelector(`[aria-controls="${node.id}"]`);
|
|
52
|
+
if (triggerEl) {
|
|
53
|
+
if (isEventWithinScrollbar(event, getNearestOverflowAncestor(triggerEl))) return false;
|
|
54
|
+
}
|
|
55
|
+
if (isEventWithinScrollbar(event, getNearestOverflowAncestor(node))) return false;
|
|
56
|
+
return !exclude?.(target);
|
|
57
|
+
}
|
|
58
|
+
const pointerdownCleanups = /* @__PURE__ */ new Set();
|
|
59
|
+
const isInShadowRoot = isShadowRoot(node?.getRootNode());
|
|
60
|
+
let isPointerDown = false;
|
|
61
|
+
function onPointerDown(event) {
|
|
62
|
+
isPointerDown = true;
|
|
63
|
+
const onPointerUp = () => {
|
|
64
|
+
isPointerDown = false;
|
|
65
|
+
};
|
|
66
|
+
doc.addEventListener("pointerup", onPointerUp, { once: true });
|
|
67
|
+
win.addEventListener("pointerup", onPointerUp, { once: true });
|
|
68
|
+
function handler(clickEvent) {
|
|
69
|
+
const func = defer && !isTouchDevice() ? raf : (v) => v();
|
|
70
|
+
const evt = clickEvent ?? event;
|
|
71
|
+
const composedPath = evt?.composedPath?.() ?? [evt?.target];
|
|
72
|
+
func(() => {
|
|
73
|
+
const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
|
|
74
|
+
if (!node || !isEventOutside(event, target)) return;
|
|
75
|
+
if (onPointerDownOutside || onInteractOutside) {
|
|
76
|
+
const handler = callAll(onPointerDownOutside, onInteractOutside);
|
|
77
|
+
node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true });
|
|
78
|
+
}
|
|
79
|
+
fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
|
|
80
|
+
bubbles: false,
|
|
81
|
+
cancelable: true,
|
|
82
|
+
detail: {
|
|
83
|
+
originalEvent: evt,
|
|
84
|
+
contextmenu: isContextMenuEvent(evt),
|
|
85
|
+
focusable: isComposedPathFocusable(composedPath),
|
|
86
|
+
target
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (event.pointerType === "touch") {
|
|
92
|
+
pointerdownCleanups.forEach((fn) => fn());
|
|
93
|
+
pointerdownCleanups.add(addDomEvent(doc, "click", handler, { once: true }));
|
|
94
|
+
pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
|
|
95
|
+
pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
|
|
96
|
+
} else handler();
|
|
97
|
+
}
|
|
98
|
+
const cleanups = /* @__PURE__ */ new Set();
|
|
99
|
+
const timer = setTimeout(() => {
|
|
100
|
+
cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
|
|
101
|
+
cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
|
|
102
|
+
cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
|
|
103
|
+
}, 0);
|
|
104
|
+
const focusFrame = AnimationFrame.create();
|
|
105
|
+
function onFocusin(event) {
|
|
106
|
+
if (isPointerDown) return;
|
|
107
|
+
(defer ? (fn) => focusFrame.request(fn) : (fn) => fn())(() => {
|
|
108
|
+
const composedPath = event?.composedPath?.() ?? [event?.target];
|
|
109
|
+
const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
|
|
110
|
+
if (!node || !isEventOutside(event, target)) return;
|
|
111
|
+
if (onFocusOutside || onInteractOutside) {
|
|
112
|
+
const handler = callAll(onFocusOutside, onInteractOutside);
|
|
113
|
+
node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
|
|
114
|
+
}
|
|
115
|
+
fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
|
|
116
|
+
bubbles: false,
|
|
117
|
+
cancelable: true,
|
|
118
|
+
detail: {
|
|
119
|
+
originalEvent: event,
|
|
120
|
+
contextmenu: false,
|
|
121
|
+
focusable: isFocusable(target),
|
|
122
|
+
target
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (!isTouchDevice()) {
|
|
128
|
+
cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
|
|
129
|
+
cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
|
|
130
|
+
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
131
|
+
}
|
|
132
|
+
return () => {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
focusFrame.cancel();
|
|
135
|
+
pointerdownCleanups.forEach((fn) => fn());
|
|
136
|
+
cleanups.forEach((fn) => fn());
|
|
137
|
+
};
|
|
163
138
|
}
|
|
164
139
|
function trackInteractOutside(nodeOrFn, options) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
cleanups.forEach((fn) => fn?.());
|
|
176
|
-
};
|
|
140
|
+
const { defer } = options;
|
|
141
|
+
const func = defer ? raf : (v) => v();
|
|
142
|
+
const cleanups = [];
|
|
143
|
+
cleanups.push(func(() => {
|
|
144
|
+
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
|
|
145
|
+
cleanups.push(trackInteractOutsideImpl(node, options));
|
|
146
|
+
}));
|
|
147
|
+
return () => {
|
|
148
|
+
cleanups.forEach((fn) => fn?.());
|
|
149
|
+
};
|
|
177
150
|
}
|
|
178
151
|
function fireCustomEvent(el, type, init) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return el.dispatchEvent(event);
|
|
152
|
+
const event = new (el.ownerDocument.defaultView || window).CustomEvent(type, init);
|
|
153
|
+
return el.dispatchEvent(event);
|
|
182
154
|
}
|
|
183
|
-
export {
|
|
184
|
-
trackInteractOutside
|
|
185
|
-
};
|
|
155
|
+
export { trackInteractOutside };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/interact-outside",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.48.0",
|
|
4
4
|
"description": "Track interactions or focus outside an element",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@zag-js/dom-query": "1.
|
|
23
|
-
"@zag-js/utils": "1.
|
|
22
|
+
"@zag-js/dom-query": "1.48.0",
|
|
23
|
+
"@zag-js/utils": "1.48.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"clean-package": "2.2.0"
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
"./package.json": "./package.json"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
|
-
"build": "
|
|
52
|
+
"build": "tsdown",
|
|
53
53
|
"test": "vitest",
|
|
54
|
-
"lint": "
|
|
54
|
+
"lint": "oxlint src",
|
|
55
55
|
"test-ci": "pnpm test --ci --runInBand -u",
|
|
56
56
|
"test-watch": "pnpm test --watchAll"
|
|
57
57
|
}
|