@zag-js/dismissable 2.0.0-next.2 → 2.0.0-next.4

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,230 +1,227 @@
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
- isPointerBlocking: () => isPointerBlocking,
24
- layerStack: () => layerStack
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
25
4
  });
26
- module.exports = __toCommonJS(layer_stack_exports);
27
- var import_dom_query = require("@zag-js/dom-query");
28
- var import_utils = require("@zag-js/utils");
29
- var LAYER_REQUEST_DISMISS_EVENT = "layer:request-dismiss";
30
- var layerId = 0;
5
+ let _zag_js_dom_query = require("@zag-js/dom-query");
6
+ let _zag_js_utils = require("@zag-js/utils");
7
+ // src/layer-stack.ts
8
+ const LAYER_REQUEST_DISMISS_EVENT = "layer:request-dismiss";
9
+ let layerId = 0;
10
+ let cancelClosingAncestors;
11
+ /** Resolved on read, so a layer's blocking can change without re-registering it. */
31
12
  function isPointerBlocking(layer) {
32
- const value = layer.pointerBlocking;
33
- return typeof value === "function" ? value() : !!value;
13
+ const value = layer.pointerBlocking;
14
+ return typeof value === "function" ? value() : !!value;
34
15
  }
35
- var layerStack = {
36
- layers: [],
37
- branches: [],
38
- recentlyRemoved: /* @__PURE__ */ new Set(),
39
- pendingReattach: /* @__PURE__ */ new Map(),
40
- count() {
41
- return this.layers.length;
42
- },
43
- pointerBlockingLayers() {
44
- return this.layers.filter(isPointerBlocking);
45
- },
46
- topMostPointerBlockingLayer() {
47
- return [...this.pointerBlockingLayers()].slice(-1)[0];
48
- },
49
- hasPointerBlockingLayer() {
50
- return this.pointerBlockingLayers().length > 0;
51
- },
52
- isBelowPointerBlockingLayer(node) {
53
- const index = this.indexOf(node);
54
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
55
- return index < highestBlockingIndex;
56
- },
57
- isTopMost(node) {
58
- const layer = this.layers[this.count() - 1];
59
- return layer?.node === node;
60
- },
61
- layerFor(node) {
62
- return this.layers.find((layer) => layer.node === node);
63
- },
64
- isDescendantOf(layer, ancestorId) {
65
- if (!ancestorId) return false;
66
- const seen = /* @__PURE__ */ new Set();
67
- let parentId = layer.parentId;
68
- while (parentId && !seen.has(parentId)) {
69
- if (parentId === ancestorId) return true;
70
- seen.add(parentId);
71
- parentId = this.layers.find((l) => l.id === parentId)?.parentId;
72
- }
73
- return false;
74
- },
75
- depthOf(layer) {
76
- const seen = /* @__PURE__ */ new Set();
77
- let depth = 0;
78
- let parentId = layer.parentId;
79
- while (parentId && !seen.has(parentId)) {
80
- depth++;
81
- seen.add(parentId);
82
- parentId = this.layers.find((l) => l.id === parentId)?.parentId;
83
- }
84
- return depth;
85
- },
86
- /** Descendants, shallowest first, so a cascade dismisses parents before their own children. */
87
- getNestedLayers(node) {
88
- const id = this.layerFor(node)?.id;
89
- if (!id) return [];
90
- return this.layers.filter((layer) => this.isDescendantOf(layer, id)).sort((a, b) => this.depthOf(a) - this.depthOf(b));
91
- },
92
- getLayersByType(type) {
93
- return this.layers.filter((layer) => layer.type === type);
94
- },
95
- getNestedLayersByType(node, type) {
96
- return this.getNestedLayers(node).filter((layer) => layer.type === type);
97
- },
98
- getParentLayerOfType(node, type) {
99
- const seen = /* @__PURE__ */ new Set();
100
- let parentId = this.layerFor(node)?.parentId;
101
- while (parentId && !seen.has(parentId)) {
102
- const parent = this.layers.find((l) => l.id === parentId);
103
- if (!parent) return void 0;
104
- if (parent.type === type) return parent;
105
- seen.add(parentId);
106
- parentId = parent.parentId;
107
- }
108
- return void 0;
109
- },
110
- countNestedLayersOfType(node, type) {
111
- return this.getNestedLayersByType(node, type).length;
112
- },
113
- isInNestedLayer(node, target) {
114
- const inNested = this.getNestedLayers(node).some((layer) => (0, import_dom_query.contains)(layer.node, target));
115
- if (inNested) return true;
116
- if (this.recentlyRemoved.size > 0) return true;
117
- return false;
118
- },
119
- isInBranch(target) {
120
- return Array.from(this.branches).some((branch) => (0, import_dom_query.contains)(branch, target));
121
- },
122
- add(layer) {
123
- const existingIndex = this.indexOf(layer.node);
124
- const existing = existingIndex !== -1 ? this.layers[existingIndex] : void 0;
125
- const pending = this.pendingReattach.get(layer.node);
126
- if (existing) {
127
- layer.id ?? (layer.id = existing.id);
128
- layer.parentId = existing.parentId;
129
- } else if (pending) {
130
- layer.id ?? (layer.id = pending.id);
131
- layer.parentId = pending.parentId;
132
- } else {
133
- layer.id ?? (layer.id = `layer-${++layerId}`);
134
- layer.parentId ?? (layer.parentId = this.layers[this.count() - 1]?.id);
135
- }
136
- if (existingIndex !== -1) {
137
- this.layers.splice(existingIndex, 1);
138
- }
139
- if (pending) {
140
- this.pendingReattach.delete(layer.node);
141
- this.layers.splice(Math.min(pending.index, this.count()), 0, layer);
142
- } else {
143
- this.layers.push(layer);
144
- }
145
- this.syncLayers();
146
- },
147
- addBranch(node) {
148
- this.branches.push(node);
149
- },
150
- remove(node, options) {
151
- const index = this.indexOf(node);
152
- if (index < 0) return;
153
- const layer = this.layers[index];
154
- if (options?.reattach) {
155
- this.pendingReattach.set(node, { id: layer.id, parentId: layer.parentId, index });
156
- } else {
157
- this.recentlyRemoved.add(node);
158
- (0, import_dom_query.nextTick)(() => this.recentlyRemoved.delete(node));
159
- this.getNestedLayers(node).forEach((nested) => layerStack.dismiss(nested.node, node));
160
- }
161
- this.layers.splice(index, 1);
162
- if (layer.snapshot) {
163
- publishSnapshot(layer, { ...layer.snapshot, active: false, blocked: false });
164
- }
165
- this.syncLayers();
166
- },
167
- removeBranch(node) {
168
- const index = this.branches.indexOf(node);
169
- if (index >= 0) this.branches.splice(index, 1);
170
- },
171
- syncLayers() {
172
- this.layers.forEach((layer, index) => {
173
- const parentOfSameType = layerStack.getParentLayerOfType(layer.node, layer.type);
174
- const nestedCount = layerStack.countNestedLayersOfType(layer.node, layer.type);
175
- const snapshot = {
176
- active: true,
177
- type: layer.type,
178
- index,
179
- nested: parentOfSameType != null,
180
- hasNested: nestedCount > 0,
181
- nestedCount,
182
- blocked: layerStack.isBelowPointerBlockingLayer(layer.node)
183
- };
184
- if (!(0, import_utils.isEqual)(layer.snapshot, snapshot)) {
185
- publishSnapshot(layer, snapshot);
186
- }
187
- });
188
- },
189
- indexOf(node) {
190
- return this.layers.findIndex((layer) => layer.node === node);
191
- },
192
- dismiss(node, parent) {
193
- const index = this.indexOf(node);
194
- if (index === -1) return;
195
- const layer = this.layers[index];
196
- addListenerOnce(node, LAYER_REQUEST_DISMISS_EVENT, (event) => {
197
- layer.requestDismiss?.(event);
198
- if (!event.defaultPrevented) {
199
- layer?.dismiss();
200
- }
201
- });
202
- fireCustomEvent(node, LAYER_REQUEST_DISMISS_EVENT, {
203
- originalLayer: node,
204
- targetLayer: parent,
205
- originalIndex: index,
206
- targetIndex: parent ? this.indexOf(parent) : -1
207
- });
208
- this.syncLayers();
209
- },
210
- clear() {
211
- this.remove(this.layers[0].node);
212
- }
16
+ function indexById(layers) {
17
+ const byId = /* @__PURE__ */ new Map();
18
+ for (const layer of layers) if (layer.id && !byId.has(layer.id)) byId.set(layer.id, layer);
19
+ return byId;
20
+ }
21
+ /** Ancestor ids, nearest first. Stops at a cycle. */
22
+ function* ancestorIds(layer, byId) {
23
+ const seen = /* @__PURE__ */ new Set();
24
+ let parentId = layer.parentId;
25
+ while (parentId && !seen.has(parentId)) {
26
+ seen.add(parentId);
27
+ yield parentId;
28
+ parentId = byId.get(parentId)?.parentId;
29
+ }
30
+ }
31
+ const layerStack = {
32
+ layers: [],
33
+ branches: [],
34
+ /** Ids of layers whose descendant just closed. */
35
+ closingAncestors: /* @__PURE__ */ new Set(),
36
+ /** Descendants queued to close with their ancestor. A new layer can't nest in them. */
37
+ dismissing: /* @__PURE__ */ new WeakSet(),
38
+ pendingReattach: /* @__PURE__ */ new Map(),
39
+ count() {
40
+ return this.layers.length;
41
+ },
42
+ topMostPointerBlockingIndex() {
43
+ for (let i = this.count() - 1; i >= 0; i--) if (isPointerBlocking(this.layers[i])) return i;
44
+ return -1;
45
+ },
46
+ topMostPointerBlockingLayer() {
47
+ return this.layers[this.topMostPointerBlockingIndex()];
48
+ },
49
+ hasPointerBlockingLayer() {
50
+ return this.topMostPointerBlockingIndex() !== -1;
51
+ },
52
+ isBelowPointerBlockingLayer(node) {
53
+ return this.indexOf(node) < this.topMostPointerBlockingIndex();
54
+ },
55
+ isTopMost(node) {
56
+ return this.layers[this.count() - 1]?.node === node;
57
+ },
58
+ layerFor(node) {
59
+ return this.layers.find((layer) => layer.node === node);
60
+ },
61
+ parentFor(layer) {
62
+ const origins = layer.origins?.filter(Boolean);
63
+ for (let i = this.count() - 1; i >= 0; i--) {
64
+ const candidate = this.layers[i];
65
+ if (this.dismissing.has(candidate.node)) continue;
66
+ if (!origins || origins.some((origin) => (0, _zag_js_dom_query.contains)(candidate.node, origin))) return candidate;
67
+ }
68
+ },
69
+ /** Top-level layers that opened from inside the new layer become its children. */
70
+ adopt(layer) {
71
+ if (!layer.origins) return [];
72
+ const adopted = this.layers.filter((other) => !other.parentId && other.origins?.some((origin) => origin && origin !== layer.node && (0, _zag_js_dom_query.contains)(layer.node, origin)));
73
+ adopted.forEach((child) => child.parentId = layer.id);
74
+ return adopted;
75
+ },
76
+ /** The most recently added layer in the same group, if any. */
77
+ peerFor(group) {
78
+ for (let i = this.count() - 1; i >= 0; i--) if (this.layers[i].group === group) return this.layers[i];
79
+ },
80
+ /** Descendants, shallowest first, so a cascade dismisses parents before their own children. */
81
+ getNestedLayers(node) {
82
+ const id = this.layerFor(node)?.id;
83
+ if (!id) return [];
84
+ const byId = indexById(this.layers);
85
+ const depth = /* @__PURE__ */ new Map();
86
+ for (const layer of this.layers) {
87
+ let level = 0;
88
+ for (const ancestorId of ancestorIds(layer, byId)) {
89
+ level++;
90
+ if (ancestorId !== id) continue;
91
+ depth.set(layer, level);
92
+ break;
93
+ }
94
+ }
95
+ return [...depth.keys()].sort((a, b) => depth.get(a) - depth.get(b));
96
+ },
97
+ isInNestedLayer(node, target) {
98
+ return this.getNestedLayers(node).some((layer) => (0, _zag_js_dom_query.contains)(layer.node, target));
99
+ },
100
+ /** Focus leaving a closing descendant shouldn't dismiss its ancestors. */
101
+ isClosingAncestor(node) {
102
+ const id = this.layerFor(node)?.id;
103
+ return !!id && this.closingAncestors.has(id);
104
+ },
105
+ isInBranch(target) {
106
+ return Array.from(this.branches).some((branch) => (0, _zag_js_dom_query.contains)(branch, target));
107
+ },
108
+ add(layer) {
109
+ const existingIndex = this.indexOf(layer.node);
110
+ const existing = existingIndex !== -1 ? this.layers[existingIndex] : void 0;
111
+ const pending = this.pendingReattach.get(layer.node);
112
+ if (existing) {
113
+ layer.id ?? (layer.id = existing.id);
114
+ layer.parentId = existing.parentId;
115
+ } else if (pending) {
116
+ layer.id ?? (layer.id = pending.id);
117
+ layer.parentId = pending.parentId;
118
+ } else {
119
+ layer.id ?? (layer.id = `layer-${++layerId}`);
120
+ const peer = layer.group ? this.peerFor(layer.group) : void 0;
121
+ layer.parentId = peer ? peer.parentId : layer.parentId ?? this.parentFor(layer)?.id;
122
+ }
123
+ const adopted = existing || pending ? [] : this.adopt(layer);
124
+ if (existing) this.layers[existingIndex] = layer;
125
+ else if (pending) {
126
+ this.pendingReattach.delete(layer.node);
127
+ this.layers.splice(Math.min(pending.index, this.count()), 0, layer);
128
+ } else if (adopted.length) this.layers.splice(this.indexOf(adopted[0].node), 0, layer);
129
+ else this.layers.push(layer);
130
+ this.syncLayers();
131
+ },
132
+ addBranch(node) {
133
+ this.branches.push(node);
134
+ },
135
+ remove(node, options) {
136
+ const index = this.indexOf(node);
137
+ if (index < 0) return;
138
+ const layer = this.layers[index];
139
+ if (options?.reattach) this.pendingReattach.set(node, {
140
+ id: layer.id,
141
+ parentId: layer.parentId,
142
+ index
143
+ });
144
+ else {
145
+ for (const id of ancestorIds(layer, indexById(this.layers))) this.closingAncestors.add(id);
146
+ cancelClosingAncestors?.();
147
+ cancelClosingAncestors = (0, _zag_js_dom_query.nextTick)(() => {
148
+ cancelClosingAncestors = void 0;
149
+ this.closingAncestors.clear();
150
+ });
151
+ this.dismissing.delete(node);
152
+ const nested = this.getNestedLayers(node);
153
+ nested.forEach((layer) => this.dismissing.add(layer.node));
154
+ if (nested.length) queueMicrotask(() => nested.forEach((layer) => this.dismiss(layer.node, node)));
155
+ for (const child of this.layers) if (child.parentId === layer.id) child.parentId = layer.parentId;
156
+ }
157
+ this.layers.splice(index, 1);
158
+ if (layer.snapshot) publishSnapshot(layer, {
159
+ ...layer.snapshot,
160
+ active: false,
161
+ blocked: false
162
+ });
163
+ this.syncLayers();
164
+ },
165
+ removeBranch(node) {
166
+ const index = this.branches.indexOf(node);
167
+ if (index >= 0) this.branches.splice(index, 1);
168
+ },
169
+ syncLayers() {
170
+ const byId = indexById(this.layers);
171
+ const nestedCounts = /* @__PURE__ */ new Map();
172
+ const nestedInSameType = /* @__PURE__ */ new Set();
173
+ for (const layer of this.layers) for (const parentId of ancestorIds(layer, byId)) {
174
+ const parent = byId.get(parentId);
175
+ if (!parent) break;
176
+ if (parent.type !== layer.type) continue;
177
+ nestedCounts.set(parentId, (nestedCounts.get(parentId) ?? 0) + 1);
178
+ nestedInSameType.add(layer);
179
+ }
180
+ const highestBlockingIndex = this.topMostPointerBlockingIndex();
181
+ this.layers.forEach((layer, index) => {
182
+ const nestedCount = nestedCounts.get(layer.id) ?? 0;
183
+ const snapshot = {
184
+ active: true,
185
+ type: layer.type,
186
+ index,
187
+ nested: nestedInSameType.has(layer),
188
+ hasNested: nestedCount > 0,
189
+ nestedCount,
190
+ blocked: index < highestBlockingIndex
191
+ };
192
+ if (!(0, _zag_js_utils.isEqual)(layer.snapshot, snapshot)) publishSnapshot(layer, snapshot);
193
+ });
194
+ },
195
+ indexOf(node) {
196
+ return this.layers.findIndex((layer) => layer.node === node);
197
+ },
198
+ dismiss(node, parent) {
199
+ const index = this.indexOf(node);
200
+ if (index === -1) return;
201
+ const layer = this.layers[index];
202
+ const event = fireCustomEvent(node, LAYER_REQUEST_DISMISS_EVENT, {
203
+ originalLayer: node,
204
+ targetLayer: parent,
205
+ originalIndex: index,
206
+ targetIndex: parent ? this.indexOf(parent) : -1
207
+ });
208
+ layer.requestDismiss?.(event);
209
+ if (event.defaultPrevented) this.dismissing.delete(node);
210
+ else layer.dismiss();
211
+ }
213
212
  };
214
213
  function publishSnapshot(layer, snapshot) {
215
- layer.snapshot = snapshot;
216
- layer.onLayerChange?.(snapshot);
214
+ layer.snapshot = snapshot;
215
+ layer.onLayerChange?.(snapshot);
217
216
  }
218
217
  function fireCustomEvent(el, type, detail) {
219
- const win = el.ownerDocument.defaultView || window;
220
- const event = new win.CustomEvent(type, { cancelable: true, bubbles: true, detail });
221
- return el.dispatchEvent(event);
218
+ const event = new (el.ownerDocument.defaultView || window).CustomEvent(type, {
219
+ cancelable: true,
220
+ bubbles: true,
221
+ detail
222
+ });
223
+ el.dispatchEvent(event);
224
+ return event;
222
225
  }
223
- function addListenerOnce(el, type, callback) {
224
- el.addEventListener(type, callback, { once: true });
225
- }
226
- // Annotate the CommonJS export names for ESM import in node:
227
- 0 && (module.exports = {
228
- isPointerBlocking,
229
- layerStack
230
- });
226
+ exports.isPointerBlocking = isPointerBlocking;
227
+ exports.layerStack = layerStack;