@zag-js/tooltip 0.0.0-20220802150625
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 +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +586 -0
- package/dist/index.mjs +558 -0
- package/package.json +50 -0
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @zag-js/tooltip
|
|
2
|
+
|
|
3
|
+
Core logic for the tooltip widget implemented as a state machine
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add @zag-js/tooltip
|
|
9
|
+
# or
|
|
10
|
+
npm i @zag-js/tooltip
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Contribution
|
|
14
|
+
|
|
15
|
+
Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
|
|
16
|
+
|
|
17
|
+
## Licence
|
|
18
|
+
|
|
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
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { RequiredBy, CommonProperties, RootProperties, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
2
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
3
|
+
import { StateMachine } from '@zag-js/core';
|
|
4
|
+
import { PositioningOptions } from '@zag-js/popper';
|
|
5
|
+
|
|
6
|
+
declare type ElementIds = Partial<{
|
|
7
|
+
trigger: string;
|
|
8
|
+
content: string;
|
|
9
|
+
}>;
|
|
10
|
+
declare type PublicContext = CommonProperties & {
|
|
11
|
+
/**
|
|
12
|
+
* The ids of the elements in the tooltip. Useful for composition.
|
|
13
|
+
*/
|
|
14
|
+
ids?: ElementIds;
|
|
15
|
+
/**
|
|
16
|
+
* The `id` of the tooltip.
|
|
17
|
+
*/
|
|
18
|
+
id: string;
|
|
19
|
+
/**
|
|
20
|
+
* The open delay of the tooltip.
|
|
21
|
+
*/
|
|
22
|
+
openDelay: number;
|
|
23
|
+
/**
|
|
24
|
+
* The close delay of the tooltip.
|
|
25
|
+
*/
|
|
26
|
+
closeDelay: number;
|
|
27
|
+
/**
|
|
28
|
+
* Whether to close the tooltip on pointerdown.
|
|
29
|
+
*/
|
|
30
|
+
closeOnPointerDown: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether to close the tooltip when the Escape key is pressed.
|
|
33
|
+
*/
|
|
34
|
+
closeOnEsc?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the tooltip's content is interactive.
|
|
37
|
+
* In this mode, the tooltip will remain open when user hovers over the content.
|
|
38
|
+
* @see https://www.w3.org/TR/WCAG21/#content-on-hover-or-focus
|
|
39
|
+
*/
|
|
40
|
+
interactive: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Function called when the tooltip is opened.
|
|
43
|
+
*/
|
|
44
|
+
onOpen?: VoidFunction;
|
|
45
|
+
/**
|
|
46
|
+
* Function called when the tooltip is closed.
|
|
47
|
+
*/
|
|
48
|
+
onClose?: VoidFunction;
|
|
49
|
+
/**
|
|
50
|
+
* Custom label for the tooltip.
|
|
51
|
+
*/
|
|
52
|
+
"aria-label"?: string;
|
|
53
|
+
/**
|
|
54
|
+
* The user provided options used to position the popover content
|
|
55
|
+
*/
|
|
56
|
+
positioning: PositioningOptions;
|
|
57
|
+
};
|
|
58
|
+
declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
59
|
+
declare type ComputedContext = Readonly<{
|
|
60
|
+
/**
|
|
61
|
+
* @computed Whether an `aria-label` is set.
|
|
62
|
+
*/
|
|
63
|
+
readonly hasAriaLabel: boolean;
|
|
64
|
+
}>;
|
|
65
|
+
declare type PrivateContext = RootProperties & {};
|
|
66
|
+
declare type MachineContext = PublicContext & ComputedContext & PrivateContext;
|
|
67
|
+
declare type MachineState = {
|
|
68
|
+
value: "unknown" | "opening" | "open" | "closing" | "closed";
|
|
69
|
+
tags: "open" | "closed";
|
|
70
|
+
};
|
|
71
|
+
declare type State = StateMachine.State<MachineContext, MachineState>;
|
|
72
|
+
declare type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
73
|
+
|
|
74
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
75
|
+
isOpen: boolean;
|
|
76
|
+
open(): void;
|
|
77
|
+
close(): void;
|
|
78
|
+
getAnimationState(): {
|
|
79
|
+
enter: boolean;
|
|
80
|
+
exit: boolean;
|
|
81
|
+
};
|
|
82
|
+
triggerProps: T["button"];
|
|
83
|
+
arrowProps: T["element"];
|
|
84
|
+
innerArrowProps: T["element"];
|
|
85
|
+
positionerProps: T["element"];
|
|
86
|
+
contentProps: T["element"];
|
|
87
|
+
labelProps: T["element"];
|
|
88
|
+
createPortal(): HTMLElement;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
declare function machine(ctx: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
92
|
+
|
|
93
|
+
export { UserDefinedContext as Context, connect, machine };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
connect: () => connect,
|
|
24
|
+
machine: () => machine
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// ../../utilities/dom/dist/index.mjs
|
|
29
|
+
var dataAttr = (guard) => {
|
|
30
|
+
return guard ? "" : void 0;
|
|
31
|
+
};
|
|
32
|
+
function getCache() {
|
|
33
|
+
const g = globalThis;
|
|
34
|
+
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
|
|
35
|
+
return g.__styleCache;
|
|
36
|
+
}
|
|
37
|
+
function getComputedStyle(el) {
|
|
38
|
+
if (!el)
|
|
39
|
+
return {};
|
|
40
|
+
const cache = getCache();
|
|
41
|
+
let style = cache.get(el);
|
|
42
|
+
if (!style) {
|
|
43
|
+
const win = (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
|
|
44
|
+
style = win.getComputedStyle(el);
|
|
45
|
+
cache.set(el, style);
|
|
46
|
+
}
|
|
47
|
+
return style;
|
|
48
|
+
}
|
|
49
|
+
var runIfFn = (v, ...a) => {
|
|
50
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
51
|
+
return res ?? void 0;
|
|
52
|
+
};
|
|
53
|
+
var isArray = (v) => Array.isArray(v);
|
|
54
|
+
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
|
|
55
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
56
|
+
var isDom = () => typeof window !== "undefined";
|
|
57
|
+
function getPlatform() {
|
|
58
|
+
const agent = navigator.userAgentData;
|
|
59
|
+
return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
|
|
60
|
+
}
|
|
61
|
+
var pt = (v) => isDom() && v.test(getPlatform());
|
|
62
|
+
var vn = (v) => isDom() && v.test(navigator.vendor);
|
|
63
|
+
var isSafari = () => isApple() && vn(/apple/i);
|
|
64
|
+
var isApple = () => pt(/mac|iphone|ipad|ipod/i);
|
|
65
|
+
function isDocument(el) {
|
|
66
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
67
|
+
}
|
|
68
|
+
function isWindow(value) {
|
|
69
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
70
|
+
}
|
|
71
|
+
function getDocument(el) {
|
|
72
|
+
if (isWindow(el))
|
|
73
|
+
return el.document;
|
|
74
|
+
if (isDocument(el))
|
|
75
|
+
return el;
|
|
76
|
+
return (el == null ? void 0 : el.ownerDocument) ?? document;
|
|
77
|
+
}
|
|
78
|
+
function getWindow(el) {
|
|
79
|
+
return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
|
|
80
|
+
}
|
|
81
|
+
function getNodeName(node) {
|
|
82
|
+
return isWindow(node) ? "" : (node == null ? void 0 : node.localName) ?? "";
|
|
83
|
+
}
|
|
84
|
+
function getParent(el) {
|
|
85
|
+
const doc = getDocument(el);
|
|
86
|
+
if (getNodeName(el) === "html")
|
|
87
|
+
return el;
|
|
88
|
+
return el.assignedSlot || el.parentElement || doc.documentElement;
|
|
89
|
+
}
|
|
90
|
+
function defineDomHelpers(helpers) {
|
|
91
|
+
const dom2 = {
|
|
92
|
+
getRootNode: (ctx) => {
|
|
93
|
+
var _a;
|
|
94
|
+
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
|
|
95
|
+
},
|
|
96
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
97
|
+
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
98
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
99
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
...dom2,
|
|
103
|
+
...helpers
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function isHTMLElement(v) {
|
|
107
|
+
return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
|
|
108
|
+
}
|
|
109
|
+
var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
|
|
110
|
+
var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
|
|
111
|
+
var supportsMouseEvent = () => isDom() && window.onmousedown === null;
|
|
112
|
+
var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
|
|
113
|
+
var isRef = (v) => hasProp(v, "current");
|
|
114
|
+
var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
|
|
115
|
+
function extractInfo(event, type = "page") {
|
|
116
|
+
const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
|
|
117
|
+
return {
|
|
118
|
+
point: {
|
|
119
|
+
x: point[`${type}X`],
|
|
120
|
+
y: point[`${type}Y`]
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function addDomEvent(target, eventName, handler, options) {
|
|
125
|
+
const node = isRef(target) ? target.current : runIfFn(target);
|
|
126
|
+
node == null ? void 0 : node.addEventListener(eventName, handler, options);
|
|
127
|
+
return () => {
|
|
128
|
+
node == null ? void 0 : node.removeEventListener(eventName, handler, options);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function addPointerEvent(target, event, listener, options) {
|
|
132
|
+
const type = getEventName(event) ?? event;
|
|
133
|
+
return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
|
|
134
|
+
}
|
|
135
|
+
function wrapHandler(fn, filter = false) {
|
|
136
|
+
const listener = (event) => {
|
|
137
|
+
fn(event, extractInfo(event));
|
|
138
|
+
};
|
|
139
|
+
return filter ? filterPrimaryPointer(listener) : listener;
|
|
140
|
+
}
|
|
141
|
+
function filterPrimaryPointer(fn) {
|
|
142
|
+
return (event) => {
|
|
143
|
+
const win = event.view ?? window;
|
|
144
|
+
const isMouseEvent2 = event instanceof win.MouseEvent;
|
|
145
|
+
const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
|
|
146
|
+
if (isPrimary)
|
|
147
|
+
fn(event);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
var mouseEventNames = {
|
|
151
|
+
pointerdown: "mousedown",
|
|
152
|
+
pointermove: "mousemove",
|
|
153
|
+
pointerup: "mouseup",
|
|
154
|
+
pointercancel: "mousecancel",
|
|
155
|
+
pointerover: "mouseover",
|
|
156
|
+
pointerout: "mouseout",
|
|
157
|
+
pointerenter: "mouseenter",
|
|
158
|
+
pointerleave: "mouseleave"
|
|
159
|
+
};
|
|
160
|
+
var touchEventNames = {
|
|
161
|
+
pointerdown: "touchstart",
|
|
162
|
+
pointermove: "touchmove",
|
|
163
|
+
pointerup: "touchend",
|
|
164
|
+
pointercancel: "touchcancel"
|
|
165
|
+
};
|
|
166
|
+
function getEventName(evt) {
|
|
167
|
+
if (supportsPointerEvent())
|
|
168
|
+
return evt;
|
|
169
|
+
if (supportsTouchEvent())
|
|
170
|
+
return touchEventNames[evt];
|
|
171
|
+
if (supportsMouseEvent())
|
|
172
|
+
return mouseEventNames[evt];
|
|
173
|
+
return evt;
|
|
174
|
+
}
|
|
175
|
+
function raf(fn) {
|
|
176
|
+
const id = globalThis.requestAnimationFrame(fn);
|
|
177
|
+
return function cleanup() {
|
|
178
|
+
globalThis.cancelAnimationFrame(id);
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function addPointerlockChangeListener(doc, fn) {
|
|
182
|
+
return addDomEvent(doc, "pointerlockchange", fn, false);
|
|
183
|
+
}
|
|
184
|
+
function isScrollParent(el) {
|
|
185
|
+
const { overflow, overflowX, overflowY } = getComputedStyle(el);
|
|
186
|
+
return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
|
|
187
|
+
}
|
|
188
|
+
function getScrollParent(el) {
|
|
189
|
+
if (["html", "body", "#document"].includes(getNodeName(el))) {
|
|
190
|
+
return getDocument(el).body;
|
|
191
|
+
}
|
|
192
|
+
if (isHTMLElement(el) && isScrollParent(el)) {
|
|
193
|
+
return el;
|
|
194
|
+
}
|
|
195
|
+
return getScrollParent(getParent(el));
|
|
196
|
+
}
|
|
197
|
+
function getScrollParents(el, list = []) {
|
|
198
|
+
const scrollParent = getScrollParent(el);
|
|
199
|
+
const isBody = scrollParent === getDocument(el).body;
|
|
200
|
+
const win = getWindow(scrollParent);
|
|
201
|
+
const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent;
|
|
202
|
+
const parents = list.concat(target);
|
|
203
|
+
if (isBody)
|
|
204
|
+
return parents;
|
|
205
|
+
return parents.concat(getScrollParents(getParent(target)));
|
|
206
|
+
}
|
|
207
|
+
var visuallyHiddenStyle = {
|
|
208
|
+
border: "0",
|
|
209
|
+
clip: "rect(0 0 0 0)",
|
|
210
|
+
height: "1px",
|
|
211
|
+
margin: "-1px",
|
|
212
|
+
overflow: "hidden",
|
|
213
|
+
padding: "0",
|
|
214
|
+
position: "absolute",
|
|
215
|
+
width: "1px",
|
|
216
|
+
whiteSpace: "nowrap",
|
|
217
|
+
wordWrap: "normal"
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/tooltip.connect.ts
|
|
221
|
+
var import_popper = require("@zag-js/popper");
|
|
222
|
+
|
|
223
|
+
// src/tooltip.dom.ts
|
|
224
|
+
var dom = defineDomHelpers({
|
|
225
|
+
getTriggerId: (ctx) => {
|
|
226
|
+
var _a;
|
|
227
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.trigger) ?? `tooltip:${ctx.id}:trigger`;
|
|
228
|
+
},
|
|
229
|
+
getContentId: (ctx) => {
|
|
230
|
+
var _a;
|
|
231
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.content) ?? `tooltip:${ctx.id}:content`;
|
|
232
|
+
},
|
|
233
|
+
getArrowId: (ctx) => `tooltip:${ctx.id}:arrow`,
|
|
234
|
+
getPositionerId: (ctx) => `tooltip:${ctx.id}:popper`,
|
|
235
|
+
portalId: "tooltip-portal",
|
|
236
|
+
getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
|
|
237
|
+
getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
|
|
238
|
+
getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
|
|
239
|
+
getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx)),
|
|
240
|
+
getScrollParent: (ctx) => getScrollParent(dom.getTriggerEl(ctx)),
|
|
241
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.portalId),
|
|
242
|
+
createPortalEl: (ctx) => {
|
|
243
|
+
const portal = dom.getDoc(ctx).createElement(dom.portalId);
|
|
244
|
+
portal.id = dom.portalId;
|
|
245
|
+
return portal;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// src/tooltip.store.ts
|
|
250
|
+
var import_core = require("@zag-js/core");
|
|
251
|
+
var store = (0, import_core.proxy)({
|
|
252
|
+
id: null,
|
|
253
|
+
prevId: null,
|
|
254
|
+
setId(val) {
|
|
255
|
+
this.prevId = this.id;
|
|
256
|
+
this.id = val;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// src/tooltip.connect.ts
|
|
261
|
+
function connect(state, send, normalize) {
|
|
262
|
+
const id = state.context.id;
|
|
263
|
+
const hasAriaLabel = state.context.hasAriaLabel;
|
|
264
|
+
const isOpen = state.hasTag("open");
|
|
265
|
+
const triggerId = dom.getTriggerId(state.context);
|
|
266
|
+
const contentId = dom.getContentId(state.context);
|
|
267
|
+
const popperStyles = (0, import_popper.getPlacementStyles)({
|
|
268
|
+
measured: !!state.context.isPlacementComplete,
|
|
269
|
+
placement: state.context.currentPlacement
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
isOpen,
|
|
273
|
+
open() {
|
|
274
|
+
send("OPEN");
|
|
275
|
+
},
|
|
276
|
+
close() {
|
|
277
|
+
send("CLOSE");
|
|
278
|
+
},
|
|
279
|
+
getAnimationState() {
|
|
280
|
+
return {
|
|
281
|
+
enter: store.prevId === null && id === store.id,
|
|
282
|
+
exit: store.id === null
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
triggerProps: normalize.button({
|
|
286
|
+
"data-part": "trigger",
|
|
287
|
+
id: triggerId,
|
|
288
|
+
"data-expanded": dataAttr(isOpen),
|
|
289
|
+
"aria-describedby": isOpen ? contentId : void 0,
|
|
290
|
+
onClick() {
|
|
291
|
+
send("CLICK");
|
|
292
|
+
},
|
|
293
|
+
onFocus() {
|
|
294
|
+
send("FOCUS");
|
|
295
|
+
},
|
|
296
|
+
onBlur() {
|
|
297
|
+
if (id === store.id) {
|
|
298
|
+
send("BLUR");
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
onPointerDown() {
|
|
302
|
+
if (id === store.id) {
|
|
303
|
+
send("POINTER_DOWN");
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
onPointerMove() {
|
|
307
|
+
send("POINTER_ENTER");
|
|
308
|
+
},
|
|
309
|
+
onPointerLeave() {
|
|
310
|
+
send("POINTER_LEAVE");
|
|
311
|
+
},
|
|
312
|
+
onPointerCancel() {
|
|
313
|
+
send("POINTER_LEAVE");
|
|
314
|
+
}
|
|
315
|
+
}),
|
|
316
|
+
arrowProps: normalize.element({
|
|
317
|
+
id: dom.getArrowId(state.context),
|
|
318
|
+
"data-part": "arrow",
|
|
319
|
+
style: popperStyles.arrow
|
|
320
|
+
}),
|
|
321
|
+
innerArrowProps: normalize.element({
|
|
322
|
+
"data-part": "arrow-inner",
|
|
323
|
+
style: popperStyles.innerArrow
|
|
324
|
+
}),
|
|
325
|
+
positionerProps: normalize.element({
|
|
326
|
+
id: dom.getPositionerId(state.context),
|
|
327
|
+
"data-part": "positioner",
|
|
328
|
+
style: popperStyles.floating
|
|
329
|
+
}),
|
|
330
|
+
contentProps: normalize.element({
|
|
331
|
+
"data-part": "content",
|
|
332
|
+
role: hasAriaLabel ? void 0 : "tooltip",
|
|
333
|
+
id: hasAriaLabel ? void 0 : contentId,
|
|
334
|
+
"data-placement": state.context.currentPlacement,
|
|
335
|
+
onPointerEnter() {
|
|
336
|
+
send("TOOLTIP_POINTER_ENTER");
|
|
337
|
+
},
|
|
338
|
+
onPointerLeave() {
|
|
339
|
+
send("TOOLTIP_POINTER_LEAVE");
|
|
340
|
+
},
|
|
341
|
+
style: {
|
|
342
|
+
pointerEvents: state.context.interactive ? "auto" : "none"
|
|
343
|
+
}
|
|
344
|
+
}),
|
|
345
|
+
labelProps: normalize.element({
|
|
346
|
+
"data-part": "label",
|
|
347
|
+
id: contentId,
|
|
348
|
+
role: "tooltip",
|
|
349
|
+
style: visuallyHiddenStyle,
|
|
350
|
+
children: state.context["aria-label"]
|
|
351
|
+
}),
|
|
352
|
+
createPortal() {
|
|
353
|
+
const doc = dom.getDoc(state.context);
|
|
354
|
+
const exist = dom.getPortalEl(state.context);
|
|
355
|
+
if (exist)
|
|
356
|
+
return exist;
|
|
357
|
+
const portal = dom.createPortalEl(state.context);
|
|
358
|
+
doc.body.appendChild(portal);
|
|
359
|
+
return portal;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/tooltip.machine.ts
|
|
365
|
+
var import_core2 = require("@zag-js/core");
|
|
366
|
+
var import_popper2 = require("@zag-js/popper");
|
|
367
|
+
function machine(ctx) {
|
|
368
|
+
return (0, import_core2.createMachine)(
|
|
369
|
+
{
|
|
370
|
+
id: "tooltip",
|
|
371
|
+
initial: "unknown",
|
|
372
|
+
context: {
|
|
373
|
+
openDelay: 1e3,
|
|
374
|
+
closeDelay: 500,
|
|
375
|
+
closeOnPointerDown: true,
|
|
376
|
+
closeOnEsc: true,
|
|
377
|
+
interactive: true,
|
|
378
|
+
currentPlacement: void 0,
|
|
379
|
+
...ctx,
|
|
380
|
+
positioning: {
|
|
381
|
+
placement: "bottom",
|
|
382
|
+
...ctx.positioning
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
computed: {
|
|
386
|
+
hasAriaLabel: (ctx2) => !!ctx2["aria-label"]
|
|
387
|
+
},
|
|
388
|
+
on: {
|
|
389
|
+
OPEN: "open",
|
|
390
|
+
CLOSE: "closed"
|
|
391
|
+
},
|
|
392
|
+
states: {
|
|
393
|
+
unknown: {
|
|
394
|
+
on: {
|
|
395
|
+
SETUP: "closed"
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
closed: {
|
|
399
|
+
tags: ["closed"],
|
|
400
|
+
entry: ["clearGlobalId", "invokeOnClose"],
|
|
401
|
+
on: {
|
|
402
|
+
FOCUS: "open",
|
|
403
|
+
POINTER_ENTER: [
|
|
404
|
+
{
|
|
405
|
+
guard: "noVisibleTooltip",
|
|
406
|
+
target: "opening"
|
|
407
|
+
},
|
|
408
|
+
{ target: "open" }
|
|
409
|
+
]
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
opening: {
|
|
413
|
+
tags: ["closed"],
|
|
414
|
+
activities: ["trackScroll", "trackPointerlockChange"],
|
|
415
|
+
after: {
|
|
416
|
+
OPEN_DELAY: "open"
|
|
417
|
+
},
|
|
418
|
+
on: {
|
|
419
|
+
POINTER_LEAVE: "closed",
|
|
420
|
+
BLUR: "closed",
|
|
421
|
+
SCROLL: "closed",
|
|
422
|
+
POINTER_LOCK_CHANGE: "closed",
|
|
423
|
+
POINTER_DOWN: {
|
|
424
|
+
guard: "closeOnPointerDown",
|
|
425
|
+
target: "closed"
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
open: {
|
|
430
|
+
tags: ["open"],
|
|
431
|
+
activities: [
|
|
432
|
+
"trackEscapeKey",
|
|
433
|
+
"trackDisabledTriggerOnSafari",
|
|
434
|
+
"trackScroll",
|
|
435
|
+
"trackPointerlockChange",
|
|
436
|
+
"computePlacement"
|
|
437
|
+
],
|
|
438
|
+
entry: ["setGlobalId", "invokeOnOpen"],
|
|
439
|
+
on: {
|
|
440
|
+
POINTER_LEAVE: [
|
|
441
|
+
{
|
|
442
|
+
guard: "isVisible",
|
|
443
|
+
target: "closing"
|
|
444
|
+
},
|
|
445
|
+
{ target: "closed" }
|
|
446
|
+
],
|
|
447
|
+
BLUR: "closed",
|
|
448
|
+
ESCAPE: "closed",
|
|
449
|
+
SCROLL: "closed",
|
|
450
|
+
POINTER_LOCK_CHANGE: "closed",
|
|
451
|
+
TOOLTIP_POINTER_LEAVE: {
|
|
452
|
+
guard: "isInteractive",
|
|
453
|
+
target: "closing"
|
|
454
|
+
},
|
|
455
|
+
POINTER_DOWN: {
|
|
456
|
+
guard: "closeOnPointerDown",
|
|
457
|
+
target: "closed"
|
|
458
|
+
},
|
|
459
|
+
CLICK: "closed"
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
closing: {
|
|
463
|
+
tags: ["open"],
|
|
464
|
+
activities: ["trackStore", "computePlacement"],
|
|
465
|
+
after: {
|
|
466
|
+
CLOSE_DELAY: "closed"
|
|
467
|
+
},
|
|
468
|
+
on: {
|
|
469
|
+
FORCE_CLOSE: "closed",
|
|
470
|
+
POINTER_ENTER: "open",
|
|
471
|
+
TOOLTIP_POINTER_ENTER: {
|
|
472
|
+
guard: "isInteractive",
|
|
473
|
+
target: "open"
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
activities: {
|
|
481
|
+
computePlacement(ctx2) {
|
|
482
|
+
ctx2.currentPlacement = ctx2.positioning.placement;
|
|
483
|
+
let cleanup;
|
|
484
|
+
raf(() => {
|
|
485
|
+
cleanup = (0, import_popper2.getPlacement)(dom.getTriggerEl(ctx2), dom.getPositionerEl(ctx2), {
|
|
486
|
+
...ctx2.positioning,
|
|
487
|
+
onComplete(data) {
|
|
488
|
+
ctx2.currentPlacement = data.placement;
|
|
489
|
+
ctx2.isPlacementComplete = true;
|
|
490
|
+
},
|
|
491
|
+
onCleanup() {
|
|
492
|
+
ctx2.currentPlacement = void 0;
|
|
493
|
+
ctx2.isPlacementComplete = false;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
return cleanup;
|
|
498
|
+
},
|
|
499
|
+
trackPointerlockChange(ctx2, _evt, { send }) {
|
|
500
|
+
return addPointerlockChangeListener(dom.getDoc(ctx2), () => {
|
|
501
|
+
send("POINTER_LOCK_CHANGE");
|
|
502
|
+
});
|
|
503
|
+
},
|
|
504
|
+
trackScroll(ctx2, _evt, { send }) {
|
|
505
|
+
const trigger = dom.getTriggerEl(ctx2);
|
|
506
|
+
if (!trigger)
|
|
507
|
+
return;
|
|
508
|
+
const cleanups = getScrollParents(trigger).map((el) => {
|
|
509
|
+
const opts = { passive: true, capture: true };
|
|
510
|
+
return addDomEvent(el, "scroll", () => send("SCROLL"), opts);
|
|
511
|
+
});
|
|
512
|
+
return () => {
|
|
513
|
+
cleanups.forEach((fn) => fn == null ? void 0 : fn());
|
|
514
|
+
};
|
|
515
|
+
},
|
|
516
|
+
trackStore(ctx2, _evt, { send }) {
|
|
517
|
+
return (0, import_core2.subscribe)(store, () => {
|
|
518
|
+
if (store.id !== ctx2.id) {
|
|
519
|
+
send("FORCE_CLOSE");
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
trackDisabledTriggerOnSafari(ctx2, _evt, { send }) {
|
|
524
|
+
if (!isSafari())
|
|
525
|
+
return;
|
|
526
|
+
const doc = dom.getDoc(ctx2);
|
|
527
|
+
return addPointerEvent(doc, "pointermove", (event) => {
|
|
528
|
+
const selector = "[data-part=trigger][data-expanded]";
|
|
529
|
+
if (isHTMLElement(event.target) && event.target.closest(selector))
|
|
530
|
+
return;
|
|
531
|
+
send("POINTER_LEAVE");
|
|
532
|
+
});
|
|
533
|
+
},
|
|
534
|
+
trackEscapeKey(ctx2, _evt, { send }) {
|
|
535
|
+
if (!ctx2.closeOnEsc)
|
|
536
|
+
return;
|
|
537
|
+
const doc = dom.getDoc(ctx2);
|
|
538
|
+
return addDomEvent(doc, "keydown", (event) => {
|
|
539
|
+
if (event.key === "Escape") {
|
|
540
|
+
send("ESCAPE");
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
actions: {
|
|
546
|
+
setGlobalId(ctx2) {
|
|
547
|
+
store.setId(ctx2.id);
|
|
548
|
+
},
|
|
549
|
+
clearGlobalId(ctx2) {
|
|
550
|
+
if (ctx2.id === store.id) {
|
|
551
|
+
store.setId(null);
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
invokeOnOpen(ctx2, evt) {
|
|
555
|
+
var _a;
|
|
556
|
+
const omit = ["TOOLTIP_POINTER_ENTER", "POINTER_ENTER"];
|
|
557
|
+
if (!omit.includes(evt.type)) {
|
|
558
|
+
(_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
|
|
559
|
+
}
|
|
560
|
+
},
|
|
561
|
+
invokeOnClose(ctx2, evt) {
|
|
562
|
+
var _a;
|
|
563
|
+
const omit = ["SETUP"];
|
|
564
|
+
if (!omit.includes(evt.type)) {
|
|
565
|
+
(_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
},
|
|
569
|
+
guards: {
|
|
570
|
+
closeOnPointerDown: (ctx2) => ctx2.closeOnPointerDown,
|
|
571
|
+
noVisibleTooltip: () => store.id === null,
|
|
572
|
+
isVisible: (ctx2) => ctx2.id === store.id,
|
|
573
|
+
isInteractive: (ctx2) => ctx2.interactive
|
|
574
|
+
},
|
|
575
|
+
delays: {
|
|
576
|
+
OPEN_DELAY: (ctx2) => ctx2.openDelay,
|
|
577
|
+
CLOSE_DELAY: (ctx2) => ctx2.closeDelay
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
583
|
+
0 && (module.exports = {
|
|
584
|
+
connect,
|
|
585
|
+
machine
|
|
586
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
// ../../utilities/dom/dist/index.mjs
|
|
2
|
+
var dataAttr = (guard) => {
|
|
3
|
+
return guard ? "" : void 0;
|
|
4
|
+
};
|
|
5
|
+
function getCache() {
|
|
6
|
+
const g = globalThis;
|
|
7
|
+
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
|
|
8
|
+
return g.__styleCache;
|
|
9
|
+
}
|
|
10
|
+
function getComputedStyle(el) {
|
|
11
|
+
if (!el)
|
|
12
|
+
return {};
|
|
13
|
+
const cache = getCache();
|
|
14
|
+
let style = cache.get(el);
|
|
15
|
+
if (!style) {
|
|
16
|
+
const win = (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
|
|
17
|
+
style = win.getComputedStyle(el);
|
|
18
|
+
cache.set(el, style);
|
|
19
|
+
}
|
|
20
|
+
return style;
|
|
21
|
+
}
|
|
22
|
+
var runIfFn = (v, ...a) => {
|
|
23
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
24
|
+
return res ?? void 0;
|
|
25
|
+
};
|
|
26
|
+
var isArray = (v) => Array.isArray(v);
|
|
27
|
+
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
|
|
28
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
29
|
+
var isDom = () => typeof window !== "undefined";
|
|
30
|
+
function getPlatform() {
|
|
31
|
+
const agent = navigator.userAgentData;
|
|
32
|
+
return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
|
|
33
|
+
}
|
|
34
|
+
var pt = (v) => isDom() && v.test(getPlatform());
|
|
35
|
+
var vn = (v) => isDom() && v.test(navigator.vendor);
|
|
36
|
+
var isSafari = () => isApple() && vn(/apple/i);
|
|
37
|
+
var isApple = () => pt(/mac|iphone|ipad|ipod/i);
|
|
38
|
+
function isDocument(el) {
|
|
39
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
40
|
+
}
|
|
41
|
+
function isWindow(value) {
|
|
42
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
43
|
+
}
|
|
44
|
+
function getDocument(el) {
|
|
45
|
+
if (isWindow(el))
|
|
46
|
+
return el.document;
|
|
47
|
+
if (isDocument(el))
|
|
48
|
+
return el;
|
|
49
|
+
return (el == null ? void 0 : el.ownerDocument) ?? document;
|
|
50
|
+
}
|
|
51
|
+
function getWindow(el) {
|
|
52
|
+
return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
|
|
53
|
+
}
|
|
54
|
+
function getNodeName(node) {
|
|
55
|
+
return isWindow(node) ? "" : (node == null ? void 0 : node.localName) ?? "";
|
|
56
|
+
}
|
|
57
|
+
function getParent(el) {
|
|
58
|
+
const doc = getDocument(el);
|
|
59
|
+
if (getNodeName(el) === "html")
|
|
60
|
+
return el;
|
|
61
|
+
return el.assignedSlot || el.parentElement || doc.documentElement;
|
|
62
|
+
}
|
|
63
|
+
function defineDomHelpers(helpers) {
|
|
64
|
+
const dom2 = {
|
|
65
|
+
getRootNode: (ctx) => {
|
|
66
|
+
var _a;
|
|
67
|
+
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
|
|
68
|
+
},
|
|
69
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
70
|
+
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
71
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
72
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
...dom2,
|
|
76
|
+
...helpers
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function isHTMLElement(v) {
|
|
80
|
+
return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
|
|
81
|
+
}
|
|
82
|
+
var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
|
|
83
|
+
var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
|
|
84
|
+
var supportsMouseEvent = () => isDom() && window.onmousedown === null;
|
|
85
|
+
var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
|
|
86
|
+
var isRef = (v) => hasProp(v, "current");
|
|
87
|
+
var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
|
|
88
|
+
function extractInfo(event, type = "page") {
|
|
89
|
+
const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
|
|
90
|
+
return {
|
|
91
|
+
point: {
|
|
92
|
+
x: point[`${type}X`],
|
|
93
|
+
y: point[`${type}Y`]
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function addDomEvent(target, eventName, handler, options) {
|
|
98
|
+
const node = isRef(target) ? target.current : runIfFn(target);
|
|
99
|
+
node == null ? void 0 : node.addEventListener(eventName, handler, options);
|
|
100
|
+
return () => {
|
|
101
|
+
node == null ? void 0 : node.removeEventListener(eventName, handler, options);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function addPointerEvent(target, event, listener, options) {
|
|
105
|
+
const type = getEventName(event) ?? event;
|
|
106
|
+
return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
|
|
107
|
+
}
|
|
108
|
+
function wrapHandler(fn, filter = false) {
|
|
109
|
+
const listener = (event) => {
|
|
110
|
+
fn(event, extractInfo(event));
|
|
111
|
+
};
|
|
112
|
+
return filter ? filterPrimaryPointer(listener) : listener;
|
|
113
|
+
}
|
|
114
|
+
function filterPrimaryPointer(fn) {
|
|
115
|
+
return (event) => {
|
|
116
|
+
const win = event.view ?? window;
|
|
117
|
+
const isMouseEvent2 = event instanceof win.MouseEvent;
|
|
118
|
+
const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
|
|
119
|
+
if (isPrimary)
|
|
120
|
+
fn(event);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
var mouseEventNames = {
|
|
124
|
+
pointerdown: "mousedown",
|
|
125
|
+
pointermove: "mousemove",
|
|
126
|
+
pointerup: "mouseup",
|
|
127
|
+
pointercancel: "mousecancel",
|
|
128
|
+
pointerover: "mouseover",
|
|
129
|
+
pointerout: "mouseout",
|
|
130
|
+
pointerenter: "mouseenter",
|
|
131
|
+
pointerleave: "mouseleave"
|
|
132
|
+
};
|
|
133
|
+
var touchEventNames = {
|
|
134
|
+
pointerdown: "touchstart",
|
|
135
|
+
pointermove: "touchmove",
|
|
136
|
+
pointerup: "touchend",
|
|
137
|
+
pointercancel: "touchcancel"
|
|
138
|
+
};
|
|
139
|
+
function getEventName(evt) {
|
|
140
|
+
if (supportsPointerEvent())
|
|
141
|
+
return evt;
|
|
142
|
+
if (supportsTouchEvent())
|
|
143
|
+
return touchEventNames[evt];
|
|
144
|
+
if (supportsMouseEvent())
|
|
145
|
+
return mouseEventNames[evt];
|
|
146
|
+
return evt;
|
|
147
|
+
}
|
|
148
|
+
function raf(fn) {
|
|
149
|
+
const id = globalThis.requestAnimationFrame(fn);
|
|
150
|
+
return function cleanup() {
|
|
151
|
+
globalThis.cancelAnimationFrame(id);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function addPointerlockChangeListener(doc, fn) {
|
|
155
|
+
return addDomEvent(doc, "pointerlockchange", fn, false);
|
|
156
|
+
}
|
|
157
|
+
function isScrollParent(el) {
|
|
158
|
+
const { overflow, overflowX, overflowY } = getComputedStyle(el);
|
|
159
|
+
return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
|
|
160
|
+
}
|
|
161
|
+
function getScrollParent(el) {
|
|
162
|
+
if (["html", "body", "#document"].includes(getNodeName(el))) {
|
|
163
|
+
return getDocument(el).body;
|
|
164
|
+
}
|
|
165
|
+
if (isHTMLElement(el) && isScrollParent(el)) {
|
|
166
|
+
return el;
|
|
167
|
+
}
|
|
168
|
+
return getScrollParent(getParent(el));
|
|
169
|
+
}
|
|
170
|
+
function getScrollParents(el, list = []) {
|
|
171
|
+
const scrollParent = getScrollParent(el);
|
|
172
|
+
const isBody = scrollParent === getDocument(el).body;
|
|
173
|
+
const win = getWindow(scrollParent);
|
|
174
|
+
const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent;
|
|
175
|
+
const parents = list.concat(target);
|
|
176
|
+
if (isBody)
|
|
177
|
+
return parents;
|
|
178
|
+
return parents.concat(getScrollParents(getParent(target)));
|
|
179
|
+
}
|
|
180
|
+
var visuallyHiddenStyle = {
|
|
181
|
+
border: "0",
|
|
182
|
+
clip: "rect(0 0 0 0)",
|
|
183
|
+
height: "1px",
|
|
184
|
+
margin: "-1px",
|
|
185
|
+
overflow: "hidden",
|
|
186
|
+
padding: "0",
|
|
187
|
+
position: "absolute",
|
|
188
|
+
width: "1px",
|
|
189
|
+
whiteSpace: "nowrap",
|
|
190
|
+
wordWrap: "normal"
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// src/tooltip.connect.ts
|
|
194
|
+
import { getPlacementStyles } from "@zag-js/popper";
|
|
195
|
+
|
|
196
|
+
// src/tooltip.dom.ts
|
|
197
|
+
var dom = defineDomHelpers({
|
|
198
|
+
getTriggerId: (ctx) => {
|
|
199
|
+
var _a;
|
|
200
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.trigger) ?? `tooltip:${ctx.id}:trigger`;
|
|
201
|
+
},
|
|
202
|
+
getContentId: (ctx) => {
|
|
203
|
+
var _a;
|
|
204
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.content) ?? `tooltip:${ctx.id}:content`;
|
|
205
|
+
},
|
|
206
|
+
getArrowId: (ctx) => `tooltip:${ctx.id}:arrow`,
|
|
207
|
+
getPositionerId: (ctx) => `tooltip:${ctx.id}:popper`,
|
|
208
|
+
portalId: "tooltip-portal",
|
|
209
|
+
getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
|
|
210
|
+
getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
|
|
211
|
+
getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
|
|
212
|
+
getArrowEl: (ctx) => dom.getById(ctx, dom.getArrowId(ctx)),
|
|
213
|
+
getScrollParent: (ctx) => getScrollParent(dom.getTriggerEl(ctx)),
|
|
214
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.portalId),
|
|
215
|
+
createPortalEl: (ctx) => {
|
|
216
|
+
const portal = dom.getDoc(ctx).createElement(dom.portalId);
|
|
217
|
+
portal.id = dom.portalId;
|
|
218
|
+
return portal;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// src/tooltip.store.ts
|
|
223
|
+
import { proxy } from "@zag-js/core";
|
|
224
|
+
var store = proxy({
|
|
225
|
+
id: null,
|
|
226
|
+
prevId: null,
|
|
227
|
+
setId(val) {
|
|
228
|
+
this.prevId = this.id;
|
|
229
|
+
this.id = val;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// src/tooltip.connect.ts
|
|
234
|
+
function connect(state, send, normalize) {
|
|
235
|
+
const id = state.context.id;
|
|
236
|
+
const hasAriaLabel = state.context.hasAriaLabel;
|
|
237
|
+
const isOpen = state.hasTag("open");
|
|
238
|
+
const triggerId = dom.getTriggerId(state.context);
|
|
239
|
+
const contentId = dom.getContentId(state.context);
|
|
240
|
+
const popperStyles = getPlacementStyles({
|
|
241
|
+
measured: !!state.context.isPlacementComplete,
|
|
242
|
+
placement: state.context.currentPlacement
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
isOpen,
|
|
246
|
+
open() {
|
|
247
|
+
send("OPEN");
|
|
248
|
+
},
|
|
249
|
+
close() {
|
|
250
|
+
send("CLOSE");
|
|
251
|
+
},
|
|
252
|
+
getAnimationState() {
|
|
253
|
+
return {
|
|
254
|
+
enter: store.prevId === null && id === store.id,
|
|
255
|
+
exit: store.id === null
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
triggerProps: normalize.button({
|
|
259
|
+
"data-part": "trigger",
|
|
260
|
+
id: triggerId,
|
|
261
|
+
"data-expanded": dataAttr(isOpen),
|
|
262
|
+
"aria-describedby": isOpen ? contentId : void 0,
|
|
263
|
+
onClick() {
|
|
264
|
+
send("CLICK");
|
|
265
|
+
},
|
|
266
|
+
onFocus() {
|
|
267
|
+
send("FOCUS");
|
|
268
|
+
},
|
|
269
|
+
onBlur() {
|
|
270
|
+
if (id === store.id) {
|
|
271
|
+
send("BLUR");
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
onPointerDown() {
|
|
275
|
+
if (id === store.id) {
|
|
276
|
+
send("POINTER_DOWN");
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
onPointerMove() {
|
|
280
|
+
send("POINTER_ENTER");
|
|
281
|
+
},
|
|
282
|
+
onPointerLeave() {
|
|
283
|
+
send("POINTER_LEAVE");
|
|
284
|
+
},
|
|
285
|
+
onPointerCancel() {
|
|
286
|
+
send("POINTER_LEAVE");
|
|
287
|
+
}
|
|
288
|
+
}),
|
|
289
|
+
arrowProps: normalize.element({
|
|
290
|
+
id: dom.getArrowId(state.context),
|
|
291
|
+
"data-part": "arrow",
|
|
292
|
+
style: popperStyles.arrow
|
|
293
|
+
}),
|
|
294
|
+
innerArrowProps: normalize.element({
|
|
295
|
+
"data-part": "arrow-inner",
|
|
296
|
+
style: popperStyles.innerArrow
|
|
297
|
+
}),
|
|
298
|
+
positionerProps: normalize.element({
|
|
299
|
+
id: dom.getPositionerId(state.context),
|
|
300
|
+
"data-part": "positioner",
|
|
301
|
+
style: popperStyles.floating
|
|
302
|
+
}),
|
|
303
|
+
contentProps: normalize.element({
|
|
304
|
+
"data-part": "content",
|
|
305
|
+
role: hasAriaLabel ? void 0 : "tooltip",
|
|
306
|
+
id: hasAriaLabel ? void 0 : contentId,
|
|
307
|
+
"data-placement": state.context.currentPlacement,
|
|
308
|
+
onPointerEnter() {
|
|
309
|
+
send("TOOLTIP_POINTER_ENTER");
|
|
310
|
+
},
|
|
311
|
+
onPointerLeave() {
|
|
312
|
+
send("TOOLTIP_POINTER_LEAVE");
|
|
313
|
+
},
|
|
314
|
+
style: {
|
|
315
|
+
pointerEvents: state.context.interactive ? "auto" : "none"
|
|
316
|
+
}
|
|
317
|
+
}),
|
|
318
|
+
labelProps: normalize.element({
|
|
319
|
+
"data-part": "label",
|
|
320
|
+
id: contentId,
|
|
321
|
+
role: "tooltip",
|
|
322
|
+
style: visuallyHiddenStyle,
|
|
323
|
+
children: state.context["aria-label"]
|
|
324
|
+
}),
|
|
325
|
+
createPortal() {
|
|
326
|
+
const doc = dom.getDoc(state.context);
|
|
327
|
+
const exist = dom.getPortalEl(state.context);
|
|
328
|
+
if (exist)
|
|
329
|
+
return exist;
|
|
330
|
+
const portal = dom.createPortalEl(state.context);
|
|
331
|
+
doc.body.appendChild(portal);
|
|
332
|
+
return portal;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/tooltip.machine.ts
|
|
338
|
+
import { createMachine, subscribe } from "@zag-js/core";
|
|
339
|
+
import { getPlacement } from "@zag-js/popper";
|
|
340
|
+
function machine(ctx) {
|
|
341
|
+
return createMachine(
|
|
342
|
+
{
|
|
343
|
+
id: "tooltip",
|
|
344
|
+
initial: "unknown",
|
|
345
|
+
context: {
|
|
346
|
+
openDelay: 1e3,
|
|
347
|
+
closeDelay: 500,
|
|
348
|
+
closeOnPointerDown: true,
|
|
349
|
+
closeOnEsc: true,
|
|
350
|
+
interactive: true,
|
|
351
|
+
currentPlacement: void 0,
|
|
352
|
+
...ctx,
|
|
353
|
+
positioning: {
|
|
354
|
+
placement: "bottom",
|
|
355
|
+
...ctx.positioning
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
computed: {
|
|
359
|
+
hasAriaLabel: (ctx2) => !!ctx2["aria-label"]
|
|
360
|
+
},
|
|
361
|
+
on: {
|
|
362
|
+
OPEN: "open",
|
|
363
|
+
CLOSE: "closed"
|
|
364
|
+
},
|
|
365
|
+
states: {
|
|
366
|
+
unknown: {
|
|
367
|
+
on: {
|
|
368
|
+
SETUP: "closed"
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
closed: {
|
|
372
|
+
tags: ["closed"],
|
|
373
|
+
entry: ["clearGlobalId", "invokeOnClose"],
|
|
374
|
+
on: {
|
|
375
|
+
FOCUS: "open",
|
|
376
|
+
POINTER_ENTER: [
|
|
377
|
+
{
|
|
378
|
+
guard: "noVisibleTooltip",
|
|
379
|
+
target: "opening"
|
|
380
|
+
},
|
|
381
|
+
{ target: "open" }
|
|
382
|
+
]
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
opening: {
|
|
386
|
+
tags: ["closed"],
|
|
387
|
+
activities: ["trackScroll", "trackPointerlockChange"],
|
|
388
|
+
after: {
|
|
389
|
+
OPEN_DELAY: "open"
|
|
390
|
+
},
|
|
391
|
+
on: {
|
|
392
|
+
POINTER_LEAVE: "closed",
|
|
393
|
+
BLUR: "closed",
|
|
394
|
+
SCROLL: "closed",
|
|
395
|
+
POINTER_LOCK_CHANGE: "closed",
|
|
396
|
+
POINTER_DOWN: {
|
|
397
|
+
guard: "closeOnPointerDown",
|
|
398
|
+
target: "closed"
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
open: {
|
|
403
|
+
tags: ["open"],
|
|
404
|
+
activities: [
|
|
405
|
+
"trackEscapeKey",
|
|
406
|
+
"trackDisabledTriggerOnSafari",
|
|
407
|
+
"trackScroll",
|
|
408
|
+
"trackPointerlockChange",
|
|
409
|
+
"computePlacement"
|
|
410
|
+
],
|
|
411
|
+
entry: ["setGlobalId", "invokeOnOpen"],
|
|
412
|
+
on: {
|
|
413
|
+
POINTER_LEAVE: [
|
|
414
|
+
{
|
|
415
|
+
guard: "isVisible",
|
|
416
|
+
target: "closing"
|
|
417
|
+
},
|
|
418
|
+
{ target: "closed" }
|
|
419
|
+
],
|
|
420
|
+
BLUR: "closed",
|
|
421
|
+
ESCAPE: "closed",
|
|
422
|
+
SCROLL: "closed",
|
|
423
|
+
POINTER_LOCK_CHANGE: "closed",
|
|
424
|
+
TOOLTIP_POINTER_LEAVE: {
|
|
425
|
+
guard: "isInteractive",
|
|
426
|
+
target: "closing"
|
|
427
|
+
},
|
|
428
|
+
POINTER_DOWN: {
|
|
429
|
+
guard: "closeOnPointerDown",
|
|
430
|
+
target: "closed"
|
|
431
|
+
},
|
|
432
|
+
CLICK: "closed"
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
closing: {
|
|
436
|
+
tags: ["open"],
|
|
437
|
+
activities: ["trackStore", "computePlacement"],
|
|
438
|
+
after: {
|
|
439
|
+
CLOSE_DELAY: "closed"
|
|
440
|
+
},
|
|
441
|
+
on: {
|
|
442
|
+
FORCE_CLOSE: "closed",
|
|
443
|
+
POINTER_ENTER: "open",
|
|
444
|
+
TOOLTIP_POINTER_ENTER: {
|
|
445
|
+
guard: "isInteractive",
|
|
446
|
+
target: "open"
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
activities: {
|
|
454
|
+
computePlacement(ctx2) {
|
|
455
|
+
ctx2.currentPlacement = ctx2.positioning.placement;
|
|
456
|
+
let cleanup;
|
|
457
|
+
raf(() => {
|
|
458
|
+
cleanup = getPlacement(dom.getTriggerEl(ctx2), dom.getPositionerEl(ctx2), {
|
|
459
|
+
...ctx2.positioning,
|
|
460
|
+
onComplete(data) {
|
|
461
|
+
ctx2.currentPlacement = data.placement;
|
|
462
|
+
ctx2.isPlacementComplete = true;
|
|
463
|
+
},
|
|
464
|
+
onCleanup() {
|
|
465
|
+
ctx2.currentPlacement = void 0;
|
|
466
|
+
ctx2.isPlacementComplete = false;
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
return cleanup;
|
|
471
|
+
},
|
|
472
|
+
trackPointerlockChange(ctx2, _evt, { send }) {
|
|
473
|
+
return addPointerlockChangeListener(dom.getDoc(ctx2), () => {
|
|
474
|
+
send("POINTER_LOCK_CHANGE");
|
|
475
|
+
});
|
|
476
|
+
},
|
|
477
|
+
trackScroll(ctx2, _evt, { send }) {
|
|
478
|
+
const trigger = dom.getTriggerEl(ctx2);
|
|
479
|
+
if (!trigger)
|
|
480
|
+
return;
|
|
481
|
+
const cleanups = getScrollParents(trigger).map((el) => {
|
|
482
|
+
const opts = { passive: true, capture: true };
|
|
483
|
+
return addDomEvent(el, "scroll", () => send("SCROLL"), opts);
|
|
484
|
+
});
|
|
485
|
+
return () => {
|
|
486
|
+
cleanups.forEach((fn) => fn == null ? void 0 : fn());
|
|
487
|
+
};
|
|
488
|
+
},
|
|
489
|
+
trackStore(ctx2, _evt, { send }) {
|
|
490
|
+
return subscribe(store, () => {
|
|
491
|
+
if (store.id !== ctx2.id) {
|
|
492
|
+
send("FORCE_CLOSE");
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
},
|
|
496
|
+
trackDisabledTriggerOnSafari(ctx2, _evt, { send }) {
|
|
497
|
+
if (!isSafari())
|
|
498
|
+
return;
|
|
499
|
+
const doc = dom.getDoc(ctx2);
|
|
500
|
+
return addPointerEvent(doc, "pointermove", (event) => {
|
|
501
|
+
const selector = "[data-part=trigger][data-expanded]";
|
|
502
|
+
if (isHTMLElement(event.target) && event.target.closest(selector))
|
|
503
|
+
return;
|
|
504
|
+
send("POINTER_LEAVE");
|
|
505
|
+
});
|
|
506
|
+
},
|
|
507
|
+
trackEscapeKey(ctx2, _evt, { send }) {
|
|
508
|
+
if (!ctx2.closeOnEsc)
|
|
509
|
+
return;
|
|
510
|
+
const doc = dom.getDoc(ctx2);
|
|
511
|
+
return addDomEvent(doc, "keydown", (event) => {
|
|
512
|
+
if (event.key === "Escape") {
|
|
513
|
+
send("ESCAPE");
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
actions: {
|
|
519
|
+
setGlobalId(ctx2) {
|
|
520
|
+
store.setId(ctx2.id);
|
|
521
|
+
},
|
|
522
|
+
clearGlobalId(ctx2) {
|
|
523
|
+
if (ctx2.id === store.id) {
|
|
524
|
+
store.setId(null);
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
invokeOnOpen(ctx2, evt) {
|
|
528
|
+
var _a;
|
|
529
|
+
const omit = ["TOOLTIP_POINTER_ENTER", "POINTER_ENTER"];
|
|
530
|
+
if (!omit.includes(evt.type)) {
|
|
531
|
+
(_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
invokeOnClose(ctx2, evt) {
|
|
535
|
+
var _a;
|
|
536
|
+
const omit = ["SETUP"];
|
|
537
|
+
if (!omit.includes(evt.type)) {
|
|
538
|
+
(_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
},
|
|
542
|
+
guards: {
|
|
543
|
+
closeOnPointerDown: (ctx2) => ctx2.closeOnPointerDown,
|
|
544
|
+
noVisibleTooltip: () => store.id === null,
|
|
545
|
+
isVisible: (ctx2) => ctx2.id === store.id,
|
|
546
|
+
isInteractive: (ctx2) => ctx2.interactive
|
|
547
|
+
},
|
|
548
|
+
delays: {
|
|
549
|
+
OPEN_DELAY: (ctx2) => ctx2.openDelay,
|
|
550
|
+
CLOSE_DELAY: (ctx2) => ctx2.closeDelay
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
export {
|
|
556
|
+
connect,
|
|
557
|
+
machine
|
|
558
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zag-js/tooltip",
|
|
3
|
+
"version": "0.0.0-20220802150625",
|
|
4
|
+
"description": "Core logic for the tooltip widget implemented as a state machine",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"js",
|
|
7
|
+
"machine",
|
|
8
|
+
"xstate",
|
|
9
|
+
"statechart",
|
|
10
|
+
"component",
|
|
11
|
+
"chakra-ui",
|
|
12
|
+
"tooltip"
|
|
13
|
+
],
|
|
14
|
+
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
15
|
+
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"module": "dist/index.mjs",
|
|
20
|
+
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/tooltip",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"files": [
|
|
23
|
+
"dist/**/*"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/chakra-ui/zag/issues"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@zag-js/core": "0.1.9",
|
|
33
|
+
"@zag-js/popper": "0.0.0-20220802150625",
|
|
34
|
+
"@zag-js/types": "0.2.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@zag-js/dom-utils": "0.1.8",
|
|
38
|
+
"@zag-js/utils": "0.1.3"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
42
|
+
"start": "pnpm build --watch",
|
|
43
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
44
|
+
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
45
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
46
|
+
"test-ci": "pnpm test --ci --runInBand",
|
|
47
|
+
"test-watch": "pnpm test --watch -u",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
49
|
+
}
|
|
50
|
+
}
|