@zag-js/interact-outside 2.0.0-next.2 → 2.0.0-next.4

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.
@@ -1,11 +1,9 @@
1
- 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;
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
- addEventListener: (event: string, listener: any, options?: any) => () => void;
8
- removeEventListener: (event: string, listener: any, options?: any) => void;
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
+ };
@@ -1,11 +1,9 @@
1
- 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;
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
- addEventListener: (event: string, listener: any, options?: any) => () => void;
8
- removeEventListener: (event: string, listener: any, options?: any) => void;
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
+ };
@@ -1,87 +1,57 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
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
- module.exports = __toCommonJS(frame_utils_exports);
5
+ // src/frame-utils.ts
27
6
  function getWindowFrames(win) {
28
- const frames = {
29
- each(cb) {
30
- for (let i = 0; i < win.frames?.length; i += 1) {
31
- const frame = win.frames[i];
32
- if (frame) cb(frame);
33
- }
34
- },
35
- addEventListener(event, listener, options) {
36
- frames.each((frame) => {
37
- try {
38
- frame.document.addEventListener(event, listener, options);
39
- } catch {
40
- }
41
- });
42
- return () => {
43
- try {
44
- frames.removeEventListener(event, listener, options);
45
- } catch {
46
- }
47
- };
48
- },
49
- removeEventListener(event, listener, options) {
50
- frames.each((frame) => {
51
- try {
52
- frame.document.removeEventListener(event, listener, options);
53
- } catch {
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
- const parent = win.frameElement != null ? win.parent : null;
62
- return {
63
- addEventListener: (event, listener, options) => {
64
- try {
65
- parent?.addEventListener(event, listener, options);
66
- } catch {
67
- }
68
- return () => {
69
- try {
70
- parent?.removeEventListener(event, listener, options);
71
- } catch {
72
- }
73
- };
74
- },
75
- removeEventListener: (event, listener, options) => {
76
- try {
77
- parent?.removeEventListener(event, listener, options);
78
- } catch {
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
- // Annotate the CommonJS export names for ESM import in node:
84
- 0 && (module.exports = {
85
- getParentWindow,
86
- getWindowFrames
87
- });
56
+ exports.getParentWindow = getParentWindow;
57
+ exports.getWindowFrames = getWindowFrames;
@@ -1,61 +1,52 @@
1
1
  // src/frame-utils.ts
2
2
  function getWindowFrames(win) {
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
- });
17
- return () => {
18
- try {
19
- frames.removeEventListener(event, listener, options);
20
- } catch {
21
- }
22
- };
23
- },
24
- removeEventListener(event, listener, options) {
25
- frames.each((frame) => {
26
- try {
27
- frame.document.removeEventListener(event, listener, options);
28
- } catch {
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
- 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
- }
43
- return () => {
44
- try {
45
- parent?.removeEventListener(event, listener, options);
46
- } catch {
47
- }
48
- };
49
- },
50
- removeEventListener: (event, listener, options) => {
51
- try {
52
- parent?.removeEventListener(event, listener, options);
53
- } catch {
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
- * 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;
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
- 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;
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
- originalEvent: T;
29
- contextmenu: boolean;
30
- focusable: boolean;
31
- target: EventTarget;
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
- * 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;
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
- 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;
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
- originalEvent: T;
29
- contextmenu: boolean;
30
- focusable: boolean;
31
- target: EventTarget;
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,161 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
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
- module.exports = __toCommonJS(index_exports);
26
- var import_dom_query = require("@zag-js/dom-query");
27
- var import_dom_query2 = require("@zag-js/dom-query");
28
- var import_utils = require("@zag-js/utils");
29
- var import_frame_utils = require("./frame-utils.js");
30
- var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
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
- for (const node of composedPath) {
34
- if ((0, import_dom_query2.isHTMLElement)(node) && (0, import_dom_query2.isFocusable)(node)) return true;
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
- var isPointerEvent = (event) => "clientY" in event;
15
+ const isPointerEvent = (event) => "clientY" in event;
39
16
  function isEventPointWithin(node, event) {
40
- if (!isPointerEvent(event) || !node) return false;
41
- const rect = node.getBoundingClientRect();
42
- if (rect.width === 0 || rect.height === 0) return false;
43
- return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
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
- return rect.y <= point.y && point.y <= rect.y + rect.height && rect.x <= point.x && point.x <= rect.x + rect.width;
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
- if (!ancestor || !isPointerEvent(event)) return false;
50
- const isScrollableY = ancestor.scrollHeight > ancestor.clientHeight;
51
- const onScrollbarY = isScrollableY && event.clientX > ancestor.offsetLeft + ancestor.clientWidth;
52
- const isScrollableX = ancestor.scrollWidth > ancestor.clientWidth;
53
- const onScrollbarX = isScrollableX && event.clientY > ancestor.offsetTop + ancestor.clientHeight;
54
- const rect = {
55
- x: ancestor.offsetLeft,
56
- y: ancestor.offsetTop,
57
- width: ancestor.clientWidth + (isScrollableY ? 16 : 0),
58
- height: ancestor.clientHeight + (isScrollableX ? 16 : 0)
59
- };
60
- const point = {
61
- x: event.clientX,
62
- y: event.clientY
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
- const {
69
- exclude,
70
- onFocusOutside,
71
- onPointerDownOutside,
72
- onInteractOutside,
73
- defer,
74
- followControlledElements = true
75
- } = options;
76
- if (!node) return;
77
- const doc = (0, import_dom_query2.getDocument)(node);
78
- const win = (0, import_dom_query2.getWindow)(node);
79
- const frames = (0, import_frame_utils.getWindowFrames)(win);
80
- const parentWin = (0, import_frame_utils.getParentWindow)(win);
81
- function isEventOutside(event, target) {
82
- if (!(0, import_dom_query2.isHTMLElement)(target)) return false;
83
- if (!target.isConnected) return false;
84
- if ((0, import_dom_query2.contains)(node, target)) return false;
85
- if (isEventPointWithin(node, event)) return false;
86
- if (followControlledElements && (0, import_dom_query2.isControlledElement)(node, target)) return false;
87
- const triggerEl = doc.querySelector(`[aria-controls~="${node.id}"]`);
88
- if (triggerEl) {
89
- const triggerAncestor = (0, import_dom_query2.getNearestOverflowAncestor)(triggerEl);
90
- if (isEventWithinScrollbar(event, triggerAncestor)) return false;
91
- }
92
- const nodeAncestor = (0, import_dom_query2.getNearestOverflowAncestor)(node);
93
- if (isEventWithinScrollbar(event, nodeAncestor)) return false;
94
- return !exclude?.(target);
95
- }
96
- const pointerdownCleanups = /* @__PURE__ */ new Set();
97
- const isInShadowRoot = (0, import_dom_query.isShadowRoot)(node?.getRootNode());
98
- let isPointerDown = false;
99
- function onPointerDown(event) {
100
- isPointerDown = true;
101
- const onPointerUp = () => {
102
- isPointerDown = false;
103
- };
104
- doc.addEventListener("pointerup", onPointerUp, { once: true });
105
- win.addEventListener("pointerup", onPointerUp, { once: true });
106
- function handler(clickEvent) {
107
- const func = defer && !(0, import_dom_query.isTouchDevice)() ? import_dom_query2.raf : (v) => v();
108
- const evt = clickEvent ?? event;
109
- const composedPath = evt?.composedPath?.() ?? [evt?.target];
110
- func(() => {
111
- const target = isInShadowRoot ? composedPath[0] : (0, import_dom_query2.getEventTarget)(event);
112
- if (!node || !isEventOutside(event, target)) return;
113
- if (onPointerDownOutside || onInteractOutside) {
114
- const handler2 = (0, import_utils.callAll)(onPointerDownOutside, onInteractOutside);
115
- node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
116
- }
117
- fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
118
- bubbles: false,
119
- cancelable: true,
120
- detail: {
121
- originalEvent: evt,
122
- contextmenu: (0, import_dom_query.isContextMenuEvent)(evt),
123
- focusable: isComposedPathFocusable(composedPath),
124
- target
125
- }
126
- });
127
- });
128
- }
129
- if (event.pointerType === "touch") {
130
- pointerdownCleanups.forEach((fn) => fn());
131
- pointerdownCleanups.add((0, import_dom_query.addDomEvent)(doc, "click", handler, { once: true }));
132
- pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
133
- pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
134
- } else {
135
- handler();
136
- }
137
- }
138
- const cleanups = /* @__PURE__ */ new Set();
139
- const timer = setTimeout(() => {
140
- cleanups.add((0, import_dom_query.addDomEvent)(doc, "pointerdown", onPointerDown, true));
141
- cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
142
- cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
143
- }, 0);
144
- function onFocusin(event) {
145
- if (isPointerDown) return;
146
- const func = defer ? import_dom_query2.raf : (v) => v();
147
- func(() => {
148
- const composedPath = event?.composedPath?.() ?? [event?.target];
149
- const target = isInShadowRoot ? composedPath[0] : (0, import_dom_query2.getEventTarget)(event);
150
- if (!node || !isEventOutside(event, target)) return;
151
- if (onFocusOutside || onInteractOutside) {
152
- const handler = (0, import_utils.callAll)(onFocusOutside, onInteractOutside);
153
- node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
154
- }
155
- fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
156
- bubbles: false,
157
- cancelable: true,
158
- detail: {
159
- originalEvent: event,
160
- contextmenu: false,
161
- focusable: (0, import_dom_query2.isFocusable)(target),
162
- target
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
+ const pointerFrame = _zag_js_dom_query.AnimationFrame.create();
66
+ function onPointerDown(event) {
67
+ isPointerDown = true;
68
+ const onPointerUp = () => {
69
+ isPointerDown = false;
70
+ };
71
+ doc.addEventListener("pointerup", onPointerUp, { once: true });
72
+ win.addEventListener("pointerup", onPointerUp, { once: true });
73
+ function handler(clickEvent) {
74
+ const func = defer && !(0, _zag_js_dom_query.isTouchDevice)() ? (fn) => pointerFrame.request(fn) : (fn) => fn();
75
+ const evt = clickEvent ?? event;
76
+ const composedPath = evt?.composedPath?.() ?? [evt?.target];
77
+ func(() => {
78
+ const target = isInShadowRoot ? composedPath[0] : (0, _zag_js_dom_query.getEventTarget)(event);
79
+ if (!node || !isEventOutside(event, target)) return;
80
+ if (onPointerDownOutside || onInteractOutside) {
81
+ const handler = (0, _zag_js_utils.callAll)(onPointerDownOutside, onInteractOutside);
82
+ node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true });
83
+ }
84
+ fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
85
+ bubbles: false,
86
+ cancelable: true,
87
+ detail: {
88
+ originalEvent: evt,
89
+ contextmenu: (0, _zag_js_dom_query.isContextMenuEvent)(evt),
90
+ focusable: isComposedPathFocusable(composedPath),
91
+ target
92
+ }
93
+ });
94
+ });
95
+ }
96
+ if (event.pointerType === "touch") {
97
+ pointerdownCleanups.forEach((fn) => fn());
98
+ pointerdownCleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "click", handler, { once: true }));
99
+ pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
100
+ pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
101
+ } else handler();
102
+ }
103
+ const cleanups = /* @__PURE__ */ new Set();
104
+ const timer = setTimeout(() => {
105
+ cleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "pointerdown", onPointerDown, true));
106
+ cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
107
+ cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
108
+ }, 0);
109
+ const focusFrame = _zag_js_dom_query.AnimationFrame.create();
110
+ function onFocusin(event) {
111
+ if (isPointerDown) return;
112
+ (defer ? (fn) => focusFrame.request(fn) : (fn) => fn())(() => {
113
+ const composedPath = event?.composedPath?.() ?? [event?.target];
114
+ const target = isInShadowRoot ? composedPath[0] : (0, _zag_js_dom_query.getEventTarget)(event);
115
+ if (!node || !isEventOutside(event, target)) return;
116
+ if (onFocusOutside || onInteractOutside) {
117
+ const handler = (0, _zag_js_utils.callAll)(onFocusOutside, onInteractOutside);
118
+ node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
119
+ }
120
+ fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
121
+ bubbles: false,
122
+ cancelable: true,
123
+ detail: {
124
+ originalEvent: event,
125
+ contextmenu: false,
126
+ focusable: (0, _zag_js_dom_query.isFocusable)(target),
127
+ target
128
+ }
129
+ });
130
+ });
131
+ }
132
+ if (!(0, _zag_js_dom_query.isTouchDevice)()) {
133
+ cleanups.add((0, _zag_js_dom_query.addDomEvent)(doc, "focusin", onFocusin, true));
134
+ cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
135
+ cleanups.add(frames.addEventListener("focusin", onFocusin, true));
136
+ }
137
+ return () => {
138
+ clearTimeout(timer);
139
+ pointerFrame.cancel();
140
+ focusFrame.cancel();
141
+ pointerdownCleanups.forEach((fn) => fn());
142
+ cleanups.forEach((fn) => fn());
143
+ };
177
144
  }
178
145
  function trackInteractOutside(nodeOrFn, options) {
179
- const { defer } = options;
180
- const func = defer ? import_dom_query2.raf : (v) => v();
181
- const cleanups = [];
182
- cleanups.push(
183
- func(() => {
184
- const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
185
- cleanups.push(trackInteractOutsideImpl(node, options));
186
- })
187
- );
188
- return () => {
189
- cleanups.forEach((fn) => fn?.());
190
- };
146
+ const { defer } = options;
147
+ const func = defer ? _zag_js_dom_query.raf : (v) => v();
148
+ const cleanups = [];
149
+ cleanups.push(func(() => {
150
+ const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
151
+ cleanups.push(trackInteractOutsideImpl(node, options));
152
+ }));
153
+ return () => {
154
+ cleanups.forEach((fn) => fn?.());
155
+ };
191
156
  }
192
157
  function fireCustomEvent(el, type, init) {
193
- const win = el.ownerDocument.defaultView || window;
194
- const event = new win.CustomEvent(type, init);
195
- return el.dispatchEvent(event);
158
+ const event = new (el.ownerDocument.defaultView || window).CustomEvent(type, init);
159
+ return el.dispatchEvent(event);
196
160
  }
197
- // Annotate the CommonJS export names for ESM import in node:
198
- 0 && (module.exports = {
199
- trackInteractOutside
200
- });
161
+ exports.trackInteractOutside = trackInteractOutside;
package/dist/index.mjs CHANGED
@@ -1,185 +1,157 @@
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
- var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
17
- var FOCUS_OUTSIDE_EVENT = "focus.outside";
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
- for (const node of composedPath) {
20
- if (isHTMLElement(node) && isFocusable(node)) return true;
21
- }
22
- return false;
8
+ for (const node of composedPath) if (isHTMLElement(node) && isFocusable(node)) return true;
9
+ return false;
23
10
  }
24
- var isPointerEvent = (event) => "clientY" in event;
11
+ const isPointerEvent = (event) => "clientY" in event;
25
12
  function isEventPointWithin(node, event) {
26
- if (!isPointerEvent(event) || !node) return false;
27
- const rect = node.getBoundingClientRect();
28
- if (rect.width === 0 || rect.height === 0) return false;
29
- return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
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
- return rect.y <= point.y && point.y <= rect.y + rect.height && rect.x <= point.x && point.x <= rect.x + rect.width;
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
- if (!ancestor || !isPointerEvent(event)) return false;
36
- const isScrollableY = ancestor.scrollHeight > ancestor.clientHeight;
37
- const onScrollbarY = isScrollableY && event.clientX > ancestor.offsetLeft + ancestor.clientWidth;
38
- const isScrollableX = ancestor.scrollWidth > ancestor.clientWidth;
39
- const onScrollbarX = isScrollableX && event.clientY > ancestor.offsetTop + ancestor.clientHeight;
40
- const rect = {
41
- x: ancestor.offsetLeft,
42
- y: ancestor.offsetTop,
43
- width: ancestor.clientWidth + (isScrollableY ? 16 : 0),
44
- height: ancestor.clientHeight + (isScrollableX ? 16 : 0)
45
- };
46
- const point = {
47
- x: event.clientX,
48
- y: event.clientY
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
- const {
55
- exclude,
56
- onFocusOutside,
57
- onPointerDownOutside,
58
- onInteractOutside,
59
- defer,
60
- followControlledElements = true
61
- } = options;
62
- if (!node) return;
63
- const doc = getDocument(node);
64
- const win = getWindow(node);
65
- const frames = getWindowFrames(win);
66
- const parentWin = getParentWindow(win);
67
- function isEventOutside(event, target) {
68
- if (!isHTMLElement(target)) return false;
69
- if (!target.isConnected) return false;
70
- if (contains(node, target)) return false;
71
- if (isEventPointWithin(node, event)) return false;
72
- if (followControlledElements && isControlledElement(node, target)) return false;
73
- const triggerEl = doc.querySelector(`[aria-controls~="${node.id}"]`);
74
- if (triggerEl) {
75
- const triggerAncestor = getNearestOverflowAncestor(triggerEl);
76
- if (isEventWithinScrollbar(event, triggerAncestor)) return false;
77
- }
78
- const nodeAncestor = getNearestOverflowAncestor(node);
79
- if (isEventWithinScrollbar(event, nodeAncestor)) return false;
80
- return !exclude?.(target);
81
- }
82
- const pointerdownCleanups = /* @__PURE__ */ new Set();
83
- const isInShadowRoot = isShadowRoot(node?.getRootNode());
84
- let isPointerDown = false;
85
- function onPointerDown(event) {
86
- isPointerDown = true;
87
- const onPointerUp = () => {
88
- isPointerDown = false;
89
- };
90
- doc.addEventListener("pointerup", onPointerUp, { once: true });
91
- win.addEventListener("pointerup", onPointerUp, { once: true });
92
- function handler(clickEvent) {
93
- const func = defer && !isTouchDevice() ? raf : (v) => v();
94
- const evt = clickEvent ?? event;
95
- const composedPath = evt?.composedPath?.() ?? [evt?.target];
96
- func(() => {
97
- const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
98
- if (!node || !isEventOutside(event, target)) return;
99
- if (onPointerDownOutside || onInteractOutside) {
100
- const handler2 = callAll(onPointerDownOutside, onInteractOutside);
101
- node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
102
- }
103
- fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
104
- bubbles: false,
105
- cancelable: true,
106
- detail: {
107
- originalEvent: evt,
108
- contextmenu: isContextMenuEvent(evt),
109
- focusable: isComposedPathFocusable(composedPath),
110
- target
111
- }
112
- });
113
- });
114
- }
115
- if (event.pointerType === "touch") {
116
- pointerdownCleanups.forEach((fn) => fn());
117
- pointerdownCleanups.add(addDomEvent(doc, "click", handler, { once: true }));
118
- pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
119
- pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
120
- } else {
121
- handler();
122
- }
123
- }
124
- const cleanups = /* @__PURE__ */ new Set();
125
- const timer = setTimeout(() => {
126
- cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
127
- cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
128
- cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
129
- }, 0);
130
- function onFocusin(event) {
131
- if (isPointerDown) return;
132
- const func = defer ? raf : (v) => v();
133
- func(() => {
134
- const composedPath = event?.composedPath?.() ?? [event?.target];
135
- const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
136
- if (!node || !isEventOutside(event, target)) return;
137
- if (onFocusOutside || onInteractOutside) {
138
- const handler = callAll(onFocusOutside, onInteractOutside);
139
- node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
140
- }
141
- fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
142
- bubbles: false,
143
- cancelable: true,
144
- detail: {
145
- originalEvent: event,
146
- contextmenu: false,
147
- focusable: isFocusable(target),
148
- target
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
+ const pointerFrame = AnimationFrame.create();
62
+ function onPointerDown(event) {
63
+ isPointerDown = true;
64
+ const onPointerUp = () => {
65
+ isPointerDown = false;
66
+ };
67
+ doc.addEventListener("pointerup", onPointerUp, { once: true });
68
+ win.addEventListener("pointerup", onPointerUp, { once: true });
69
+ function handler(clickEvent) {
70
+ const func = defer && !isTouchDevice() ? (fn) => pointerFrame.request(fn) : (fn) => fn();
71
+ const evt = clickEvent ?? event;
72
+ const composedPath = evt?.composedPath?.() ?? [evt?.target];
73
+ func(() => {
74
+ const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
75
+ if (!node || !isEventOutside(event, target)) return;
76
+ if (onPointerDownOutside || onInteractOutside) {
77
+ const handler = callAll(onPointerDownOutside, onInteractOutside);
78
+ node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true });
79
+ }
80
+ fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
81
+ bubbles: false,
82
+ cancelable: true,
83
+ detail: {
84
+ originalEvent: evt,
85
+ contextmenu: isContextMenuEvent(evt),
86
+ focusable: isComposedPathFocusable(composedPath),
87
+ target
88
+ }
89
+ });
90
+ });
91
+ }
92
+ if (event.pointerType === "touch") {
93
+ pointerdownCleanups.forEach((fn) => fn());
94
+ pointerdownCleanups.add(addDomEvent(doc, "click", handler, { once: true }));
95
+ pointerdownCleanups.add(parentWin.addEventListener("click", handler, { once: true }));
96
+ pointerdownCleanups.add(frames.addEventListener("click", handler, { once: true }));
97
+ } else handler();
98
+ }
99
+ const cleanups = /* @__PURE__ */ new Set();
100
+ const timer = setTimeout(() => {
101
+ cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
102
+ cleanups.add(parentWin.addEventListener("pointerdown", onPointerDown, true));
103
+ cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
104
+ }, 0);
105
+ const focusFrame = AnimationFrame.create();
106
+ function onFocusin(event) {
107
+ if (isPointerDown) return;
108
+ (defer ? (fn) => focusFrame.request(fn) : (fn) => fn())(() => {
109
+ const composedPath = event?.composedPath?.() ?? [event?.target];
110
+ const target = isInShadowRoot ? composedPath[0] : getEventTarget(event);
111
+ if (!node || !isEventOutside(event, target)) return;
112
+ if (onFocusOutside || onInteractOutside) {
113
+ const handler = callAll(onFocusOutside, onInteractOutside);
114
+ node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
115
+ }
116
+ fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
117
+ bubbles: false,
118
+ cancelable: true,
119
+ detail: {
120
+ originalEvent: event,
121
+ contextmenu: false,
122
+ focusable: isFocusable(target),
123
+ target
124
+ }
125
+ });
126
+ });
127
+ }
128
+ if (!isTouchDevice()) {
129
+ cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
130
+ cleanups.add(parentWin.addEventListener("focusin", onFocusin, true));
131
+ cleanups.add(frames.addEventListener("focusin", onFocusin, true));
132
+ }
133
+ return () => {
134
+ clearTimeout(timer);
135
+ pointerFrame.cancel();
136
+ focusFrame.cancel();
137
+ pointerdownCleanups.forEach((fn) => fn());
138
+ cleanups.forEach((fn) => fn());
139
+ };
163
140
  }
164
141
  function trackInteractOutside(nodeOrFn, options) {
165
- const { defer } = options;
166
- const func = defer ? raf : (v) => v();
167
- const cleanups = [];
168
- cleanups.push(
169
- func(() => {
170
- const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
171
- cleanups.push(trackInteractOutsideImpl(node, options));
172
- })
173
- );
174
- return () => {
175
- cleanups.forEach((fn) => fn?.());
176
- };
142
+ const { defer } = options;
143
+ const func = defer ? raf : (v) => v();
144
+ const cleanups = [];
145
+ cleanups.push(func(() => {
146
+ const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
147
+ cleanups.push(trackInteractOutsideImpl(node, options));
148
+ }));
149
+ return () => {
150
+ cleanups.forEach((fn) => fn?.());
151
+ };
177
152
  }
178
153
  function fireCustomEvent(el, type, init) {
179
- const win = el.ownerDocument.defaultView || window;
180
- const event = new win.CustomEvent(type, init);
181
- return el.dispatchEvent(event);
154
+ const event = new (el.ownerDocument.defaultView || window).CustomEvent(type, init);
155
+ return el.dispatchEvent(event);
182
156
  }
183
- export {
184
- trackInteractOutside
185
- };
157
+ export { trackInteractOutside };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/interact-outside",
3
- "version": "2.0.0-next.2",
3
+ "version": "2.0.0-next.4",
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": "2.0.0-next.2",
23
- "@zag-js/utils": "2.0.0-next.2"
22
+ "@zag-js/dom-query": "2.0.0-next.4",
23
+ "@zag-js/utils": "2.0.0-next.4"
24
24
  },
25
25
  "devDependencies": {
26
26
  "clean-package": "2.2.0"
@@ -49,7 +49,7 @@
49
49
  "./package.json": "./package.json"
50
50
  },
51
51
  "scripts": {
52
- "build": "tsup",
52
+ "build": "tsdown",
53
53
  "test": "vitest",
54
54
  "lint": "oxlint src",
55
55
  "test-ci": "pnpm test --ci --runInBand -u",