@zag-js/interact-outside 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -12,11 +12,8 @@ npm i @zag-js/interact-outside
12
12
 
13
13
  ## Contribution
14
14
 
15
- Yes please! See the
16
- [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md)
17
- for details.
15
+ Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
18
16
 
19
17
  ## Licence
20
18
 
21
- This project is licensed under the terms of the
22
- [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
19
+ This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
package/dist/index.d.ts CHANGED
@@ -1,6 +1,19 @@
1
- export declare type InteractOutsideOptions = {
1
+ declare type InteractOutsideHandlers = {
2
+ onPointerDownOutside?: (event: PointerDownOutsideEvent) => void;
3
+ onFocusOutside?: (event: FocusOutsideEvent) => void;
4
+ onInteractOutside?: (event: InteractOutsideEvent) => void;
5
+ };
6
+ declare type InteractOutsideOptions = InteractOutsideHandlers & {
2
7
  exclude?: (target: HTMLElement) => boolean;
3
- onPointerDownOutside?: (event: Event) => void;
4
- onFocusOutside?: (event: Event) => void;
5
8
  };
6
- export declare function trackInteractOutside(node: HTMLElement | null, options: InteractOutsideOptions): () => void;
9
+ declare type EventDetails<T> = {
10
+ originalEvent: T;
11
+ contextmenu: boolean;
12
+ focusable: boolean;
13
+ };
14
+ declare type PointerDownOutsideEvent = CustomEvent<EventDetails<PointerEvent>>;
15
+ declare type FocusOutsideEvent = CustomEvent<EventDetails<FocusEvent>>;
16
+ declare type InteractOutsideEvent = PointerDownOutsideEvent | FocusOutsideEvent;
17
+ declare function trackInteractOutside(node: HTMLElement | null, options: InteractOutsideOptions): (() => void) | undefined;
18
+
19
+ export { FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, InteractOutsideOptions, PointerDownOutsideEvent, trackInteractOutside };
package/dist/index.js CHANGED
@@ -25,35 +25,38 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // ../dom/dist/index.mjs
28
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
29
28
  var runIfFn = (v, ...a) => {
30
29
  const res = typeof v === "function" ? v(...a) : v;
31
- return res != null ? res : void 0;
30
+ return res ?? void 0;
32
31
  };
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
- };
32
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
33
+ var isDom = () => typeof window !== "undefined";
34
+ function getPlatform() {
35
+ const agent = navigator.userAgentData;
36
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
37
+ }
38
+ var pt = (v) => isDom() && v.test(getPlatform());
39
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
40
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
41
+ function isDocument(el) {
42
+ return el.nodeType === Node.DOCUMENT_NODE;
40
43
  }
41
44
  function isWindow(value) {
42
45
  return (value == null ? void 0 : value.toString()) === "[object Window]";
43
46
  }
44
- function getOwnerDocument(el) {
45
- var _a;
47
+ function getDocument(el) {
46
48
  if (isWindow(el))
47
49
  return el.document;
48
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
50
+ if (isDocument(el))
51
+ return el;
52
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
49
53
  }
50
- function getOwnerWindow(el) {
51
- var _a;
52
- return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
54
+ function getWindow(el) {
55
+ return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
53
56
  }
54
57
  function getEventTarget(event) {
55
- var _a, _b;
56
- return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
58
+ var _a;
59
+ return ((_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) ?? event.target;
57
60
  }
58
61
  function contains(parent, child) {
59
62
  if (!parent)
@@ -63,21 +66,59 @@ function contains(parent, child) {
63
66
  function isHTMLElement(v) {
64
67
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
65
68
  }
69
+ function isVisible(el) {
70
+ if (!isHTMLElement(el))
71
+ return false;
72
+ return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
73
+ }
74
+ var isContextMenuEvent = (e) => {
75
+ return e.button === 2 || isCtrlKey(e) && e.button === 0;
76
+ };
77
+ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaKey;
78
+ function fireCustomEvent(el, type, init) {
79
+ if (!el)
80
+ return;
81
+ const win = getWindow(el);
82
+ const event = new win.CustomEvent(type, init);
83
+ return el.dispatchEvent(event);
84
+ }
85
+ var focusableSelector = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
86
+ function isFocusable(element) {
87
+ if (!element)
88
+ return false;
89
+ return element.matches(focusableSelector) && isVisible(element);
90
+ }
91
+ var isRef = (v) => hasProp(v, "current");
92
+ function addDomEvent(target, eventName, handler, options) {
93
+ const node = isRef(target) ? target.current : runIfFn(target);
94
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
95
+ return () => {
96
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
97
+ };
98
+ }
99
+
100
+ // ../core/dist/index.mjs
101
+ var callAll = (...fns) => (...a) => {
102
+ fns.forEach(function(fn) {
103
+ fn == null ? void 0 : fn(...a);
104
+ });
105
+ };
66
106
 
67
107
  // src/index.ts
108
+ var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
109
+ var FOCUS_OUTSIDE_EVENT = "focus.outside";
68
110
  function trackInteractOutside(node, options) {
69
- const { exclude, onPointerDownOutside, onFocusOutside } = options;
111
+ const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside } = options;
70
112
  if (!node)
71
113
  return;
72
- const doc = getOwnerDocument(node);
73
- const win = getOwnerWindow(node);
114
+ const doc = getDocument(node);
115
+ const win = getWindow(node);
74
116
  function isEventOutside(event) {
75
117
  const target = getEventTarget(event);
76
118
  if (!(target instanceof win.HTMLElement)) {
77
119
  return false;
78
120
  }
79
- const doc2 = target.ownerDocument;
80
- if (!contains(doc2.documentElement, target)) {
121
+ if (!contains(doc.documentElement, target)) {
81
122
  return false;
82
123
  }
83
124
  if (contains(node, target)) {
@@ -85,23 +126,63 @@ function trackInteractOutside(node, options) {
85
126
  }
86
127
  return !(exclude == null ? void 0 : exclude(target));
87
128
  }
129
+ let clickHandler;
88
130
  function onPointerDown(event) {
89
- if (isEventOutside(event)) {
90
- onPointerDownOutside == null ? void 0 : onPointerDownOutside(event);
131
+ function handler() {
132
+ if (!node || !isEventOutside(event))
133
+ return;
134
+ if (onPointerDownOutside || onInteractOutside) {
135
+ const handler2 = callAll(onPointerDownOutside, onInteractOutside);
136
+ node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
137
+ }
138
+ fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
139
+ bubbles: false,
140
+ cancelable: true,
141
+ detail: {
142
+ originalEvent: event,
143
+ contextmenu: isContextMenuEvent(event),
144
+ focusable: isFocusable(getEventTarget(event))
145
+ }
146
+ });
147
+ }
148
+ if (event.pointerType === "touch") {
149
+ doc.removeEventListener("click", handler);
150
+ clickHandler = handler;
151
+ doc.addEventListener("click", handler, { once: true });
152
+ } else {
153
+ handler();
91
154
  }
92
155
  }
93
156
  const cleanups = /* @__PURE__ */ new Set();
94
157
  const timer = setTimeout(() => {
95
158
  cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
96
- });
159
+ }, 0);
97
160
  function onFocusin(event) {
98
- if (isEventOutside(event)) {
99
- onFocusOutside == null ? void 0 : onFocusOutside(event);
161
+ if (!node || !isEventOutside(event))
162
+ return;
163
+ if (onFocusOutside || onInteractOutside) {
164
+ const handler = callAll(onFocusOutside, onInteractOutside);
165
+ node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
100
166
  }
167
+ fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
168
+ bubbles: false,
169
+ cancelable: true,
170
+ detail: {
171
+ originalEvent: event,
172
+ contextmenu: false,
173
+ focusable: isFocusable(getEventTarget(event))
174
+ }
175
+ });
101
176
  }
102
177
  cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
103
178
  return () => {
104
179
  clearTimeout(timer);
180
+ if (clickHandler)
181
+ doc.removeEventListener("click", clickHandler);
105
182
  cleanups.forEach((fn) => fn());
106
183
  };
107
184
  }
185
+ // Annotate the CommonJS export names for ESM import in node:
186
+ 0 && (module.exports = {
187
+ trackInteractOutside
188
+ });
package/dist/index.mjs CHANGED
@@ -1,35 +1,36 @@
1
- "use strict";
2
-
3
1
  // ../dom/dist/index.mjs
4
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
5
2
  var runIfFn = (v, ...a) => {
6
3
  const res = typeof v === "function" ? v(...a) : v;
7
- return res != null ? res : void 0;
4
+ return res ?? void 0;
8
5
  };
9
- var isRef = (v) => hasProp(v, "current");
10
- function addDomEvent(target, eventName, handler, options) {
11
- const node = isRef(target) ? target.current : runIfFn(target);
12
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
13
- return () => {
14
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
15
- };
6
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
7
+ var isDom = () => typeof window !== "undefined";
8
+ function getPlatform() {
9
+ const agent = navigator.userAgentData;
10
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
11
+ }
12
+ var pt = (v) => isDom() && v.test(getPlatform());
13
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
14
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
15
+ function isDocument(el) {
16
+ return el.nodeType === Node.DOCUMENT_NODE;
16
17
  }
17
18
  function isWindow(value) {
18
19
  return (value == null ? void 0 : value.toString()) === "[object Window]";
19
20
  }
20
- function getOwnerDocument(el) {
21
- var _a;
21
+ function getDocument(el) {
22
22
  if (isWindow(el))
23
23
  return el.document;
24
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
24
+ if (isDocument(el))
25
+ return el;
26
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
25
27
  }
26
- function getOwnerWindow(el) {
27
- var _a;
28
- return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
28
+ function getWindow(el) {
29
+ return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
29
30
  }
30
31
  function getEventTarget(event) {
31
- var _a, _b;
32
- return (_b = (_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) != null ? _b : event.target;
32
+ var _a;
33
+ return ((_a = event.composedPath) == null ? void 0 : _a.call(event)[0]) ?? event.target;
33
34
  }
34
35
  function contains(parent, child) {
35
36
  if (!parent)
@@ -39,21 +40,59 @@ function contains(parent, child) {
39
40
  function isHTMLElement(v) {
40
41
  return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
41
42
  }
43
+ function isVisible(el) {
44
+ if (!isHTMLElement(el))
45
+ return false;
46
+ return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
47
+ }
48
+ var isContextMenuEvent = (e) => {
49
+ return e.button === 2 || isCtrlKey(e) && e.button === 0;
50
+ };
51
+ var isCtrlKey = (v) => isMac() ? v.metaKey && !v.ctrlKey : v.ctrlKey && !v.metaKey;
52
+ function fireCustomEvent(el, type, init) {
53
+ if (!el)
54
+ return;
55
+ const win = getWindow(el);
56
+ const event = new win.CustomEvent(type, init);
57
+ return el.dispatchEvent(event);
58
+ }
59
+ var focusableSelector = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), details > summary:first-of-type";
60
+ function isFocusable(element) {
61
+ if (!element)
62
+ return false;
63
+ return element.matches(focusableSelector) && isVisible(element);
64
+ }
65
+ var isRef = (v) => hasProp(v, "current");
66
+ function addDomEvent(target, eventName, handler, options) {
67
+ const node = isRef(target) ? target.current : runIfFn(target);
68
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
69
+ return () => {
70
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
71
+ };
72
+ }
73
+
74
+ // ../core/dist/index.mjs
75
+ var callAll = (...fns) => (...a) => {
76
+ fns.forEach(function(fn) {
77
+ fn == null ? void 0 : fn(...a);
78
+ });
79
+ };
42
80
 
43
81
  // src/index.ts
82
+ var POINTER_OUTSIDE_EVENT = "pointerdown.outside";
83
+ var FOCUS_OUTSIDE_EVENT = "focus.outside";
44
84
  function trackInteractOutside(node, options) {
45
- const { exclude, onPointerDownOutside, onFocusOutside } = options;
85
+ const { exclude, onFocusOutside, onPointerDownOutside, onInteractOutside } = options;
46
86
  if (!node)
47
87
  return;
48
- const doc = getOwnerDocument(node);
49
- const win = getOwnerWindow(node);
88
+ const doc = getDocument(node);
89
+ const win = getWindow(node);
50
90
  function isEventOutside(event) {
51
91
  const target = getEventTarget(event);
52
92
  if (!(target instanceof win.HTMLElement)) {
53
93
  return false;
54
94
  }
55
- const doc2 = target.ownerDocument;
56
- if (!contains(doc2.documentElement, target)) {
95
+ if (!contains(doc.documentElement, target)) {
57
96
  return false;
58
97
  }
59
98
  if (contains(node, target)) {
@@ -61,23 +100,59 @@ function trackInteractOutside(node, options) {
61
100
  }
62
101
  return !(exclude == null ? void 0 : exclude(target));
63
102
  }
103
+ let clickHandler;
64
104
  function onPointerDown(event) {
65
- if (isEventOutside(event)) {
66
- onPointerDownOutside == null ? void 0 : onPointerDownOutside(event);
105
+ function handler() {
106
+ if (!node || !isEventOutside(event))
107
+ return;
108
+ if (onPointerDownOutside || onInteractOutside) {
109
+ const handler2 = callAll(onPointerDownOutside, onInteractOutside);
110
+ node.addEventListener(POINTER_OUTSIDE_EVENT, handler2, { once: true });
111
+ }
112
+ fireCustomEvent(node, POINTER_OUTSIDE_EVENT, {
113
+ bubbles: false,
114
+ cancelable: true,
115
+ detail: {
116
+ originalEvent: event,
117
+ contextmenu: isContextMenuEvent(event),
118
+ focusable: isFocusable(getEventTarget(event))
119
+ }
120
+ });
121
+ }
122
+ if (event.pointerType === "touch") {
123
+ doc.removeEventListener("click", handler);
124
+ clickHandler = handler;
125
+ doc.addEventListener("click", handler, { once: true });
126
+ } else {
127
+ handler();
67
128
  }
68
129
  }
69
130
  const cleanups = /* @__PURE__ */ new Set();
70
131
  const timer = setTimeout(() => {
71
132
  cleanups.add(addDomEvent(doc, "pointerdown", onPointerDown, true));
72
- });
133
+ }, 0);
73
134
  function onFocusin(event) {
74
- if (isEventOutside(event)) {
75
- onFocusOutside == null ? void 0 : onFocusOutside(event);
135
+ if (!node || !isEventOutside(event))
136
+ return;
137
+ if (onFocusOutside || onInteractOutside) {
138
+ const handler = callAll(onFocusOutside, onInteractOutside);
139
+ node.addEventListener(FOCUS_OUTSIDE_EVENT, handler, { once: true });
76
140
  }
141
+ fireCustomEvent(node, FOCUS_OUTSIDE_EVENT, {
142
+ bubbles: false,
143
+ cancelable: true,
144
+ detail: {
145
+ originalEvent: event,
146
+ contextmenu: false,
147
+ focusable: isFocusable(getEventTarget(event))
148
+ }
149
+ });
77
150
  }
78
151
  cleanups.add(addDomEvent(doc, "focusin", onFocusin, true));
79
152
  return () => {
80
153
  clearTimeout(timer);
154
+ if (clickHandler)
155
+ doc.removeEventListener("click", clickHandler);
81
156
  cleanups.forEach((fn) => fn());
82
157
  };
83
158
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/interact-outside",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Track interations or focus outside an element",
5
5
  "keywords": [
6
6
  "js",
@@ -18,22 +18,23 @@
18
18
  "files": [
19
19
  "dist/**/*"
20
20
  ],
21
- "scripts": {
22
- "build:fast": "yarn zag build",
23
- "start": "yarn zag build --watch",
24
- "build": "yarn zag build --prod",
25
- "test": "jest --config ../../../jest.config.js --rootDir tests",
26
- "lint": "eslint src --ext .ts,.tsx",
27
- "test:ci": "yarn test --ci --runInBand --updateSnapshot",
28
- "test:watch": "yarn test --watchAll"
29
- },
30
- "dependencies": {
31
- "@zag-js/dom-utils": "0.1.5"
21
+ "devDependencies": {
22
+ "@zag-js/dom-utils": "0.1.8",
23
+ "@zag-js/utils": "0.1.3"
32
24
  },
33
25
  "publishConfig": {
34
26
  "access": "public"
35
27
  },
36
28
  "bugs": {
37
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
+ },
31
+ "scripts": {
32
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
33
+ "start": "pnpm build --watch",
34
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
35
+ "test": "jest --config ../../../jest.config.js --rootDir tests",
36
+ "lint": "eslint src --ext .ts,.tsx",
37
+ "test-ci": "pnpm test --ci --runInBand -u",
38
+ "test-watch": "pnpm test --watchAll"
38
39
  }
39
- }
40
+ }