@zag-js/tooltip 1.34.1 → 1.35.1

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.
@@ -0,0 +1,396 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/tooltip.machine.ts
31
+ var tooltip_machine_exports = {};
32
+ __export(tooltip_machine_exports, {
33
+ machine: () => machine
34
+ });
35
+ module.exports = __toCommonJS(tooltip_machine_exports);
36
+ var import_core = require("@zag-js/core");
37
+ var import_dom_query = require("@zag-js/dom-query");
38
+ var import_focus_visible = require("@zag-js/focus-visible");
39
+ var import_popper = require("@zag-js/popper");
40
+ var dom = __toESM(require("./tooltip.dom.cjs"));
41
+ var import_tooltip = require("./tooltip.store.cjs");
42
+ var import_utils = require("@zag-js/utils");
43
+ var { and, not } = (0, import_core.createGuards)();
44
+ var machine = (0, import_core.createMachine)({
45
+ initialState: ({ prop }) => {
46
+ const open = prop("open") || prop("defaultOpen");
47
+ return open ? "open" : "closed";
48
+ },
49
+ props({ props }) {
50
+ (0, import_utils.ensureProps)(props, ["id"]);
51
+ const closeOnClick = props.closeOnClick ?? true;
52
+ const closeOnPointerDown = props.closeOnPointerDown ?? closeOnClick;
53
+ return {
54
+ openDelay: 400,
55
+ closeDelay: 150,
56
+ closeOnEscape: true,
57
+ interactive: false,
58
+ closeOnScroll: true,
59
+ disabled: false,
60
+ ...props,
61
+ closeOnPointerDown,
62
+ closeOnClick,
63
+ positioning: {
64
+ placement: "bottom",
65
+ ...props.positioning
66
+ }
67
+ };
68
+ },
69
+ effects: ["trackFocusVisible", "trackStore"],
70
+ context: ({ bindable }) => ({
71
+ currentPlacement: bindable(() => ({ defaultValue: void 0 })),
72
+ hasPointerMoveOpened: bindable(() => ({ defaultValue: false }))
73
+ }),
74
+ watch({ track, action, prop }) {
75
+ track([() => prop("disabled")], () => {
76
+ action(["closeIfDisabled"]);
77
+ });
78
+ track([() => prop("open")], () => {
79
+ action(["toggleVisibility"]);
80
+ });
81
+ },
82
+ states: {
83
+ closed: {
84
+ entry: ["clearGlobalId"],
85
+ on: {
86
+ "controlled.open": {
87
+ target: "open"
88
+ },
89
+ open: [
90
+ {
91
+ guard: "isOpenControlled",
92
+ actions: ["invokeOnOpen"]
93
+ },
94
+ {
95
+ target: "open",
96
+ actions: ["invokeOnOpen"]
97
+ }
98
+ ],
99
+ "pointer.leave": {
100
+ actions: ["clearPointerMoveOpened"]
101
+ },
102
+ "pointer.move": [
103
+ {
104
+ guard: and("noVisibleTooltip", not("hasPointerMoveOpened")),
105
+ target: "opening"
106
+ },
107
+ {
108
+ guard: not("hasPointerMoveOpened"),
109
+ target: "open",
110
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
111
+ }
112
+ ]
113
+ }
114
+ },
115
+ opening: {
116
+ effects: ["trackScroll", "trackPointerlockChange", "waitForOpenDelay"],
117
+ on: {
118
+ "after.openDelay": [
119
+ {
120
+ guard: "isOpenControlled",
121
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
122
+ },
123
+ {
124
+ target: "open",
125
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
126
+ }
127
+ ],
128
+ "controlled.open": {
129
+ target: "open"
130
+ },
131
+ "controlled.close": {
132
+ target: "closed"
133
+ },
134
+ open: [
135
+ {
136
+ guard: "isOpenControlled",
137
+ actions: ["invokeOnOpen"]
138
+ },
139
+ {
140
+ target: "open",
141
+ actions: ["invokeOnOpen"]
142
+ }
143
+ ],
144
+ "pointer.leave": [
145
+ {
146
+ guard: "isOpenControlled",
147
+ // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
148
+ actions: ["clearPointerMoveOpened", "invokeOnClose", "toggleVisibility"]
149
+ },
150
+ {
151
+ target: "closed",
152
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
153
+ }
154
+ ],
155
+ close: [
156
+ {
157
+ guard: "isOpenControlled",
158
+ // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
159
+ actions: ["invokeOnClose", "toggleVisibility"]
160
+ },
161
+ {
162
+ target: "closed",
163
+ actions: ["invokeOnClose"]
164
+ }
165
+ ]
166
+ }
167
+ },
168
+ open: {
169
+ effects: ["trackEscapeKey", "trackScroll", "trackPointerlockChange", "trackPositioning"],
170
+ entry: ["setGlobalId"],
171
+ on: {
172
+ "controlled.close": {
173
+ target: "closed"
174
+ },
175
+ close: [
176
+ {
177
+ guard: "isOpenControlled",
178
+ actions: ["invokeOnClose"]
179
+ },
180
+ {
181
+ target: "closed",
182
+ actions: ["invokeOnClose"]
183
+ }
184
+ ],
185
+ "pointer.leave": [
186
+ {
187
+ guard: "isVisible",
188
+ target: "closing",
189
+ actions: ["clearPointerMoveOpened"]
190
+ },
191
+ // == group ==
192
+ {
193
+ guard: "isOpenControlled",
194
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
195
+ },
196
+ {
197
+ target: "closed",
198
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
199
+ }
200
+ ],
201
+ "content.pointer.leave": {
202
+ guard: "isInteractive",
203
+ target: "closing"
204
+ },
205
+ "positioning.set": {
206
+ actions: ["reposition"]
207
+ }
208
+ }
209
+ },
210
+ closing: {
211
+ effects: ["trackPositioning", "waitForCloseDelay"],
212
+ on: {
213
+ "after.closeDelay": [
214
+ {
215
+ guard: "isOpenControlled",
216
+ actions: ["invokeOnClose"]
217
+ },
218
+ {
219
+ target: "closed",
220
+ actions: ["invokeOnClose"]
221
+ }
222
+ ],
223
+ "controlled.close": {
224
+ target: "closed"
225
+ },
226
+ "controlled.open": {
227
+ target: "open"
228
+ },
229
+ close: [
230
+ {
231
+ guard: "isOpenControlled",
232
+ actions: ["invokeOnClose"]
233
+ },
234
+ {
235
+ target: "closed",
236
+ actions: ["invokeOnClose"]
237
+ }
238
+ ],
239
+ "pointer.move": [
240
+ {
241
+ guard: "isOpenControlled",
242
+ // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
243
+ actions: ["setPointerMoveOpened", "invokeOnOpen", "toggleVisibility"]
244
+ },
245
+ {
246
+ target: "open",
247
+ actions: ["setPointerMoveOpened", "invokeOnOpen"]
248
+ }
249
+ ],
250
+ "content.pointer.move": {
251
+ guard: "isInteractive",
252
+ target: "open"
253
+ },
254
+ "positioning.set": {
255
+ actions: ["reposition"]
256
+ }
257
+ }
258
+ }
259
+ },
260
+ implementations: {
261
+ guards: {
262
+ noVisibleTooltip: () => import_tooltip.store.get("id") === null,
263
+ isVisible: ({ prop }) => prop("id") === import_tooltip.store.get("id"),
264
+ isInteractive: ({ prop }) => !!prop("interactive"),
265
+ hasPointerMoveOpened: ({ context }) => context.get("hasPointerMoveOpened"),
266
+ isOpenControlled: ({ prop }) => prop("open") !== void 0
267
+ },
268
+ actions: {
269
+ setGlobalId: ({ prop }) => {
270
+ const prevId = import_tooltip.store.get("id");
271
+ const isInstant = prevId !== null && prevId !== prop("id");
272
+ import_tooltip.store.update({ id: prop("id"), prevId: isInstant ? prevId : null, instant: isInstant });
273
+ },
274
+ clearGlobalId: ({ prop }) => {
275
+ if (prop("id") === import_tooltip.store.get("id")) {
276
+ import_tooltip.store.update({ id: null, prevId: null, instant: false });
277
+ }
278
+ },
279
+ invokeOnOpen: ({ prop }) => {
280
+ prop("onOpenChange")?.({ open: true });
281
+ },
282
+ invokeOnClose: ({ prop }) => {
283
+ prop("onOpenChange")?.({ open: false });
284
+ },
285
+ closeIfDisabled: ({ prop, send }) => {
286
+ if (!prop("disabled")) return;
287
+ send({ type: "close", src: "disabled.change" });
288
+ },
289
+ reposition: ({ context, event, prop, scope }) => {
290
+ if (event.type !== "positioning.set") return;
291
+ const getPositionerEl2 = () => dom.getPositionerEl(scope);
292
+ return (0, import_popper.getPlacement)(dom.getTriggerEl(scope), getPositionerEl2, {
293
+ ...prop("positioning"),
294
+ ...event.options,
295
+ defer: true,
296
+ listeners: false,
297
+ onComplete(data) {
298
+ context.set("currentPlacement", data.placement);
299
+ }
300
+ });
301
+ },
302
+ toggleVisibility: ({ prop, event, send }) => {
303
+ queueMicrotask(() => {
304
+ send({
305
+ type: prop("open") ? "controlled.open" : "controlled.close",
306
+ previousEvent: event
307
+ });
308
+ });
309
+ },
310
+ setPointerMoveOpened: ({ context }) => {
311
+ context.set("hasPointerMoveOpened", true);
312
+ },
313
+ clearPointerMoveOpened: ({ context }) => {
314
+ context.set("hasPointerMoveOpened", false);
315
+ }
316
+ },
317
+ effects: {
318
+ trackFocusVisible: ({ scope }) => {
319
+ return (0, import_focus_visible.trackFocusVisible)({ root: scope.getRootNode?.() });
320
+ },
321
+ trackPositioning: ({ context, prop, scope }) => {
322
+ if (!context.get("currentPlacement")) {
323
+ context.set("currentPlacement", prop("positioning").placement);
324
+ }
325
+ const getPositionerEl2 = () => dom.getPositionerEl(scope);
326
+ return (0, import_popper.getPlacement)(dom.getTriggerEl(scope), getPositionerEl2, {
327
+ ...prop("positioning"),
328
+ defer: true,
329
+ onComplete(data) {
330
+ context.set("currentPlacement", data.placement);
331
+ }
332
+ });
333
+ },
334
+ trackPointerlockChange: ({ send, scope }) => {
335
+ const doc = scope.getDoc();
336
+ const onChange = () => send({ type: "close", src: "pointerlock:change" });
337
+ return (0, import_dom_query.addDomEvent)(doc, "pointerlockchange", onChange, false);
338
+ },
339
+ trackScroll: ({ send, prop, scope }) => {
340
+ if (!prop("closeOnScroll")) return;
341
+ const triggerEl = dom.getTriggerEl(scope);
342
+ if (!triggerEl) return;
343
+ const overflowParents = (0, import_dom_query.getOverflowAncestors)(triggerEl);
344
+ const cleanups = overflowParents.map((overflowParent) => {
345
+ const onScroll = () => {
346
+ send({ type: "close", src: "scroll" });
347
+ };
348
+ return (0, import_dom_query.addDomEvent)(overflowParent, "scroll", onScroll, {
349
+ passive: true,
350
+ capture: true
351
+ });
352
+ });
353
+ return () => {
354
+ cleanups.forEach((fn) => fn?.());
355
+ };
356
+ },
357
+ trackStore: ({ prop, send }) => {
358
+ let cleanup;
359
+ queueMicrotask(() => {
360
+ cleanup = import_tooltip.store.subscribe(() => {
361
+ if (import_tooltip.store.get("id") !== prop("id")) {
362
+ send({ type: "close", src: "id.change" });
363
+ }
364
+ });
365
+ });
366
+ return () => cleanup?.();
367
+ },
368
+ trackEscapeKey: ({ send, prop }) => {
369
+ if (!prop("closeOnEscape")) return;
370
+ const onKeyDown = (event) => {
371
+ if ((0, import_dom_query.isComposingEvent)(event)) return;
372
+ if (event.key !== "Escape") return;
373
+ event.stopPropagation();
374
+ send({ type: "close", src: "keydown.escape" });
375
+ };
376
+ return (0, import_dom_query.addDomEvent)(document, "keydown", onKeyDown, true);
377
+ },
378
+ waitForOpenDelay: ({ send, prop }) => {
379
+ const id = setTimeout(() => {
380
+ send({ type: "after.openDelay" });
381
+ }, prop("openDelay"));
382
+ return () => clearTimeout(id);
383
+ },
384
+ waitForCloseDelay: ({ send, prop }) => {
385
+ const id = setTimeout(() => {
386
+ send({ type: "after.closeDelay" });
387
+ }, prop("closeDelay"));
388
+ return () => clearTimeout(id);
389
+ }
390
+ }
391
+ }
392
+ });
393
+ // Annotate the CommonJS export names for ESM import in node:
394
+ 0 && (module.exports = {
395
+ machine
396
+ });