@zag-js/dismissable 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,33 @@
1
- // ../dom/dist/index.js
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
+ trackDismissableElement: () => trackDismissableElement
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // ../dom/dist/index.mjs
2
28
  var runIfFn = (v, ...a) => {
3
29
  const res = typeof v === "function" ? v(...a) : v;
4
- return res ?? void 0;
30
+ return res != null ? res : void 0;
5
31
  };
6
32
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
7
33
  function isDocument(el) {
@@ -11,15 +37,16 @@ function isWindow(value) {
11
37
  return (value == null ? void 0 : value.toString()) === "[object Window]";
12
38
  }
13
39
  function getDocument(el) {
40
+ var _a;
14
41
  if (isWindow(el))
15
42
  return el.document;
16
43
  if (isDocument(el))
17
44
  return el;
18
- return (el == null ? void 0 : el.ownerDocument) ?? document;
45
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
19
46
  }
20
47
  function getEventTarget(event) {
21
- var _a;
22
- return ((_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) ?? event.target;
48
+ var _a, _b;
49
+ return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
23
50
  }
24
51
  function contains(parent, child) {
25
52
  if (!parent)
@@ -39,11 +66,9 @@ function addDomEvent(target, eventName, handler, options) {
39
66
  }
40
67
 
41
68
  // src/dismissable-layer.ts
42
- import {
43
- trackInteractOutside
44
- } from "@zag-js/interact-outside";
69
+ var import_interact_outside = require("@zag-js/interact-outside");
45
70
 
46
- // ../core/dist/index.js
71
+ // ../core/dist/index.mjs
47
72
  function warn(...a) {
48
73
  const m = a.length === 1 ? a[0] : a[1];
49
74
  const c = a.length === 2 ? a[0] : true;
@@ -215,7 +240,7 @@ function trackDismissableElement(node, options) {
215
240
  const cleanups = [
216
241
  pointerBlocking ? disablePointerEventsOutside(node) : void 0,
217
242
  trackEscapeKeydown(onEscapeKeyDown),
218
- trackInteractOutside(node, { exclude, onFocusOutside, onPointerDownOutside })
243
+ (0, import_interact_outside.trackInteractOutside)(node, { exclude, onFocusOutside, onPointerDownOutside })
219
244
  ];
220
245
  return () => {
221
246
  layerStack.remove(node);
@@ -224,6 +249,7 @@ function trackDismissableElement(node, options) {
224
249
  cleanups.forEach((fn) => fn == null ? void 0 : fn());
225
250
  };
226
251
  }
227
- export {
252
+ // Annotate the CommonJS export names for ESM import in node:
253
+ 0 && (module.exports = {
228
254
  trackDismissableElement
229
- };
255
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,230 @@
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
+ 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
+ }
228
+ export {
229
+ trackDismissableElement
230
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
- "type": "module",
3
2
  "name": "@zag-js/dismissable",
4
- "version": "0.1.5",
3
+ "version": "0.2.0",
5
4
  "description": "Dismissable layer utilities for the DOM",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
6
8
  "keywords": [
7
9
  "js",
8
10
  "utils",
@@ -15,8 +17,6 @@
15
17
  "author": "Segun Adebayo <sage@adebayosegun.com>",
16
18
  "homepage": "https://github.com/chakra-ui/zag#readme",
17
19
  "license": "MIT",
18
- "main": "dist/index.js",
19
- "types": "dist/index.d.ts",
20
20
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/interact-outside",
21
21
  "sideEffects": false,
22
22
  "files": [
@@ -26,19 +26,19 @@
26
26
  "access": "public"
27
27
  },
28
28
  "dependencies": {
29
- "@zag-js/interact-outside": "0.1.5"
29
+ "@zag-js/interact-outside": "0.2.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@zag-js/dom-utils": "0.1.12",
33
- "@zag-js/utils": "0.1.5"
32
+ "@zag-js/dom-utils": "0.2.0",
33
+ "@zag-js/utils": "0.2.0"
34
34
  },
35
35
  "bugs": {
36
36
  "url": "https://github.com/chakra-ui/zag/issues"
37
37
  },
38
38
  "scripts": {
39
- "build-fast": "tsup src/index.ts --format=esm",
39
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
40
40
  "start": "pnpm build --watch",
41
- "build": "tsup src/index.ts --format=esm --dts",
41
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
42
42
  "test": "jest --config ../../../jest.config.js --rootDir tests",
43
43
  "lint": "eslint src --ext .ts,.tsx",
44
44
  "test-ci": "pnpm test --ci --runInBand -u",