@zag-js/dismissable 0.10.2 → 0.10.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,18 +1,16 @@
1
- import { InteractOutsideHandlers } from '@zag-js/interact-outside';
2
-
1
+ import { InteractOutsideHandlers } from "@zag-js/interact-outside";
3
2
  type MaybeElement = HTMLElement | null;
4
3
  type Container = MaybeElement | Array<MaybeElement>;
5
4
  type NodeOrFn = MaybeElement | (() => MaybeElement);
6
- type DismissableElementHandlers = InteractOutsideHandlers & {
5
+ export type DismissableElementHandlers = InteractOutsideHandlers & {
7
6
  onEscapeKeyDown?: (event: KeyboardEvent) => void;
8
7
  };
9
- type DismissableElementOptions = DismissableElementHandlers & {
8
+ export type DismissableElementOptions = DismissableElementHandlers & {
10
9
  debug?: boolean;
11
10
  pointerBlocking?: boolean;
12
11
  onDismiss: () => void;
13
12
  exclude?: Container | (() => Container);
14
13
  defer?: boolean;
15
14
  };
16
- declare function trackDismissableElement(nodeOrFn: NodeOrFn, options: DismissableElementOptions): () => void;
17
-
18
- export { DismissableElementHandlers, DismissableElementOptions, trackDismissableElement };
15
+ export declare function trackDismissableElement(nodeOrFn: NodeOrFn, options: DismissableElementOptions): () => void;
16
+ export {};
@@ -1,152 +1,26 @@
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/dismissable-layer.ts
21
- var dismissable_layer_exports = {};
22
- __export(dismissable_layer_exports, {
23
- trackDismissableElement: () => trackDismissableElement
24
- });
25
- module.exports = __toCommonJS(dismissable_layer_exports);
26
- var import_dom_query4 = require("@zag-js/dom-query");
27
- var import_interact_outside = require("@zag-js/interact-outside");
28
- var import_utils = require("@zag-js/utils");
29
-
30
- // src/escape-keydown.ts
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) {
34
- const handleKeyDown = (event) => {
35
- if (event.key === "Escape")
36
- fn?.(event);
37
- };
38
- return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
39
- }
1
+ 'use strict';
40
2
 
41
- // src/layer-stack.ts
42
- var import_dom_query2 = require("@zag-js/dom-query");
43
- var layerStack = {
44
- layers: [],
45
- branches: [],
46
- count() {
47
- return this.layers.length;
48
- },
49
- pointerBlockingLayers() {
50
- return this.layers.filter((layer) => layer.pointerBlocking);
51
- },
52
- topMostPointerBlockingLayer() {
53
- return [...this.pointerBlockingLayers()].slice(-1)[0];
54
- },
55
- hasPointerBlockingLayer() {
56
- return this.pointerBlockingLayers().length > 0;
57
- },
58
- isBelowPointerBlockingLayer(node) {
59
- const index = this.indexOf(node);
60
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
61
- return index < highestBlockingIndex;
62
- },
63
- isTopMost(node) {
64
- const layer = this.layers[this.count() - 1];
65
- return layer?.node === node;
66
- },
67
- getNestedLayers(node) {
68
- return Array.from(this.layers).slice(this.indexOf(node) + 1);
69
- },
70
- isInNestedLayer(node, target) {
71
- return this.getNestedLayers(node).some((layer) => (0, import_dom_query2.contains)(layer.node, target));
72
- },
73
- isInBranch(target) {
74
- return Array.from(this.branches).some((branch) => (0, import_dom_query2.contains)(branch, target));
75
- },
76
- add(layer) {
77
- this.layers.push(layer);
78
- },
79
- addBranch(node) {
80
- this.branches.push(node);
81
- },
82
- remove(node) {
83
- const index = this.indexOf(node);
84
- if (index < 0)
85
- return;
86
- if (index < this.count() - 1) {
87
- const _layers = this.getNestedLayers(node);
88
- _layers.forEach((layer) => layer.dismiss());
89
- }
90
- this.layers.splice(index, 1);
91
- },
92
- removeBranch(node) {
93
- const index = this.branches.indexOf(node);
94
- if (index >= 0)
95
- this.branches.splice(index, 1);
96
- },
97
- indexOf(node) {
98
- return this.layers.findIndex((layer) => layer.node === node);
99
- },
100
- dismiss(node) {
101
- this.layers[this.indexOf(node)]?.dismiss();
102
- },
103
- clear() {
104
- this.remove(this.layers[0].node);
105
- }
106
- };
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
107
4
 
108
- // src/pointer-event-outside.ts
109
- var import_dom_query3 = require("@zag-js/dom-query");
110
- var originalBodyPointerEvents;
111
- function assignPointerEventToLayers() {
112
- layerStack.layers.forEach(({ node }) => {
113
- node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
114
- });
115
- }
116
- function clearPointerEvent(node) {
117
- node.style.pointerEvents = "";
118
- }
119
- var DATA_ATTR = "data-inert";
120
- function disablePointerEventsOutside(node) {
121
- const doc = (0, import_dom_query3.getDocument)(node);
122
- if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
123
- originalBodyPointerEvents = document.body.style.pointerEvents;
124
- doc.body.style.pointerEvents = "none";
125
- doc.body.setAttribute(DATA_ATTR, "");
126
- }
127
- return () => {
128
- if (layerStack.hasPointerBlockingLayer())
129
- return;
130
- doc.body.style.pointerEvents = originalBodyPointerEvents;
131
- doc.body.removeAttribute(DATA_ATTR);
132
- if (doc.body.style.length === 0)
133
- doc.body.removeAttribute("style");
134
- };
135
- }
5
+ const domQuery = require('@zag-js/dom-query');
6
+ const interactOutside = require('@zag-js/interact-outside');
7
+ const utils = require('@zag-js/utils');
8
+ const escapeKeydown = require('./escape-keydown.js');
9
+ const layerStack = require('./layer-stack.js');
10
+ const pointerEventOutside = require('./pointer-event-outside.js');
136
11
 
