@zag-js/tooltip 2.0.0-next.3 → 2.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,446 +1,604 @@
1
- // src/tooltip.machine.ts
2
- import { createGuards, createMachine } from "@zag-js/core";
3
- import { addDomEvent, getOverflowAncestors, isComposingEvent } from "@zag-js/dom-query";
1
+ import { getArrowEl, getContentEl, getPopupEl, getPositionerEl, getTriggerEl, getViewportEl } from "./tooltip.dom.mjs";
2
+ import { isWarm, store } from "./tooltip.store.mjs";
3
+ import { getViewportSizeStyle } from "./tooltip.style.mjs";
4
+ import { AnimationFrame, addDomEvent, getOverflowAncestors, getPopupActivationDirection, isComposingEvent, raf, resizeObserverBorderBox, waitForAnimationEnd } from "@zag-js/dom-query";
4
5
  import { trackFocusVisible } from "@zag-js/focus-visible";
5
6
  import { getPlacement } from "@zag-js/popper";
6
- import { trackSafeArea } from "@zag-js/safe-area";
7
7
  import { ensureProps } from "@zag-js/utils";
8
- import * as dom from "./tooltip.dom.mjs";
9
- import { store } from "./tooltip.store.mjs";
10
- var { and, not } = createGuards();
11
- var machine = createMachine({
12
- initialState: ({ prop }) => {
13
- const open = prop("open") || prop("defaultOpen");
14
- return open ? "open" : "closed";
15
- },
16
- props({ props }) {
17
- ensureProps(props, ["id"]);
18
- const closeOnClick = props.closeOnClick ?? true;
19
- const closeOnPointerDown = props.closeOnPointerDown ?? closeOnClick;
20
- return {
21
- openDelay: 400,
22
- closeDelay: 150,
23
- closeOnEscape: true,
24
- interactive: false,
25
- closeOnScroll: true,
26
- disabled: false,
27
- ...props,
28
- closeOnPointerDown,
29
- closeOnClick,
30
- positioning: {
31
- placement: "bottom",
32
- ...props.positioning
33
- }
34
- };
35
- },
36
- effects: ["trackFocusVisible", "trackStore"],
37
- context: ({ bindable, prop, scope }) => ({
38
- currentPlacement: bindable(() => ({ defaultValue: void 0 })),
39
- hasPointerMoveOpened: bindable(() => ({ defaultValue: null })),
40
- isPointer: bindable(() => ({ defaultValue: false })),
41
- triggerValue: bindable(() => ({
42
- defaultValue: prop("defaultTriggerValue") ?? null,
43
- value: prop("triggerValue"),
44
- onChange(value) {
45
- const onTriggerValueChange = prop("onTriggerValueChange");
46
- if (!onTriggerValueChange) return;
47
- const triggerElement = dom.getActiveTriggerEl(scope, value);
48
- onTriggerValueChange({ value, triggerElement });
49
- }
50
- }))
51
- }),
52
- watch({ track, action, prop }) {
53
- track([() => prop("disabled")], () => {
54
- action(["closeIfDisabled"]);
55
- });
56
- track([() => prop("open")], () => {
57
- action(["toggleVisibility"]);
58
- });
59
- track([() => prop("triggerValue")], () => {
60
- action(["repositionImmediate"]);
61
- });
62
- },
63
- on: {
64
- "TRIGGER_VALUE.SET": {
65
- actions: ["setTriggerValue", "repositionImmediate"]
66
- }
67
- },
68
- states: {
69
- closed: {
70
- entry: ["clearGlobalId", "clearIsPointer"],
71
- on: {
72
- "CONTROLLED.OPEN": {
73
- target: "open"
74
- },
75
- OPEN: [
76
- {
77
- guard: "isOpenControlled",
78
- actions: ["setTriggerValue", "invokeOnOpen"]
79
- },
80
- {
81
- target: "open",
82
- actions: ["setTriggerValue", "invokeOnOpen"]
83
- }
84
- ],
85
- POINTER_LEAVE: {
86
- actions: ["clearPointerMoveOpened"]
87
- },
88
- POINTER_MOVE: [
89
- {
90
- guard: and("noVisibleTooltip", not("hasPointerMoveOpened")),
91
- target: "opening",
92
- actions: ["setTriggerValue"]
93
- },
94
- {
95
- guard: not("hasPointerMoveOpened"),
96
- target: "open",
97
- actions: ["setPointerMoveOpened", "setIsPointer", "invokeOnOpen", "setTriggerValue"]
98
- }
99
- ]
100
- }
101
- },
102
- opening: {
103
- effects: ["trackScroll", "trackPointerlockChange", "waitForOpenDelay"],
104
- on: {
105
- OPEN_DELAY: [
106
- {
107
- guard: "isOpenControlled",
108
- actions: ["setPointerMoveOpened", "setIsPointer", "invokeOnOpen"]
109
- },
110
- {
111
- target: "open",
112
- actions: ["setPointerMoveOpened", "setIsPointer", "invokeOnOpen"]
113
- }
114
- ],
115
- "CONTROLLED.OPEN": {
116
- target: "open"
117
- },
118
- "CONTROLLED.CLOSE": {
119
- target: "closed"
120
- },
121
- OPEN: [
122
- {
123
- guard: "isOpenControlled",
124
- actions: ["setTriggerValue", "invokeOnOpen"]
125
- },
126
- {
127
- target: "open",
128
- actions: ["setTriggerValue", "invokeOnOpen"]
129
- }
130
- ],
131
- POINTER_LEAVE: [
132
- {
133
- guard: "isOpenControlled",
134
- // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
135
- actions: ["clearPointerMoveOpened", "invokeOnClose", "toggleVisibility"]
136
- },
137
- {
138
- target: "closed",
139
- actions: ["clearPointerMoveOpened", "invokeOnClose"]
140
- }
141
- ],
142
- CLOSE: [
143
- {
144
- guard: "isOpenControlled",
145
- // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
146
- actions: ["invokeOnClose", "toggleVisibility"]
147
- },
148
- {
149
- target: "closed",
150
- actions: ["invokeOnClose"]
151
- }
152
- ]
153
- }
154
- },
155
- open: {
156
- effects: ["trackEscapeKey", "trackScroll", "trackPointerlockChange", "trackPositioning", "trackSafeArea"],
157
- entry: ["setGlobalId"],
158
- on: {
159
- "CONTROLLED.CLOSE": {
160
- target: "closed"
161
- },
162
- CLOSE: [
163
- {
164
- guard: "isOpenControlled",
165
- actions: ["invokeOnClose"]
166
- },
167
- {
168
- target: "closed",
169
- actions: ["invokeOnClose"]
170
- }
171
- ],
172
- // Hoverable content means the pointer may be travelling to it (WCAG 1.4.13), so the
173
- // safe area decides when it has really left.
174
- POINTER_LEAVE: [
175
- {
176
- guard: and(not("isInteractive"), "isVisible"),
177
- target: "closing",
178
- actions: ["clearPointerMoveOpened"]
179
- },
180
- // == group ==
181
- {
182
- guard: and(not("isInteractive"), "isOpenControlled"),
183
- actions: ["clearPointerMoveOpened", "invokeOnClose"]
184
- },
185
- {
186
- guard: not("isInteractive"),
187
- target: "closed",
188
- actions: ["clearPointerMoveOpened", "invokeOnClose"]
189
- }
190
- ],
191
- "SAFE_AREA.EXIT": {
192
- target: "closing",
193
- // Same bookkeeping the `POINTER_LEAVE` path does, or `closed` refuses the next hover.
194
- actions: ["clearPointerMoveOpened"]
195
- },
196
- "POSITIONING.SET": {
197
- actions: ["reposition"]
198
- },
199
- "TRIGGER_VALUE.SET": {
200
- // Transition to closing (which cleans up trackPositioning) then immediately back to open
201
- // This re-creates the positioning effect with the new trigger
202
- target: "closing",
203
- actions: ["setTriggerValue", "immediateReopen"]
204
- }
205
- }
206
- },
207
- closing: {
208
- effects: ["trackPositioning", "waitForCloseDelay"],
209
- on: {
210
- CLOSE_DELAY: [
211
- {
212
- guard: "isOpenControlled",
213
- actions: ["invokeOnClose"]
214
- },
215
- {
216
- target: "closed",
217
- actions: ["invokeOnClose"]
218
- }
219
- ],
220
- "CONTROLLED.CLOSE": {
221
- target: "closed"
222
- },
223
- "CONTROLLED.OPEN": {
224
- target: "open"
225
- },
226
- CLOSE: [
227
- {
228
- guard: "isOpenControlled",
229
- actions: ["invokeOnClose"]
230
- },
231
- {
232
- target: "closed",
233
- actions: ["invokeOnClose"]
234
- }
235
- ],
236
- POINTER_MOVE: [
237
- {
238
- guard: "isOpenControlled",
239
- // We trigger toggleVisibility manually since the `ctx.open` has not changed yet (at this point)
240
- actions: ["setPointerMoveOpened", "setIsPointer", "setTriggerValue", "invokeOnOpen", "toggleVisibility"]
241
- },
242
- {
243
- target: "open",
244
- actions: ["setPointerMoveOpened", "setIsPointer", "setTriggerValue", "invokeOnOpen"]
245
- }
246
- ],
247
- "TRIGGER_VALUE.SET": {
248
- target: "open",
249
- actions: ["setTriggerValue", "repositionImmediate"]
250
- },
251
- REOPEN: {
252
- target: "open"
253
- },
254
- CONTENT_POINTER_MOVE: {
255
- guard: "isInteractive",
256
- target: "open"
257
- },
258
- "POSITIONING.SET": {
259
- actions: ["reposition"]
260
- }
261
- }
262
- }
263
- },
264
- implementations: {
265
- guards: {
266
- noVisibleTooltip: () => store.get("id") === null,
267
- isVisible: ({ prop }) => prop("id") === store.get("id"),
268
- isInteractive: ({ prop }) => !!prop("interactive"),
269
- hasPointerMoveOpened: ({ context }) => !!context.get("hasPointerMoveOpened"),
270
- isOpenControlled: ({ prop }) => prop("open") !== void 0
271
- },
272
- actions: {
273
- setGlobalId: ({ prop, event }) => {
274
- const prevId = store.get("id");
275
- const isInstant = event.src === "trigger.focus" || prevId !== null && prevId !== prop("id");
276
- store.update({ id: prop("id"), prevId: isInstant && prevId !== null ? prevId : null, instant: isInstant });
277
- },
278
- clearGlobalId: ({ prop }) => {
279
- if (prop("id") === store.get("id")) {
280
- store.update({ id: null, prevId: null, instant: false });
281
- }
282
- },
283
- invokeOnOpen: ({ prop }) => {
284
- prop("onOpenChange")?.({ open: true });
285
- },
286
- invokeOnClose: ({ prop }) => {
287
- prop("onOpenChange")?.({ open: false });
288
- },
289
- closeIfDisabled: ({ prop, send }) => {
290
- if (!prop("disabled")) return;
291
- send({ type: "CLOSE", src: "disabled.change" });
292
- },
293
- reposition: ({ context, event, prop, scope }) => {
294
- if (event.type !== "POSITIONING.SET") return;
295
- const getPositionerEl2 = () => dom.getPositionerEl(scope);
296
- const getTriggerEl = () => dom.getActiveTriggerEl(scope, context.get("triggerValue"));
297
- getPlacement(getTriggerEl, getPositionerEl2, {
298
- getArrowElement: () => dom.getArrowEl(scope),
299
- ...prop("positioning"),
300
- ...event.options,
301
- listeners: false,
302
- onComplete(data) {
303
- context.set("currentPlacement", data.placement);
304
- }
305
- });
306
- },
307
- repositionImmediate: ({ context, event, prop, scope }) => {
308
- const triggerValue = event.value ?? context.get("triggerValue");
309
- const getPositionerEl2 = () => dom.getPositionerEl(scope);
310
- const getTriggerEl = () => dom.getActiveTriggerEl(scope, triggerValue);
311
- return getPlacement(getTriggerEl, getPositionerEl2, {
312
- getArrowElement: () => dom.getArrowEl(scope),
313
- ...prop("positioning"),
314
- onComplete(data) {
315
- context.set("currentPlacement", data.placement);
316
- }
317
- });
318
- },
319
- toggleVisibility: ({ prop, event, send }) => {
320
- queueMicrotask(() => {
321
- send({
322
- type: prop("open") ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
323
- previousEvent: event
324
- });
325
- });
326
- },
327
- setPointerMoveOpened: ({ context, event }) => {
328
- const triggerId = event.triggerId ?? event.previousEvent?.triggerId;
329
- context.set("hasPointerMoveOpened", triggerId ?? null);
330
- },
331
- clearPointerMoveOpened: ({ context }) => {
332
- context.set("hasPointerMoveOpened", null);
333
- },
334
- setIsPointer: ({ context }) => {
335
- context.set("isPointer", true);
336
- },
337
- clearIsPointer: ({ context }) => {
338
- context.set("isPointer", false);
339
- },
340
- setTriggerValue: ({ context, event }) => {
341
- if (event.value === void 0) return;
342
- context.set("triggerValue", event.value);
343
- },
344
- immediateReopen: ({ send }) => {
345
- queueMicrotask(() => {
346
- send({ type: "REOPEN" });
347
- });
348
- }
349
- },
350
- effects: {
351
- trackFocusVisible: ({ scope }) => {
352
- return trackFocusVisible({ root: scope.getRootNode?.() });
353
- },
354
- // Only when hoverable: with `interactive: false` the content has `pointer-events: none`,
355
- // so there is no journey to protect and its rect is unreachable.
356
- trackSafeArea: ({ send, prop, scope, context }) => {
357
- if (!prop("interactive")) return;
358
- return trackSafeArea({
359
- getTriggerEl: () => dom.getActiveTriggerEl(scope, context.get("triggerValue")),
360
- getContentEl: () => dom.getContentEl(scope),
361
- openedByPointer: () => context.get("isPointer"),
362
- defer: true,
363
- onLeave() {
364
- send({ type: "SAFE_AREA.EXIT" });
365
- }
366
- });
367
- },
368
- trackPositioning: ({ context, prop, scope }) => {
369
- if (!context.get("currentPlacement")) {
370
- context.set("currentPlacement", prop("positioning").placement);
371
- }
372
- const getPositionerEl2 = () => dom.getPositionerEl(scope);
373
- const getTriggerEl = () => dom.getActiveTriggerEl(scope, context.get("triggerValue"));
374
- return getPlacement(getTriggerEl, getPositionerEl2, {
375
- getArrowElement: () => dom.getArrowEl(scope),
376
- ...prop("positioning"),
377
- defer: true,
378
- onComplete(data) {
379
- context.set("currentPlacement", data.placement);
380
- }
381
- });
382
- },
383
- trackPointerlockChange: ({ send, scope }) => {
384
- const doc = scope.getDoc();
385
- const onChange = () => send({ type: "CLOSE", src: "pointerlock:change" });
386
- return addDomEvent(doc, "pointerlockchange", onChange, false);
387
- },
388
- trackScroll: ({ send, prop, scope, context }) => {
389
- if (!prop("closeOnScroll")) return;
390
- const triggerValue = context.get("triggerValue");
391
- const triggerEl = dom.getActiveTriggerEl(scope, triggerValue);
392
- if (!triggerEl) return;
393
- const overflowParents = getOverflowAncestors(triggerEl);
394
- const cleanups = overflowParents.map((overflowParent) => {
395
- const onScroll = () => {
396
- send({ type: "CLOSE", src: "scroll" });
397
- };
398
- return addDomEvent(overflowParent, "scroll", onScroll, {
399
- passive: true,
400
- capture: true
401
- });
402
- });
403
- return () => {
404
- cleanups.forEach((fn) => fn?.());
405
- };
406
- },
407
- trackStore: ({ prop, send }) => {
408
- let cleanup;
409
- queueMicrotask(() => {
410
- cleanup = store.subscribe(() => {
411
- const id = store.get("id");
412
- if (id !== null && id !== prop("id")) {
413
- send({ type: "CLOSE", src: "id.change" });
414
- }
415
- });
416
- });
417
- return () => cleanup?.();
418
- },
419
- trackEscapeKey: ({ send, prop }) => {
420
- if (!prop("closeOnEscape")) return;
421
- const onKeyDown = (event) => {
422
- if (isComposingEvent(event)) return;
423
- if (event.key !== "Escape") return;
424
- event.stopPropagation();
425
- send({ type: "CLOSE", src: "keydown.escape" });
426
- };
427
- return addDomEvent(document, "keydown", onKeyDown, true);
428
- },
429
- waitForOpenDelay: ({ send, prop, event }) => {
430
- const id = setTimeout(() => {
431
- send({ type: "OPEN_DELAY", previousEvent: event });
432
- }, prop("openDelay"));
433
- return () => clearTimeout(id);
434
- },
435
- waitForCloseDelay: ({ send, prop, event }) => {
436
- const id = setTimeout(() => {
437
- send({ type: "CLOSE_DELAY", previousEvent: event });
438
- }, prop("closeDelay"));
439
- return () => clearTimeout(id);
440
- }
441
- }
442
- }
8
+ import { createGuards, createMachine } from "@zag-js/core";
9
+ import { trackSafeArea } from "@zag-js/safe-area";
10
+ // src/tooltip.machine.ts
11
+ const { and, not } = createGuards();
12
+ const machine = createMachine({
13
+ initialState: ({ prop }) => {
14
+ return prop("open") || prop("defaultOpen") ? "open" : "closed";
15
+ },
16
+ props({ props }) {
17
+ ensureProps(props, ["id"]);
18
+ const closeOnClick = props.closeOnClick ?? true;
19
+ const closeOnPointerDown = props.closeOnPointerDown ?? closeOnClick;
20
+ return {
21
+ openDelay: 400,
22
+ closeDelay: 150,
23
+ closeOnEscape: true,
24
+ interactive: false,
25
+ closeOnScroll: true,
26
+ disabled: false,
27
+ ...props,
28
+ closeOnPointerDown,
29
+ closeOnClick,
30
+ positioning: {
31
+ placement: "bottom",
32
+ ...props.positioning
33
+ }
34
+ };
35
+ },
36
+ effects: ["trackFocusVisible", "trackStore"],
37
+ exit: ["clearGlobalId", "endTriggerSwitch"],
38
+ context: ({ bindable, prop, scope }) => ({
39
+ currentPlacement: bindable(() => ({ defaultValue: void 0 })),
40
+ hasPointerMoveOpened: bindable(() => ({ defaultValue: null })),
41
+ isPointer: bindable(() => ({ defaultValue: false })),
42
+ triggerValue: bindable(() => ({
43
+ defaultValue: prop("defaultTriggerValue") ?? null,
44
+ value: prop("triggerValue"),
45
+ onChange(value) {
46
+ const onTriggerValueChange = prop("onTriggerValueChange");
47
+ if (!onTriggerValueChange) return;
48
+ onTriggerValueChange({
49
+ value,
50
+ triggerElement: getTriggerEl(scope, value)
51
+ });
52
+ }
53
+ })),
54
+ activationDirection: bindable(() => ({ defaultValue: null })),
55
+ previousTriggerValue: bindable(() => ({ defaultValue: null })),
56
+ transitioning: bindable(() => ({ defaultValue: false })),
57
+ viewportRendered: bindable(() => ({ defaultValue: false })),
58
+ viewportSize: bindable(() => ({ defaultValue: null }))
59
+ }),
60
+ refs: () => ({
61
+ cancelTriggerSwitch: null,
62
+ awaitingViewportSize: false
63
+ }),
64
+ watch({ track, action, prop }) {
65
+ track([() => prop("disabled")], () => {
66
+ action(["closeIfDisabled"]);
67
+ });
68
+ track([() => prop("open")], () => {
69
+ action(["toggleVisibility"]);
70
+ });
71
+ track([() => prop("triggerValue")], () => {
72
+ action(["repositionImmediate"]);
73
+ });
74
+ },
75
+ on: {
76
+ "TRIGGER_VALUE.SET": { actions: ["setTriggerValue", "repositionImmediate"] },
77
+ "TRIGGER_SWITCH.END": { actions: ["endTriggerSwitch"] }
78
+ },
79
+ states: {
80
+ closed: {
81
+ entry: [
82
+ "clearGlobalId",
83
+ "clearIsPointer",
84
+ "endTriggerSwitch",
85
+ "clearActivationDirection"
86
+ ],
87
+ on: {
88
+ "CONTROLLED.OPEN": { target: "open" },
89
+ OPEN: [{
90
+ guard: "isOpenControlled",
91
+ actions: ["setTriggerValue", "invokeOnOpen"]
92
+ }, {
93
+ target: "open",
94
+ actions: ["setTriggerValue", "invokeOnOpen"]
95
+ }],
96
+ POINTER_LEAVE: { actions: ["clearPointerMoveOpened"] },
97
+ POINTER_MOVE: [{
98
+ guard: and(not("isWarm"), not("hasPointerMoveOpened")),
99
+ target: "opening",
100
+ actions: ["setTriggerValue", "setPointerMoveOpened"]
101
+ }, {
102
+ guard: not("hasPointerMoveOpened"),
103
+ target: "open",
104
+ actions: [
105
+ "setPointerMoveOpened",
106
+ "setIsPointer",
107
+ "invokeOnOpen",
108
+ "setTriggerValue"
109
+ ]
110
+ }]
111
+ }
112
+ },
113
+ opening: {
114
+ effects: [
115
+ "trackScroll",
116
+ "trackPointerlockChange",
117
+ "waitForOpenDelay"
118
+ ],
119
+ on: {
120
+ OPEN_DELAY: [{
121
+ guard: "isOpenControlled",
122
+ actions: [
123
+ "setPointerMoveOpened",
124
+ "setIsPointer",
125
+ "invokeOnOpen"
126
+ ]
127
+ }, {
128
+ target: "open",
129
+ actions: [
130
+ "setPointerMoveOpened",
131
+ "setIsPointer",
132
+ "invokeOnOpen"
133
+ ]
134
+ }],
135
+ "CONTROLLED.OPEN": { target: "open" },
136
+ "CONTROLLED.CLOSE": { target: "closed" },
137
+ OPEN: [{
138
+ guard: "isOpenControlled",
139
+ actions: ["setTriggerValue", "invokeOnOpen"]
140
+ }, {
141
+ target: "open",
142
+ actions: ["setTriggerValue", "invokeOnOpen"]
143
+ }],
144
+ POINTER_LEAVE: [
145
+ {
146
+ guard: "isDisabled",
147
+ actions: ["clearPointerMoveOpened"]
148
+ },
149
+ {
150
+ guard: "isOpenControlled",
151
+ actions: [
152
+ "clearPointerMoveOpened",
153
+ "invokeOnClose",
154
+ "toggleVisibility"
155
+ ]
156
+ },
157
+ {
158
+ target: "closed",
159
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
160
+ }
161
+ ],
162
+ CLOSE: [{
163
+ guard: "isOpenControlled",
164
+ actions: ["invokeOnClose", "toggleVisibility"]
165
+ }, {
166
+ target: "closed",
167
+ actions: ["invokeOnClose"]
168
+ }]
169
+ }
170
+ },
171
+ open: {
172
+ effects: [
173
+ "trackEscapeKey",
174
+ "trackScroll",
175
+ "trackPointerlockChange",
176
+ "trackPositioning",
177
+ "trackViewportSize",
178
+ "trackSafeArea"
179
+ ],
180
+ entry: ["setGlobalId"],
181
+ on: {
182
+ "CONTROLLED.CLOSE": { target: "closed" },
183
+ CLOSE: [{
184
+ guard: "isOpenControlled",
185
+ actions: ["invokeOnClose"]
186
+ }, {
187
+ target: "closed",
188
+ actions: ["invokeOnClose"]
189
+ }],
190
+ POINTER_LEAVE: [
191
+ {
192
+ guard: "isDisabled",
193
+ actions: ["clearPointerMoveOpened"]
194
+ },
195
+ {
196
+ guard: and(not("isInteractive"), "isVisible"),
197
+ target: "closing",
198
+ actions: ["clearPointerMoveOpened"]
199
+ },
200
+ {
201
+ guard: and(not("isInteractive"), "isOpenControlled"),
202
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
203
+ },
204
+ {
205
+ guard: not("isInteractive"),
206
+ target: "closed",
207
+ actions: ["clearPointerMoveOpened", "invokeOnClose"]
208
+ }
209
+ ],
210
+ "SAFE_AREA.EXIT": {
211
+ target: "closing",
212
+ actions: ["clearPointerMoveOpened"]
213
+ },
214
+ "POSITIONING.SET": { actions: ["reposition"] },
215
+ "TRIGGER_VALUE.SET": {
216
+ target: "closing",
217
+ actions: [
218
+ "setActivationDirection",
219
+ "setPreviousTriggerValue",
220
+ "setTriggerValue",
221
+ "startTriggerSwitch",
222
+ "immediateReopen"
223
+ ]
224
+ }
225
+ }
226
+ },
227
+ closing: {
228
+ effects: [
229
+ "trackPositioning",
230
+ "trackViewportSize",
231
+ "waitForCloseDelay"
232
+ ],
233
+ on: {
234
+ CLOSE_DELAY: [{
235
+ guard: "isOpenControlled",
236
+ actions: ["invokeOnClose"]
237
+ }, {
238
+ target: "closed",
239
+ actions: ["invokeOnClose"]
240
+ }],
241
+ "CONTROLLED.CLOSE": { target: "closed" },
242
+ "CONTROLLED.OPEN": { target: "open" },
243
+ CLOSE: [{
244
+ guard: "isOpenControlled",
245
+ actions: ["invokeOnClose"]
246
+ }, {
247
+ target: "closed",
248
+ actions: ["invokeOnClose"]
249
+ }],
250
+ POINTER_MOVE: [
251
+ {
252
+ guard: and("isOpenControlled", "isNewTriggerValue"),
253
+ actions: [
254
+ "setPointerMoveOpened",
255
+ "setIsPointer",
256
+ "setActivationDirection",
257
+ "setPreviousTriggerValue",
258
+ "setTriggerValue",
259
+ "startTriggerSwitch",
260
+ "invokeOnOpen",
261
+ "toggleVisibility"
262
+ ]
263
+ },
264
+ {
265
+ guard: "isOpenControlled",
266
+ actions: [
267
+ "setPointerMoveOpened",
268
+ "setIsPointer",
269
+ "setTriggerValue",
270
+ "invokeOnOpen",
271
+ "toggleVisibility"
272
+ ]
273
+ },
274
+ {
275
+ guard: "isNewTriggerValue",
276
+ target: "open",
277
+ actions: [
278
+ "setPointerMoveOpened",
279
+ "setIsPointer",
280
+ "setActivationDirection",
281
+ "setPreviousTriggerValue",
282
+ "setTriggerValue",
283
+ "startTriggerSwitch",
284
+ "invokeOnOpen"
285
+ ]
286
+ },
287
+ {
288
+ target: "open",
289
+ actions: [
290
+ "setPointerMoveOpened",
291
+ "setIsPointer",
292
+ "setTriggerValue",
293
+ "invokeOnOpen"
294
+ ]
295
+ }
296
+ ],
297
+ "TRIGGER_VALUE.SET": {
298
+ target: "open",
299
+ actions: [
300
+ "setActivationDirection",
301
+ "setPreviousTriggerValue",
302
+ "setTriggerValue",
303
+ "startTriggerSwitch",
304
+ "repositionImmediate"
305
+ ]
306
+ },
307
+ REOPEN: { target: "open" },
308
+ CONTENT_POINTER_MOVE: {
309
+ guard: "isInteractive",
310
+ target: "open"
311
+ },
312
+ "POSITIONING.SET": { actions: ["reposition"] }
313
+ }
314
+ }
315
+ },
316
+ implementations: {
317
+ guards: {
318
+ isWarm: () => isWarm(),
319
+ isVisible: ({ prop }) => prop("id") === store.get("id"),
320
+ isInteractive: ({ prop }) => !!prop("interactive"),
321
+ hasPointerMoveOpened: ({ context }) => !!context.get("hasPointerMoveOpened"),
322
+ isOpenControlled: ({ prop }) => prop("open") !== void 0,
323
+ isDisabled: ({ prop }) => !!prop("disabled"),
324
+ isNewTriggerValue: ({ context, event }) => event.value != null && event.value !== context.get("triggerValue")
325
+ },
326
+ actions: {
327
+ setGlobalId: ({ prop, event }) => {
328
+ const prevId = store.get("id");
329
+ const switching = prevId !== null && prevId !== prop("id");
330
+ const isInstant = event.src === "trigger-focus" || switching || isWarm();
331
+ store.update({
332
+ id: prop("id"),
333
+ prevId: switching ? prevId : null,
334
+ instant: isInstant
335
+ });
336
+ },
337
+ clearGlobalId: ({ prop }) => {
338
+ if (prop("id") === store.get("id")) store.update({
339
+ id: null,
340
+ prevId: null,
341
+ instant: false,
342
+ closedAt: Date.now()
343
+ });
344
+ },
345
+ invokeOnOpen: ({ prop, event }) => {
346
+ prop("onOpenChange")?.({
347
+ open: true,
348
+ reason: getOpenChangeReason(event)
349
+ });
350
+ },
351
+ invokeOnClose: ({ prop, event }) => {
352
+ prop("onOpenChange")?.({
353
+ open: false,
354
+ reason: getOpenChangeReason(event)
355
+ });
356
+ },
357
+ closeIfDisabled: ({ prop, send }) => {
358
+ if (!prop("disabled")) return;
359
+ send({
360
+ type: "CLOSE",
361
+ src: "disabled"
362
+ });
363
+ },
364
+ reposition: ({ context, event, prop, scope, refs }) => {
365
+ if (event.type !== "POSITIONING.SET") return;
366
+ if (refs.get("awaitingViewportSize")) return;
367
+ const getPositionerEl$3 = () => getPositionerEl(scope);
368
+ const getTriggerEl$3 = () => getTriggerEl(scope, context.get("triggerValue"));
369
+ getPlacement(getTriggerEl$3, getPositionerEl$3, {
370
+ getArrowElement: () => getArrowEl(scope),
371
+ ...prop("positioning"),
372
+ ...event.options,
373
+ listeners: false,
374
+ onComplete(data) {
375
+ context.set("currentPlacement", data.placement);
376
+ }
377
+ });
378
+ },
379
+ repositionImmediate: ({ context, event, prop, scope, refs }) => {
380
+ if (refs.get("awaitingViewportSize")) return;
381
+ const triggerValue = event.value ?? context.get("triggerValue");
382
+ const getPositionerEl$1 = () => getPositionerEl(scope);
383
+ const getTriggerEl$1 = () => getTriggerEl(scope, triggerValue);
384
+ return getPlacement(getTriggerEl$1, getPositionerEl$1, {
385
+ getArrowElement: () => getArrowEl(scope),
386
+ ...prop("positioning"),
387
+ onComplete(data) {
388
+ context.set("currentPlacement", data.placement);
389
+ }
390
+ });
391
+ },
392
+ toggleVisibility: ({ prop, event, send }) => {
393
+ queueMicrotask(() => {
394
+ send({
395
+ type: prop("open") ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
396
+ previousEvent: event
397
+ });
398
+ });
399
+ },
400
+ setPointerMoveOpened: ({ context, event }) => {
401
+ const triggerId = event.triggerId ?? event.previousEvent?.triggerId;
402
+ context.set("hasPointerMoveOpened", triggerId ?? null);
403
+ },
404
+ clearPointerMoveOpened: ({ context }) => {
405
+ context.set("hasPointerMoveOpened", null);
406
+ },
407
+ setIsPointer: ({ context }) => {
408
+ context.set("isPointer", true);
409
+ },
410
+ clearIsPointer: ({ context }) => {
411
+ context.set("isPointer", false);
412
+ },
413
+ setTriggerValue: ({ context, event }) => {
414
+ if (event.value === void 0) return;
415
+ context.set("triggerValue", event.value);
416
+ },
417
+ setActivationDirection: ({ context, event, scope }) => {
418
+ const fromEl = getTriggerEl(scope, context.get("triggerValue"));
419
+ const toEl = getTriggerEl(scope, event.value);
420
+ context.set("activationDirection", getPopupActivationDirection(fromEl, toEl));
421
+ },
422
+ clearActivationDirection: ({ context }) => {
423
+ context.set("activationDirection", null);
424
+ },
425
+ setPreviousTriggerValue: ({ context }) => {
426
+ context.set("previousTriggerValue", context.get("triggerValue"));
427
+ },
428
+ startTriggerSwitch: ({ context, refs, scope, send }) => {
429
+ refs.get("cancelTriggerSwitch")?.();
430
+ context.set("transitioning", true);
431
+ const positionerEl = getPositionerEl(scope);
432
+ if (!positionerEl) return;
433
+ positionerEl.setAttribute("data-transitioning", "");
434
+ refs.set("awaitingViewportSize", !!getViewportEl(scope));
435
+ const cancel = waitForAnimationEnd(positionerEl, () => send({ type: "TRIGGER_SWITCH.END" }));
436
+ refs.set("cancelTriggerSwitch", cancel);
437
+ },
438
+ endTriggerSwitch: ({ context, refs }) => {
439
+ context.set("previousTriggerValue", null);
440
+ refs.get("cancelTriggerSwitch")?.();
441
+ refs.set("cancelTriggerSwitch", null);
442
+ refs.set("awaitingViewportSize", false);
443
+ context.set("transitioning", false);
444
+ },
445
+ immediateReopen: ({ send }) => {
446
+ queueMicrotask(() => {
447
+ send({ type: "REOPEN" });
448
+ });
449
+ }
450
+ },
451
+ effects: {
452
+ trackFocusVisible: ({ scope }) => {
453
+ return trackFocusVisible({ root: scope.getRootNode?.() });
454
+ },
455
+ trackSafeArea: ({ send, prop, scope, context }) => {
456
+ if (!prop("interactive")) return;
457
+ return trackSafeArea({
458
+ getTriggerEl: () => getTriggerEl(scope, context.get("triggerValue")),
459
+ getContentEl: () => getPopupEl(scope, context.get("triggerValue")),
460
+ openedByPointer: () => context.get("isPointer"),
461
+ defer: true,
462
+ onLeave() {
463
+ send({
464
+ type: "SAFE_AREA.EXIT",
465
+ src: "trigger-hover"
466
+ });
467
+ }
468
+ });
469
+ },
470
+ trackPositioning: ({ context, prop, scope, refs }) => {
471
+ if (!context.get("currentPlacement")) context.set("currentPlacement", prop("positioning").placement);
472
+ const getPositionerEl$2 = () => getPositionerEl(scope);
473
+ const getTriggerEl$2 = () => getTriggerEl(scope, context.get("triggerValue"));
474
+ const positioning = prop("positioning");
475
+ return getPlacement(getTriggerEl$2, getPositionerEl$2, {
476
+ getArrowElement: () => getArrowEl(scope),
477
+ ...positioning,
478
+ updatePosition(data) {
479
+ if (refs.get("awaitingViewportSize")) return;
480
+ return positioning.updatePosition ? positioning.updatePosition(data) : data.updatePosition();
481
+ },
482
+ defer: true,
483
+ onComplete(data) {
484
+ context.set("currentPlacement", data.placement);
485
+ }
486
+ });
487
+ },
488
+ trackViewportSize: (params) => {
489
+ const { context, scope, watchEffect } = params;
490
+ return watchEffect([() => context.get("triggerValue")], () => {
491
+ const frame = AnimationFrame.create();
492
+ const stopObserving = raf(() => {
493
+ const contentEl = getContentEl(scope, context.get("triggerValue"));
494
+ const viewportRendered = !!getViewportEl(scope);
495
+ context.set("viewportRendered", viewportRendered);
496
+ if (!viewportRendered || !contentEl) return;
497
+ return resizeObserverBorderBox.observe(contentEl, (entry) => {
498
+ const [{ inlineSize, blockSize }] = entry.borderBoxSize;
499
+ frame.request(() => setViewportSize(params, {
500
+ width: inlineSize,
501
+ height: blockSize
502
+ }));
503
+ });
504
+ });
505
+ return () => {
506
+ stopObserving();
507
+ frame.cancel();
508
+ };
509
+ });
510
+ },
511
+ trackPointerlockChange: ({ send, scope }) => {
512
+ const doc = scope.getDoc();
513
+ const onChange = () => send({
514
+ type: "CLOSE",
515
+ src: "interact-outside"
516
+ });
517
+ return addDomEvent(doc, "pointerlockchange", onChange, false);
518
+ },
519
+ trackScroll: ({ send, prop, scope, context }) => {
520
+ if (!prop("closeOnScroll")) return;
521
+ const triggerValue = context.get("triggerValue");
522
+ const triggerEl = getTriggerEl(scope, triggerValue);
523
+ if (!triggerEl) return;
524
+ const cleanups = getOverflowAncestors(triggerEl).map((overflowParent) => {
525
+ const onScroll = () => {
526
+ send({
527
+ type: "CLOSE",
528
+ src: "scroll"
529
+ });
530
+ };
531
+ return addDomEvent(overflowParent, "scroll", onScroll, {
532
+ passive: true,
533
+ capture: true
534
+ });
535
+ });
536
+ return () => {
537
+ cleanups.forEach((fn) => fn?.());
538
+ };
539
+ },
540
+ trackStore: ({ prop, send }) => {
541
+ let cleanup;
542
+ let cancelled = false;
543
+ queueMicrotask(() => {
544
+ if (cancelled) return;
545
+ cleanup = store.subscribe(() => {
546
+ const id = store.get("id");
547
+ if (id !== null && id !== prop("id")) send({
548
+ type: "CLOSE",
549
+ src: "sibling-open"
550
+ });
551
+ });
552
+ });
553
+ return () => {
554
+ cancelled = true;
555
+ cleanup?.();
556
+ };
557
+ },
558
+ trackEscapeKey: ({ send, prop, scope }) => {
559
+ if (!prop("closeOnEscape")) return;
560
+ const onKeyDown = (event) => {
561
+ if (isComposingEvent(event)) return;
562
+ if (event.key !== "Escape") return;
563
+ event.stopPropagation();
564
+ send({
565
+ type: "CLOSE",
566
+ src: "escape-key"
567
+ });
568
+ };
569
+ return addDomEvent(scope.getDoc(), "keydown", onKeyDown, true);
570
+ },
571
+ waitForOpenDelay: ({ send, prop, event }) => {
572
+ const id = setTimeout(() => {
573
+ send({
574
+ type: "OPEN_DELAY",
575
+ previousEvent: event
576
+ });
577
+ }, prop("openDelay"));
578
+ return () => clearTimeout(id);
579
+ },
580
+ waitForCloseDelay: ({ send, prop, event }) => {
581
+ const id = setTimeout(() => {
582
+ send({
583
+ type: "CLOSE_DELAY",
584
+ previousEvent: event
585
+ });
586
+ }, prop("closeDelay"));
587
+ return () => clearTimeout(id);
588
+ }
589
+ }
590
+ }
443
591
  });
444
- export {
445
- machine
446
- };
592
+ function setViewportSize(params, size) {
593
+ const { context, scope, refs, send } = params;
594
+ context.set("viewportSize", size);
595
+ const positionerEl = getPositionerEl(scope);
596
+ for (const [key, value] of Object.entries(getViewportSizeStyle(size))) positionerEl?.style.setProperty(key, value);
597
+ if (!refs.get("awaitingViewportSize")) return;
598
+ refs.set("awaitingViewportSize", false);
599
+ send({ type: "POSITIONING.SET" });
600
+ }
601
+ function getOpenChangeReason(event) {
602
+ return (event.previousEvent ?? event).src;
603
+ }
604
+ export { machine };