@zag-js/dismissable 0.2.1 → 0.2.3

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,30 +1,21 @@
1
1
  import {
2
2
  trackEscapeKeydown
3
- } from "./chunk-3ZVQOINJ.mjs";
3
+ } from "./chunk-BBFIKTYR.mjs";
4
4
  import {
5
5
  assignPointerEventToLayers,
6
6
  clearPointerEvent,
7
7
  disablePointerEventsOutside
8
- } from "./chunk-6YFBZALL.mjs";
8
+ } from "./chunk-C5KU67MN.mjs";
9
9
  import {
10
- contains,
11
- getEventTarget,
12
10
  layerStack
13
- } from "./chunk-PFLX3TD5.mjs";
14
-
15
- // ../core/src/warning.ts
16
- function warn(...a) {
17
- const m = a.length === 1 ? a[0] : a[1];
18
- const c = a.length === 2 ? a[0] : true;
19
- if (c && process.env.NODE_ENV !== "production") {
20
- console.warn(m);
21
- }
22
- }
11
+ } from "./chunk-G4GHYT27.mjs";
23
12
 
24
13
  // src/dismissable-layer.ts
14
+ import { contains, getEventTarget } from "@zag-js/dom-query";
25
15
  import {
26
16
  trackInteractOutside
27
17
  } from "@zag-js/interact-outside";