137
- // src/dismissable-layer.ts
138
12
  function trackDismissableElementImpl(node, options) {
139
13
  if (!node) {
140
- (0, import_utils.warn)("[@zag-js/dismissable] node is `null` or `undefined`");
14
+ utils.warn("[@zag-js/dismissable] node is `null` or `undefined`");
141
15
  return;
142
16
  }
143
17
  const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
144
18
  const layer = { dismiss: onDismiss, node, pointerBlocking };
145
- layerStack.add(layer);
146
- assignPointerEventToLayers();
19
+ layerStack.layerStack.add(layer);
20
+ pointerEventOutside.assignPointerEventToLayers();
147
21
  function onPointerDownOutside(event) {
148
- const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
149
- if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
22
+ const target = domQuery.getEventTarget(event.detail.originalEvent);
23
+ if (layerStack.layerStack.isBelowPointerBlockingLayer(node) || layerStack.layerStack.isInBranch(target))
150
24
  return;
151
25
  options.onPointerDownOutside?.(event);
152
26
  options.onInteractOutside?.(event);
@@ -158,8 +32,8 @@ function trackDismissableElementImpl(node, options) {
158
32
  onDismiss?.();
159
33
  }
160
34
  function onFocusOutside(event) {
161
- const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
162
- if (layerStack.isInBranch(target))
35
+ const target = domQuery.getEventTarget(event.detail.originalEvent);
36
+ if (layerStack.layerStack.isInBranch(target))
163
37
  return;
164
38
  options.onFocusOutside?.(event);
165
39
  options.onInteractOutside?.(event);
@@ -171,7 +45,7 @@ function trackDismissableElementImpl(node, options) {
171
45
  onDismiss?.();
172
46
  }
173
47
  function onEscapeKeyDown(event) {
174
- if (!layerStack.isTopMost(node))
48
+ if (!layerStack.layerStack.isTopMost(node))
175
49
  return;
176
50
  options.onEscapeKeyDown?.(event);
177
51
  if (!event.defaultPrevented && onDismiss) {
@@ -184,23 +58,23 @@ function trackDismissableElementImpl(node, options) {
184
58
  return false;
185
59
  const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
186
60
  const _containers = Array.isArray(containers) ? containers : [containers];
187
- return _containers.some((node2) => (0, import_dom_query4.contains)(node2, target)) || layerStack.isInNestedLayer(node, target);
61
+ return _containers.some((node2) => domQuery.contains(node2, target)) || layerStack.layerStack.isInNestedLayer(node, target);
188
62
  }
189
63
  const cleanups = [
190
- pointerBlocking ? disablePointerEventsOutside(node) : void 0,
191
- trackEscapeKeydown(node, onEscapeKeyDown),
192
- (0, import_interact_outside.trackInteractOutside)(node, { exclude, onFocusOutside, onPointerDownOutside })
64
+ pointerBlocking ? pointerEventOutside.disablePointerEventsOutside(node) : void 0,
65
+ escapeKeydown.trackEscapeKeydown(node, onEscapeKeyDown),
66
+ interactOutside.trackInteractOutside(node, { exclude, onFocusOutside, onPointerDownOutside })
193
67
  ];
194
68
  return () => {
195
- layerStack.remove(node);
196
- assignPointerEventToLayers();
197
- clearPointerEvent(node);
69
+ layerStack.layerStack.remove(node);
70
+ pointerEventOutside.assignPointerEventToLayers();
71
+ pointerEventOutside.clearPointerEvent(node);
198
72
  cleanups.forEach((fn) => fn?.());
199
73
  };
200
74
  }
201
75
  function trackDismissableElement(nodeOrFn, options) {
202
76
  const { defer } = options;
203
- const func = defer ? import_dom_query4.raf : (v) => v();
77
+ const func = defer ? domQuery.raf : (v) => v();
204
78
  const cleanups = [];
205
79
  cleanups.push(
206
80
  func(() => {
@@ -212,7 +86,5 @@ function trackDismissableElement(nodeOrFn, options) {
212
86
  cleanups.forEach((fn) => fn?.());
213
87
  };
214
88
  }
215
- // Annotate the CommonJS export names for ESM import in node:
216
- 0 && (module.exports = {
217
- trackDismissableElement
218
- });
89
+
90
+ exports.trackDismissableElement = trackDismissableElement;
@@ -1,9 +1,86 @@
1
- import {
2
- trackDismissableElement
3
- } from "./chunk-HTX46HNU.mjs";
4
- import "./chunk-BBFIKTYR.mjs";
5
- import "./chunk-C5KU67MN.mjs";
6
- import "./chunk-G4GHYT27.mjs";
7
- export {
8
- trackDismissableElement
9
- };
1
+ import { raf, getEventTarget, contains } from '@zag-js/dom-query';
2
+ import { trackInteractOutside } from '@zag-js/interact-outside';
3
+ import { warn } from '@zag-js/utils';
4
+ import { trackEscapeKeydown } from './escape-keydown.mjs';
5
+ import { layerStack } from './layer-stack.mjs';
6
+ import { assignPointerEventToLayers, disablePointerEventsOutside, clearPointerEvent } from './pointer-event-outside.mjs';
7
+
8
+ function trackDismissableElementImpl(node, options) {
9
+ if (!node) {
10
+ warn("[@zag-js/dismissable] node is `null` or `undefined`");
11
+ return;
12
+ }
13
+ const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
14
+ const layer = { dismiss: onDismiss, node, pointerBlocking };
15
+ layerStack.add(layer);
16
+ assignPointerEventToLayers();
17
+ function onPointerDownOutside(event) {
18
+ const target = getEventTarget(event.detail.originalEvent);
19
+ if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
20
+ return;
21
+ options.onPointerDownOutside?.(event);
22
+ options.onInteractOutside?.(event);
23
+ if (event.defaultPrevented)
24
+ return;
25
+ if (debug) {
26
+ console.log("onPointerDownOutside:", event.detail.originalEvent);
27
+ }
28
+ onDismiss?.();
29
+ }
30
+ function onFocusOutside(event) {
31
+ const target = getEventTarget(event.detail.originalEvent);
32
+ if (layerStack.isInBranch(target))
33
+ return;
34
+ options.onFocusOutside?.(event);
35
+ options.onInteractOutside?.(event);
36
+ if (event.defaultPrevented)
37
+ return;
38
+ if (debug) {
39
+ console.log("onFocusOutside:", event.detail.originalEvent);
40
+ }
41
+ onDismiss?.();
42
+ }
43
+ function onEscapeKeyDown(event) {
44
+ if (!layerStack.isTopMost(node))
45
+ return;
46
+ options.onEscapeKeyDown?.(event);
47
+ if (!event.defaultPrevented && onDismiss) {
48
+ event.preventDefault();
49
+ onDismiss();
50
+ }
51
+ }
52
+ function exclude(target) {
53
+ if (!node)
54
+ return false;
55
+ const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
56
+ const _containers = Array.isArray(containers) ? containers : [containers];
57
+ return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
58
+ }
59
+ const cleanups = [
60
+ pointerBlocking ? disablePointerEventsOutside(node) : void 0,
61
+ trackEscapeKeydown(node, onEscapeKeyDown),
62
+ trackInteractOutside(node, { exclude, onFocusOutside, onPointerDownOutside })
63
+ ];
64
+ return () => {
65
+ layerStack.remove(node);
66
+ assignPointerEventToLayers();
67
+ clearPointerEvent(node);
68
+ cleanups.forEach((fn) => fn?.());
69
+ };
70
+ }
71
+ function trackDismissableElement(nodeOrFn, options) {
72
+ const { defer } = options;
73
+ const func = defer ? raf : (v) => v();
74
+ const cleanups = [];
75
+ cleanups.push(
76
+ func(() => {
77
+ const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
78
+ cleanups.push(trackDismissableElementImpl(node, options));
79
+ })
80
+ );
81
+ return () => {
82
+ cleanups.forEach((fn) => fn?.());
83
+ };
84
+ }
85
+
86
+ export { trackDismissableElement };
@@ -1,3 +1 @@
1
- declare function trackEscapeKeydown(node: HTMLElement, fn?: (event: KeyboardEvent) => void): () => void;
2
-
3
- export { trackEscapeKeydown };
1
+ export declare function trackEscapeKeydown(node: HTMLElement, fn?: (event: KeyboardEvent) => void): () => void;
@@ -1,38 +1,16 @@
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);
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const domEvent = require('@zag-js/dom-event');
6
+ const domQuery = require('@zag-js/dom-query');
19
7
 
20
- // src/escape-keydown.ts
21
- var escape_keydown_exports = {};
22
- __export(escape_keydown_exports, {
23
- trackEscapeKeydown: () => trackEscapeKeydown
24
- });
25
- module.exports = __toCommonJS(escape_keydown_exports);
26
- var import_dom_event = require("@zag-js/dom-event");
27
- var import_dom_query = require("@zag-js/dom-query");
28
8
  function trackEscapeKeydown(node, fn) {
29
9
  const handleKeyDown = (event) => {
30
10
  if (event.key === "Escape")
31
11
  fn?.(event);
32
12
  };
33
- return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
13
+ return domEvent.addDomEvent(domQuery.getDocument(node), "keydown", handleKeyDown);
34
14
  }
35
- // Annotate the CommonJS export names for ESM import in node:
36
- 0 && (module.exports = {
37
- trackEscapeKeydown
38
- });
15
+
16
+ exports.trackEscapeKeydown = trackEscapeKeydown;
@@ -1,6 +1,12 @@
1
- import {
2
- trackEscapeKeydown
3
- } from "./chunk-BBFIKTYR.mjs";
4
- export {
5
- trackEscapeKeydown
6
- };
1
+ import { addDomEvent } from '@zag-js/dom-event';
2
+ import { getDocument } from '@zag-js/dom-query';
3
+
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 { trackEscapeKeydown };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { DismissableElementHandlers, DismissableElementOptions, trackDismissableElement } from './dismissable-layer.js';
2
- export { FocusOutsideEvent, InteractOutsideEvent, PointerDownOutsideEvent } from '@zag-js/interact-outside';
1
+ export * from "./dismissable-layer";
2
+ export type { InteractOutsideEvent, PointerDownOutsideEvent, FocusOutsideEvent } from "@zag-js/interact-outside";
package/dist/index.js CHANGED
@@ -1,220 +1,9 @@
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);
1
+ 'use strict';
19
2
 
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- trackDismissableElement: () => trackDismissableElement
24
- });
25
- module.exports = __toCommonJS(src_exports);
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
26
4
 
27
- // src/dismissable-layer.ts
28
- var import_dom_query4 = require("@zag-js/dom-query");
29
- var import_interact_outside = require("@zag-js/interact-outside");
30
- var import_utils = require("@zag-js/utils");
5
+ const dismissableLayer = require('./dismissable-layer.js');
31
6
 
32
- // src/escape-keydown.ts
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) {
36
- const handleKeyDown = (event) => {
37
- if (event.key === "Escape")
38
- fn?.(event);
39
- };
40
- return (0, import_dom_event.addDomEvent)((0, import_dom_query.getDocument)(node), "keydown", handleKeyDown);
41
- }
42
7
 
43
- // src/layer-stack.ts
44
- var import_dom_query2 = require("@zag-js/dom-query");
45
- var layerStack = {
46
- layers: [],
47
- branches: [],
48
- count() {
49
- return this.layers.length;
50
- },
51
- pointerBlockingLayers() {
52
- return this.layers.filter((layer) => layer.pointerBlocking);
53
- },
54
- topMostPointerBlockingLayer() {
55
- return [...this.pointerBlockingLayers()].slice(-1)[0];
56
- },
57
- hasPointerBlockingLayer() {
58
- return this.pointerBlockingLayers().length > 0;
59
- },
60
- isBelowPointerBlockingLayer(node) {
61
- const index = this.indexOf(node);
62
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
63
- return index < highestBlockingIndex;
64
- },
65
- isTopMost(node) {
66
- const layer = this.layers[this.count() - 1];
67
- return layer?.node === node;
68
- },
69
- getNestedLayers(node) {
70
- return Array.from(this.layers).slice(this.indexOf(node) + 1);
71
- },
72
- isInNestedLayer(node, target) {
73
- return this.getNestedLayers(node).some((layer) => (0, import_dom_query2.contains)(layer.node, target));
74
- },
75
- isInBranch(target) {
76
- return Array.from(this.branches).some((branch) => (0, import_dom_query2.contains)(branch, target));
77
- },
78
- add(layer) {
79
- this.layers.push(layer);
80
- },
81
- addBranch(node) {
82
- this.branches.push(node);
83
- },
84
- remove(node) {
85
- const index = this.indexOf(node);
86
- if (index < 0)
87
- return;
88
- if (index < this.count() - 1) {
89
- const _layers = this.getNestedLayers(node);
90
- _layers.forEach((layer) => layer.dismiss());
91
- }
92
- this.layers.splice(index, 1);
93
- },
94
- removeBranch(node) {
95
- const index = this.branches.indexOf(node);
96
- if (index >= 0)
97
- this.branches.splice(index, 1);
98
- },
99
- indexOf(node) {
100
- return this.layers.findIndex((layer) => layer.node === node);
101
- },
102
- dismiss(node) {
103
- this.layers[this.indexOf(node)]?.dismiss();
104
- },
105
- clear() {
106
- this.remove(this.layers[0].node);
107
- }
108
- };
109
8
 
110
- // src/pointer-event-outside.ts
111
- var import_dom_query3 = require("@zag-js/dom-query");
112
- var originalBodyPointerEvents;
113
- function assignPointerEventToLayers() {
114
- layerStack.layers.forEach(({ node }) => {
115
- node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
116
- });
117
- }
118
- function clearPointerEvent(node) {
119
- node.style.pointerEvents = "";
120
- }
121
- var DATA_ATTR = "data-inert";
122
- function disablePointerEventsOutside(node) {
123
- const doc = (0, import_dom_query3.getDocument)(node);
124
- if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
125
- originalBodyPointerEvents = document.body.style.pointerEvents;
126
- doc.body.style.pointerEvents = "none";
127
- doc.body.setAttribute(DATA_ATTR, "");
128
- }
129
- return () => {
130
- if (layerStack.hasPointerBlockingLayer())
131
- return;
132
- doc.body.style.pointerEvents = originalBodyPointerEvents;
133
- doc.body.removeAttribute(DATA_ATTR);
134
- if (doc.body.style.length === 0)
135
- doc.body.removeAttribute("style");
136
- };
137
- }
138
-
139
- // src/dismissable-layer.ts
140
- function trackDismissableElementImpl(node, options) {
141
- if (!node) {
142
- (0, import_utils.warn)("[@zag-js/dismissable] node is `null` or `undefined`");
143
- return;
144
- }
145
- const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
146
- const layer = { dismiss: onDismiss, node, pointerBlocking };
147
- layerStack.add(layer);
148
- assignPointerEventToLayers();
149
- function onPointerDownOutside(event) {
150
- const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
151
- if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
152
- return;
153
- options.onPointerDownOutside?.(event);
154
- options.onInteractOutside?.(event);
155
- if (event.defaultPrevented)
156
- return;
157
- if (debug) {
158
- console.log("onPointerDownOutside:", event.detail.originalEvent);
159
- }
160
- onDismiss?.();
161
- }
162
- function onFocusOutside(event) {
163
- const target = (0, import_dom_query4.getEventTarget)(event.detail.originalEvent);
164
- if (layerStack.isInBranch(target))
165
- return;
166
- options.onFocusOutside?.(event);
167
- options.onInteractOutside?.(event);
168
- if (event.defaultPrevented)
169
- return;
170
- if (debug) {
171
- console.log("onFocusOutside:", event.detail.originalEvent);
172
- }
173
- onDismiss?.();
174
- }
175
- function onEscapeKeyDown(event) {
176
- if (!layerStack.isTopMost(node))
177
- return;
178
- options.onEscapeKeyDown?.(event);
179
- if (!event.defaultPrevented && onDismiss) {
180
- event.preventDefault();
181
- onDismiss();
182
- }
183
- }
184
- function exclude(target) {
185
- if (!node)
186
- return false;
187
- const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
188
- const _containers = Array.isArray(containers) ? containers : [containers];
189
- return _containers.some((node2) => (0, import_dom_query4.contains)(node2, target)) || layerStack.isInNestedLayer(node, target);
190
- }
191
- const cleanups = [
192
- pointerBlocking ? disablePointerEventsOutside(node) : void 0,
193
- trackEscapeKeydown(node, onEscapeKeyDown),
194
- (0, import_interact_outside.trackInteractOutside)(node, { exclude, onFocusOutside, onPointerDownOutside })
195
- ];
196
- return () => {
197
- layerStack.remove(node);
198
- assignPointerEventToLayers();
199
- clearPointerEvent(node);
200
- cleanups.forEach((fn) => fn?.());
201
- };
202
- }
203
- function trackDismissableElement(nodeOrFn, options) {
204
- const { defer } = options;
205
- const func = defer ? import_dom_query4.raf : (v) => v();
206
- const cleanups = [];
207
- cleanups.push(
208
- func(() => {
209
- const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
210
- cleanups.push(trackDismissableElementImpl(node, options));
211
- })
212
- );
213
- return () => {
214
- cleanups.forEach((fn) => fn?.());
215
- };
216
- }
217
- // Annotate the CommonJS export names for ESM import in node:
218
- 0 && (module.exports = {
219
- trackDismissableElement
220
- });
9
+ exports.trackDismissableElement = dismissableLayer.trackDismissableElement;
package/dist/index.mjs CHANGED
@@ -1,9 +1 @@
1
- import {
2
- trackDismissableElement
3
- } from "./chunk-HTX46HNU.mjs";
4
- import "./chunk-BBFIKTYR.mjs";
5
- import "./chunk-C5KU67MN.mjs";
6
- import "./chunk-G4GHYT27.mjs";
7
- export {
8
- trackDismissableElement
9
- };
1
+ export { trackDismissableElement } from './dismissable-layer.mjs';
@@ -1,9 +1,9 @@
1
- type Layer = {
1
+ export type Layer = {
2
2
  dismiss: VoidFunction;
3
3
  node: HTMLElement;
4
4
  pointerBlocking?: boolean;
5
5
  };
6
- declare const layerStack: {
6
+ export declare const layerStack: {
7
7
  layers: Layer[];
8
8
  branches: HTMLElement[];
9
9
  count(): number;
@@ -23,5 +23,3 @@ declare const layerStack: {
23
23
  dismiss(node: HTMLElement): void;
24
24
  clear(): void;
25
25
  };
26
-
27
- export { Layer, layerStack };
@@ -1,30 +1,10 @@
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);
1
+ 'use strict';
19
2
 
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
- var import_dom_query = require("@zag-js/dom-query");
27
- var layerStack = {
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const domQuery = require('@zag-js/dom-query');
6
+
7
+ const layerStack = {
28
8
  layers: [],
29
9
  branches: [],
30
10
  count() {
@@ -52,10 +32,10 @@ var layerStack = {
52
32
  return Array.from(this.layers).slice(this.indexOf(node) + 1);
53
33
  },
54
34
  isInNestedLayer(node, target) {
55
- return this.getNestedLayers(node).some((layer) => (0, import_dom_query.contains)(layer.node, target));
35
+ return this.getNestedLayers(node).some((layer) => domQuery.contains(layer.node, target));
56
36
  },
57
37
  isInBranch(target) {
58
- return Array.from(this.branches).some((branch) => (0, import_dom_query.contains)(branch, target));
38
+ return Array.from(this.branches).some((branch) => domQuery.contains(branch, target));
59
39
  },
60
40
  add(layer) {
61
41
  this.layers.push(layer);
@@ -88,7 +68,5 @@ var layerStack = {
88
68
  this.remove(this.layers[0].node);
89
69
  }
90
70
  };
91
- // Annotate the CommonJS export names for ESM import in node:
92
- 0 && (module.exports = {
93
- layerStack
94
- });
71
+
72
+ exports.layerStack = layerStack;
@@ -1,6 +1,68 @@
1
- import {
2
- layerStack
3
- } from "./chunk-G4GHYT27.mjs";
4
- export {
5
- layerStack
1
+ import { contains } from '@zag-js/dom-query';
2
+
3
+ const layerStack = {
4
+ layers: [],
5
+ branches: [],
6
+ count() {
7
+ return this.layers.length;
8
+ },
9
+ pointerBlockingLayers() {
10
+ return this.layers.filter((layer) => layer.pointerBlocking);
11
+ },
12
+ topMostPointerBlockingLayer() {
13
+ return [...this.pointerBlockingLayers()].slice(-1)[0];
14
+ },
15
+ hasPointerBlockingLayer() {
16
+ return this.pointerBlockingLayers().length > 0;
17
+ },
18
+ isBelowPointerBlockingLayer(node) {
19
+ const index = this.indexOf(node);
20
+ const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
21
+ return index < highestBlockingIndex;
22
+ },
23
+ isTopMost(node) {
24
+ const layer = this.layers[this.count() - 1];
25
+ return layer?.node === node;
26
+ },
27
+ getNestedLayers(node) {
28
+ return Array.from(this.layers).slice(this.indexOf(node) + 1);
29
+ },
30
+ isInNestedLayer(node, target) {
31
+ return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
32
+ },
33
+ isInBranch(target) {
34
+ return Array.from(this.branches).some((branch) => contains(branch, target));
35
+ },
36
+ add(layer) {
37
+ this.layers.push(layer);
38
+ },
39
+ addBranch(node) {
40
+ this.branches.push(node);
41
+ },
42
+ remove(node) {
43
+ const index = this.indexOf(node);
44
+ if (index < 0)
45
+ return;
46
+ if (index < this.count() - 1) {
47
+ const _layers = this.getNestedLayers(node);
48
+ _layers.forEach((layer) => layer.dismiss());
49
+ }
50
+ this.layers.splice(index, 1);
51
+ },
52
+ removeBranch(node) {
53
+ const index = this.branches.indexOf(node);
54
+ if (index >= 0)
55
+ this.branches.splice(index, 1);
56
+ },
57
+ indexOf(node) {
58
+ return this.layers.findIndex((layer) => layer.node === node);
59
+ },
60
+ dismiss(node) {
61
+ this.layers[this.indexOf(node)]?.dismiss();
62
+ },
63
+ clear() {
64
+ this.remove(this.layers[0].node);
65
+ }
6
66
  };
67
+
68
+ export { layerStack };
@@ -1,5 +1,3 @@
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 };
1
+ export declare function assignPointerEventToLayers(): void;
2
+ export declare function clearPointerEvent(node: HTMLElement): void;
3
+ export declare function disablePointerEventsOutside(node: HTMLElement): () => void;
@@ -1,119 +1,29 @@
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);
1
+ 'use strict';
19
2
 
20
- // src/pointer-event-outside.ts
21
- var pointer_event_outside_exports = {};
22
- __export(pointer_event_outside_exports, {
23
- assignPointerEventToLayers: () => assignPointerEventToLayers,
24
- clearPointerEvent: () => clearPointerEvent,
25
- disablePointerEventsOutside: () => disablePointerEventsOutside
26
- });
27
- module.exports = __toCommonJS(pointer_event_outside_exports);
28
- var import_dom_query2 = require("@zag-js/dom-query");
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
29
4
 
30
- // src/layer-stack.ts
31
- var import_dom_query = require("@zag-js/dom-query");
32
- var layerStack = {
33
- layers: [],
34
- branches: [],
35
- count() {
36
- return this.layers.length;
37
- },
38
- pointerBlockingLayers() {
39
- return this.layers.filter((layer) => layer.pointerBlocking);
40
- },
41
- topMostPointerBlockingLayer() {
42
- return [...this.pointerBlockingLayers()].slice(-1)[0];
43
- },
44
- hasPointerBlockingLayer() {
45
- return this.pointerBlockingLayers().length > 0;
46
- },
47
- isBelowPointerBlockingLayer(node) {
48
- const index = this.indexOf(node);
49
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
50
- return index < highestBlockingIndex;
51
- },
52
- isTopMost(node) {
53
- const layer = this.layers[this.count() - 1];
54
- return layer?.node === node;
55
- },
56
- getNestedLayers(node) {
57
- return Array.from(this.layers).slice(this.indexOf(node) + 1);
58
- },
59
- isInNestedLayer(node, target) {
60
- return this.getNestedLayers(node).some((layer) => (0, import_dom_query.contains)(layer.node, target));
61
- },
62
- isInBranch(target) {
63
- return Array.from(this.branches).some((branch) => (0, import_dom_query.contains)(branch, target));
64
- },
65
- add(layer) {
66
- this.layers.push(layer);
67
- },
68
- addBranch(node) {
69
- this.branches.push(node);
70
- },
71
- remove(node) {
72
- const index = this.indexOf(node);
73
- if (index < 0)
74
- return;
75
- if (index < this.count() - 1) {
76
- const _layers = this.getNestedLayers(node);
77
- _layers.forEach((layer) => layer.dismiss());
78
- }
79
- this.layers.splice(index, 1);
80
- },
81
- removeBranch(node) {
82
- const index = this.branches.indexOf(node);
83
- if (index >= 0)
84
- this.branches.splice(index, 1);
85
- },
86
- indexOf(node) {
87
- return this.layers.findIndex((layer) => layer.node === node);
88
- },
89
- dismiss(node) {
90
- this.layers[this.indexOf(node)]?.dismiss();
91
- },
92
- clear() {
93
- this.remove(this.layers[0].node);
94
- }
95
- };
5
+ const domQuery = require('@zag-js/dom-query');
6
+ const layerStack = require('./layer-stack.js');
96
7
 
97
- // src/pointer-event-outside.ts
98
- var originalBodyPointerEvents;
8
+ let originalBodyPointerEvents;
99
9
  function assignPointerEventToLayers() {
100
- layerStack.layers.forEach(({ node }) => {
101
- node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
10
+ layerStack.layerStack.layers.forEach(({ node }) => {
11
+ node.style.pointerEvents = layerStack.layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
102
12
  });
103
13
  }
104
14
  function clearPointerEvent(node) {
105
15
  node.style.pointerEvents = "";
106
16
  }
107
- var DATA_ATTR = "data-inert";
17
+ const DATA_ATTR = "data-inert";
108
18
  function disablePointerEventsOutside(node) {
109
- const doc = (0, import_dom_query2.getDocument)(node);
110
- if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
19
+ const doc = domQuery.getDocument(node);
20
+ if (layerStack.layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
111
21
  originalBodyPointerEvents = document.body.style.pointerEvents;
112
22
  doc.body.style.pointerEvents = "none";
113
23
  doc.body.setAttribute(DATA_ATTR, "");
114
24
  }
115
25
  return () => {
116
- if (layerStack.hasPointerBlockingLayer())
26
+ if (layerStack.layerStack.hasPointerBlockingLayer())
117
27
  return;
118
28
  doc.body.style.pointerEvents = originalBodyPointerEvents;
119
29
  doc.body.removeAttribute(DATA_ATTR);
@@ -121,9 +31,7 @@ function disablePointerEventsOutside(node) {
121
31
  doc.body.removeAttribute("style");
122
32
  };
123
33
  }
124
- // Annotate the CommonJS export names for ESM import in node:
125
- 0 && (module.exports = {
126
- assignPointerEventToLayers,
127
- clearPointerEvent,
128
- disablePointerEventsOutside
129
- });
34
+
35
+ exports.assignPointerEventToLayers = assignPointerEventToLayers;
36
+ exports.clearPointerEvent = clearPointerEvent;
37
+ exports.disablePointerEventsOutside = disablePointerEventsOutside;
@@ -1,11 +1,31 @@
1
- import {
2
- assignPointerEventToLayers,
3
- clearPointerEvent,
4
- disablePointerEventsOutside
5
- } from "./chunk-C5KU67MN.mjs";
6
- import "./chunk-G4GHYT27.mjs";
7
- export {
8
- assignPointerEventToLayers,
9
- clearPointerEvent,
10
- disablePointerEventsOutside
11
- };
1
+ import { getDocument } from '@zag-js/dom-query';
2
+ import { layerStack } from './layer-stack.mjs';
3
+
4
+ let originalBodyPointerEvents;
5
+ function assignPointerEventToLayers() {
6
+ layerStack.layers.forEach(({ node }) => {
7
+ node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
8
+ });
9
+ }
10
+ function clearPointerEvent(node) {
11
+ node.style.pointerEvents = "";
12
+ }
13
+ const DATA_ATTR = "data-inert";
14
+ function disablePointerEventsOutside(node) {
15
+ const doc = getDocument(node);
16
+ if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
17
+ originalBodyPointerEvents = document.body.style.pointerEvents;
18
+ doc.body.style.pointerEvents = "none";
19
+ doc.body.setAttribute(DATA_ATTR, "");
20
+ }
21
+ return () => {
22
+ if (layerStack.hasPointerBlockingLayer())
23
+ return;
24
+ doc.body.style.pointerEvents = originalBodyPointerEvents;
25
+ doc.body.removeAttribute(DATA_ATTR);
26
+ if (doc.body.style.length === 0)
27
+ doc.body.removeAttribute("style");
28
+ };
29
+ }
30
+
31
+ export { assignPointerEventToLayers, clearPointerEvent, disablePointerEventsOutside };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/dismissable",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "description": "Dismissable layer utilities for the DOM",
5
5
  "keywords": [
6
6
  "js",
@@ -24,10 +24,10 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@zag-js/interact-outside": "0.10.2",
28
- "@zag-js/dom-query": "0.10.2",
29
- "@zag-js/dom-event": "0.10.2",
30
- "@zag-js/utils": "0.10.2"
27
+ "@zag-js/interact-outside": "0.10.4",
28
+ "@zag-js/dom-query": "0.10.4",
29
+ "@zag-js/dom-event": "0.10.4",
30
+ "@zag-js/utils": "0.10.4"
31
31
  },
32
32
  "devDependencies": {
33
33
  "clean-package": "2.2.0"
@@ -48,9 +48,7 @@
48
48
  "./package.json": "./package.json"
49
49
  },
50
50
  "scripts": {
51
- "build-fast": "tsup src",
52
- "start": "pnpm build --watch",
53
- "build": "tsup src --dts",
51
+ "build": "vite build -c ../../../vite.config.ts",
54
52
  "test": "jest --config ../../../jest.config.js --rootDir tests",
55
53
  "lint": "eslint src --ext .ts,.tsx",
56
54
  "test-ci": "pnpm test --ci --runInBand -u",
@@ -1,14 +0,0 @@
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,38 +0,0 @@
1
- import {
2
- layerStack
3
- } from "./chunk-G4GHYT27.mjs";
4
-
5
- // src/pointer-event-outside.ts
6
- import { getDocument } from "@zag-js/dom-query";
7
- var originalBodyPointerEvents;
8
- function assignPointerEventToLayers() {
9
- layerStack.layers.forEach(({ node }) => {
10
- node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
11
- });
12
- }
13
- function clearPointerEvent(node) {
14
- node.style.pointerEvents = "";
15
- }
16
- var DATA_ATTR = "data-inert";
17
- function disablePointerEventsOutside(node) {
18
- const doc = getDocument(node);
19
- if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute(DATA_ATTR)) {
20
- originalBodyPointerEvents = document.body.style.pointerEvents;
21
- doc.body.style.pointerEvents = "none";
22
- doc.body.setAttribute(DATA_ATTR, "");
23
- }
24
- return () => {
25
- if (layerStack.hasPointerBlockingLayer())
26
- return;
27
- doc.body.style.pointerEvents = originalBodyPointerEvents;
28
- doc.body.removeAttribute(DATA_ATTR);
29
- if (doc.body.style.length === 0)
30
- doc.body.removeAttribute("style");
31
- };
32
- }
33
-
34
- export {
35
- assignPointerEventToLayers,
36
- clearPointerEvent,
37
- disablePointerEventsOutside
38
- };
@@ -1,70 +0,0 @@
1
- // src/layer-stack.ts
2
- import { contains } from "@zag-js/dom-query";
3
- var layerStack = {
4
- layers: [],
5
- branches: [],
6
- count() {
7
- return this.layers.length;
8
- },
9
- pointerBlockingLayers() {
10
- return this.layers.filter((layer) => layer.pointerBlocking);
11
- },
12
- topMostPointerBlockingLayer() {
13
- return [...this.pointerBlockingLayers()].slice(-1)[0];
14
- },
15
- hasPointerBlockingLayer() {
16
- return this.pointerBlockingLayers().length > 0;
17
- },
18
- isBelowPointerBlockingLayer(node) {
19
- const index = this.indexOf(node);
20
- const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
21
- return index < highestBlockingIndex;
22
- },
23
- isTopMost(node) {
24
- const layer = this.layers[this.count() - 1];
25
- return layer?.node === node;
26
- },
27
- getNestedLayers(node) {
28
- return Array.from(this.layers).slice(this.indexOf(node) + 1);
29
- },
30
- isInNestedLayer(node, target) {
31
- return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
32
- },
33
- isInBranch(target) {
34
- return Array.from(this.branches).some((branch) => contains(branch, target));
35
- },
36
- add(layer) {
37
- this.layers.push(layer);
38
- },
39
- addBranch(node) {
40
- this.branches.push(node);
41
- },
42
- remove(node) {
43
- const index = this.indexOf(node);
44
- if (index < 0)
45
- return;
46
- if (index < this.count() - 1) {
47
- const _layers = this.getNestedLayers(node);
48
- _layers.forEach((layer) => layer.dismiss());
49
- }
50
- this.layers.splice(index, 1);
51
- },
52
- removeBranch(node) {
53
- const index = this.branches.indexOf(node);
54
- if (index >= 0)
55
- this.branches.splice(index, 1);
56
- },
57
- indexOf(node) {
58
- return this.layers.findIndex((layer) => layer.node === node);
59
- },
60
- dismiss(node) {
61
- this.layers[this.indexOf(node)]?.dismiss();
62
- },
63
- clear() {
64
- this.remove(this.layers[0].node);
65
- }
66
- };
67
-
68
- export {
69
- layerStack
70
- };
@@ -1,99 +0,0 @@
1
- import {
2
- trackEscapeKeydown
3
- } from "./chunk-BBFIKTYR.mjs";
4
- import {
5
- assignPointerEventToLayers,
6
- clearPointerEvent,
7
- disablePointerEventsOutside
8
- } from "./chunk-C5KU67MN.mjs";
9
- import {
10
- layerStack
11
- } from "./chunk-G4GHYT27.mjs";
12
-
13
- // src/dismissable-layer.ts
14
- import { contains, getEventTarget, raf } from "@zag-js/dom-query";
15
- import {
16
- trackInteractOutside
17
- } from "@zag-js/interact-outside";
18
- import { warn } from "@zag-js/utils";
19
- function trackDismissableElementImpl(node, options) {
20
- if (!node) {
21
- warn("[@zag-js/dismissable] node is `null` or `undefined`");
22
- return;
23
- }
24
- const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
25
- const layer = { dismiss: onDismiss, node, pointerBlocking };
26
- layerStack.add(layer);
27
- assignPointerEventToLayers();
28
- function onPointerDownOutside(event) {
29
- const target = getEventTarget(event.detail.originalEvent);
30
- if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target))
31
- return;
32
- options.onPointerDownOutside?.(event);
33
- options.onInteractOutside?.(event);
34
- if (event.defaultPrevented)
35
- return;
36
- if (debug) {
37
- console.log("onPointerDownOutside:", event.detail.originalEvent);
38
- }
39
- onDismiss?.();
40
- }
41
- function onFocusOutside(event) {
42
- const target = getEventTarget(event.detail.originalEvent);
43
- if (layerStack.isInBranch(target))
44
- return;
45
- options.onFocusOutside?.(event);
46
- options.onInteractOutside?.(event);
47
- if (event.defaultPrevented)
48
- return;
49
- if (debug) {
50
- console.log("onFocusOutside:", event.detail.originalEvent);
51
- }
52
- onDismiss?.();
53
- }
54
- function onEscapeKeyDown(event) {
55
- if (!layerStack.isTopMost(node))
56
- return;
57
- options.onEscapeKeyDown?.(event);
58
- if (!event.defaultPrevented && onDismiss) {
59
- event.preventDefault();
60
- onDismiss();
61
- }
62
- }
63
- function exclude(target) {
64
- if (!node)
65
- return false;
66
- const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
67
- const _containers = Array.isArray(containers) ? containers : [containers];
68
- return _containers.some((node2) => contains(node2, target)) || layerStack.isInNestedLayer(node, target);
69
- }
70
- const cleanups = [
71
- pointerBlocking ? disablePointerEventsOutside(node) : void 0,
72
- trackEscapeKeydown(node, onEscapeKeyDown),
73
- trackInteractOutside(node, { exclude, onFocusOutside, onPointerDownOutside })
74
- ];
75
- return () => {
76
- layerStack.remove(node);
77
- assignPointerEventToLayers();
78
- clearPointerEvent(node);
79
- cleanups.forEach((fn) => fn?.());
80
- };
81
- }
82
- function trackDismissableElement(nodeOrFn, options) {
83
- const { defer } = options;
84
- const func = defer ? raf : (v) => v();
85
- const cleanups = [];
86
- cleanups.push(
87
- func(() => {
88
- const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
89
- cleanups.push(trackDismissableElementImpl(node, options));
90
- })
91
- );
92
- return () => {
93
- cleanups.forEach((fn) => fn?.());
94
- };
95
- }
96
-
97
- export {
98
- trackDismissableElement
99
- };