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