18
+ import { warn } from "@zag-js/utils";
28
19
  function trackDismissableElement(node, options) {
29
20
  if (!node) {
30
21
  warn("[@zag-js/dismissable] node is `null` or `undefined`");
@@ -35,38 +26,35 @@ function trackDismissableElement(node, options) {
35
26
  layerStack.add(layer);
36
27
  assignPointerEventToLayers();
37
28
  function onPointerDownOutside(event) {
38
- var _a, _b;
39
29
  const target = getEventTarget(event.detail.originalEvent);
40
30
  if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
41
31
  return;
42
- (_a = options.onPointerDownOutside) == null ? void 0 : _a.call(options, event);
43
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
32
+ options.onPointerDownOutside?.(event);
33
+ options.onInteractOutside?.(event);
44
34
  if (event.defaultPrevented)
45
35
  return;
46
36
  if (debug) {
47
37
  console.log("onPointerDownOutside:", event.detail.originalEvent);
48
38
  }
49
- onDismiss == null ? void 0 : onDismiss();
39
+ onDismiss?.();
50
40
  }
51
41
  function onFocusOutside(event) {
52
- var _a, _b;
53
42
  const target = getEventTarget(event.detail.originalEvent);
54
43
  if (layerStack.isInBranch(target))
55
44
  return;
56
- (_a = options.onFocusOutside) == null ? void 0 : _a.call(options, event);
57
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
45
+ options.onFocusOutside?.(event);
46
+ options.onInteractOutside?.(event);
58
47
  if (event.defaultPrevented)
59
48
  return;
60
49
  if (debug) {
61
50
  console.log("onFocusOutside:", event.detail.originalEvent);
62
51
  }
63
- onDismiss == null ? void 0 : onDismiss();
52
+ onDismiss?.();
64
53
  }
65
54
  function onEscapeKeyDown(event) {
66
- var _a;
67
55
  if (!layerStack.isTopMost(node))
68
56
  return;
69
- (_a = options.onEscapeKeyDown) == null ? void 0 : _a.call(options, event);
57
+ options.onEscapeKeyDown?.(event);
70
58
  if (!event.defaultPrevented && onDismiss) {
71
59
  event.preventDefault();
72
60
  onDismiss();
@@ -81,14 +69,14 @@ function trackDismissableElement(node, options) {
81
69
  }
82
70
  const cleanups = [
83
71
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
84
- trackEscapeKeydown(onEscapeKeyDown),
72
+ trackEscapeKeydown(node, onEscapeKeyDown),
85
73
  trackInteractOutside(node, { exclude, onFocusOutside, onPointerDownOutside })
86
74
  ];
87
75
  return () => {
88
76
  layerStack.remove(node);
89
77
  assignPointerEventToLayers();
90
78
  clearPointerEvent(node);
91
- cleanups.forEach((fn) => fn == null ? void 0 : fn());
79
+ cleanups.forEach((fn) => fn?.());
92
80
  };
93
81
  }
94
82
 
@@ -0,0 +1,14 @@
1
+ // src/escape-keydown.ts
2
+ import { addDomEvent } from "@zag-js/dom-event";
3
+ import { getDocument } from "@zag-js/dom-query";
4
+ function trackEscapeKeydown(node, fn) {
5
+ const handleKeyDown = (event) => {
6
+ if (event.key === "Escape")
7
+ fn?.(event);
8
+ };
9
+ return addDomEvent(getDocument(node), "keydown", handleKeyDown);
10
+ }
11
+
12
+ export {
13
+ trackEscapeKeydown
14
+ };
@@ -1,9 +1,9 @@
1
1
  import {
2
- getDocument,
3
2
  layerStack
4
- } from "./chunk-PFLX3TD5.mjs";
3
+ } from "./chunk-G4GHYT27.mjs";
5
4
 
6
5
  // src/pointer-event-outside.ts
6
+ import { getDocument } from "@zag-js/dom-query";
7
7
  var originalBodyPointerEvents;
8
8
  function assignPointerEventToLayers() {
9
9
  layerStack.layers.forEach(({ node }) => {
@@ -1,32 +1,5 @@
1
- // ../dom/src/query.ts
2
- function isDocument(el) {
3
- return el.nodeType === Node.DOCUMENT_NODE;
4
- }
5
- function isWindow(value) {
6
- return (value == null ? void 0 : value.toString()) === "[object Window]";
7
- }
8
- function getDocument(el) {
9
- var _a;
10
- if (isWindow(el))
11
- return el.document;
12
- if (isDocument(el))
13
- return el;
14
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
15
- }
16
- function getEventTarget(event) {
17
- var _a, _b;
18
- return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
19
- }
20
- function contains(parent, child) {
21
- if (!parent)
22
- return false;
23
- return parent === child || isHTMLElement(parent) && isHTMLElement(child) && parent.contains(child);
24
- }
25
- function isHTMLElement(v) {
26
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
27
- }
28
-
29
1
  // src/layer-stack.ts
2
+ import { contains } from "@zag-js/dom-query";
30
3
  var layerStack = {
31
4
  layers: [],
32
5
  branches: [],
@@ -43,14 +16,13 @@ var layerStack = {
43
16
  return this.pointerBlockingLayers().length > 0;
44
17
  },
45
18
  isBelowPointerBlockingLayer(node) {
46
- var _a;
47
19
  const index = this.indexOf(node);
48
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf((_a = this.topMostPointerBlockingLayer()) == null ? void 0 : _a.node) : -1;
20
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
49
21
  return index < highestBlockingIndex;
50
22
  },
51
23
  isTopMost(node) {
52
24
  const layer = this.layers[this.count() - 1];
53
- return (layer == null ? void 0 : layer.node) === node;
25
+ return layer?.node === node;
54
26
  },
55
27
  getNestedLayers(node) {
56
28
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
@@ -86,8 +58,7 @@ var layerStack = {
86
58
  return this.layers.findIndex((layer) => layer.node === node);
87
59
  },
88
60
  dismiss(node) {
89
- var _a;
90
- (_a = this.layers[this.indexOf(node)]) == null ? void 0 : _a.dismiss();
61
+ this.layers[this.indexOf(node)]?.dismiss();
91
62
  },
92
63
  clear() {
93
64
  this.remove(this.layers[0].node);
@@ -95,8 +66,5 @@ var layerStack = {
95
66
  };
96
67
 
97
68
  export {
98
- getDocument,
99
- getEventTarget,
100
- contains,
101
69
  layerStack
102
70
  };
@@ -23,76 +23,23 @@ __export(dismissable_layer_exports, {
23
23
  trackDismissableElement: () => trackDismissableElement
24
24
  });
25
25
  module.exports = __toCommonJS(dismissable_layer_exports);
26
-
27
- // ../core/src/functions.ts
28
- var runIfFn = (v, ...a) => {
29
- const res = typeof v === "function" ? v(...a) : v;
30
- return res != null ? res : void 0;
31
- };
32
-
33
- // ../core/src/guard.ts
34
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
35
-
36
- // ../core/src/warning.ts
37
- function warn(...a) {
38
- const m = a.length === 1 ? a[0] : a[1];
39
- const c = a.length === 2 ? a[0] : true;
40
- if (c && process.env.NODE_ENV !== "production") {
41
- console.warn(m);
42
- }
43
- }
44
-
45
- // ../dom/src/query.ts
46
- function isDocument(el) {
47
- return el.nodeType === Node.DOCUMENT_NODE;
48
- }
49
- function isWindow(value) {
50
- return (value == null ? void 0 : value.toString()) === "[object Window]";
51
- }
52
- function getDocument(el) {
53
- var _a;
54
- if (isWindow(el))
55
- return el.document;
56
- if (isDocument(el))
57
- return el;
58
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
59
- }
60
- function getEventTarget(event) {
61
- var _a, _b;
62
- return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
63
- }
64
- function contains(parent, child) {
65
- if (!parent)
66
- return false;
67
- return parent === child || isHTMLElement(parent) && isHTMLElement(child) && parent.contains(child);
68
- }
69
- function isHTMLElement(v) {
70
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
71
- }
72
-
73
- // ../dom/src/listener.ts
74
- var isRef = (v) => hasProp(v, "current");
75
- function addDomEvent(target, eventName, handler, options) {
76
- const node = isRef(target) ? target.current : runIfFn(target);
77
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
78
- return () => {
79
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
80
- };
81
- }
82
-
83
- // src/dismissable-layer.ts
26
+ var import_dom_query4 = require("@zag-js/dom-query");
84
27
  var import_interact_outside = require("@zag-js/interact-outside");
28
+ var import_utils = require("@zag-js/utils");
85
29
 
86
30
  // src/escape-keydown.ts
87
- function trackEscapeKeydown(fn) {
31
+ var import_dom_event = require("@zag-js/dom-event");
32
+ var import_dom_query = require("@zag-js/dom-query");
33
+ function trackEscapeKeydown(node, fn) {
88
34
  const handleKeyDown = (event) => {
89
35
  if (event.key === "Escape")
90
- fn == null ? void 0 : fn(event);
36
+ fn?.(event);
91
37
  };
92
- return addDomEvent(document, "keydown", handleKeyDown);
38
+ return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
93
39
  }
94
40
 
95
41
  // src/layer-stack.ts
42
+ var import_dom_query2 = require("@zag-js/dom-query");
96
43
  var layerStack = {
97
44
  layers: [],
98
45
  branches: [],
@@ -109,23 +56,22 @@ var layerStack = {
109
56
  return this.pointerBlockingLayers().length > 0;
110
57
  },
111
58
  isBelowPointerBlockingLayer(node) {
112
- var _a;
113
59
  const index = this.indexOf(node);
114
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf((_a = this.topMostPointerBlockingLayer()) == null ? void 0 : _a.node) : -1;
60
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
115
61
  return index < highestBlockingIndex;
116
62
  },
117
63
  isTopMost(node) {
118
64
  const layer = this.layers[this.count() - 1];
119
- return (layer == null ? void 0 : layer.node) === node;
65
+ return layer?.node === node;
120
66
  },
121
67
  getNestedLayers(node) {
122
68
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
123
69
  },
124
70
  isInNestedLayer(node, target) {
125
- return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
71
+ return this.getNestedLayers(node).some((layer) => (0, import_dom_query2.contains)(layer.node, target));
126
72
  },
127
73
  isInBranch(target) {
128
- return Array.from(this.branches).some((branch) => contains(branch, target));
74
+ return Array.from(this.branches).some((branch) => (0, import_dom_query2.contains)(branch, target));
129
75
  },
130
76
  add(layer) {
131
77
  this.layers.push(layer);
@@ -152,8 +98,7 @@ var layerStack = {
152
98
  return this.layers.findIndex((layer) => layer.node === node);
153
99
  },
154
100
  dismiss(node) {
155
- var _a;
156
- (_a = this.layers[this.indexOf(node)]) == null ? void 0 : _a.dismiss();
101
+ this.layers[this.indexOf(node)]?.dismiss();
157
102
  },
158
103
  clear() {
159
104
  this.remove(this.layers[0].node);
@@ -161,6 +106,7 @@ var layerStack = {
161
106
  };
162
107
 
163
108
  // src/pointer-event-outside.ts
109
+ var import_dom_query3 = require("@zag-js/dom-query");
164
110
  var originalBodyPointerEvents;
165
111
  function assignPointerEventToLayers() {
166
112
  layerStack.layers.forEach(({ node }) => {
@@ -172,7 +118,7 @@ function clearPointerEvent(node) {
172
118
  }
173
119
  var DATA_ATTR = "data-inert";
174
120
  function disablePointerEventsOutside(node) {
175
- const doc = getDocument(node);
121
+ const doc = (0, import_dom_query3.getDocument)(node);
176
122
  if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
177
123
  originalBodyPointerEvents = document.body.style.pointerEvents;
178
124
  doc.body.style.pointerEvents = "none";
@@ -191,7 +137,7 @@ function disablePointerEventsOutside(node) {
191
137
  // src/dismissable-layer.ts
192
138
  function trackDismissableElement(node, options) {
193
139
  if (!node) {
194
- warn("[@zag-js/dismissable] node is `null` or `undefined`");
140
+ (0, import_utils.warn)("[@zag-js/dismissable] node is `null` or `undefined`");
195
141
  return;
196
142
  }
197
143
  const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
@@ -199,38 +145,35 @@ function trackDismissableElement(node, options) {
199
145
  layerStack.add(layer);
200
146
  assignPointerEventToLayers();
201
147
  function onPointerDownOutside(event) {
202
- var _a, _b;
203
- const target = getEventTarget(event.detail.originalEvent);
148
+ const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
204
149
  if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
205
150
  return;
206
- (_a = options.onPointerDownOutside) == null ? void 0 : _a.call(options, event);
207
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
151
+ options.onPointerDownOutside?.(event);
152
+ options.onInteractOutside?.(event);
208
153
  if (event.defaultPrevented)
209
154
  return;
210
155
  if (debug) {
211
156
  console.log("onPointerDownOutside:", event.detail.originalEvent);
212
157
  }
213
- onDismiss == null ? void 0 : onDismiss();
158
+ onDismiss?.();
214
159
  }
215
160
  function onFocusOutside(event) {
216
- var _a, _b;
217
- const target = getEventTarget(event.detail.originalEvent);
161
+ const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
218
162
  if (layerStack.isInBranch(target))
219
163
  return;
220
- (_a = options.onFocusOutside) == null ? void 0 : _a.call(options, event);
221
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
164
+ options.onFocusOutside?.(event);
165
+ options.onInteractOutside?.(event);
222
166
  if (event.defaultPrevented)
223
167
  return;
224
168
  if (debug) {
225
169
  console.log("onFocusOutside:", event.detail.originalEvent);
226
170
  }
227
- onDismiss == null ? void 0 : onDismiss();
171
+ onDismiss?.();
228
172
  }
229
173
  function onEscapeKeyDown(event) {
230
- var _a;
231
174
  if (!layerStack.isTopMost(node))
232
175
  return;
233
- (_a = options.onEscapeKeyDown) == null ? void 0 : _a.call(options, event);
176
+ options.onEscapeKeyDown?.(event);
234
177
  if (!event.defaultPrevented && onDismiss) {
235
178
  event.preventDefault();
236
179
  onDismiss();
@@ -241,18 +184,18 @@ function trackDismissableElement(node, options) {
241
184
  return false;
242
185
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
243
186
  const _containers = Array.isArray(containers) ? containers : [containers];
244
- return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
187
+ return _containers.some((node2) => (0, import_dom_query4.contains)(node2, target)) || layerStack.isInNestedLayer(node, target);
245
188
  }
246
189
  const cleanups = [
247
190
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
248
- trackEscapeKeydown(onEscapeKeyDown),
191
+ trackEscapeKeydown(node, onEscapeKeyDown),
249
192
  (0, import_interact_outside.trackInteractOutside)(node, { exclude, onFocusOutside, onPointerDownOutside })
250
193
  ];
251
194
  return () => {
252
195
  layerStack.remove(node);
253
196
  assignPointerEventToLayers();
254
197
  clearPointerEvent(node);
255
- cleanups.forEach((fn) => fn == null ? void 0 : fn());
198
+ cleanups.forEach((fn) => fn?.());
256
199
  };
257
200
  }
258
201
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  trackDismissableElement
3
- } from "./chunk-PYR5T5VL.mjs";
4
- import "./chunk-3ZVQOINJ.mjs";
5
- import "./chunk-6YFBZALL.mjs";
6
- import "./chunk-PFLX3TD5.mjs";
3
+ } from "./chunk-5BMACJNB.mjs";
4
+ import "./chunk-BBFIKTYR.mjs";
5
+ import "./chunk-C5KU67MN.mjs";
6
+ import "./chunk-G4GHYT27.mjs";
7
7
  export {
8
8
  trackDismissableElement
9
9
  };
@@ -1,3 +1,3 @@
1
- declare function trackEscapeKeydown(fn?: (event: KeyboardEvent) => void): () => void;
1
+ declare function trackEscapeKeydown(node: HTMLElement, fn?: (event: KeyboardEvent) => void): () => void;
2
2
 
3
3
  export { trackEscapeKeydown };
@@ -23,33 +23,14 @@ __export(escape_keydown_exports, {
23
23
  trackEscapeKeydown: () => trackEscapeKeydown
24
24
  });
25
25
  module.exports = __toCommonJS(escape_keydown_exports);
26
-
27
- // ../core/src/functions.ts
28
- var runIfFn = (v, ...a) => {
29
- const res = typeof v === "function" ? v(...a) : v;
30
- return res != null ? res : void 0;
31
- };
32
-
33
- // ../core/src/guard.ts
34
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
35
-
36
- // ../dom/src/listener.ts
37
- var isRef = (v) => hasProp(v, "current");
38
- function addDomEvent(target, eventName, handler, options) {
39
- const node = isRef(target) ? target.current : runIfFn(target);
40
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
41
- return () => {
42
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
43
- };
44
- }
45
-
46
- // src/escape-keydown.ts
47
- function trackEscapeKeydown(fn) {
26
+ var import_dom_event = require("@zag-js/dom-event");
27
+ var import_dom_query = require("@zag-js/dom-query");
28
+ function trackEscapeKeydown(node, fn) {
48
29
  const handleKeyDown = (event) => {
49
30
  if (event.key === "Escape")
50
- fn == null ? void 0 : fn(event);
31
+ fn?.(event);
51
32
  };
52
- return addDomEvent(document, "keydown", handleKeyDown);
33
+ return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
53
34
  }
54
35
  // Annotate the CommonJS export names for ESM import in node:
55
36
  0 && (module.exports = {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  trackEscapeKeydown
3
- } from "./chunk-3ZVQOINJ.mjs";
3
+ } from "./chunk-BBFIKTYR.mjs";
4
4
  export {
5
5
  trackEscapeKeydown
6
6
  };
package/dist/index.js CHANGED
@@ -24,75 +24,24 @@ __export(src_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
- // ../core/src/functions.ts
28
- var runIfFn = (v, ...a) => {
29
- const res = typeof v === "function" ? v(...a) : v;
30
- return res != null ? res : void 0;
31
- };
32
-
33
- // ../core/src/guard.ts
34
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
35
-
36
- // ../core/src/warning.ts
37
- function warn(...a) {
38
- const m = a.length === 1 ? a[0] : a[1];
39
- const c = a.length === 2 ? a[0] : true;
40
- if (c && process.env.NODE_ENV !== "production") {
41
- console.warn(m);
42
- }
43
- }
44
-
45
- // ../dom/src/query.ts
46
- function isDocument(el) {
47
- return el.nodeType === Node.DOCUMENT_NODE;
48
- }
49
- function isWindow(value) {
50
- return (value == null ? void 0 : value.toString()) === "[object Window]";
51
- }
52
- function getDocument(el) {
53
- var _a;
54
- if (isWindow(el))
55
- return el.document;
56
- if (isDocument(el))
57
- return el;
58
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
59
- }
60
- function getEventTarget(event) {
61
- var _a, _b;
62
- return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
63
- }
64
- function contains(parent, child) {
65
- if (!parent)
66
- return false;
67
- return parent === child || isHTMLElement(parent) && isHTMLElement(child) && parent.contains(child);
68
- }
69
- function isHTMLElement(v) {
70
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
71
- }
72
-
73
- // ../dom/src/listener.ts
74
- var isRef = (v) => hasProp(v, "current");
75
- function addDomEvent(target, eventName, handler, options) {
76
- const node = isRef(target) ? target.current : runIfFn(target);
77
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
78
- return () => {
79
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
80
- };
81
- }
82
-
83
27
  // src/dismissable-layer.ts
28
+ var import_dom_query4 = require("@zag-js/dom-query");
84
29
  var import_interact_outside = require("@zag-js/interact-outside");
30
+ var import_utils = require("@zag-js/utils");
85
31
 
86
32
  // src/escape-keydown.ts
87
- function trackEscapeKeydown(fn) {
33
+ var import_dom_event = require("@zag-js/dom-event");
34
+ var import_dom_query = require("@zag-js/dom-query");
35
+ function trackEscapeKeydown(node, fn) {
88
36
  const handleKeyDown = (event) => {
89
37
  if (event.key === "Escape")
90
- fn == null ? void 0 : fn(event);
38
+ fn?.(event);
91
39
  };
92
- return addDomEvent(document, "keydown", handleKeyDown);
40
+ return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
93
41
  }
94
42
 
95
43
  // src/layer-stack.ts
44
+ var import_dom_query2 = require("@zag-js/dom-query");
96
45
  var layerStack = {
97
46
  layers: [],
98
47
  branches: [],
@@ -109,23 +58,22 @@ var layerStack = {
109
58
  return this.pointerBlockingLayers().length > 0;
110
59
  },
111
60
  isBelowPointerBlockingLayer(node) {
112
- var _a;
113
61
  const index = this.indexOf(node);
114
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf((_a = this.topMostPointerBlockingLayer()) == null ? void 0 : _a.node) : -1;
62
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
115
63
  return index < highestBlockingIndex;
116
64
  },
117
65
  isTopMost(node) {
118
66
  const layer = this.layers[this.count() - 1];
119
- return (layer == null ? void 0 : layer.node) === node;
67
+ return layer?.node === node;
120
68
  },
121
69
  getNestedLayers(node) {
122
70
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
123
71
  },
124
72
  isInNestedLayer(node, target) {
125
- return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
73
+ return this.getNestedLayers(node).some((layer) => (0, import_dom_query2.contains)(layer.node, target));
126
74
  },
127
75
  isInBranch(target) {
128
- return Array.from(this.branches).some((branch) => contains(branch, target));
76
+ return Array.from(this.branches).some((branch) => (0, import_dom_query2.contains)(branch, target));
129
77
  },
130
78
  add(layer) {
131
79
  this.layers.push(layer);
@@ -152,8 +100,7 @@ var layerStack = {
152
100
  return this.layers.findIndex((layer) => layer.node === node);
153
101
  },
154
102
  dismiss(node) {
155
- var _a;
156
- (_a = this.layers[this.indexOf(node)]) == null ? void 0 : _a.dismiss();
103
+ this.layers[this.indexOf(node)]?.dismiss();
157
104
  },
158
105
  clear() {
159
106
  this.remove(this.layers[0].node);
@@ -161,6 +108,7 @@ var layerStack = {
161
108
  };
162
109
 
163
110
  // src/pointer-event-outside.ts
111
+ var import_dom_query3 = require("@zag-js/dom-query");
164
112
  var originalBodyPointerEvents;
165
113
  function assignPointerEventToLayers() {
166
114
  layerStack.layers.forEach(({ node }) => {
@@ -172,7 +120,7 @@ function clearPointerEvent(node) {
172
120
  }
173
121
  var DATA_ATTR = "data-inert";
174
122
  function disablePointerEventsOutside(node) {
175
- const doc = getDocument(node);
123
+ const doc = (0, import_dom_query3.getDocument)(node);
176
124
  if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
177
125
  originalBodyPointerEvents = document.body.style.pointerEvents;
178
126
  doc.body.style.pointerEvents = "none";
@@ -191,7 +139,7 @@ function disablePointerEventsOutside(node) {
191
139
  // src/dismissable-layer.ts
192
140
  function trackDismissableElement(node, options) {
193
141
  if (!node) {
194
- warn("[@zag-js/dismissable] node is `null` or `undefined`");
142
+ (0, import_utils.warn)("[@zag-js/dismissable] node is `null` or `undefined`");
195
143
  return;
196
144
  }
197
145
  const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
@@ -199,38 +147,35 @@ function trackDismissableElement(node, options) {
199
147
  layerStack.add(layer);
200
148
  assignPointerEventToLayers();
201
149
  function onPointerDownOutside(event) {
202
- var _a, _b;
203
- const target = getEventTarget(event.detail.originalEvent);
150
+ const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
204
151
  if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
205
152
  return;
206
- (_a = options.onPointerDownOutside) == null ? void 0 : _a.call(options, event);
207
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
153
+ options.onPointerDownOutside?.(event);
154
+ options.onInteractOutside?.(event);
208
155
  if (event.defaultPrevented)
209
156
  return;
210
157
  if (debug) {
211
158
  console.log("onPointerDownOutside:", event.detail.originalEvent);
212
159
  }
213
- onDismiss == null ? void 0 : onDismiss();
160
+ onDismiss?.();
214
161
  }
215
162
  function onFocusOutside(event) {
216
- var _a, _b;
217
- const target = getEventTarget(event.detail.originalEvent);
163
+ const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
218
164
  if (layerStack.isInBranch(target))
219
165
  return;
220
- (_a = options.onFocusOutside) == null ? void 0 : _a.call(options, event);
221
- (_b = options.onInteractOutside) == null ? void 0 : _b.call(options, event);
166
+ options.onFocusOutside?.(event);
167
+ options.onInteractOutside?.(event);
222
168
  if (event.defaultPrevented)
223
169
  return;
224
170
  if (debug) {
225
171
  console.log("onFocusOutside:", event.detail.originalEvent);
226
172
  }
227
- onDismiss == null ? void 0 : onDismiss();
173
+ onDismiss?.();
228
174
  }
229
175
  function onEscapeKeyDown(event) {
230
- var _a;
231
176
  if (!layerStack.isTopMost(node))
232
177
  return;
233
- (_a = options.onEscapeKeyDown) == null ? void 0 : _a.call(options, event);
178
+ options.onEscapeKeyDown?.(event);
234
179
  if (!event.defaultPrevented && onDismiss) {
235
180
  event.preventDefault();
236
181
  onDismiss();
@@ -241,18 +186,18 @@ function trackDismissableElement(node, options) {
241
186
  return false;
242
187
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
243
188
  const _containers = Array.isArray(containers) ? containers : [containers];
244
- return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
189
+ return _containers.some((node2) => (0, import_dom_query4.contains)(node2, target)) || layerStack.isInNestedLayer(node, target);
245
190
  }
246
191
  const cleanups = [
247
192
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
248
- trackEscapeKeydown(onEscapeKeyDown),
193
+ trackEscapeKeydown(node, onEscapeKeyDown),
249
194
  (0, import_interact_outside.trackInteractOutside)(node, { exclude, onFocusOutside, onPointerDownOutside })
250
195
  ];
251
196
  return () => {
252
197
  layerStack.remove(node);
253
198
  assignPointerEventToLayers();
254
199
  clearPointerEvent(node);
255
- cleanups.forEach((fn) => fn == null ? void 0 : fn());
200
+ cleanups.forEach((fn) => fn?.());
256
201
  };
257
202
  }
258
203
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  trackDismissableElement
3
- } from "./chunk-PYR5T5VL.mjs";
4
- import "./chunk-3ZVQOINJ.mjs";
5
- import "./chunk-6YFBZALL.mjs";
6
- import "./chunk-PFLX3TD5.mjs";
3
+ } from "./chunk-5BMACJNB.mjs";
4
+ import "./chunk-BBFIKTYR.mjs";
5
+ import "./chunk-C5KU67MN.mjs";
6
+ import "./chunk-G4GHYT27.mjs";
7
7
  export {
8
8
  trackDismissableElement
9
9
  };
@@ -23,18 +23,7 @@ __export(layer_stack_exports, {
23
23
  layerStack: () => layerStack
24
24
  });
25
25
  module.exports = __toCommonJS(layer_stack_exports);
26
-
27
- // ../dom/src/query.ts
28
- function contains(parent, child) {
29
- if (!parent)
30
- return false;
31
- return parent === child || isHTMLElement(parent) && isHTMLElement(child) && parent.contains(child);
32
- }
33
- function isHTMLElement(v) {
34
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
35
- }
36
-
37
- // src/layer-stack.ts
26
+ var import_dom_query = require("@zag-js/dom-query");
38
27
  var layerStack = {
39
28
  layers: [],
40
29
  branches: [],
@@ -51,23 +40,22 @@ var layerStack = {
51
40
  return this.pointerBlockingLayers().length > 0;
52
41
  },
53
42
  isBelowPointerBlockingLayer(node) {
54
- var _a;
55
43
  const index = this.indexOf(node);
56
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf((_a = this.topMostPointerBlockingLayer()) == null ? void 0 : _a.node) : -1;
44
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
57
45
  return index < highestBlockingIndex;
58
46
  },
59
47
  isTopMost(node) {
60
48
  const layer = this.layers[this.count() - 1];
61
- return (layer == null ? void 0 : layer.node) === node;
49
+ return layer?.node === node;
62
50
  },
63
51
  getNestedLayers(node) {
64
52
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
65
53
  },
66
54
  isInNestedLayer(node, target) {
67
- return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
55
+ return this.getNestedLayers(node).some((layer) => (0, import_dom_query.contains)(layer.node, target));
68
56
  },
69
57
  isInBranch(target) {
70
- return Array.from(this.branches).some((branch) => contains(branch, target));
58
+ return Array.from(this.branches).some((branch) => (0, import_dom_query.contains)(branch, target));
71
59
  },
72
60
  add(layer) {
73
61
  this.layers.push(layer);
@@ -94,8 +82,7 @@ var layerStack = {
94
82
  return this.layers.findIndex((layer) => layer.node === node);
95
83
  },
96
84
  dismiss(node) {
97
- var _a;
98
- (_a = this.layers[this.indexOf(node)]) == null ? void 0 : _a.dismiss();
85
+ this.layers[this.indexOf(node)]?.dismiss();
99
86
  },
100
87
  clear() {
101
88
  this.remove(this.layers[0].node);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  layerStack
3
- } from "./chunk-PFLX3TD5.mjs";
3
+ } from "./chunk-G4GHYT27.mjs";
4
4
  export {
5
5
  layerStack
6
6
  };
@@ -25,32 +25,10 @@ __export(pointer_event_outside_exports, {
25
25
  disablePointerEventsOutside: () => disablePointerEventsOutside
26
26
  });
27
27
  module.exports = __toCommonJS(pointer_event_outside_exports);
28
-
29
- // ../dom/src/query.ts
30
- function isDocument(el) {
31
- return el.nodeType === Node.DOCUMENT_NODE;
32
- }
33
- function isWindow(value) {
34
- return (value == null ? void 0 : value.toString()) === "[object Window]";
35
- }
36
- function getDocument(el) {
37
- var _a;
38
- if (isWindow(el))
39
- return el.document;
40
- if (isDocument(el))
41
- return el;
42
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
43
- }
44
- function contains(parent, child) {
45
- if (!parent)
46
- return false;
47
- return parent === child || isHTMLElement(parent) && isHTMLElement(child) && parent.contains(child);
48
- }
49
- function isHTMLElement(v) {
50
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
51
- }
28
+ var import_dom_query2 = require("@zag-js/dom-query");
52
29
 
53
30
  // src/layer-stack.ts
31
+ var import_dom_query = require("@zag-js/dom-query");
54
32
  var layerStack = {
55
33
  layers: [],
56
34
  branches: [],
@@ -67,23 +45,22 @@ var layerStack = {
67
45
  return this.pointerBlockingLayers().length > 0;
68
46
  },
69
47
  isBelowPointerBlockingLayer(node) {
70
- var _a;
71
48
  const index = this.indexOf(node);
72
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf((_a = this.topMostPointerBlockingLayer()) == null ? void 0 : _a.node) : -1;
49
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
73
50
  return index < highestBlockingIndex;
74
51
  },
75
52
  isTopMost(node) {
76
53
  const layer = this.layers[this.count() - 1];
77
- return (layer == null ? void 0 : layer.node) === node;
54
+ return layer?.node === node;
78
55
  },
79
56
  getNestedLayers(node) {
80
57
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
81
58
  },
82
59
  isInNestedLayer(node, target) {
83
- return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
60
+ return this.getNestedLayers(node).some((layer) => (0, import_dom_query.contains)(layer.node, target));
84
61
  },
85
62
  isInBranch(target) {
86
- return Array.from(this.branches).some((branch) => contains(branch, target));
63
+ return Array.from(this.branches).some((branch) => (0, import_dom_query.contains)(branch, target));
87
64
  },
88
65
  add(layer) {
89
66
  this.layers.push(layer);
@@ -110,8 +87,7 @@ var layerStack = {
110
87
  return this.layers.findIndex((layer) => layer.node === node);
111
88
  },
112
89
  dismiss(node) {
113
- var _a;
114
- (_a = this.layers[this.indexOf(node)]) == null ? void 0 : _a.dismiss();
90
+ this.layers[this.indexOf(node)]?.dismiss();
115
91
  },
116
92
  clear() {
117
93
  this.remove(this.layers[0].node);
@@ -130,7 +106,7 @@ function clearPointerEvent(node) {
130
106
  }
131
107
  var DATA_ATTR = "data-inert";
132
108
  function disablePointerEventsOutside(node) {
133
- const doc = getDocument(node);
109
+ const doc = (0, import_dom_query2.getDocument)(node);
134
110
  if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
135
111
  originalBodyPointerEvents = document.body.style.pointerEvents;
136
112
  doc.body.style.pointerEvents = "none";
@@ -2,8 +2,8 @@ import {
2
2
  assignPointerEventToLayers,
3
3
  clearPointerEvent,
4
4
  disablePointerEventsOutside
5
- } from "./chunk-6YFBZALL.mjs";
6
- import "./chunk-PFLX3TD5.mjs";
5
+ } from "./chunk-C5KU67MN.mjs";
6
+ import "./chunk-G4GHYT27.mjs";
7
7
  export {
8
8
  assignPointerEventToLayers,
9
9
  clearPointerEvent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/dismissable",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Dismissable layer utilities for the DOM",
5
5
  "keywords": [
6
6
  "js",
@@ -23,12 +23,13 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
- "@zag-js/interact-outside": "0.2.1"
26
+ "@zag-js/interact-outside": "0.2.3",
27
+ "@zag-js/dom-event": "0.0.1",
28
+ "@zag-js/utils": "0.3.3",
29
+ "@zag-js/dom-query": "0.1.3"
27
30
  },
28
31
  "devDependencies": {
29
- "clean-package": "2.2.0",
30
- "@zag-js/dom-utils": "0.2.2",
31
- "@zag-js/utils": "0.3.2"
32
+ "clean-package": "2.2.0"
32
33
  },
33
34
  "bugs": {
34
35
  "url": "https://github.com/chakra-ui/zag/issues"
@@ -1,31 +0,0 @@
1
- // ../core/src/functions.ts
2
- var runIfFn = (v, ...a) => {
3
- const res = typeof v === "function" ? v(...a) : v;
4
- return res != null ? res : void 0;
5
- };
6
-
7
- // ../core/src/guard.ts
8
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
9
-
10
- // ../dom/src/listener.ts
11
- var isRef = (v) => hasProp(v, "current");
12
- function addDomEvent(target, eventName, handler, options) {
13
- const node = isRef(target) ? target.current : runIfFn(target);
14
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
15
- return () => {
16
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
17
- };
18
- }
19
-
20
- // src/escape-keydown.ts
21
- function trackEscapeKeydown(fn) {
22
- const handleKeyDown = (event) => {
23
- if (event.key === "Escape")
24
- fn == null ? void 0 : fn(event);
25
- };
26
- return addDomEvent(document, "keydown", handleKeyDown);
27
- }
28
-
29
- export {
30
- trackEscapeKeydown
31
- };