@zag-js/popover 2.0.0-next.2 → 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.
- package/dist/index.d.mts +8 -10
- package/dist/index.d.ts +8 -10
- package/dist/index.js +12 -38
- package/dist/index.mjs +2 -7
- package/dist/popover.anatomy.d.mts +2 -6
- package/dist/popover.anatomy.d.ts +2 -6
- package/dist/popover.anatomy.js +8 -44
- package/dist/popover.anatomy.mjs +4 -18
- package/dist/popover.connect.d.mts +3 -9
- package/dist/popover.connect.d.ts +3 -9
- package/dist/popover.connect.js +270 -212
- package/dist/popover.connect.mjs +263 -174
- package/dist/popover.dom.d.mts +19 -21
- package/dist/popover.dom.d.ts +19 -21
- package/dist/popover.dom.js +59 -84
- package/dist/popover.dom.mjs +38 -43
- package/dist/popover.machine.d.mts +2 -9
- package/dist/popover.machine.d.ts +2 -9
- package/dist/popover.machine.js +466 -357
- package/dist/popover.machine.mjs +459 -319
- package/dist/popover.props.d.mts +3 -10
- package/dist/popover.props.d.ts +3 -10
- package/dist/popover.props.js +34 -59
- package/dist/popover.props.mjs +30 -33
- package/dist/popover.style.d.mts +15 -0
- package/dist/popover.style.d.ts +15 -0
- package/dist/popover.style.js +15 -0
- package/dist/popover.style.mjs +11 -0
- package/dist/popover.types.d.mts +350 -237
- package/dist/popover.types.d.ts +350 -237
- package/dist/popover.types.js +4 -18
- package/dist/popover.types.mjs +1 -0
- package/package.json +12 -12
package/dist/popover.machine.mjs
CHANGED
|
@@ -1,324 +1,464 @@
|
|
|
1
|
-
|
|
1
|
+
import { getAnchorEl, getArrowEl, getContentEl, getDescriptionEl, getLayerEl, getPositionerEl, getTitleEl, getTriggerEl, getTriggerEls, getViewportEl } from "./popover.dom.mjs";
|
|
2
|
+
import { getViewportSizeStyle } from "./popover.style.mjs";
|
|
3
|
+
import { getDismissReason, trackDismissableElement } from "@zag-js/dismissable";
|
|
4
|
+
import { AnimationFrame, contains, getInitialFocus, getPopupActivationDirection, proxyTabFocus, raf, resizeObserverBorderBox, waitForAnimationEnd } from "@zag-js/dom-query";
|
|
5
|
+
import { getPlacement } from "@zag-js/popper";
|
|
2
6
|
import { ariaHidden } from "@zag-js/aria-hidden";
|
|
3
7
|
import { createMachine } from "@zag-js/core";
|
|
4
|
-
import { trackDismissableElement } from "@zag-js/dismissable";
|
|
5
|
-
import { contains, getInitialFocus, proxyTabFocus, raf } from "@zag-js/dom-query";
|
|
6
8
|
import { trapFocus } from "@zag-js/focus-trap";
|
|
7
|
-
import { getPlacement } from "@zag-js/popper";
|
|
8
9
|
import { preventBodyScroll } from "@zag-js/remove-scroll";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
10
|
+
// src/popover.machine.ts
|
|
11
|
+
const machine = createMachine({
|
|
12
|
+
props({ props }) {
|
|
13
|
+
return {
|
|
14
|
+
closeOnInteractOutside: true,
|
|
15
|
+
closeOnEscape: true,
|
|
16
|
+
autoFocus: true,
|
|
17
|
+
modal: false,
|
|
18
|
+
restoreFocus: true,
|
|
19
|
+
...props,
|
|
20
|
+
positioning: {
|
|
21
|
+
placement: "bottom",
|
|
22
|
+
...props.positioning
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
initialState({ prop }) {
|
|
27
|
+
return prop("open") || prop("defaultOpen") ? "open" : "closed";
|
|
28
|
+
},
|
|
29
|
+
context({ bindable, prop, scope }) {
|
|
30
|
+
return {
|
|
31
|
+
layer: bindable(() => ({ defaultValue: null })),
|
|
32
|
+
currentPlacement: bindable(() => ({ defaultValue: void 0 })),
|
|
33
|
+
renderedElements: bindable(() => ({ defaultValue: {
|
|
34
|
+
title: true,
|
|
35
|
+
description: true,
|
|
36
|
+
viewport: false
|
|
37
|
+
} })),
|
|
38
|
+
triggerValue: bindable(() => ({
|
|
39
|
+
defaultValue: prop("defaultTriggerValue") ?? null,
|
|
40
|
+
value: prop("triggerValue"),
|
|
41
|
+
onChange(value) {
|
|
42
|
+
const onTriggerValueChange = prop("onTriggerValueChange");
|
|
43
|
+
if (!onTriggerValueChange) return;
|
|
44
|
+
onTriggerValueChange({
|
|
45
|
+
value,
|
|
46
|
+
triggerElement: getTriggerEl(scope, value)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
})),
|
|
50
|
+
activationDirection: bindable(() => ({ defaultValue: null })),
|
|
51
|
+
previousTriggerValue: bindable(() => ({ defaultValue: null })),
|
|
52
|
+
instant: bindable(() => ({ defaultValue: null })),
|
|
53
|
+
transitioning: bindable(() => ({ defaultValue: false })),
|
|
54
|
+
viewportSize: bindable(() => ({ defaultValue: null }))
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
refs() {
|
|
58
|
+
return {
|
|
59
|
+
renderedFrame: AnimationFrame.create(),
|
|
60
|
+
initialFocusFrame: AnimationFrame.create(),
|
|
61
|
+
cancelTriggerSwitch: null,
|
|
62
|
+
awaitingViewportSize: false
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
watch({ track, prop, action }) {
|
|
66
|
+
track([() => prop("open")], () => {
|
|
67
|
+
action(["toggleVisibility"]);
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
entry: ["checkRenderedElements"],
|
|
71
|
+
exit: ["cancelFrames"],
|
|
72
|
+
states: {
|
|
73
|
+
closed: { on: {
|
|
74
|
+
"TRIGGER_VALUE.SET": { actions: ["setTriggerValue"] },
|
|
75
|
+
"CONTROLLED.OPEN": {
|
|
76
|
+
target: "open",
|
|
77
|
+
actions: ["setInitialFocus"]
|
|
78
|
+
},
|
|
79
|
+
TOGGLE: [{
|
|
80
|
+
guard: "isOpenControlled",
|
|
81
|
+
actions: [
|
|
82
|
+
"setInstant",
|
|
83
|
+
"invokeOnOpen",
|
|
84
|
+
"setTriggerValue"
|
|
85
|
+
]
|
|
86
|
+
}, {
|
|
87
|
+
target: "open",
|
|
88
|
+
actions: [
|
|
89
|
+
"setInstant",
|
|
90
|
+
"invokeOnOpen",
|
|
91
|
+
"setTriggerValue",
|
|
92
|
+
"setInitialFocus"
|
|
93
|
+
]
|
|
94
|
+
}],
|
|
95
|
+
OPEN: [{
|
|
96
|
+
guard: "isOpenControlled",
|
|
97
|
+
actions: [
|
|
98
|
+
"setInstant",
|
|
99
|
+
"invokeOnOpen",
|
|
100
|
+
"setTriggerValue"
|
|
101
|
+
]
|
|
102
|
+
}, {
|
|
103
|
+
target: "open",
|
|
104
|
+
actions: [
|
|
105
|
+
"setInstant",
|
|
106
|
+
"invokeOnOpen",
|
|
107
|
+
"setTriggerValue",
|
|
108
|
+
"setInitialFocus"
|
|
109
|
+
]
|
|
110
|
+
}]
|
|
111
|
+
} },
|
|
112
|
+
open: {
|
|
113
|
+
entry: ["checkRenderedElements"],
|
|
114
|
+
exit: [
|
|
115
|
+
"cancelInitialFocus",
|
|
116
|
+
"endTriggerSwitch",
|
|
117
|
+
"clearActivationDirection"
|
|
118
|
+
],
|
|
119
|
+
effects: [
|
|
120
|
+
"trapFocus",
|
|
121
|
+
"preventScroll",
|
|
122
|
+
"hideContentBelow",
|
|
123
|
+
"trackDismissableElement",
|
|
124
|
+
"trackPositioning",
|
|
125
|
+
"trackViewportSize",
|
|
126
|
+
"proxyTabFocus"
|
|
127
|
+
],
|
|
128
|
+
on: {
|
|
129
|
+
"CONTROLLED.CLOSE": {
|
|
130
|
+
target: "closed",
|
|
131
|
+
actions: ["setFinalFocus"]
|
|
132
|
+
},
|
|
133
|
+
CLOSE: [{
|
|
134
|
+
guard: "isOpenControlled",
|
|
135
|
+
actions: ["setInstant", "invokeOnClose"]
|
|
136
|
+
}, {
|
|
137
|
+
target: "closed",
|
|
138
|
+
actions: [
|
|
139
|
+
"setInstant",
|
|
140
|
+
"invokeOnClose",
|
|
141
|
+
"setFinalFocus"
|
|
142
|
+
]
|
|
143
|
+
}],
|
|
144
|
+
TOGGLE: [{
|
|
145
|
+
guard: "isOpenControlled",
|
|
146
|
+
actions: ["setInstant", "invokeOnClose"]
|
|
147
|
+
}, {
|
|
148
|
+
target: "closed",
|
|
149
|
+
actions: ["setInstant", "invokeOnClose"]
|
|
150
|
+
}],
|
|
151
|
+
"POSITIONING.SET": { actions: ["reposition"] },
|
|
152
|
+
"TRIGGER_VALUE.SET": {
|
|
153
|
+
guard: "isNewTriggerValue",
|
|
154
|
+
actions: [
|
|
155
|
+
"setActivationDirection",
|
|
156
|
+
"setPreviousTriggerValue",
|
|
157
|
+
"setTriggerValue",
|
|
158
|
+
"startTriggerSwitch",
|
|
159
|
+
"reposition"
|
|
160
|
+
]
|
|
161
|
+
},
|
|
162
|
+
"TRIGGER_SWITCH.END": { actions: ["endTriggerSwitch", "setTriggerChangeInstant"] }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
implementations: {
|
|
167
|
+
guards: {
|
|
168
|
+
isOpenControlled: ({ prop }) => prop("open") != void 0,
|
|
169
|
+
isNewTriggerValue: ({ context, event }) => event.value !== context.get("triggerValue")
|
|
170
|
+
},
|
|
171
|
+
effects: {
|
|
172
|
+
trackPositioning({ context, prop, scope, refs }) {
|
|
173
|
+
context.set("currentPlacement", prop("positioning").placement);
|
|
174
|
+
const anchorEl = getAnchorEl(scope);
|
|
175
|
+
const getPositionerEl$2 = () => getPositionerEl(scope);
|
|
176
|
+
const getTriggerEl$2 = () => anchorEl ?? getTriggerEl(scope, context.get("triggerValue"));
|
|
177
|
+
const positioning = prop("positioning");
|
|
178
|
+
return getPlacement(getTriggerEl$2, getPositionerEl$2, {
|
|
179
|
+
getArrowElement: () => getArrowEl(scope),
|
|
180
|
+
...positioning,
|
|
181
|
+
updatePosition(data) {
|
|
182
|
+
if (refs.get("awaitingViewportSize")) return;
|
|
183
|
+
return positioning.updatePosition ? positioning.updatePosition(data) : data.updatePosition();
|
|
184
|
+
},
|
|
185
|
+
defer: true,
|
|
186
|
+
onComplete(data) {
|
|
187
|
+
context.set("currentPlacement", data.placement);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
trackViewportSize(params) {
|
|
192
|
+
const { context, scope, watchEffect } = params;
|
|
193
|
+
return watchEffect([() => context.get("triggerValue")], () => {
|
|
194
|
+
const frame = AnimationFrame.create();
|
|
195
|
+
const stopObserving = raf(() => {
|
|
196
|
+
const contentEl = getContentEl(scope, context.get("triggerValue"));
|
|
197
|
+
if (!getViewportEl(scope) || !contentEl) return;
|
|
198
|
+
return resizeObserverBorderBox.observe(contentEl, (entry) => {
|
|
199
|
+
const [{ inlineSize, blockSize }] = entry.borderBoxSize;
|
|
200
|
+
frame.request(() => setViewportSize(params, {
|
|
201
|
+
width: inlineSize,
|
|
202
|
+
height: blockSize
|
|
203
|
+
}));
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
return () => {
|
|
207
|
+
stopObserving();
|
|
208
|
+
frame.cancel();
|
|
209
|
+
};
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
trackDismissableElement({ send, prop, scope, context, watchEffect }) {
|
|
213
|
+
return watchEffect([() => prop("modal")], () => {
|
|
214
|
+
const getLayerEl$1 = () => getLayerEl(scope, context.get("triggerValue"));
|
|
215
|
+
let restoreFocus = true;
|
|
216
|
+
return trackDismissableElement(getLayerEl$1, {
|
|
217
|
+
type: "popover",
|
|
218
|
+
pointerBlocking: prop("modal"),
|
|
219
|
+
onLayerChange(layer) {
|
|
220
|
+
context.set("layer", layer);
|
|
221
|
+
},
|
|
222
|
+
exclude: getTriggerEls(scope),
|
|
223
|
+
defer: true,
|
|
224
|
+
getRootNode: scope.getRootNode,
|
|
225
|
+
onEscapeKeyDown(event) {
|
|
226
|
+
prop("onEscapeKeyDown")?.(event);
|
|
227
|
+
if (prop("closeOnEscape")) return;
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
},
|
|
230
|
+
onInteractOutside(event) {
|
|
231
|
+
prop("onInteractOutside")?.(event);
|
|
232
|
+
if (event.defaultPrevented) return;
|
|
233
|
+
restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
|
|
234
|
+
if (!prop("closeOnInteractOutside")) event.preventDefault();
|
|
235
|
+
},
|
|
236
|
+
onPointerDownOutside: prop("onPointerDownOutside"),
|
|
237
|
+
onFocusOutside: prop("onFocusOutside"),
|
|
238
|
+
persistentElements: prop("persistentElements"),
|
|
239
|
+
onRequestDismiss: prop("onRequestDismiss"),
|
|
240
|
+
onDismiss(event) {
|
|
241
|
+
send({
|
|
242
|
+
type: "CLOSE",
|
|
243
|
+
src: getDismissReason(event),
|
|
244
|
+
restoreFocus,
|
|
245
|
+
focusVisible: event?.type === "keydown",
|
|
246
|
+
instant: getDismissInstant(event)
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
proxyTabFocus({ prop, scope, context, watchEffect }) {
|
|
253
|
+
return watchEffect([() => prop("modal"), () => context.get("triggerValue")], () => {
|
|
254
|
+
if (prop("modal")) return;
|
|
255
|
+
const getContentEl$2 = () => getContentEl(scope, context.get("triggerValue"));
|
|
256
|
+
let cleanup;
|
|
257
|
+
const rafCleanup = raf(() => {
|
|
258
|
+
const triggerEl = getTriggerEl(scope, context.get("triggerValue"));
|
|
259
|
+
const contentEl = getContentEl$2();
|
|
260
|
+
if (!triggerEl || !contentEl || contains(triggerEl.parentElement, contentEl)) return;
|
|
261
|
+
cleanup = proxyTabFocus(getContentEl$2, {
|
|
262
|
+
triggerElement: triggerEl,
|
|
263
|
+
getShadowRoot: true,
|
|
264
|
+
onFocus(el) {
|
|
265
|
+
el.focus({ preventScroll: true });
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
return () => {
|
|
270
|
+
rafCleanup();
|
|
271
|
+
cleanup?.();
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
},
|
|
275
|
+
hideContentBelow({ prop, scope, context, watchEffect }) {
|
|
276
|
+
return watchEffect([() => prop("modal"), () => context.get("triggerValue")], () => {
|
|
277
|
+
if (!prop("modal")) return;
|
|
278
|
+
const getElements = () => [getLayerEl(scope, context.get("triggerValue")), getTriggerEl(scope, context.get("triggerValue"))];
|
|
279
|
+
return ariaHidden(getElements, { defer: true });
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
preventScroll({ prop, scope, watchEffect }) {
|
|
283
|
+
return watchEffect([() => prop("modal")], () => {
|
|
284
|
+
if (!prop("modal")) return;
|
|
285
|
+
return preventBodyScroll(scope.getDoc());
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
trapFocus({ prop, scope, context, watchEffect }) {
|
|
289
|
+
return watchEffect([() => prop("modal"), () => context.get("triggerValue")], () => {
|
|
290
|
+
if (!prop("modal")) return;
|
|
291
|
+
const getContentEl$1 = () => getContentEl(scope, context.get("triggerValue"));
|
|
292
|
+
const destroy = trapFocus(getContentEl$1, {
|
|
293
|
+
preventScroll: true,
|
|
294
|
+
returnFocusOnDeactivate: !!prop("restoreFocus"),
|
|
295
|
+
initialFocus: () => getInitialFocus({
|
|
296
|
+
root: getContentEl(scope, context.get("triggerValue")),
|
|
297
|
+
getInitialEl: prop("initialFocusEl"),
|
|
298
|
+
enabled: prop("autoFocus")
|
|
299
|
+
}),
|
|
300
|
+
setReturnFocus: (el) => {
|
|
301
|
+
const finalFocusEl = prop("finalFocusEl")?.();
|
|
302
|
+
if (finalFocusEl) return finalFocusEl;
|
|
303
|
+
const triggerValue = context.get("triggerValue");
|
|
304
|
+
if (triggerValue) {
|
|
305
|
+
const triggerEl = getTriggerEl(scope, triggerValue);
|
|
306
|
+
if (triggerEl) return triggerEl;
|
|
307
|
+
}
|
|
308
|
+
const fallbackTrigger = getTriggerEls(scope)[0];
|
|
309
|
+
if (fallbackTrigger) return fallbackTrigger;
|
|
310
|
+
return el;
|
|
311
|
+
},
|
|
312
|
+
getShadowRoot: true
|
|
313
|
+
});
|
|
314
|
+
return (reason) => destroy({ returnFocus: reason === "exit" && !!prop("restoreFocus") && !!prop("modal") });
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
actions: {
|
|
319
|
+
reposition({ event, prop, scope, context, refs }) {
|
|
320
|
+
if (refs.get("awaitingViewportSize")) return;
|
|
321
|
+
const anchorEl = getAnchorEl(scope);
|
|
322
|
+
const getPositionerEl$1 = () => getPositionerEl(scope);
|
|
323
|
+
const getTriggerEl$1 = () => anchorEl ?? getTriggerEl(scope, context.get("triggerValue"));
|
|
324
|
+
getPlacement(getTriggerEl$1, getPositionerEl$1, {
|
|
325
|
+
getArrowElement: () => getArrowEl(scope),
|
|
326
|
+
...prop("positioning"),
|
|
327
|
+
...event.options,
|
|
328
|
+
defer: true,
|
|
329
|
+
listeners: false,
|
|
330
|
+
onComplete(data) {
|
|
331
|
+
context.set("currentPlacement", data.placement);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
cancelInitialFocus({ refs }) {
|
|
336
|
+
refs.get("initialFocusFrame").cancel();
|
|
337
|
+
},
|
|
338
|
+
cancelFrames({ refs }) {
|
|
339
|
+
refs.get("renderedFrame").cancel();
|
|
340
|
+
refs.get("initialFocusFrame").cancel();
|
|
341
|
+
refs.get("cancelTriggerSwitch")?.();
|
|
342
|
+
},
|
|
343
|
+
setActivationDirection({ context, event, scope }) {
|
|
344
|
+
const fromEl = getTriggerEl(scope, context.get("triggerValue"));
|
|
345
|
+
const toEl = getTriggerEl(scope, event.value);
|
|
346
|
+
context.set("activationDirection", getPopupActivationDirection(fromEl, toEl));
|
|
347
|
+
},
|
|
348
|
+
clearActivationDirection({ context }) {
|
|
349
|
+
context.set("activationDirection", null);
|
|
350
|
+
},
|
|
351
|
+
setPreviousTriggerValue({ context }) {
|
|
352
|
+
context.set("previousTriggerValue", context.get("triggerValue"));
|
|
353
|
+
},
|
|
354
|
+
startTriggerSwitch({ context, refs, scope, send }) {
|
|
355
|
+
refs.get("cancelTriggerSwitch")?.();
|
|
356
|
+
context.set("transitioning", true);
|
|
357
|
+
context.set("instant", null);
|
|
358
|
+
const positionerEl = getPositionerEl(scope);
|
|
359
|
+
if (!positionerEl) return;
|
|
360
|
+
refs.set("awaitingViewportSize", !!getViewportEl(scope));
|
|
361
|
+
const cancel = waitForAnimationEnd(positionerEl, () => send({ type: "TRIGGER_SWITCH.END" }));
|
|
362
|
+
refs.set("cancelTriggerSwitch", cancel);
|
|
363
|
+
},
|
|
364
|
+
endTriggerSwitch({ context, refs }) {
|
|
365
|
+
context.set("previousTriggerValue", null);
|
|
366
|
+
refs.get("cancelTriggerSwitch")?.();
|
|
367
|
+
refs.set("cancelTriggerSwitch", null);
|
|
368
|
+
refs.set("awaitingViewportSize", false);
|
|
369
|
+
context.set("transitioning", false);
|
|
370
|
+
},
|
|
371
|
+
setInstant({ context, event }) {
|
|
372
|
+
context.set("instant", event.instant ?? null);
|
|
373
|
+
},
|
|
374
|
+
setTriggerChangeInstant({ context }) {
|
|
375
|
+
context.set("instant", "trigger-change");
|
|
376
|
+
},
|
|
377
|
+
setTriggerValue({ context, event }) {
|
|
378
|
+
if (event.value === void 0) return;
|
|
379
|
+
context.set("triggerValue", event.value);
|
|
380
|
+
},
|
|
381
|
+
checkRenderedElements({ context, scope, refs }) {
|
|
382
|
+
refs.get("renderedFrame").request(() => {
|
|
383
|
+
context.set("renderedElements", {
|
|
384
|
+
title: !!getTitleEl(scope),
|
|
385
|
+
description: !!getDescriptionEl(scope),
|
|
386
|
+
viewport: !!getViewportEl(scope)
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
},
|
|
390
|
+
setInitialFocus({ prop, scope, refs, context }) {
|
|
391
|
+
if (prop("modal")) return;
|
|
392
|
+
refs.get("initialFocusFrame").request(() => {
|
|
393
|
+
const element = getInitialFocus({
|
|
394
|
+
root: getContentEl(scope, context.get("triggerValue")),
|
|
395
|
+
getInitialEl: prop("initialFocusEl"),
|
|
396
|
+
enabled: prop("autoFocus")
|
|
397
|
+
});
|
|
398
|
+
if (!element) return;
|
|
399
|
+
element.focus({ preventScroll: true });
|
|
400
|
+
});
|
|
401
|
+
},
|
|
402
|
+
setFinalFocus({ event, prop, scope, context }) {
|
|
403
|
+
const eventRestoreFocus = event.restoreFocus ?? event.previousEvent?.restoreFocus;
|
|
404
|
+
if (eventRestoreFocus != null && !eventRestoreFocus) return;
|
|
405
|
+
if (!prop("restoreFocus")) return;
|
|
406
|
+
const focusOptions = getReturnFocusOptions(event);
|
|
407
|
+
raf(() => {
|
|
408
|
+
const finalFocusEl = prop("finalFocusEl")?.();
|
|
409
|
+
if (finalFocusEl) {
|
|
410
|
+
finalFocusEl.focus(focusOptions);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
getTriggerEl(scope, context.get("triggerValue"))?.focus(focusOptions);
|
|
414
|
+
});
|
|
415
|
+
},
|
|
416
|
+
invokeOnOpen({ prop, flush, event }) {
|
|
417
|
+
flush(() => {
|
|
418
|
+
prop("onOpenChange")?.({
|
|
419
|
+
open: true,
|
|
420
|
+
reason: getOpenChangeReason(event)
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
},
|
|
424
|
+
invokeOnClose({ prop, flush, event }) {
|
|
425
|
+
flush(() => {
|
|
426
|
+
prop("onOpenChange")?.({
|
|
427
|
+
open: false,
|
|
428
|
+
reason: getOpenChangeReason(event)
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
toggleVisibility({ event, send, prop }) {
|
|
433
|
+
send({
|
|
434
|
+
type: prop("open") ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
|
|
435
|
+
previousEvent: event
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
321
440
|
});
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
441
|
+
function getReturnFocusOptions(event) {
|
|
442
|
+
return event.focusVisible ?? event.previousEvent?.focusVisible ? {
|
|
443
|
+
preventScroll: true,
|
|
444
|
+
focusVisible: true
|
|
445
|
+
} : { preventScroll: true };
|
|
446
|
+
}
|
|
447
|
+
function setViewportSize(params, size) {
|
|
448
|
+
const { context, scope, refs, send } = params;
|
|
449
|
+
context.set("viewportSize", size);
|
|
450
|
+
const positionerEl = getPositionerEl(scope);
|
|
451
|
+
for (const [key, value] of Object.entries(getViewportSizeStyle(size))) positionerEl?.style.setProperty(key, value);
|
|
452
|
+
if (!refs.get("awaitingViewportSize")) return;
|
|
453
|
+
refs.set("awaitingViewportSize", false);
|
|
454
|
+
send({ type: "POSITIONING.SET" });
|
|
455
|
+
}
|
|
456
|
+
function getOpenChangeReason(event) {
|
|
457
|
+
return event.src;
|
|
458
|
+
}
|
|
459
|
+
function getDismissInstant(event) {
|
|
460
|
+
if (event?.type === "keydown") return "dismiss";
|
|
461
|
+
if (event?.type === "focus.outside") return "focus";
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
export { machine };
|