@zag-js/interact-outside 0.50.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -200
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -175
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,201 +1,2 @@
|
|
|
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 src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
trackInteractOutside: () => trackInteractOutside
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
var import_dom_event = require("@zag-js/dom-event");
|
|
27
|
-
var import_dom_query = require("@zag-js/dom-query");
|
|
28
|
-
var import_utils = require("@zag-js/utils");
|
|
29
|
-
|
|
30
|
-
// src/get-window-frames.ts
|
|
31
|
-
function getWindowFrames(win) {
|
|
32
|
-
const frames = {
|
|
33
|
-
each(cb) {
|
|
34
|
-
for (let i = 0; i < win.frames?.length; i += 1) {
|
|
35
|
-
const frame = win.frames[i];
|
|
36
|
-
if (frame)
|
|
37
|
-
cb(frame);
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
addEventListener(event, listener, options) {
|
|
41
|
-
frames.each((frame) => {
|
|
42
|
-
try {
|
|
43
|
-
frame.document.addEventListener(event, listener, options);
|
|
44
|
-
} catch {
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return () => {
|
|
48
|
-
try {
|
|
49
|
-
frames.removeEventListener(event, listener, options);
|
|
50
|
-
} catch {
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
},
|
|
54
|
-
removeEventListener(event, listener, options) {
|
|
55
|
-
frames.each((frame) => {
|
|
56
|
-
try {
|
|
57
|
-
frame.document.removeEventListener(event, listener, options);
|
|
58
|
-
} catch {
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
return frames;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// src/index.ts
|
|
67
|
-
var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
|
|
68
|
-
var FOCUS_OUTSIDE_EVENT = "focus.outside";
|
|
69
|
-
function isComposedPathFocusable(composedPath) {
|
|
70
|
-
for (const node of composedPath) {
|
|
71
|
-
if ((0, import_dom_query.isHTMLElement)(node) && (0, import_dom_query.isFocusable)(node))
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
var isPointerEvent = (event) => "clientY" in event;
|
|
77
|
-
function isEventPointWithin(node, event) {
|
|
78
|
-
if (!isPointerEvent(event) || !node)
|
|
79
|
-
return false;
|
|
80
|
-
const rect = node.getBoundingClientRect();
|
|
81
|
-
if (rect.width === 0 || rect.height === 0)
|
|
82
|
-
return false;
|
|
83
|
-
return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
|
|
84
|
-
}
|
|
85
|
-
function isEventWithinScrollbar(event) {
|
|
86
|
-
const target = (0, import_dom_query.getEventTarget)(event);
|
|
87
|
-
if (!target || !isPointerEvent(event))
|
|
88
|
-
return false;
|
|
89
|
-
const isScrollableY = target.scrollHeight > target.clientHeight;
|
|
90
|
-
const onScrollbarY = isScrollableY && event.clientX > target.clientWidth;
|
|
91
|
-
const isScrollableX = target.scrollWidth > target.clientWidth;
|
|
92
|
-
const onScrollbarX = isScrollableX && event.clientY > target.clientHeight;
|
|
93
|
-
return onScrollbarY || onScrollbarX;
|
|
94
|
-
}
|
|
95
|
-
function trackInteractOutsideImpl(node, options) {
|
|
96
|
-
const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options;
|
|
97
|
-
if (!node)
|
|
98
|
-
return;
|
|
99
|
-
const doc = (0, import_dom_query.getDocument)(node);
|
|
100
|
-
const win = (0, import_dom_query.getWindow)(node);
|
|
101
|
-
const frames = getWindowFrames(win);
|
|
102
|
-
function isEventOutside(event) {
|
|
103
|
-
const target = (0, import_dom_query.getEventTarget)(event);
|
|
104
|
-
if (!(0, import_dom_query.isHTMLElement)(target))
|
|
105
|
-
return false;
|
|
106
|
-
if ((0, import_dom_query.contains)(node, target))
|
|
107
|
-
return false;
|
|
108
|
-
if (isEventPointWithin(node, event))
|
|
109
|
-
return false;
|
|
110
|
-
if (isEventWithinScrollbar(event))
|
|
111
|
-
return false;
|
|
112
|
-
return !exclude?.(target);
|
|
113
|
-
}
|
|
114
|
-
let clickHandler;
|
|
115
|
-
function onPointerDown(event) {
|
|
116
|
-
function handler() {
|
|
117
|
-
const func = defer ? import_dom_query.raf : (v) => v();
|
|
118
|
-
const composedPath = event.composedPath?.() ?? [event.target];
|
|
119
|
-
func(() => {
|
|
120
|
-
if (!node || !isEventOutside(event))
|
|
121
|
-
return;
|
|
122
|
-
if (onPointerDownOutside || onInteractOutside) {
|
|
123
|
-
const handler2 = (0, import_utils.callAll)(onPointerDownOutside, onInteractOutside);
|
|
124
|
-
node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
|
|
125
|
-
}
|
|
126
|
-
(0, import_dom_event.fireCustomEvent)(node, POINTER_OUTSIDE_EVENT, {
|
|
127
|
-
bubbles: false,
|
|
128
|
-
cancelable: true,
|
|
129
|
-
detail: {
|
|
130
|
-
originalEvent: event,
|
|
131
|
-
contextmenu: (0, import_dom_event.isContextMenuEvent)(event),
|
|
132
|
-
focusable: isComposedPathFocusable(composedPath)
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
if (event.pointerType === "touch") {
|
|
138
|
-
frames.removeEventListener("click", handler);
|
|
139
|
-
doc.removeEventListener("click", handler);
|
|
140
|
-
clickHandler = handler;
|
|
141
|
-
doc.addEventListener("click", handler, { once: true });
|
|
142
|
-
frames.addEventListener("click", handler, { once: true });
|
|
143
|
-
} else {
|
|
144
|
-
handler();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
const cleanups = /* @__PURE__ */ new Set();
|
|
148
|
-
const timer = setTimeout(() => {
|
|
149
|
-
cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
|
|
150
|
-
cleanups.add((0, import_dom_event.addDomEvent)(doc, "pointerdown", onPointerDown, true));
|
|
151
|
-
}, 0);
|
|
152
|
-
function onFocusin(event) {
|
|
153
|
-
const func = defer ? import_dom_query.raf : (v) => v();
|
|
154
|
-
func(() => {
|
|
155
|
-
if (!node || !isEventOutside(event))
|
|
156
|
-
return;
|
|
157
|
-
if (onFocusOutside || onInteractOutside) {
|
|
158
|
-
const handler = (0, import_utils.callAll)(onFocusOutside, onInteractOutside);
|
|
159
|
-
node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
|
|
160
|
-
}
|
|
161
|
-
(0, import_dom_event.fireCustomEvent)(node, FOCUS_OUTSIDE_EVENT, {
|
|
162
|
-
bubbles: false,
|
|
163
|
-
cancelable: true,
|
|
164
|
-
detail: {
|
|
165
|
-
originalEvent: event,
|
|
166
|
-
contextmenu: false,
|
|
167
|
-
focusable: (0, import_dom_query.isFocusable)((0, import_dom_query.getEventTarget)(event))
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
cleanups.add((0, import_dom_event.addDomEvent)(doc, "focusin", onFocusin, true));
|
|
173
|
-
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
174
|
-
return () => {
|
|
175
|
-
clearTimeout(timer);
|
|
176
|
-
if (clickHandler) {
|
|
177
|
-
frames.removeEventListener("click", clickHandler);
|
|
178
|
-
doc.removeEventListener("click", clickHandler);
|
|
179
|
-
}
|
|
180
|
-
cleanups.forEach((fn) => fn());
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
function trackInteractOutside(nodeOrFn, options) {
|
|
184
|
-
const { defer } = options;
|
|
185
|
-
const func = defer ? import_dom_query.raf : (v) => v();
|
|
186
|
-
const cleanups = [];
|
|
187
|
-
cleanups.push(
|
|
188
|
-
func(() => {
|
|
189
|
-
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
|
|
190
|
-
cleanups.push(trackInteractOutsideImpl(node, options));
|
|
191
|
-
})
|
|
192
|
-
);
|
|
193
|
-
return () => {
|
|
194
|
-
cleanups.forEach((fn) => fn?.());
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
-
0 && (module.exports = {
|
|
199
|
-
trackInteractOutside
|
|
200
|
-
});
|
|
1
|
+
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var src_exports={};__export(src_exports,{trackInteractOutside:()=>trackInteractOutside});module.exports=__toCommonJS(src_exports);var import_dom_event=require("@zag-js/dom-event");var import_dom_query=require("@zag-js/dom-query");var import_utils=require("@zag-js/utils");function getWindowFrames(win){const frames={each(cb){for(let i=0;i<win.frames?.length;i+=1){const frame=win.frames[i];if(frame)cb(frame)}},addEventListener(event,listener,options){frames.each(frame=>{try{frame.document.addEventListener(event,listener,options)}catch{}});return()=>{try{frames.removeEventListener(event,listener,options)}catch{}}},removeEventListener(event,listener,options){frames.each(frame=>{try{frame.document.removeEventListener(event,listener,options)}catch{}})}};return frames}var POINTER_OUTSIDE_EVENT="pointerdown.outside";var FOCUS_OUTSIDE_EVENT="focus.outside";function isComposedPathFocusable(composedPath){for(const node of composedPath){if((0,import_dom_query.isHTMLElement)(node)&&(0,import_dom_query.isFocusable)(node))return true}return false}var isPointerEvent=event=>"clientY"in event;function isEventPointWithin(node,event){if(!isPointerEvent(event)||!node)return false;const rect=node.getBoundingClientRect();if(rect.width===0||rect.height===0)return false;return rect.top<=event.clientY&&event.clientY<=rect.top+rect.height&&rect.left<=event.clientX&&event.clientX<=rect.left+rect.width}function isEventWithinScrollbar(event){const target=(0,import_dom_query.getEventTarget)(event);if(!target||!isPointerEvent(event))return false;const isScrollableY=target.scrollHeight>target.clientHeight;const onScrollbarY=isScrollableY&&event.clientX>target.clientWidth;const isScrollableX=target.scrollWidth>target.clientWidth;const onScrollbarX=isScrollableX&&event.clientY>target.clientHeight;return onScrollbarY||onScrollbarX}function trackInteractOutsideImpl(node,options){const{exclude,onFocusOutside,onPointerDownOutside,onInteractOutside,defer}=options;if(!node)return;const doc=(0,import_dom_query.getDocument)(node);const win=(0,import_dom_query.getWindow)(node);const frames=getWindowFrames(win);function isEventOutside(event){const target=(0,import_dom_query.getEventTarget)(event);if(!(0,import_dom_query.isHTMLElement)(target))return false;if((0,import_dom_query.contains)(node,target))return false;if(isEventPointWithin(node,event))return false;if(isEventWithinScrollbar(event))return false;return!exclude?.(target)}let clickHandler;function onPointerDown(event){function handler(){const func=defer?import_dom_query.raf:v=>v();const composedPath=event.composedPath?.()??[event.target];func(()=>{if(!node||!isEventOutside(event))return;if(onPointerDownOutside||onInteractOutside){const handler2=(0,import_utils.callAll)(onPointerDownOutside,onInteractOutside);node.addEventListener(POINTER_OUTSIDE_EVENT,handler2,{once:true})}(0,import_dom_event.fireCustomEvent)(node,POINTER_OUTSIDE_EVENT,{bubbles:false,cancelable:true,detail:{originalEvent:event,contextmenu:(0,import_dom_event.isContextMenuEvent)(event),focusable:isComposedPathFocusable(composedPath)}})})}if(event.pointerType==="touch"){frames.removeEventListener("click",handler);doc.removeEventListener("click",handler);clickHandler=handler;doc.addEventListener("click",handler,{once:true});frames.addEventListener("click",handler,{once:true})}else{handler()}}const cleanups=new Set;const timer=setTimeout(()=>{cleanups.add(frames.addEventListener("pointerdown",onPointerDown,true));cleanups.add((0,import_dom_event.addDomEvent)(doc,"pointerdown",onPointerDown,true))},0);function onFocusin(event){const func=defer?import_dom_query.raf:v=>v();func(()=>{if(!node||!isEventOutside(event))return;if(onFocusOutside||onInteractOutside){const handler=(0,import_utils.callAll)(onFocusOutside,onInteractOutside);node.addEventListener(FOCUS_OUTSIDE_EVENT,handler,{once:true})}(0,import_dom_event.fireCustomEvent)(node,FOCUS_OUTSIDE_EVENT,{bubbles:false,cancelable:true,detail:{originalEvent:event,contextmenu:false,focusable:(0,import_dom_query.isFocusable)((0,import_dom_query.getEventTarget)(event))}})})}cleanups.add((0,import_dom_event.addDomEvent)(doc,"focusin",onFocusin,true));cleanups.add(frames.addEventListener("focusin",onFocusin,true));return()=>{clearTimeout(timer);if(clickHandler){frames.removeEventListener("click",clickHandler);doc.removeEventListener("click",clickHandler)}cleanups.forEach(fn=>fn())}}function trackInteractOutside(nodeOrFn,options){const{defer}=options;const func=defer?import_dom_query.raf:v=>v();const cleanups=[];cleanups.push(func(()=>{const node=typeof nodeOrFn==="function"?nodeOrFn():nodeOrFn;cleanups.push(trackInteractOutsideImpl(node,options))}));return()=>{cleanups.forEach(fn=>fn?.())}}0&&(module.exports={trackInteractOutside});
|
|
201
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/get-window-frames.ts"],"sourcesContent":["import { addDomEvent, fireCustomEvent, isContextMenuEvent } from \"@zag-js/dom-event\"\nimport { contains, getDocument, getEventTarget, getWindow, isFocusable, isHTMLElement, raf } from \"@zag-js/dom-query\"\nimport { callAll } from \"@zag-js/utils\"\nimport { getWindowFrames } from \"./get-window-frames\"\n\nexport interface InteractOutsideHandlers {\n /**\n * Function called when the pointer is pressed down outside the component\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Function called when the focus is moved outside the component\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Function called when an interaction happens outside the component\n */\n onInteractOutside?: (event: InteractOutsideEvent) => void\n}\n\nexport interface InteractOutsideOptions extends InteractOutsideHandlers {\n exclude?: (target: HTMLElement) => boolean\n defer?: boolean\n}\n\nexport interface EventDetails<T> {\n originalEvent: T\n contextmenu: boolean\n focusable: boolean\n}\n\nconst POINTER_OUTSIDE_EVENT = \"pointerdown.outside\"\nconst FOCUS_OUTSIDE_EVENT = \"focus.outside\"\n\nexport type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>\n\nexport type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>\n\nexport type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent\n\nexport type MaybeElement = HTMLElement | null | undefined\nexport type NodeOrFn = MaybeElement | (() => MaybeElement)\n\nfunction isComposedPathFocusable(composedPath: EventTarget[]) {\n for (const node of composedPath) {\n if (isHTMLElement(node) && isFocusable(node)) return true\n }\n return false\n}\n\nconst isPointerEvent = (event: Event): event is PointerEvent => \"clientY\" in event\n\nfunction isEventPointWithin(node: MaybeElement, event: Event) {\n if (!isPointerEvent(event) || !node) return false\n\n const rect = node.getBoundingClientRect()\n if (rect.width === 0 || rect.height === 0) return false\n\n return (\n rect.top <= event.clientY &&\n event.clientY <= rect.top + rect.height &&\n rect.left <= event.clientX &&\n event.clientX <= rect.left + rect.width\n )\n}\n\nfunction isEventWithinScrollbar(event: Event): boolean {\n const target = getEventTarget<HTMLElement>(event)\n if (!target || !isPointerEvent(event)) return false\n\n const isScrollableY = target.scrollHeight > target.clientHeight\n const onScrollbarY = isScrollableY && event.clientX > target.clientWidth\n\n const isScrollableX = target.scrollWidth > target.clientWidth\n const onScrollbarX = isScrollableX && event.clientY > target.clientHeight\n\n return onScrollbarY || onScrollbarX\n}\n\nfunction trackInteractOutsideImpl(node: MaybeElement, options: InteractOutsideOptions) {\n const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options\n\n if (!node) return\n\n const doc = getDocument(node)\n const win = getWindow(node)\n const frames = getWindowFrames(win)\n\n function isEventOutside(event: Event): boolean {\n const target = getEventTarget(event)\n if (!isHTMLElement(target)) return false\n if (contains(node, target)) return false\n if (isEventPointWithin(node, event)) return false\n if (isEventWithinScrollbar(event)) return false\n return !exclude?.(target)\n }\n\n let clickHandler: VoidFunction\n\n function onPointerDown(event: PointerEvent) {\n //\n function handler() {\n const func = defer ? raf : (v: any) => v()\n const composedPath = event.composedPath?.() ?? [event.target]\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onPointerDownOutside || onInteractOutside) {\n const handler = callAll(onPointerDownOutside, onInteractOutside) as EventListener\n node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: isContextMenuEvent(event),\n focusable: isComposedPathFocusable(composedPath),\n },\n })\n })\n }\n\n if (event.pointerType === \"touch\") {\n frames.removeEventListener(\"click\", handler)\n doc.removeEventListener(\"click\", handler)\n\n clickHandler = handler\n\n doc.addEventListener(\"click\", handler, { once: true })\n frames.addEventListener(\"click\", handler, { once: true })\n } else {\n handler()\n }\n }\n const cleanups = new Set<VoidFunction>()\n\n const timer = setTimeout(() => {\n cleanups.add(frames.addEventListener(\"pointerdown\", onPointerDown, true))\n cleanups.add(addDomEvent(doc, \"pointerdown\", onPointerDown, true))\n }, 0)\n\n function onFocusin(event: FocusEvent) {\n //\n const func = defer ? raf : (v: any) => v()\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onFocusOutside || onInteractOutside) {\n const handler = callAll(onFocusOutside, onInteractOutside) as EventListener\n node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: false,\n focusable: isFocusable(getEventTarget(event)),\n },\n })\n })\n }\n\n cleanups.add(addDomEvent(doc, \"focusin\", onFocusin, true))\n cleanups.add(frames.addEventListener(\"focusin\", onFocusin, true))\n\n return () => {\n clearTimeout(timer)\n if (clickHandler) {\n frames.removeEventListener(\"click\", clickHandler)\n doc.removeEventListener(\"click\", clickHandler)\n }\n cleanups.forEach((fn) => fn())\n }\n}\n\nexport function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions) {\n const { defer } = options\n const func = defer ? raf : (v: any) => v()\n const cleanups: (VoidFunction | undefined)[] = []\n cleanups.push(\n func(() => {\n const node = typeof nodeOrFn === \"function\" ? nodeOrFn() : nodeOrFn\n cleanups.push(trackInteractOutsideImpl(node, options))\n }),\n )\n return () => {\n cleanups.forEach((fn) => fn?.())\n }\n}\n","export function getWindowFrames(win: Window) {\n const frames = {\n each(cb: (win: Window) => void) {\n for (let i = 0; i < win.frames?.length; i += 1) {\n const frame = win.frames[i]\n if (frame) cb(frame)\n }\n },\n addEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.addEventListener(event, listener, options)\n } catch {}\n })\n return () => {\n try {\n frames.removeEventListener(event, listener, options)\n } catch {}\n }\n },\n removeEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.removeEventListener(event, listener, options)\n } catch {}\n })\n },\n }\n return frames\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAiE;AACjE,uBAAkG;AAClG,mBAAwB;;;ACFjB,SAAS,gBAAgB,KAAa;AAC3C,QAAM,SAAS;AAAA,IACb,KAAK,IAA2B;AAC9B,eAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,QAAQ,KAAK,GAAG;AAC9C,cAAM,QAAQ,IAAI,OAAO,CAAC;AAC1B,YAAI;AAAO,aAAG,KAAK;AAAA,MACrB;AAAA,IACF;AAAA,IACA,iBAAiB,OAAe,UAAe,SAAe;AAC5D,aAAO,KAAK,CAAC,UAAU;AACrB,YAAI;AACF,gBAAM,SAAS,iBAAiB,OAAO,UAAU,OAAO;AAAA,QAC1D,QAAQ;AAAA,QAAC;AAAA,MACX,CAAC;AACD,aAAO,MAAM;AACX,YAAI;AACF,iBAAO,oBAAoB,OAAO,UAAU,OAAO;AAAA,QACrD,QAAQ;AAAA,QAAC;AAAA,MACX;AAAA,IACF;AAAA,IACA,oBAAoB,OAAe,UAAe,SAAe;AAC/D,aAAO,KAAK,CAAC,UAAU;AACrB,YAAI;AACF,gBAAM,SAAS,oBAAoB,OAAO,UAAU,OAAO;AAAA,QAC7D,QAAQ;AAAA,QAAC;AAAA,MACX,CAAC;AAAA,IACH;AAAA,EACF;AACA,SAAO;AACT;;;ADEA,IAAM,wBAAwB;AAC9B,IAAM,sBAAsB;AAW5B,SAAS,wBAAwB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AC,QAAQ,CAAC,eAAe,KAAK;AAAG;AAErC,UAAI,kBAAkB,mBAAmB;AACvC,cAAM,cAAU,sBAAQ,gBAAgB,iBAAiB;AACzD,aAAK,iBAAiB,qBAAqB,SAAS,EAAE,MAAM,KAAK,CAAC;AAAA,MACpE;AAEA,4CAAgB,MAAM,qBAAqB;AAAA,QACzC,SAAS;AAAA,QACT,YAAY;AAAA,QACZ,QAAQ;AAAA,UACN,eAAe;AAAA,UACf,aAAa;AAAA,UACb,eAAW,kCAAY,iCAAe,KAAK,CAAC;AAAA,QAC9C;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAEA,WAAS,QAAI,8BAAY,KAAK,WAAW,WAAW,IAAI,CAAC;AACzD,WAAS,IAAI,OAAO,iBAAiB,WAAW,WAAW,IAAI,CAAC;AAEhE,SAAO,MAAM;AACX,iBAAa,KAAK;AAClB,QAAI,cAAc;AAChB,aAAO,oBAAoB,SAAS,YAAY;AAChD,UAAI,oBAAoB,SAAS,YAAY;AAAA,IAC/C;AACA,aAAS,QAAQ,CAAC,OAAO,GAAG,CAAC;AAAA,EAC/B;AACF;AAEO,SAAS,qBAAqB,UAAoB,SAAiC;AACxF,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,OAAO,QAAQ,uBAAM,CAAC,MAAW,EAAE;AACzC,QAAM,WAAyC,CAAC;AAChD,WAAS;AAAA,IACP,KAAK,MAAM;AACT,YAAM,OAAO,OAAO,aAAa,aAAa,SAAS,IAAI;AAC3D,eAAS,KAAK,yBAAyB,MAAM,OAAO,CAAC;AAAA,IACvD,CAAC;AAAA,EACH;AACA,SAAO,MAAM;AACX,aAAS,QAAQ,CAAC,OAAO,KAAK,CAAC;AAAA,EACjC;AACF;","names":["handler"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/get-window-frames.ts"],"sourcesContent":["import { addDomEvent, fireCustomEvent, isContextMenuEvent } from \"@zag-js/dom-event\"\nimport { contains, getDocument, getEventTarget, getWindow, isFocusable, isHTMLElement, raf } from \"@zag-js/dom-query\"\nimport { callAll } from \"@zag-js/utils\"\nimport { getWindowFrames } from \"./get-window-frames\"\n\nexport interface InteractOutsideHandlers {\n /**\n * Function called when the pointer is pressed down outside the component\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Function called when the focus is moved outside the component\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Function called when an interaction happens outside the component\n */\n onInteractOutside?: (event: InteractOutsideEvent) => void\n}\n\nexport interface InteractOutsideOptions extends InteractOutsideHandlers {\n exclude?: (target: HTMLElement) => boolean\n defer?: boolean\n}\n\nexport interface EventDetails<T> {\n originalEvent: T\n contextmenu: boolean\n focusable: boolean\n}\n\nconst POINTER_OUTSIDE_EVENT = \"pointerdown.outside\"\nconst FOCUS_OUTSIDE_EVENT = \"focus.outside\"\n\nexport type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>\n\nexport type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>\n\nexport type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent\n\nexport type MaybeElement = HTMLElement | null | undefined\nexport type NodeOrFn = MaybeElement | (() => MaybeElement)\n\nfunction isComposedPathFocusable(composedPath: EventTarget[]) {\n for (const node of composedPath) {\n if (isHTMLElement(node) && isFocusable(node)) return true\n }\n return false\n}\n\nconst isPointerEvent = (event: Event): event is PointerEvent => \"clientY\" in event\n\nfunction isEventPointWithin(node: MaybeElement, event: Event) {\n if (!isPointerEvent(event) || !node) return false\n\n const rect = node.getBoundingClientRect()\n if (rect.width === 0 || rect.height === 0) return false\n\n return (\n rect.top <= event.clientY &&\n event.clientY <= rect.top + rect.height &&\n rect.left <= event.clientX &&\n event.clientX <= rect.left + rect.width\n )\n}\n\nfunction isEventWithinScrollbar(event: Event): boolean {\n const target = getEventTarget<HTMLElement>(event)\n if (!target || !isPointerEvent(event)) return false\n\n const isScrollableY = target.scrollHeight > target.clientHeight\n const onScrollbarY = isScrollableY && event.clientX > target.clientWidth\n\n const isScrollableX = target.scrollWidth > target.clientWidth\n const onScrollbarX = isScrollableX && event.clientY > target.clientHeight\n\n return onScrollbarY || onScrollbarX\n}\n\nfunction trackInteractOutsideImpl(node: MaybeElement, options: InteractOutsideOptions) {\n const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options\n\n if (!node) return\n\n const doc = getDocument(node)\n const win = getWindow(node)\n const frames = getWindowFrames(win)\n\n function isEventOutside(event: Event): boolean {\n const target = getEventTarget(event)\n if (!isHTMLElement(target)) return false\n if (contains(node, target)) return false\n if (isEventPointWithin(node, event)) return false\n if (isEventWithinScrollbar(event)) return false\n return !exclude?.(target)\n }\n\n let clickHandler: VoidFunction\n\n function onPointerDown(event: PointerEvent) {\n //\n function handler() {\n const func = defer ? raf : (v: any) => v()\n const composedPath = event.composedPath?.() ?? [event.target]\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onPointerDownOutside || onInteractOutside) {\n const handler = callAll(onPointerDownOutside, onInteractOutside) as EventListener\n node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: isContextMenuEvent(event),\n focusable: isComposedPathFocusable(composedPath),\n },\n })\n })\n }\n\n if (event.pointerType === \"touch\") {\n frames.removeEventListener(\"click\", handler)\n doc.removeEventListener(\"click\", handler)\n\n clickHandler = handler\n\n doc.addEventListener(\"click\", handler, { once: true })\n frames.addEventListener(\"click\", handler, { once: true })\n } else {\n handler()\n }\n }\n const cleanups = new Set<VoidFunction>()\n\n const timer = setTimeout(() => {\n cleanups.add(frames.addEventListener(\"pointerdown\", onPointerDown, true))\n cleanups.add(addDomEvent(doc, \"pointerdown\", onPointerDown, true))\n }, 0)\n\n function onFocusin(event: FocusEvent) {\n //\n const func = defer ? raf : (v: any) => v()\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onFocusOutside || onInteractOutside) {\n const handler = callAll(onFocusOutside, onInteractOutside) as EventListener\n node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: false,\n focusable: isFocusable(getEventTarget(event)),\n },\n })\n })\n }\n\n cleanups.add(addDomEvent(doc, \"focusin\", onFocusin, true))\n cleanups.add(frames.addEventListener(\"focusin\", onFocusin, true))\n\n return () => {\n clearTimeout(timer)\n if (clickHandler) {\n frames.removeEventListener(\"click\", clickHandler)\n doc.removeEventListener(\"click\", clickHandler)\n }\n cleanups.forEach((fn) => fn())\n }\n}\n\nexport function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions) {\n const { defer } = options\n const func = defer ? raf : (v: any) => v()\n const cleanups: (VoidFunction | undefined)[] = []\n cleanups.push(\n func(() => {\n const node = typeof nodeOrFn === \"function\" ? nodeOrFn() : nodeOrFn\n cleanups.push(trackInteractOutsideImpl(node, options))\n }),\n )\n return () => {\n cleanups.forEach((fn) => fn?.())\n }\n}\n","export function getWindowFrames(win: Window) {\n const frames = {\n each(cb: (win: Window) => void) {\n for (let i = 0; i < win.frames?.length; i += 1) {\n const frame = win.frames[i]\n if (frame) cb(frame)\n }\n },\n addEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.addEventListener(event, listener, options)\n } catch {}\n })\n return () => {\n try {\n frames.removeEventListener(event, listener, options)\n } catch {}\n }\n },\n removeEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.removeEventListener(event, listener, options)\n } catch {}\n })\n },\n }\n return frames\n}\n"],"mappings":"qqBAAA,uJAAiE,6BACjE,qBAAkG,6BAClG,iBAAwB,yBCFjB,SAAS,gBAAgB,IAAa,CAC3C,MAAM,OAAS,CACb,KAAK,GAA2B,CAC9B,QAAS,EAAI,EAAG,EA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KAAO,OAAO,WAAa,WAAa,SAAS,EAAI,SAC3D,SAAS,KAAK,yBAAyB,KAAM,OAAO,CAAC,CACvD,CAAC,CACH,EACA,MAAO,IAAM,CACX,SAAS,QAAS,IAAO,KAAK,CAAC,CACjC,CACF","names":["handler"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,176 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { addDomEvent, fireCustomEvent, isContextMenuEvent } from "@zag-js/dom-event";
|
|
3
|
-
import { contains, getDocument, getEventTarget, getWindow, isFocusable, isHTMLElement, raf } from "@zag-js/dom-query";
|
|
4
|
-
import { callAll } from "@zag-js/utils";
|
|
5
|
-
|
|
6
|
-
// src/get-window-frames.ts
|
|
7
|
-
function getWindowFrames(win) {
|
|
8
|
-
const frames = {
|
|
9
|
-
each(cb) {
|
|
10
|
-
for (let i = 0; i < win.frames?.length; i += 1) {
|
|
11
|
-
const frame = win.frames[i];
|
|
12
|
-
if (frame)
|
|
13
|
-
cb(frame);
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
addEventListener(event, listener, options) {
|
|
17
|
-
frames.each((frame) => {
|
|
18
|
-
try {
|
|
19
|
-
frame.document.addEventListener(event, listener, options);
|
|
20
|
-
} catch {
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
return () => {
|
|
24
|
-
try {
|
|
25
|
-
frames.removeEventListener(event, listener, options);
|
|
26
|
-
} catch {
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
},
|
|
30
|
-
removeEventListener(event, listener, options) {
|
|
31
|
-
frames.each((frame) => {
|
|
32
|
-
try {
|
|
33
|
-
frame.document.removeEventListener(event, listener, options);
|
|
34
|
-
} catch {
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
return frames;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// src/index.ts
|
|
43
|
-
var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
|
|
44
|
-
var FOCUS_OUTSIDE_EVENT = "focus.outside";
|
|
45
|
-
function isComposedPathFocusable(composedPath) {
|
|
46
|
-
for (const node of composedPath) {
|
|
47
|
-
if (isHTMLElement(node) && isFocusable(node))
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
var isPointerEvent = (event) => "clientY" in event;
|
|
53
|
-
function isEventPointWithin(node, event) {
|
|
54
|
-
if (!isPointerEvent(event) || !node)
|
|
55
|
-
return false;
|
|
56
|
-
const rect = node.getBoundingClientRect();
|
|
57
|
-
if (rect.width === 0 || rect.height === 0)
|
|
58
|
-
return false;
|
|
59
|
-
return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
|
|
60
|
-
}
|
|
61
|
-
function isEventWithinScrollbar(event) {
|
|
62
|
-
const target = getEventTarget(event);
|
|
63
|
-
if (!target || !isPointerEvent(event))
|
|
64
|
-
return false;
|
|
65
|
-
const isScrollableY = target.scrollHeight > target.clientHeight;
|
|
66
|
-
const onScrollbarY = isScrollableY && event.clientX > target.clientWidth;
|
|
67
|
-
const isScrollableX = target.scrollWidth > target.clientWidth;
|
|
68
|
-
const onScrollbarX = isScrollableX && event.clientY > target.clientHeight;
|
|
69
|
-
return onScrollbarY || onScrollbarX;
|
|
70
|
-
}
|
|
71
|
-
function trackInteractOutsideImpl(node, options) {
|
|
72
|
-
const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options;
|
|
73
|
-
if (!node)
|
|
74
|
-
return;
|
|
75
|
-
const doc = getDocument(node);
|
|
76
|
-
const win = getWindow(node);
|
|
77
|
-
const frames = getWindowFrames(win);
|
|
78
|
-
function isEventOutside(event) {
|
|
79
|
-
const target = getEventTarget(event);
|
|
80
|
-
if (!isHTMLElement(target))
|
|
81
|
-
return false;
|
|
82
|
-
if (contains(node, target))
|
|
83
|
-
return false;
|
|
84
|
-
if (isEventPointWithin(node, event))
|
|
85
|
-
return false;
|
|
86
|
-
if (isEventWithinScrollbar(event))
|
|
87
|
-
return false;
|
|
88
|
-
return !exclude?.(target);
|
|
89
|
-
}
|
|
90
|
-
let clickHandler;
|
|
91
|
-
function onPointerDown(event) {
|
|
92
|
-
function handler() {
|
|
93
|
-
const func = defer ? raf : (v) => v();
|
|
94
|
-
const composedPath = event.composedPath?.() ?? [event.target];
|
|
95
|
-
func(() => {
|
|
96
|
-
if (!node || !isEventOutside(event))
|
|
97
|
-
return;
|
|
98
|
-
if (onPointerDownOutside || onInteractOutside) {
|
|
99
|
-
const handler2 = callAll(onPointerDownOutside, onInteractOutside);
|
|
100
|
-
node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
|
|
101
|
-
}
|
|
102
|
-
fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
|
|
103
|
-
bubbles: false,
|
|
104
|
-
cancelable: true,
|
|
105
|
-
detail: {
|
|
106
|
-
originalEvent: event,
|
|
107
|
-
contextmenu: isContextMenuEvent(event),
|
|
108
|
-
focusable: isComposedPathFocusable(composedPath)
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
if (event.pointerType === "touch") {
|
|
114
|
-
frames.removeEventListener("click", handler);
|
|
115
|
-
doc.removeEventListener("click", handler);
|
|
116
|
-
clickHandler = handler;
|
|
117
|
-
doc.addEventListener("click", handler, { once: true });
|
|
118
|
-
frames.addEventListener("click", handler, { once: true });
|
|
119
|
-
} else {
|
|
120
|
-
handler();
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const cleanups = /* @__PURE__ */ new Set();
|
|
124
|
-
const timer = setTimeout(() => {
|
|
125
|
-
cleanups.add(frames.addEventListener("pointerdown", onPointerDown, true));
|
|
126
|
-
cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
|
|
127
|
-
}, 0);
|
|
128
|
-
function onFocusin(event) {
|
|
129
|
-
const func = defer ? raf : (v) => v();
|
|
130
|
-
func(() => {
|
|
131
|
-
if (!node || !isEventOutside(event))
|
|
132
|
-
return;
|
|
133
|
-
if (onFocusOutside || onInteractOutside) {
|
|
134
|
-
const handler = callAll(onFocusOutside, onInteractOutside);
|
|
135
|
-
node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
|
|
136
|
-
}
|
|
137
|
-
fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
|
|
138
|
-
bubbles: false,
|
|
139
|
-
cancelable: true,
|
|
140
|
-
detail: {
|
|
141
|
-
originalEvent: event,
|
|
142
|
-
contextmenu: false,
|
|
143
|
-
focusable: isFocusable(getEventTarget(event))
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
|
|
149
|
-
cleanups.add(frames.addEventListener("focusin", onFocusin, true));
|
|
150
|
-
return () => {
|
|
151
|
-
clearTimeout(timer);
|
|
152
|
-
if (clickHandler) {
|
|
153
|
-
frames.removeEventListener("click", clickHandler);
|
|
154
|
-
doc.removeEventListener("click", clickHandler);
|
|
155
|
-
}
|
|
156
|
-
cleanups.forEach((fn) => fn());
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
function trackInteractOutside(nodeOrFn, options) {
|
|
160
|
-
const { defer } = options;
|
|
161
|
-
const func = defer ? raf : (v) => v();
|
|
162
|
-
const cleanups = [];
|
|
163
|
-
cleanups.push(
|
|
164
|
-
func(() => {
|
|
165
|
-
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
|
|
166
|
-
cleanups.push(trackInteractOutsideImpl(node, options));
|
|
167
|
-
})
|
|
168
|
-
);
|
|
169
|
-
return () => {
|
|
170
|
-
cleanups.forEach((fn) => fn?.());
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
export {
|
|
174
|
-
trackInteractOutside
|
|
175
|
-
};
|
|
1
|
+
import{addDomEvent,fireCustomEvent,isContextMenuEvent}from"@zag-js/dom-event";import{contains,getDocument,getEventTarget,getWindow,isFocusable,isHTMLElement,raf}from"@zag-js/dom-query";import{callAll}from"@zag-js/utils";function getWindowFrames(win){const frames={each(cb){for(let i=0;i<win.frames?.length;i+=1){const frame=win.frames[i];if(frame)cb(frame)}},addEventListener(event,listener,options){frames.each(frame=>{try{frame.document.addEventListener(event,listener,options)}catch{}});return()=>{try{frames.removeEventListener(event,listener,options)}catch{}}},removeEventListener(event,listener,options){frames.each(frame=>{try{frame.document.removeEventListener(event,listener,options)}catch{}})}};return frames}var POINTER_OUTSIDE_EVENT="pointerdown.outside";var FOCUS_OUTSIDE_EVENT="focus.outside";function isComposedPathFocusable(composedPath){for(const node of composedPath){if(isHTMLElement(node)&&isFocusable(node))return true}return false}var isPointerEvent=event=>"clientY"in event;function isEventPointWithin(node,event){if(!isPointerEvent(event)||!node)return false;const rect=node.getBoundingClientRect();if(rect.width===0||rect.height===0)return false;return rect.top<=event.clientY&&event.clientY<=rect.top+rect.height&&rect.left<=event.clientX&&event.clientX<=rect.left+rect.width}function isEventWithinScrollbar(event){const target=getEventTarget(event);if(!target||!isPointerEvent(event))return false;const isScrollableY=target.scrollHeight>target.clientHeight;const onScrollbarY=isScrollableY&&event.clientX>target.clientWidth;const isScrollableX=target.scrollWidth>target.clientWidth;const onScrollbarX=isScrollableX&&event.clientY>target.clientHeight;return onScrollbarY||onScrollbarX}function trackInteractOutsideImpl(node,options){const{exclude,onFocusOutside,onPointerDownOutside,onInteractOutside,defer}=options;if(!node)return;const doc=getDocument(node);const win=getWindow(node);const frames=getWindowFrames(win);function isEventOutside(event){const target=getEventTarget(event);if(!isHTMLElement(target))return false;if(contains(node,target))return false;if(isEventPointWithin(node,event))return false;if(isEventWithinScrollbar(event))return false;return!exclude?.(target)}let clickHandler;function onPointerDown(event){function handler(){const func=defer?raf:v=>v();const composedPath=event.composedPath?.()??[event.target];func(()=>{if(!node||!isEventOutside(event))return;if(onPointerDownOutside||onInteractOutside){const handler2=callAll(onPointerDownOutside,onInteractOutside);node.addEventListener(POINTER_OUTSIDE_EVENT,handler2,{once:true})}fireCustomEvent(node,POINTER_OUTSIDE_EVENT,{bubbles:false,cancelable:true,detail:{originalEvent:event,contextmenu:isContextMenuEvent(event),focusable:isComposedPathFocusable(composedPath)}})})}if(event.pointerType==="touch"){frames.removeEventListener("click",handler);doc.removeEventListener("click",handler);clickHandler=handler;doc.addEventListener("click",handler,{once:true});frames.addEventListener("click",handler,{once:true})}else{handler()}}const cleanups=new Set;const timer=setTimeout(()=>{cleanups.add(frames.addEventListener("pointerdown",onPointerDown,true));cleanups.add(addDomEvent(doc,"pointerdown",onPointerDown,true))},0);function onFocusin(event){const func=defer?raf:v=>v();func(()=>{if(!node||!isEventOutside(event))return;if(onFocusOutside||onInteractOutside){const handler=callAll(onFocusOutside,onInteractOutside);node.addEventListener(FOCUS_OUTSIDE_EVENT,handler,{once:true})}fireCustomEvent(node,FOCUS_OUTSIDE_EVENT,{bubbles:false,cancelable:true,detail:{originalEvent:event,contextmenu:false,focusable:isFocusable(getEventTarget(event))}})})}cleanups.add(addDomEvent(doc,"focusin",onFocusin,true));cleanups.add(frames.addEventListener("focusin",onFocusin,true));return()=>{clearTimeout(timer);if(clickHandler){frames.removeEventListener("click",clickHandler);doc.removeEventListener("click",clickHandler)}cleanups.forEach(fn=>fn())}}function trackInteractOutside(nodeOrFn,options){const{defer}=options;const func=defer?raf:v=>v();const cleanups=[];cleanups.push(func(()=>{const node=typeof nodeOrFn==="function"?nodeOrFn():nodeOrFn;cleanups.push(trackInteractOutsideImpl(node,options))}));return()=>{cleanups.forEach(fn=>fn?.())}}export{trackInteractOutside};
|
|
176
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/get-window-frames.ts"],"sourcesContent":["import { addDomEvent, fireCustomEvent, isContextMenuEvent } from \"@zag-js/dom-event\"\nimport { contains, getDocument, getEventTarget, getWindow, isFocusable, isHTMLElement, raf } from \"@zag-js/dom-query\"\nimport { callAll } from \"@zag-js/utils\"\nimport { getWindowFrames } from \"./get-window-frames\"\n\nexport interface InteractOutsideHandlers {\n /**\n * Function called when the pointer is pressed down outside the component\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Function called when the focus is moved outside the component\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Function called when an interaction happens outside the component\n */\n onInteractOutside?: (event: InteractOutsideEvent) => void\n}\n\nexport interface InteractOutsideOptions extends InteractOutsideHandlers {\n exclude?: (target: HTMLElement) => boolean\n defer?: boolean\n}\n\nexport interface EventDetails<T> {\n originalEvent: T\n contextmenu: boolean\n focusable: boolean\n}\n\nconst POINTER_OUTSIDE_EVENT = \"pointerdown.outside\"\nconst FOCUS_OUTSIDE_EVENT = \"focus.outside\"\n\nexport type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>\n\nexport type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>\n\nexport type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent\n\nexport type MaybeElement = HTMLElement | null | undefined\nexport type NodeOrFn = MaybeElement | (() => MaybeElement)\n\nfunction isComposedPathFocusable(composedPath: EventTarget[]) {\n for (const node of composedPath) {\n if (isHTMLElement(node) && isFocusable(node)) return true\n }\n return false\n}\n\nconst isPointerEvent = (event: Event): event is PointerEvent => \"clientY\" in event\n\nfunction isEventPointWithin(node: MaybeElement, event: Event) {\n if (!isPointerEvent(event) || !node) return false\n\n const rect = node.getBoundingClientRect()\n if (rect.width === 0 || rect.height === 0) return false\n\n return (\n rect.top <= event.clientY &&\n event.clientY <= rect.top + rect.height &&\n rect.left <= event.clientX &&\n event.clientX <= rect.left + rect.width\n )\n}\n\nfunction isEventWithinScrollbar(event: Event): boolean {\n const target = getEventTarget<HTMLElement>(event)\n if (!target || !isPointerEvent(event)) return false\n\n const isScrollableY = target.scrollHeight > target.clientHeight\n const onScrollbarY = isScrollableY && event.clientX > target.clientWidth\n\n const isScrollableX = target.scrollWidth > target.clientWidth\n const onScrollbarX = isScrollableX && event.clientY > target.clientHeight\n\n return onScrollbarY || onScrollbarX\n}\n\nfunction trackInteractOutsideImpl(node: MaybeElement, options: InteractOutsideOptions) {\n const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options\n\n if (!node) return\n\n const doc = getDocument(node)\n const win = getWindow(node)\n const frames = getWindowFrames(win)\n\n function isEventOutside(event: Event): boolean {\n const target = getEventTarget(event)\n if (!isHTMLElement(target)) return false\n if (contains(node, target)) return false\n if (isEventPointWithin(node, event)) return false\n if (isEventWithinScrollbar(event)) return false\n return !exclude?.(target)\n }\n\n let clickHandler: VoidFunction\n\n function onPointerDown(event: PointerEvent) {\n //\n function handler() {\n const func = defer ? raf : (v: any) => v()\n const composedPath = event.composedPath?.() ?? [event.target]\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onPointerDownOutside || onInteractOutside) {\n const handler = callAll(onPointerDownOutside, onInteractOutside) as EventListener\n node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: isContextMenuEvent(event),\n focusable: isComposedPathFocusable(composedPath),\n },\n })\n })\n }\n\n if (event.pointerType === \"touch\") {\n frames.removeEventListener(\"click\", handler)\n doc.removeEventListener(\"click\", handler)\n\n clickHandler = handler\n\n doc.addEventListener(\"click\", handler, { once: true })\n frames.addEventListener(\"click\", handler, { once: true })\n } else {\n handler()\n }\n }\n const cleanups = new Set<VoidFunction>()\n\n const timer = setTimeout(() => {\n cleanups.add(frames.addEventListener(\"pointerdown\", onPointerDown, true))\n cleanups.add(addDomEvent(doc, \"pointerdown\", onPointerDown, true))\n }, 0)\n\n function onFocusin(event: FocusEvent) {\n //\n const func = defer ? raf : (v: any) => v()\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onFocusOutside || onInteractOutside) {\n const handler = callAll(onFocusOutside, onInteractOutside) as EventListener\n node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: false,\n focusable: isFocusable(getEventTarget(event)),\n },\n })\n })\n }\n\n cleanups.add(addDomEvent(doc, \"focusin\", onFocusin, true))\n cleanups.add(frames.addEventListener(\"focusin\", onFocusin, true))\n\n return () => {\n clearTimeout(timer)\n if (clickHandler) {\n frames.removeEventListener(\"click\", clickHandler)\n doc.removeEventListener(\"click\", clickHandler)\n }\n cleanups.forEach((fn) => fn())\n }\n}\n\nexport function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions) {\n const { defer } = options\n const func = defer ? raf : (v: any) => v()\n const cleanups: (VoidFunction | undefined)[] = []\n cleanups.push(\n func(() => {\n const node = typeof nodeOrFn === \"function\" ? nodeOrFn() : nodeOrFn\n cleanups.push(trackInteractOutsideImpl(node, options))\n }),\n )\n return () => {\n cleanups.forEach((fn) => fn?.())\n }\n}\n","export function getWindowFrames(win: Window) {\n const frames = {\n each(cb: (win: Window) => void) {\n for (let i = 0; i < win.frames?.length; i += 1) {\n const frame = win.frames[i]\n if (frame) cb(frame)\n }\n },\n addEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.addEventListener(event, listener, options)\n } catch {}\n })\n return () => {\n try {\n frames.removeEventListener(event, listener, options)\n } catch {}\n }\n },\n removeEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.removeEventListener(event, listener, options)\n } catch {}\n })\n },\n }\n return frames\n}\n"],"mappings":";AAAA,SAAS,aAAa,iBAAiB,0BAA0B;AACjE,SAAS,UAAU,aAAa,gBAAgB,WAAW,aAAa,eAAe,WAAW;AAClG,SAAS,eAAe;;;ACF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AAS,IAAI,OAAO,iBAAiB,WAAW,WAAW,IAAI,CAAC;AAEhE,SAAO,MAAM;AACX,iBAAa,KAAK;AAClB,QAAI,cAAc;AAChB,aAAO,oBAAoB,SAAS,YAAY;AAChD,UAAI,oBAAoB,SAAS,YAAY;AAAA,IAC/C;AACA,aAAS,QAAQ,CAAC,OAAO,GAAG,CAAC;AAAA,EAC/B;AACF;AAEO,SAAS,qBAAqB,UAAoB,SAAiC;AACxF,QAAM,EAAE,MAAM,IAAI;AAClB,QAAM,OAAO,QAAQ,MAAM,CAAC,MAAW,EAAE;AACzC,QAAM,WAAyC,CAAC;AAChD,WAAS;AAAA,IACP,KAAK,MAAM;AACT,YAAM,OAAO,OAAO,aAAa,aAAa,SAAS,IAAI;AAC3D,eAAS,KAAK,yBAAyB,MAAM,OAAO,CAAC;AAAA,IACvD,CAAC;AAAA,EACH;AACA,SAAO,MAAM;AACX,aAAS,QAAQ,CAAC,OAAO,KAAK,CAAC;AAAA,EACjC;AACF;","names":["handler"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/get-window-frames.ts"],"sourcesContent":["import { addDomEvent, fireCustomEvent, isContextMenuEvent } from \"@zag-js/dom-event\"\nimport { contains, getDocument, getEventTarget, getWindow, isFocusable, isHTMLElement, raf } from \"@zag-js/dom-query\"\nimport { callAll } from \"@zag-js/utils\"\nimport { getWindowFrames } from \"./get-window-frames\"\n\nexport interface InteractOutsideHandlers {\n /**\n * Function called when the pointer is pressed down outside the component\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Function called when the focus is moved outside the component\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Function called when an interaction happens outside the component\n */\n onInteractOutside?: (event: InteractOutsideEvent) => void\n}\n\nexport interface InteractOutsideOptions extends InteractOutsideHandlers {\n exclude?: (target: HTMLElement) => boolean\n defer?: boolean\n}\n\nexport interface EventDetails<T> {\n originalEvent: T\n contextmenu: boolean\n focusable: boolean\n}\n\nconst POINTER_OUTSIDE_EVENT = \"pointerdown.outside\"\nconst FOCUS_OUTSIDE_EVENT = \"focus.outside\"\n\nexport type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>\n\nexport type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>\n\nexport type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent\n\nexport type MaybeElement = HTMLElement | null | undefined\nexport type NodeOrFn = MaybeElement | (() => MaybeElement)\n\nfunction isComposedPathFocusable(composedPath: EventTarget[]) {\n for (const node of composedPath) {\n if (isHTMLElement(node) && isFocusable(node)) return true\n }\n return false\n}\n\nconst isPointerEvent = (event: Event): event is PointerEvent => \"clientY\" in event\n\nfunction isEventPointWithin(node: MaybeElement, event: Event) {\n if (!isPointerEvent(event) || !node) return false\n\n const rect = node.getBoundingClientRect()\n if (rect.width === 0 || rect.height === 0) return false\n\n return (\n rect.top <= event.clientY &&\n event.clientY <= rect.top + rect.height &&\n rect.left <= event.clientX &&\n event.clientX <= rect.left + rect.width\n )\n}\n\nfunction isEventWithinScrollbar(event: Event): boolean {\n const target = getEventTarget<HTMLElement>(event)\n if (!target || !isPointerEvent(event)) return false\n\n const isScrollableY = target.scrollHeight > target.clientHeight\n const onScrollbarY = isScrollableY && event.clientX > target.clientWidth\n\n const isScrollableX = target.scrollWidth > target.clientWidth\n const onScrollbarX = isScrollableX && event.clientY > target.clientHeight\n\n return onScrollbarY || onScrollbarX\n}\n\nfunction trackInteractOutsideImpl(node: MaybeElement, options: InteractOutsideOptions) {\n const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside, defer } = options\n\n if (!node) return\n\n const doc = getDocument(node)\n const win = getWindow(node)\n const frames = getWindowFrames(win)\n\n function isEventOutside(event: Event): boolean {\n const target = getEventTarget(event)\n if (!isHTMLElement(target)) return false\n if (contains(node, target)) return false\n if (isEventPointWithin(node, event)) return false\n if (isEventWithinScrollbar(event)) return false\n return !exclude?.(target)\n }\n\n let clickHandler: VoidFunction\n\n function onPointerDown(event: PointerEvent) {\n //\n function handler() {\n const func = defer ? raf : (v: any) => v()\n const composedPath = event.composedPath?.() ?? [event.target]\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onPointerDownOutside || onInteractOutside) {\n const handler = callAll(onPointerDownOutside, onInteractOutside) as EventListener\n node.addEventListener(POINTER_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: isContextMenuEvent(event),\n focusable: isComposedPathFocusable(composedPath),\n },\n })\n })\n }\n\n if (event.pointerType === \"touch\") {\n frames.removeEventListener(\"click\", handler)\n doc.removeEventListener(\"click\", handler)\n\n clickHandler = handler\n\n doc.addEventListener(\"click\", handler, { once: true })\n frames.addEventListener(\"click\", handler, { once: true })\n } else {\n handler()\n }\n }\n const cleanups = new Set<VoidFunction>()\n\n const timer = setTimeout(() => {\n cleanups.add(frames.addEventListener(\"pointerdown\", onPointerDown, true))\n cleanups.add(addDomEvent(doc, \"pointerdown\", onPointerDown, true))\n }, 0)\n\n function onFocusin(event: FocusEvent) {\n //\n const func = defer ? raf : (v: any) => v()\n func(() => {\n if (!node || !isEventOutside(event)) return\n\n if (onFocusOutside || onInteractOutside) {\n const handler = callAll(onFocusOutside, onInteractOutside) as EventListener\n node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true })\n }\n\n fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {\n bubbles: false,\n cancelable: true,\n detail: {\n originalEvent: event,\n contextmenu: false,\n focusable: isFocusable(getEventTarget(event)),\n },\n })\n })\n }\n\n cleanups.add(addDomEvent(doc, \"focusin\", onFocusin, true))\n cleanups.add(frames.addEventListener(\"focusin\", onFocusin, true))\n\n return () => {\n clearTimeout(timer)\n if (clickHandler) {\n frames.removeEventListener(\"click\", clickHandler)\n doc.removeEventListener(\"click\", clickHandler)\n }\n cleanups.forEach((fn) => fn())\n }\n}\n\nexport function trackInteractOutside(nodeOrFn: NodeOrFn, options: InteractOutsideOptions) {\n const { defer } = options\n const func = defer ? raf : (v: any) => v()\n const cleanups: (VoidFunction | undefined)[] = []\n cleanups.push(\n func(() => {\n const node = typeof nodeOrFn === \"function\" ? nodeOrFn() : nodeOrFn\n cleanups.push(trackInteractOutsideImpl(node, options))\n }),\n )\n return () => {\n cleanups.forEach((fn) => fn?.())\n }\n}\n","export function getWindowFrames(win: Window) {\n const frames = {\n each(cb: (win: Window) => void) {\n for (let i = 0; i < win.frames?.length; i += 1) {\n const frame = win.frames[i]\n if (frame) cb(frame)\n }\n },\n addEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.addEventListener(event, listener, options)\n } catch {}\n })\n return () => {\n try {\n frames.removeEventListener(event, listener, options)\n } catch {}\n }\n },\n removeEventListener(event: string, listener: any, options?: any) {\n frames.each((frame) => {\n try {\n frame.document.removeEventListener(event, listener, options)\n } catch {}\n })\n },\n }\n return frames\n}\n"],"mappings":"AAAA,OAAS,YAAa,gBAAiB,uBAA0B,oBACjE,OAAS,SAAU,YAAa,eAAgB,UAAW,YAAa,cAAe,QAAW,oBAClG,OAAS,YAAe,gBCFjB,SAAS,gBAAgB,IAAa,CAC3C,MAAM,OAAS,CACb,KAAK,GAA2B,CAC9B,QAAS,EAAI,EAAG,EAAI,IAAI,QAAQ,OAAQ,GAAK,EAAG,CAC9C,MAAM,MAAQ,IAAI,OAAO,CAAC,EAC1B,GAAI,MAAO,GAAG,KAAK,CACrB,CACF,EACA,iBAAiB,MAAe,SAAe,QAAe,CAC5D,OAAO,KAAM,OAAU,CACrB,GAAI,CACF,MAAM,SAAS,iBAAiB,MAAO,SAAU,OAAO,CAC1D,MAAQ,CAAC,CACX,CAAC,EACD,MAAO,IAAM,CACX,GAAI,CACF,OAAO,oBAAoB,MAAO,SAAU,OAAO,CACrD,MAAQ,CAAC,CACX,CACF,EACA,oBAAoB,MAAe,SAAe,QAAe,CAC/D,OAAO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,EACzC,KAAK,IAAM,CACT,GAAI,CAAC,MAAQ,CAAC,eAAe,KAAK,EAAG,OAErC,GAAI,gBAAkB,kBAAmB,CACvC,MAAM,QAAU,QAAQ,eAAgB,iBAAiB,EACzD,KAAK,iBAAiB,oBAAqB,QAAS,CAAE,KAAM,IAAK,CAAC,CACpE,CAEA,gBAAgB,KAAM,oBAAqB,CACzC,QAAS,MACT,WAAY,KACZ,OAAQ,CACN,cAAe,MACf,YAAa,MACb,UAAW,YAAY,eAAe,KAAK,CAAC,CAC9C,CACF,CAAC,CACH,CAAC,CACH,CAEA,SAAS,IAAI,YAAY,IAAK,UAAW,UAAW,IAAI,CAAC,EACzD,SAAS,IAAI,OAAO,iBAAiB,UAAW,UAAW,IAAI,CAAC,EAEhE,MAAO,IAAM,CACX,aAAa,KAAK,EAClB,GAAI,aAAc,CAChB,OAAO,oBAAoB,QAAS,YAAY,EAChD,IAAI,oBAAoB,QAAS,YAAY,CAC/C,CACA,SAAS,QAAS,IAAO,GAAG,CAAC,CAC/B,CACF,CAEO,SAAS,qBAAqB,SAAoB,QAAiC,CACxF,KAAM,CAAE,KAAM,EAAI,QAClB,MAAM,KAAO,MAAQ,IAAO,GAAW,EAAE,EACzC,MAAM,SAAyC,CAAC,EAChD,SAAS,KACP,KAAK,IAAM,CACT,MAAM,KAAO,OAAO,WAAa,WAAa,SAAS,EAAI,SAC3D,SAAS,KAAK,yBAAyB,KAAM,OAAO,CAAC,CACvD,CAAC,CACH,EACA,MAAO,IAAM,CACX,SAAS,QAAS,IAAO,KAAK,CAAC,CACjC,CACF","names":["handler"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/interact-outside",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.1",
|
|
4
4
|
"description": "Track interations or focus outside an element",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"src"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@zag-js/dom-query": "0.
|
|
21
|
-
"@zag-js/dom-event": "0.
|
|
22
|
-
"@zag-js/utils": "0.
|
|
20
|
+
"@zag-js/dom-query": "0.51.1",
|
|
21
|
+
"@zag-js/dom-event": "0.51.1",
|
|
22
|
+
"@zag-js/utils": "0.51.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"clean-package": "2.2.0"
|