@zag-js/tooltip 0.1.6 → 0.1.9

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.mjs CHANGED
@@ -22,16 +22,16 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
22
  var dataAttr = (guard) => {
23
23
  return guard ? "" : void 0;
24
24
  };
25
- function getStyleCache() {
26
- ;
27
- globalThis.__styleCache__ = globalThis.__styleCache__ || /* @__PURE__ */ new WeakMap();
28
- return globalThis.__styleCache__;
25
+ function getCache() {
26
+ const g = globalThis;
27
+ g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
28
+ return g.__styleCache;
29
29
  }
30
- function getComputedStyle2(el) {
30
+ function getComputedStyle(el) {
31
31
  var _a;
32
32
  if (!el)
33
33
  return {};
34
- const cache = getStyleCache();
34
+ const cache = getCache();
35
35
  let style = cache.get(el);
36
36
  if (!style) {
37
37
  const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
@@ -40,83 +40,57 @@ function getComputedStyle2(el) {
40
40
  }
41
41
  return style;
42
42
  }
43
- function raf(fn) {
44
- const id = globalThis.requestAnimationFrame(fn);
45
- return function cleanup() {
46
- globalThis.cancelAnimationFrame(id);
47
- };
48
- }
49
- var noop = () => {
43
+ var runIfFn = (v, ...a) => {
44
+ const res = typeof v === "function" ? v(...a) : v;
45
+ return res != null ? res : void 0;
50
46
  };
51
- function getListenerElements() {
52
- ;
53
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
54
- return globalThis.__listenerElements__;
47
+ var isArray = (v) => Array.isArray(v);
48
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
49
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
50
+ var isDom = () => typeof window !== "undefined";
51
+ function getPlatform() {
52
+ var _a;
53
+ const agent = navigator.userAgentData;
54
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
55
55
  }
56
- function getListenerCache() {
57
- ;
58
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
59
- return globalThis.__listenerCache__;
56
+ var pt = (v) => isDom() && v.test(getPlatform());
57
+ var vn = (v) => isDom() && v.test(navigator.vendor);
58
+ var isSafari = () => isApple() && vn(/apple/i);
59
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
60
+ function isWindow(value) {
61
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
60
62
  }
61
- function globalEventBus(node, type, handler, options) {
63
+ function getDocument(el) {
62
64
  var _a;
63
- if (!node)
64
- return noop;
65
- const hash = JSON.stringify({ type, options });
66
- const listenerElements = getListenerElements();
67
- const listenerCache = getListenerCache();
68
- const group = listenerElements.get(node);
69
- if (!listenerElements.has(node)) {
70
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
71
- listenerElements.set(node, group2);
72
- } else if (group == null ? void 0 : group.has(hash)) {
73
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
74
- } else {
75
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
76
- }
77
- function attach(node2) {
78
- var _a2, _b;
79
- function listener(event) {
80
- var _a3;
81
- const group2 = listenerElements.get(node2);
82
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
83
- }
84
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
85
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
86
- node2.addEventListener(type, listener, options);
87
- return;
88
- }
89
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
90
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
91
- node2.addEventListener(type, listener, options);
92
- }
93
- }
94
- attach(node);
95
- return function remove() {
96
- var _a2, _b, _c, _d;
97
- if (!listenerElements.has(node))
98
- return;
99
- const group2 = listenerElements.get(node);
100
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
101
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
102
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
103
- node.removeEventListener(type, listener, options);
104
- group2 == null ? void 0 : group2.delete(hash);
105
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
106
- if ((group2 == null ? void 0 : group2.size) === 0) {
107
- listenerElements.delete(node);
108
- listenerCache.delete(node);
109
- }
110
- }
111
- };
65
+ if (isWindow(el))
66
+ return el.document;
67
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
68
+ }
69
+ function getWindow(el) {
70
+ var _a;
71
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
72
+ }
73
+ function getNodeName(node) {
74
+ var _a;
75
+ return isWindow(node) ? "" : (_a = node == null ? void 0 : node.localName) != null ? _a : "";
112
76
  }
113
- var t = (v) => Object.prototype.toString.call(v).slice(8, -1);
114
- var isRef = (v) => t(v) === "Object" && "current" in v;
115
- var runIfFn = (fn) => t(fn) === "Function" ? fn() : fn;
116
- var isTouchEvent = (v) => t(v) === "Object" && !!v.touches;
117
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
77
+ function getParent(el) {
78
+ const doc = getDocument(el);
79
+ if (getNodeName(el) === "html")
80
+ return el;
81
+ return el.assignedSlot || el.parentElement || doc.documentElement;
82
+ }
83
+ function isHTMLElement(v) {
84
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
85
+ }
86
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
87
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
88
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
89
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
90
+ var isRef = (v) => hasProp(v, "current");
91
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
118
92
  function extractInfo(event, type = "page") {
119
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
93
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
120
94
  return {
121
95
  point: {
122
96
  x: point[`${type}X`],
@@ -124,9 +98,12 @@ function extractInfo(event, type = "page") {
124
98
  }
125
99
  };
126
100
  }
127
- function addDomEvent(target, event, listener, options) {
101
+ function addDomEvent(target, eventName, handler, options) {
128
102
  const node = isRef(target) ? target.current : runIfFn(target);
129
- return globalEventBus(node, event, listener, options);
103
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
104
+ return () => {
105
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
106
+ };
130
107
  }
131
108
  function addPointerEvent(target, event, listener, options) {
132
109
  var _a;
@@ -149,9 +126,6 @@ function filterPrimaryPointer(fn) {
149
126
  fn(event);
150
127
  };
151
128
  }
152
- var supportsPointerEvent = () => typeof window !== "undefined" && window.onpointerdown === null;
153
- var supportsTouchEvent = () => typeof window !== "undefined" && window.ontouchstart === null;
154
- var supportsMouseEvent = () => typeof window !== "undefined" && window.onmousedown === null;
155
129
  var mouseEventNames = {
156
130
  pointerdown: "mousedown",
157
131
  pointermove: "mousemove",
@@ -177,38 +151,34 @@ function getEventName(evt) {
177
151
  return mouseEventNames[evt];
178
152
  return evt;
179
153
  }
180
- function getOwnerDocument(el) {
181
- var _a;
182
- if (isWindow(el))
183
- return el.document;
184
- return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
185
- }
186
- function getOwnerWindow(el) {
187
- var _a;
188
- return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
189
- }
190
- function getNodeName(node) {
191
- return isWindow(node) ? "" : node ? node.localName || "" : "";
192
- }
193
- function getParent(el) {
194
- const doc = getOwnerDocument(el);
195
- if (getNodeName(el) === "html")
196
- return el;
197
- return el.assignedSlot || el.parentElement || doc.documentElement;
198
- }
199
- function isHTMLElement(v) {
200
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
154
+ var visuallyHiddenStyle = {
155
+ border: "0",
156
+ clip: "rect(0 0 0 0)",
157
+ height: "1px",
158
+ margin: "-1px",
159
+ overflow: "hidden",
160
+ padding: "0",
161
+ position: "absolute",
162
+ width: "1px",
163
+ whiteSpace: "nowrap",
164
+ wordWrap: "normal"
165
+ };
166
+ function raf(fn) {
167
+ const id = globalThis.requestAnimationFrame(fn);
168
+ return function cleanup() {
169
+ globalThis.cancelAnimationFrame(id);
170
+ };
201
171
  }
202
- function isWindow(value) {
203
- return (value == null ? void 0 : value.toString()) === "[object Window]";
172
+ function addPointerlockChangeListener(doc, fn) {
173
+ return addDomEvent(doc, "pointerlockchange", fn, false);
204
174
  }
205
175
  function isScrollParent(el) {
206
- const { overflow, overflowX, overflowY } = getComputedStyle2(el);
176
+ const { overflow, overflowX, overflowY } = getComputedStyle(el);
207
177
  return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
208
178
  }
209
179
  function getScrollParent(el) {
210
180
  if (["html", "body", "#document"].includes(getNodeName(el))) {
211
- return getOwnerDocument(el).body;
181
+ return getDocument(el).body;
212
182
  }
213
183
  if (isHTMLElement(el) && isScrollParent(el)) {
214
184
  return el;
@@ -217,95 +187,18 @@ function getScrollParent(el) {
217
187
  }
218
188
  function getScrollParents(el, list = []) {
219
189
  const scrollParent = getScrollParent(el);
220
- const isBody = scrollParent === getOwnerDocument(el).body;
221
- const win = getOwnerWindow(scrollParent);
190
+ const isBody = scrollParent === getDocument(el).body;
191
+ const win = getWindow(scrollParent);
222
192
  const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent;
223
193
  const parents = list.concat(target);
224
194
  if (isBody)
225
195
  return parents;
226
196
  return parents.concat(getScrollParents(getParent(target)));
227
197
  }
228
- var visuallyHiddenStyle = {
229
- border: "0",
230
- clip: "rect(0 0 0 0)",
231
- height: "1px",
232
- margin: "-1px",
233
- overflow: "hidden",
234
- padding: "0",
235
- position: "absolute",
236
- width: "1px",
237
- whiteSpace: "nowrap",
238
- wordWrap: "normal"
239
- };
240
- function itemById(v, id) {
241
- return v.find((node) => node.id === id);
242
- }
243
- function indexOfId(v, id) {
244
- const item = itemById(v, id);
245
- return item ? v.indexOf(item) : -1;
246
- }
247
- var getValueText = (item) => {
248
- var _a, _b;
249
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
250
- };
251
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
252
- var wrap = (v, idx) => {
253
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
254
- };
255
- function findByText(v, text, currentId) {
256
- const index = currentId ? indexOfId(v, currentId) : -1;
257
- let items = currentId ? wrap(v, index) : v;
258
- const isSingleKey = text.length === 1;
259
- if (isSingleKey) {
260
- items = items.filter((item) => item.id !== currentId);
261
- }
262
- return items.find((item) => match(getValueText(item), text));
263
- }
264
- function addPointerlockChangeListener(doc, fn) {
265
- return addDomEvent(doc, "pointerlockchange", fn, false);
266
- }
267
- function findByTypeahead(_items, options) {
268
- const { state: state2, activeId, key, timeout = 350 } = options;
269
- const search = state2.keysSoFar + key;
270
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
271
- const query2 = isRepeated ? search[0] : search;
272
- let items = _items.slice();
273
- const next = findByText(items, query2, activeId);
274
- function cleanup() {
275
- clearTimeout(state2.timer);
276
- state2.timer = -1;
277
- }
278
- function update(value) {
279
- state2.keysSoFar = value;
280
- cleanup();
281
- if (value !== "") {
282
- state2.timer = +setTimeout(() => {
283
- update("");
284
- cleanup();
285
- }, timeout);
286
- }
287
- }
288
- update(search);
289
- return next;
290
- }
291
- findByTypeahead.defaultOptions = {
292
- keysSoFar: "",
293
- timer: -1
294
- };
295
198
 
296
199
  // src/tooltip.connect.ts
297
200
  import { getPlacementStyles } from "@zag-js/popper";
298
201
 
299
- // ../../types/dist/index.mjs
300
- function createNormalizer(fn) {
301
- return new Proxy({}, {
302
- get() {
303
- return fn;
304
- }
305
- });
306
- }
307
- var normalizeProp = createNormalizer((v) => v);
308
-
309
202
  // src/tooltip.dom.ts
310
203
  var dom = {
311
204
  getDoc: (ctx) => {
@@ -356,7 +249,7 @@ var store = proxy({
356
249
  });
357
250
 
358
251
  // src/tooltip.connect.ts
359
- function connect(state, send, normalize = normalizeProp) {
252
+ function connect(state, send, normalize) {
360
253
  const id = state.context.id;
361
254
  const hasAriaLabel = state.context.hasAriaLabel;
362
255
  const isOpen = state.hasTag("open");
@@ -464,11 +357,8 @@ import { createMachine, ref, subscribe } from "@zag-js/core";
464
357
  import { getPlacement } from "@zag-js/popper";
465
358
 
466
359
  // ../../utilities/core/dist/index.mjs
467
- var noop2 = () => {
360
+ var noop = () => {
468
361
  };
469
- var ua = (v) => isDom() && v.test(navigator.userAgent);
470
- var isDom = () => !!(typeof window !== "undefined");
471
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
472
362
 
473
363
  // src/tooltip.machine.ts
474
364
  function machine(ctx = {}) {
@@ -629,7 +519,7 @@ function machine(ctx = {}) {
629
519
  },
630
520
  trackDisabledTriggerOnSafari(ctx2, _evt, { send }) {
631
521
  if (!isSafari())
632
- return noop2;
522
+ return noop;
633
523
  const doc = dom.getDoc(ctx2);
634
524
  return addPointerEvent(doc, "pointermove", (event) => {
635
525
  const selector = "[data-part=trigger][data-expanded]";
@@ -696,4 +586,3 @@ export {
696
586
  connect,
697
587
  machine
698
588
  };
699
- //# sourceMappingURL=index.mjs.map
@@ -1,6 +1,6 @@
1
- import { PropTypes, ReactPropTypes } from "@zag-js/types";
2
- import { Send, State } from "./tooltip.types";
3
- export declare function connect<T extends PropTypes = ReactPropTypes>(state: State, send: Send, normalize?: import("@zag-js/types").NormalizeProps): {
1
+ import type { NormalizeProps, PropTypes } from "@zag-js/types";
2
+ import type { Send, State } from "./tooltip.types";
3
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
4
4
  isOpen: boolean;
5
5
  open(): void;
6
6
  close(): void;
@@ -16,4 +16,3 @@ export declare function connect<T extends PropTypes = ReactPropTypes>(state: Sta
16
16
  labelProps: T["element"];
17
17
  createPortal(): HTMLElement;
18
18
  };
19
- //# sourceMappingURL=tooltip.connect.d.ts.map
@@ -1,4 +1,4 @@
1
- import { MachineContext as Ctx } from "./tooltip.types";
1
+ import type { MachineContext as Ctx } from "./tooltip.types";
2
2
  export declare const dom: {
3
3
  getDoc: (ctx: Ctx) => Document;
4
4
  getWin: (ctx: Ctx) => Window & typeof globalThis;
@@ -16,4 +16,3 @@ export declare const dom: {
16
16
  getPortalEl: (ctx: Ctx) => HTMLElement;
17
17
  createPortalEl: (ctx: Ctx) => HTMLElement;
18
18
  };
19
- //# sourceMappingURL=tooltip.dom.d.ts.map
@@ -1,3 +1,2 @@
1
- import { UserDefinedContext, MachineContext, MachineState } from "./tooltip.types";
1
+ import type { MachineContext, MachineState, UserDefinedContext } from "./tooltip.types";
2
2
  export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
3
- //# sourceMappingURL=tooltip.machine.d.ts.map
@@ -6,4 +6,3 @@ declare type Store = {
6
6
  };
7
7
  export declare const store: Store;
8
8
  export {};
9
- //# sourceMappingURL=tooltip.store.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import type { StateMachine as S } from "@zag-js/core";
2
- import { Placement, PositioningOptions } from "@zag-js/popper";
3
- import { RootProperties } from "@zag-js/types";
2
+ import type { Placement, PositioningOptions } from "@zag-js/popper";
3
+ import type { RootProperties } from "@zag-js/types";
4
4
  declare type ElementIds = Partial<{
5
5
  trigger: string;
6
6
  content: string;
@@ -80,4 +80,3 @@ export declare type MachineState = {
80
80
  export declare type State = S.State<MachineContext, MachineState>;
81
81
  export declare type Send = S.Send<S.AnyEventObject>;
82
82
  export {};
83
- //# sourceMappingURL=tooltip.types.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/tooltip",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "description": "Core logic for the tooltip widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,10 +29,10 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.5",
33
- "@zag-js/popper": "0.1.4",
34
- "@zag-js/dom-utils": "0.1.3",
35
- "@zag-js/types": "0.1.2"
32
+ "@zag-js/core": "0.1.7",
33
+ "@zag-js/dom-utils": "0.1.6",
34
+ "@zag-js/popper": "0.1.7",
35
+ "@zag-js/types": "0.2.1"
36
36
  },
37
37
  "scripts": {
38
38
  "build:fast": "zag build",
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC3C,YAAY,EAAE,kBAAkB,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAA"}