@zag-js/dismissable 0.2.0 → 0.2.2

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