@zag-js/popper 1.34.0 → 1.35.0
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/get-anchor.d.mts +7 -0
- package/dist/get-anchor.d.ts +7 -0
- package/dist/get-anchor.js +66 -0
- package/dist/get-anchor.mjs +40 -0
- package/dist/get-placement.d.mts +8 -0
- package/dist/get-placement.d.ts +8 -0
- package/dist/get-placement.js +237 -0
- package/dist/get-placement.mjs +212 -0
- package/dist/get-styles.d.mts +41 -0
- package/dist/get-styles.d.ts +41 -0
- package/dist/get-styles.js +73 -0
- package/dist/get-styles.mjs +48 -0
- package/dist/index.d.mts +5 -187
- package/dist/index.d.ts +5 -187
- package/dist/index.js +34 -378
- package/dist/index.mjs +9 -376
- package/dist/middleware.d.mts +35 -0
- package/dist/middleware.d.ts +35 -0
- package/dist/middleware.js +108 -0
- package/dist/middleware.mjs +80 -0
- package/dist/placement.d.mts +12 -0
- package/dist/placement.d.ts +12 -0
- package/dist/placement.js +43 -0
- package/dist/placement.mjs +16 -0
- package/dist/types.d.mts +122 -0
- package/dist/types.d.ts +122 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,377 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const rect = {
|
|
11
|
-
x,
|
|
12
|
-
y,
|
|
13
|
-
width,
|
|
14
|
-
height,
|
|
15
|
-
top: y,
|
|
16
|
-
right: x + width,
|
|
17
|
-
bottom: y + height,
|
|
18
|
-
left: x
|
|
19
|
-
};
|
|
20
|
-
return { ...rect, toJSON: () => rect };
|
|
21
|
-
}
|
|
22
|
-
function getDOMRect(anchorRect) {
|
|
23
|
-
if (!anchorRect) return createDOMRect();
|
|
24
|
-
const { x, y, width, height } = anchorRect;
|
|
25
|
-
return createDOMRect(x, y, width, height);
|
|
26
|
-
}
|
|
27
|
-
function getAnchorElement(anchorElement, getAnchorRect) {
|
|
28
|
-
return {
|
|
29
|
-
contextElement: isHTMLElement(anchorElement) ? anchorElement : anchorElement?.contextElement,
|
|
30
|
-
getBoundingClientRect: () => {
|
|
31
|
-
const anchor = anchorElement;
|
|
32
|
-
const anchorRect = getAnchorRect?.(anchor);
|
|
33
|
-
if (anchorRect || !anchor) {
|
|
34
|
-
return getDOMRect(anchorRect);
|
|
35
|
-
}
|
|
36
|
-
return anchor.getBoundingClientRect();
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/middleware.ts
|
|
42
|
-
var toVar = (value) => ({ variable: value, reference: `var(${value})` });
|
|
43
|
-
var cssVars = {
|
|
44
|
-
arrowSize: toVar("--arrow-size"),
|
|
45
|
-
arrowSizeHalf: toVar("--arrow-size-half"),
|
|
46
|
-
arrowBg: toVar("--arrow-background"),
|
|
47
|
-
transformOrigin: toVar("--transform-origin"),
|
|
48
|
-
arrowOffset: toVar("--arrow-offset")
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { getPlacement } from "./get-placement.mjs";
|
|
3
|
+
import { getPlacementStyles } from "./get-styles.mjs";
|
|
4
|
+
import { getPlacementSide, isValidPlacement } from "./placement.mjs";
|
|
5
|
+
export {
|
|
6
|
+
getPlacement,
|
|
7
|
+
getPlacementSide,
|
|
8
|
+
getPlacementStyles,
|
|
9
|
+
isValidPlacement
|
|
49
10
|
};
|
|
50
|
-
var getSideAxis = (side) => side === "top" || side === "bottom" ? "y" : "x";
|
|
51
|
-
function createTransformOriginMiddleware(opts, arrowEl) {
|
|
52
|
-
return {
|
|
53
|
-
name: "transformOrigin",
|
|
54
|
-
fn(state) {
|
|
55
|
-
const { elements, middlewareData, placement, rects, y } = state;
|
|
56
|
-
const side = placement.split("-")[0];
|
|
57
|
-
const axis = getSideAxis(side);
|
|
58
|
-
const arrowX = middlewareData.arrow?.x || 0;
|
|
59
|
-
const arrowY = middlewareData.arrow?.y || 0;
|
|
60
|
-
const arrowWidth = arrowEl?.clientWidth || 0;
|
|
61
|
-
const arrowHeight = arrowEl?.clientHeight || 0;
|
|
62
|
-
const transformX = arrowX + arrowWidth / 2;
|
|
63
|
-
const transformY = arrowY + arrowHeight / 2;
|
|
64
|
-
const shiftY = Math.abs(middlewareData.shift?.y || 0);
|
|
65
|
-
const halfAnchorHeight = rects.reference.height / 2;
|
|
66
|
-
const arrowOffset = arrowHeight / 2;
|
|
67
|
-
const gutter = opts.offset?.mainAxis ?? opts.gutter;
|
|
68
|
-
const sideOffsetValue = typeof gutter === "number" ? gutter + arrowOffset : gutter ?? arrowOffset;
|
|
69
|
-
const isOverlappingAnchor = shiftY > sideOffsetValue;
|
|
70
|
-
const adjacentTransformOrigin = {
|
|
71
|
-
top: `${transformX}px calc(100% + ${sideOffsetValue}px)`,
|
|
72
|
-
bottom: `${transformX}px ${-sideOffsetValue}px`,
|
|
73
|
-
left: `calc(100% + ${sideOffsetValue}px) ${transformY}px`,
|
|
74
|
-
right: `${-sideOffsetValue}px ${transformY}px`
|
|
75
|
-
}[side];
|
|
76
|
-
const overlapTransformOrigin = `${transformX}px ${rects.reference.y + halfAnchorHeight - y}px`;
|
|
77
|
-
const useOverlap = Boolean(opts.overlap) && axis === "y" && isOverlappingAnchor;
|
|
78
|
-
elements.floating.style.setProperty(
|
|
79
|
-
cssVars.transformOrigin.variable,
|
|
80
|
-
useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
81
|
-
);
|
|
82
|
-
return {
|
|
83
|
-
data: {
|
|
84
|
-
transformOrigin: useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
var rectMiddleware = {
|
|
91
|
-
name: "rects",
|
|
92
|
-
fn({ rects }) {
|
|
93
|
-
return {
|
|
94
|
-
data: rects
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
var shiftArrowMiddleware = (arrowEl) => {
|
|
99
|
-
if (!arrowEl) return;
|
|
100
|
-
return {
|
|
101
|
-
name: "shiftArrow",
|
|
102
|
-
fn({ placement, middlewareData }) {
|
|
103
|
-
if (!middlewareData.arrow) return {};
|
|
104
|
-
const { x, y } = middlewareData.arrow;
|
|
105
|
-
const dir = placement.split("-")[0];
|
|
106
|
-
Object.assign(arrowEl.style, {
|
|
107
|
-
left: x != null ? `${x}px` : "",
|
|
108
|
-
top: y != null ? `${y}px` : "",
|
|
109
|
-
[dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
|
|
110
|
-
});
|
|
111
|
-
return {};
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
// src/placement.ts
|
|
117
|
-
function isValidPlacement(v) {
|
|
118
|
-
return /^(?:top|bottom|left|right)(?:-(?:start|end))?$/.test(v);
|
|
119
|
-
}
|
|
120
|
-
function getPlacementDetails(placement) {
|
|
121
|
-
const [side, align] = placement.split("-");
|
|
122
|
-
return { side, align, hasAlign: align != null };
|
|
123
|
-
}
|
|
124
|
-
function getPlacementSide(placement) {
|
|
125
|
-
return placement.split("-")[0];
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// src/get-placement.ts
|
|
129
|
-
var defaultOptions = {
|
|
130
|
-
strategy: "absolute",
|
|
131
|
-
placement: "bottom",
|
|
132
|
-
listeners: true,
|
|
133
|
-
gutter: 8,
|
|
134
|
-
flip: true,
|
|
135
|
-
slide: true,
|
|
136
|
-
overlap: false,
|
|
137
|
-
sameWidth: false,
|
|
138
|
-
fitViewport: false,
|
|
139
|
-
overflowPadding: 8,
|
|
140
|
-
arrowPadding: 4
|
|
141
|
-
};
|
|
142
|
-
function roundByDpr(win, value) {
|
|
143
|
-
const dpr = win.devicePixelRatio || 1;
|
|
144
|
-
return Math.round(value * dpr) / dpr;
|
|
145
|
-
}
|
|
146
|
-
function isApproximatelyEqual(a, b) {
|
|
147
|
-
return a != null && Math.abs(a - b) < 0.5;
|
|
148
|
-
}
|
|
149
|
-
function resolveBoundaryOption(boundary) {
|
|
150
|
-
if (typeof boundary === "function") return boundary();
|
|
151
|
-
if (boundary === "clipping-ancestors") return "clippingAncestors";
|
|
152
|
-
return boundary;
|
|
153
|
-
}
|
|
154
|
-
function getArrowMiddleware(arrowElement, doc, opts) {
|
|
155
|
-
const element = arrowElement || doc.createElement("div");
|
|
156
|
-
return arrow({ element, padding: opts.arrowPadding });
|
|
157
|
-
}
|
|
158
|
-
function getOffsetMiddleware(arrowElement, opts) {
|
|
159
|
-
if (isNull(opts.offset ?? opts.gutter)) return;
|
|
160
|
-
return offset(({ placement }) => {
|
|
161
|
-
const arrowOffset = (arrowElement?.clientHeight || 0) / 2;
|
|
162
|
-
const gutter = opts.offset?.mainAxis ?? opts.gutter;
|
|
163
|
-
const mainAxis = typeof gutter === "number" ? gutter + arrowOffset : gutter ?? arrowOffset;
|
|
164
|
-
const { hasAlign } = getPlacementDetails(placement);
|
|
165
|
-
const shift2 = !hasAlign ? opts.shift : void 0;
|
|
166
|
-
const crossAxis = opts.offset?.crossAxis ?? shift2;
|
|
167
|
-
return compact({
|
|
168
|
-
crossAxis,
|
|
169
|
-
mainAxis,
|
|
170
|
-
alignmentAxis: opts.shift
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function getFlipMiddleware(opts) {
|
|
175
|
-
if (!opts.flip) return;
|
|
176
|
-
const boundary = resolveBoundaryOption(opts.boundary);
|
|
177
|
-
return flip({
|
|
178
|
-
...boundary ? { boundary } : void 0,
|
|
179
|
-
padding: opts.overflowPadding,
|
|
180
|
-
fallbackPlacements: opts.flip === true ? void 0 : opts.flip
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
function getShiftMiddleware(opts) {
|
|
184
|
-
if (!opts.slide && !opts.overlap) return;
|
|
185
|
-
const boundary = resolveBoundaryOption(opts.boundary);
|
|
186
|
-
return shift({
|
|
187
|
-
...boundary ? { boundary } : void 0,
|
|
188
|
-
mainAxis: opts.slide,
|
|
189
|
-
crossAxis: opts.overlap,
|
|
190
|
-
padding: opts.overflowPadding,
|
|
191
|
-
limiter: limitShift()
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
function getSizeMiddleware(opts) {
|
|
195
|
-
if (opts.sizeMiddleware === false && !opts.sameWidth && !opts.fitViewport) return;
|
|
196
|
-
let lastReferenceWidth;
|
|
197
|
-
let lastReferenceHeight;
|
|
198
|
-
let lastAvailableWidth;
|
|
199
|
-
let lastAvailableHeight;
|
|
200
|
-
return size({
|
|
201
|
-
padding: opts.overflowPadding,
|
|
202
|
-
apply({ elements, rects, availableHeight, availableWidth }) {
|
|
203
|
-
const floating = elements.floating;
|
|
204
|
-
const referenceWidth = Math.round(rects.reference.width);
|
|
205
|
-
const referenceHeight = Math.round(rects.reference.height);
|
|
206
|
-
availableWidth = Math.floor(availableWidth);
|
|
207
|
-
availableHeight = Math.floor(availableHeight);
|
|
208
|
-
if (!isApproximatelyEqual(lastReferenceWidth, referenceWidth)) {
|
|
209
|
-
floating.style.setProperty("--reference-width", `${referenceWidth}px`);
|
|
210
|
-
lastReferenceWidth = referenceWidth;
|
|
211
|
-
}
|
|
212
|
-
if (!isApproximatelyEqual(lastReferenceHeight, referenceHeight)) {
|
|
213
|
-
floating.style.setProperty("--reference-height", `${referenceHeight}px`);
|
|
214
|
-
lastReferenceHeight = referenceHeight;
|
|
215
|
-
}
|
|
216
|
-
if (!isApproximatelyEqual(lastAvailableWidth, availableWidth)) {
|
|
217
|
-
floating.style.setProperty("--available-width", `${availableWidth}px`);
|
|
218
|
-
lastAvailableWidth = availableWidth;
|
|
219
|
-
}
|
|
220
|
-
if (!isApproximatelyEqual(lastAvailableHeight, availableHeight)) {
|
|
221
|
-
floating.style.setProperty("--available-height", `${availableHeight}px`);
|
|
222
|
-
lastAvailableHeight = availableHeight;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
function hideWhenDetachedMiddleware(opts) {
|
|
228
|
-
if (!opts.hideWhenDetached) return;
|
|
229
|
-
return hide({ strategy: "referenceHidden", boundary: resolveBoundaryOption(opts.boundary) ?? "clippingAncestors" });
|
|
230
|
-
}
|
|
231
|
-
function getAutoUpdateOptions(opts) {
|
|
232
|
-
if (!opts) return {};
|
|
233
|
-
if (opts === true) {
|
|
234
|
-
return { ancestorResize: true, ancestorScroll: true, elementResize: true, layoutShift: true };
|
|
235
|
-
}
|
|
236
|
-
return opts;
|
|
237
|
-
}
|
|
238
|
-
function getPlacementImpl(referenceOrVirtual, floating, opts = {}) {
|
|
239
|
-
const anchor = opts.getAnchorElement?.() ?? referenceOrVirtual;
|
|
240
|
-
const reference = getAnchorElement(anchor, opts.getAnchorRect);
|
|
241
|
-
if (!floating || !reference) return;
|
|
242
|
-
const options = Object.assign({}, defaultOptions, opts);
|
|
243
|
-
const arrowEl = floating.querySelector("[data-part=arrow]");
|
|
244
|
-
const middleware = [
|
|
245
|
-
getOffsetMiddleware(arrowEl, options),
|
|
246
|
-
getFlipMiddleware(options),
|
|
247
|
-
getShiftMiddleware(options),
|
|
248
|
-
getArrowMiddleware(arrowEl, floating.ownerDocument, options),
|
|
249
|
-
shiftArrowMiddleware(arrowEl),
|
|
250
|
-
createTransformOriginMiddleware(
|
|
251
|
-
{ gutter: options.gutter, offset: options.offset, overlap: options.overlap },
|
|
252
|
-
arrowEl
|
|
253
|
-
),
|
|
254
|
-
getSizeMiddleware(options),
|
|
255
|
-
hideWhenDetachedMiddleware(options),
|
|
256
|
-
rectMiddleware
|
|
257
|
-
];
|
|
258
|
-
const { placement, strategy, onComplete, onPositioned } = options;
|
|
259
|
-
let lastX;
|
|
260
|
-
let lastY;
|
|
261
|
-
let zIndexComputed = false;
|
|
262
|
-
const updatePosition = async () => {
|
|
263
|
-
if (!reference || !floating) return;
|
|
264
|
-
const pos = await computePosition(reference, floating, {
|
|
265
|
-
placement,
|
|
266
|
-
middleware,
|
|
267
|
-
strategy
|
|
268
|
-
});
|
|
269
|
-
onComplete?.(pos);
|
|
270
|
-
const win = getWindow(floating);
|
|
271
|
-
const x = roundByDpr(win, pos.x);
|
|
272
|
-
const y = roundByDpr(win, pos.y);
|
|
273
|
-
floating.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
274
|
-
if (!isApproximatelyEqual(lastX, x)) {
|
|
275
|
-
floating.style.setProperty("--x", `${x}px`);
|
|
276
|
-
lastX = x;
|
|
277
|
-
}
|
|
278
|
-
if (!isApproximatelyEqual(lastY, y)) {
|
|
279
|
-
floating.style.setProperty("--y", `${y}px`);
|
|
280
|
-
lastY = y;
|
|
281
|
-
}
|
|
282
|
-
if (options.hideWhenDetached) {
|
|
283
|
-
const isHidden = pos.middlewareData.hide?.referenceHidden;
|
|
284
|
-
if (isHidden) {
|
|
285
|
-
floating.style.setProperty("visibility", "hidden");
|
|
286
|
-
floating.style.setProperty("pointer-events", "none");
|
|
287
|
-
} else {
|
|
288
|
-
floating.style.removeProperty("visibility");
|
|
289
|
-
floating.style.removeProperty("pointer-events");
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
if (!zIndexComputed) {
|
|
293
|
-
const contentEl = floating.firstElementChild;
|
|
294
|
-
if (contentEl) {
|
|
295
|
-
floating.style.setProperty("--z-index", getComputedStyle(contentEl).zIndex);
|
|
296
|
-
zIndexComputed = true;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
const update = async () => {
|
|
301
|
-
if (opts.updatePosition) {
|
|
302
|
-
await opts.updatePosition({ updatePosition, floatingElement: floating });
|
|
303
|
-
onPositioned?.({ placed: true });
|
|
304
|
-
} else {
|
|
305
|
-
await updatePosition();
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
const autoUpdateOptions = getAutoUpdateOptions(options.listeners);
|
|
309
|
-
const cancelAutoUpdate = options.listeners ? autoUpdate(reference, floating, update, autoUpdateOptions) : noop;
|
|
310
|
-
update();
|
|
311
|
-
return () => {
|
|
312
|
-
cancelAutoUpdate?.();
|
|
313
|
-
onPositioned?.({ placed: false });
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
|
|
317
|
-
const { defer, ...options } = opts;
|
|
318
|
-
const func = defer ? raf : (v) => v();
|
|
319
|
-
const cleanups = [];
|
|
320
|
-
cleanups.push(
|
|
321
|
-
func(() => {
|
|
322
|
-
const reference = typeof referenceOrFn === "function" ? referenceOrFn() : referenceOrFn;
|
|
323
|
-
const floating = typeof floatingOrFn === "function" ? floatingOrFn() : floatingOrFn;
|
|
324
|
-
cleanups.push(getPlacementImpl(reference, floating, options));
|
|
325
|
-
})
|
|
326
|
-
);
|
|
327
|
-
return () => {
|
|
328
|
-
cleanups.forEach((fn) => fn?.());
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// src/get-styles.ts
|
|
333
|
-
var ARROW_FLOATING_STYLE = {
|
|
334
|
-
bottom: "rotate(45deg)",
|
|
335
|
-
left: "rotate(135deg)",
|
|
336
|
-
top: "rotate(225deg)",
|
|
337
|
-
right: "rotate(315deg)"
|
|
338
|
-
};
|
|
339
|
-
function getPlacementStyles(options = {}) {
|
|
340
|
-
const { placement, sameWidth, fitViewport, strategy = "absolute" } = options;
|
|
341
|
-
return {
|
|
342
|
-
arrow: {
|
|
343
|
-
position: "absolute",
|
|
344
|
-
width: cssVars.arrowSize.reference,
|
|
345
|
-
height: cssVars.arrowSize.reference,
|
|
346
|
-
[cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
|
|
347
|
-
[cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
|
|
348
|
-
},
|
|
349
|
-
arrowTip: {
|
|
350
|
-
// @ts-expect-error - Fix this
|
|
351
|
-
transform: placement ? ARROW_FLOATING_STYLE[placement.split("-")[0]] : void 0,
|
|
352
|
-
background: cssVars.arrowBg.reference,
|
|
353
|
-
top: "0",
|
|
354
|
-
left: "0",
|
|
355
|
-
width: "100%",
|
|
356
|
-
height: "100%",
|
|
357
|
-
position: "absolute",
|
|
358
|
-
zIndex: "inherit"
|
|
359
|
-
},
|
|
360
|
-
floating: {
|
|
361
|
-
position: strategy,
|
|
362
|
-
isolation: "isolate",
|
|
363
|
-
minWidth: sameWidth ? void 0 : "max-content",
|
|
364
|
-
width: sameWidth ? "var(--reference-width)" : void 0,
|
|
365
|
-
maxWidth: fitViewport ? "var(--available-width)" : void 0,
|
|
366
|
-
maxHeight: fitViewport ? "var(--available-height)" : void 0,
|
|
367
|
-
pointerEvents: !placement ? "none" : void 0,
|
|
368
|
-
top: "0px",
|
|
369
|
-
left: "0px",
|
|
370
|
-
// move off-screen if placement is not defined
|
|
371
|
-
transform: placement ? "translate3d(var(--x), var(--y), 0)" : "translate3d(0, -100vh, 0)",
|
|
372
|
-
zIndex: "var(--z-index)"
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export { getPlacement, getPlacementSide, getPlacementStyles, isValidPlacement };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Middleware } from '@floating-ui/dom';
|
|
2
|
+
|
|
3
|
+
declare const cssVars: {
|
|
4
|
+
arrowSize: {
|
|
5
|
+
variable: string;
|
|
6
|
+
reference: string;
|
|
7
|
+
};
|
|
8
|
+
arrowSizeHalf: {
|
|
9
|
+
variable: string;
|
|
10
|
+
reference: string;
|
|
11
|
+
};
|
|
12
|
+
arrowBg: {
|
|
13
|
+
variable: string;
|
|
14
|
+
reference: string;
|
|
15
|
+
};
|
|
16
|
+
transformOrigin: {
|
|
17
|
+
variable: string;
|
|
18
|
+
reference: string;
|
|
19
|
+
};
|
|
20
|
+
arrowOffset: {
|
|
21
|
+
variable: string;
|
|
22
|
+
reference: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
declare function createTransformOriginMiddleware(opts: {
|
|
26
|
+
gutter?: number | undefined;
|
|
27
|
+
offset?: {
|
|
28
|
+
mainAxis?: number | undefined;
|
|
29
|
+
} | undefined;
|
|
30
|
+
overlap?: boolean | undefined;
|
|
31
|
+
}, arrowEl: HTMLElement | null): Middleware;
|
|
32
|
+
declare const rectMiddleware: Middleware;
|
|
33
|
+
declare const shiftArrowMiddleware: (arrowEl: HTMLElement | null) => Middleware | undefined;
|
|
34
|
+
|
|
35
|
+
export { createTransformOriginMiddleware, cssVars, rectMiddleware, shiftArrowMiddleware };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Middleware } from '@floating-ui/dom';
|
|
2
|
+
|
|
3
|
+
declare const cssVars: {
|
|
4
|
+
arrowSize: {
|
|
5
|
+
variable: string;
|
|
6
|
+
reference: string;
|
|
7
|
+
};
|
|
8
|
+
arrowSizeHalf: {
|
|
9
|
+
variable: string;
|
|
10
|
+
reference: string;
|
|
11
|
+
};
|
|
12
|
+
arrowBg: {
|
|
13
|
+
variable: string;
|
|
14
|
+
reference: string;
|
|
15
|
+
};
|
|
16
|
+
transformOrigin: {
|
|
17
|
+
variable: string;
|
|
18
|
+
reference: string;
|
|
19
|
+
};
|
|
20
|
+
arrowOffset: {
|
|
21
|
+
variable: string;
|
|
22
|
+
reference: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
declare function createTransformOriginMiddleware(opts: {
|
|
26
|
+
gutter?: number | undefined;
|
|
27
|
+
offset?: {
|
|
28
|
+
mainAxis?: number | undefined;
|
|
29
|
+
} | undefined;
|
|
30
|
+
overlap?: boolean | undefined;
|
|
31
|
+
}, arrowEl: HTMLElement | null): Middleware;
|
|
32
|
+
declare const rectMiddleware: Middleware;
|
|
33
|
+
declare const shiftArrowMiddleware: (arrowEl: HTMLElement | null) => Middleware | undefined;
|
|
34
|
+
|
|
35
|
+
export { createTransformOriginMiddleware, cssVars, rectMiddleware, shiftArrowMiddleware };
|
|
@@ -0,0 +1,108 @@
|
|
|
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/middleware.ts
|
|
21
|
+
var middleware_exports = {};
|
|
22
|
+
__export(middleware_exports, {
|
|
23
|
+
createTransformOriginMiddleware: () => createTransformOriginMiddleware,
|
|
24
|
+
cssVars: () => cssVars,
|
|
25
|
+
rectMiddleware: () => rectMiddleware,
|
|
26
|
+
shiftArrowMiddleware: () => shiftArrowMiddleware
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
29
|
+
var toVar = (value) => ({ variable: value, reference: `var(${value})` });
|
|
30
|
+
var cssVars = {
|
|
31
|
+
arrowSize: toVar("--arrow-size"),
|
|
32
|
+
arrowSizeHalf: toVar("--arrow-size-half"),
|
|
33
|
+
arrowBg: toVar("--arrow-background"),
|
|
34
|
+
transformOrigin: toVar("--transform-origin"),
|
|
35
|
+
arrowOffset: toVar("--arrow-offset")
|
|
36
|
+
};
|
|
37
|
+
var getSideAxis = (side) => side === "top" || side === "bottom" ? "y" : "x";
|
|
38
|
+
function createTransformOriginMiddleware(opts, arrowEl) {
|
|
39
|
+
return {
|
|
40
|
+
name: "transformOrigin",
|
|
41
|
+
fn(state) {
|
|
42
|
+
const { elements, middlewareData, placement, rects, y } = state;
|
|
43
|
+
const side = placement.split("-")[0];
|
|
44
|
+
const axis = getSideAxis(side);
|
|
45
|
+
const arrowX = middlewareData.arrow?.x || 0;
|
|
46
|
+
const arrowY = middlewareData.arrow?.y || 0;
|
|
47
|
+
const arrowWidth = arrowEl?.clientWidth || 0;
|
|
48
|
+
const arrowHeight = arrowEl?.clientHeight || 0;
|
|
49
|
+
const transformX = arrowX + arrowWidth / 2;
|
|
50
|
+
const transformY = arrowY + arrowHeight / 2;
|
|
51
|
+
const shiftY = Math.abs(middlewareData.shift?.y || 0);
|
|
52
|
+
const halfAnchorHeight = rects.reference.height / 2;
|
|
53
|
+
const arrowOffset = arrowHeight / 2;
|
|
54
|
+
const gutter = opts.offset?.mainAxis ?? opts.gutter;
|
|
55
|
+
const sideOffsetValue = typeof gutter === "number" ? gutter + arrowOffset : gutter ?? arrowOffset;
|
|
56
|
+
const isOverlappingAnchor = shiftY > sideOffsetValue;
|
|
57
|
+
const adjacentTransformOrigin = {
|
|
58
|
+
top: `${transformX}px calc(100% + ${sideOffsetValue}px)`,
|
|
59
|
+
bottom: `${transformX}px ${-sideOffsetValue}px`,
|
|
60
|
+
left: `calc(100% + ${sideOffsetValue}px) ${transformY}px`,
|
|
61
|
+
right: `${-sideOffsetValue}px ${transformY}px`
|
|
62
|
+
}[side];
|
|
63
|
+
const overlapTransformOrigin = `${transformX}px ${rects.reference.y + halfAnchorHeight - y}px`;
|
|
64
|
+
const useOverlap = Boolean(opts.overlap) && axis === "y" && isOverlappingAnchor;
|
|
65
|
+
elements.floating.style.setProperty(
|
|
66
|
+
cssVars.transformOrigin.variable,
|
|
67
|
+
useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
68
|
+
);
|
|
69
|
+
return {
|
|
70
|
+
data: {
|
|
71
|
+
transformOrigin: useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var rectMiddleware = {
|
|
78
|
+
name: "rects",
|
|
79
|
+
fn({ rects }) {
|
|
80
|
+
return {
|
|
81
|
+
data: rects
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var shiftArrowMiddleware = (arrowEl) => {
|
|
86
|
+
if (!arrowEl) return;
|
|
87
|
+
return {
|
|
88
|
+
name: "shiftArrow",
|
|
89
|
+
fn({ placement, middlewareData }) {
|
|
90
|
+
if (!middlewareData.arrow) return {};
|
|
91
|
+
const { x, y } = middlewareData.arrow;
|
|
92
|
+
const dir = placement.split("-")[0];
|
|
93
|
+
Object.assign(arrowEl.style, {
|
|
94
|
+
left: x != null ? `${x}px` : "",
|
|
95
|
+
top: y != null ? `${y}px` : "",
|
|
96
|
+
[dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
|
|
97
|
+
});
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
createTransformOriginMiddleware,
|
|
105
|
+
cssVars,
|
|
106
|
+
rectMiddleware,
|
|
107
|
+
shiftArrowMiddleware
|
|
108
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// src/middleware.ts
|
|
2
|
+
var toVar = (value) => ({ variable: value, reference: `var(${value})` });
|
|
3
|
+
var cssVars = {
|
|
4
|
+
arrowSize: toVar("--arrow-size"),
|
|
5
|
+
arrowSizeHalf: toVar("--arrow-size-half"),
|
|
6
|
+
arrowBg: toVar("--arrow-background"),
|
|
7
|
+
transformOrigin: toVar("--transform-origin"),
|
|
8
|
+
arrowOffset: toVar("--arrow-offset")
|
|
9
|
+
};
|
|
10
|
+
var getSideAxis = (side) => side === "top" || side === "bottom" ? "y" : "x";
|
|
11
|
+
function createTransformOriginMiddleware(opts, arrowEl) {
|
|
12
|
+
return {
|
|
13
|
+
name: "transformOrigin",
|
|
14
|
+
fn(state) {
|
|
15
|
+
const { elements, middlewareData, placement, rects, y } = state;
|
|
16
|
+
const side = placement.split("-")[0];
|
|
17
|
+
const axis = getSideAxis(side);
|
|
18
|
+
const arrowX = middlewareData.arrow?.x || 0;
|
|
19
|
+
const arrowY = middlewareData.arrow?.y || 0;
|
|
20
|
+
const arrowWidth = arrowEl?.clientWidth || 0;
|
|
21
|
+
const arrowHeight = arrowEl?.clientHeight || 0;
|
|
22
|
+
const transformX = arrowX + arrowWidth / 2;
|
|
23
|
+
const transformY = arrowY + arrowHeight / 2;
|
|
24
|
+
const shiftY = Math.abs(middlewareData.shift?.y || 0);
|
|
25
|
+
const halfAnchorHeight = rects.reference.height / 2;
|
|
26
|
+
const arrowOffset = arrowHeight / 2;
|
|
27
|
+
const gutter = opts.offset?.mainAxis ?? opts.gutter;
|
|
28
|
+
const sideOffsetValue = typeof gutter === "number" ? gutter + arrowOffset : gutter ?? arrowOffset;
|
|
29
|
+
const isOverlappingAnchor = shiftY > sideOffsetValue;
|
|
30
|
+
const adjacentTransformOrigin = {
|
|
31
|
+
top: `${transformX}px calc(100% + ${sideOffsetValue}px)`,
|
|
32
|
+
bottom: `${transformX}px ${-sideOffsetValue}px`,
|
|
33
|
+
left: `calc(100% + ${sideOffsetValue}px) ${transformY}px`,
|
|
34
|
+
right: `${-sideOffsetValue}px ${transformY}px`
|
|
35
|
+
}[side];
|
|
36
|
+
const overlapTransformOrigin = `${transformX}px ${rects.reference.y + halfAnchorHeight - y}px`;
|
|
37
|
+
const useOverlap = Boolean(opts.overlap) && axis === "y" && isOverlappingAnchor;
|
|
38
|
+
elements.floating.style.setProperty(
|
|
39
|
+
cssVars.transformOrigin.variable,
|
|
40
|
+
useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
41
|
+
);
|
|
42
|
+
return {
|
|
43
|
+
data: {
|
|
44
|
+
transformOrigin: useOverlap ? overlapTransformOrigin : adjacentTransformOrigin
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var rectMiddleware = {
|
|
51
|
+
name: "rects",
|
|
52
|
+
fn({ rects }) {
|
|
53
|
+
return {
|
|
54
|
+
data: rects
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var shiftArrowMiddleware = (arrowEl) => {
|
|
59
|
+
if (!arrowEl) return;
|
|
60
|
+
return {
|
|
61
|
+
name: "shiftArrow",
|
|
62
|
+
fn({ placement, middlewareData }) {
|
|
63
|
+
if (!middlewareData.arrow) return {};
|
|
64
|
+
const { x, y } = middlewareData.arrow;
|
|
65
|
+
const dir = placement.split("-")[0];
|
|
66
|
+
Object.assign(arrowEl.style, {
|
|
67
|
+
left: x != null ? `${x}px` : "",
|
|
68
|
+
top: y != null ? `${y}px` : "",
|
|
69
|
+
[dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
|
|
70
|
+
});
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
export {
|
|
76
|
+
createTransformOriginMiddleware,
|
|
77
|
+
cssVars,
|
|
78
|
+
rectMiddleware,
|
|
79
|
+
shiftArrowMiddleware
|
|
80
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Placement } from '@floating-ui/dom';
|
|
2
|
+
import { PlacementSide, PlacementAlign } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
declare function isValidPlacement(v: string): v is Placement;
|
|
5
|
+
declare function getPlacementDetails(placement: Placement): {
|
|
6
|
+
side: PlacementSide;
|
|
7
|
+
align: PlacementAlign | undefined;
|
|
8
|
+
hasAlign: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare function getPlacementSide(placement: Placement): PlacementSide;
|
|
11
|
+
|
|
12
|
+
export { getPlacementDetails, getPlacementSide, isValidPlacement };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Placement } from '@floating-ui/dom';
|
|
2
|
+
import { PlacementSide, PlacementAlign } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare function isValidPlacement(v: string): v is Placement;
|
|
5
|
+
declare function getPlacementDetails(placement: Placement): {
|
|
6
|
+
side: PlacementSide;
|
|
7
|
+
align: PlacementAlign | undefined;
|
|
8
|
+
hasAlign: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare function getPlacementSide(placement: Placement): PlacementSide;
|
|
11
|
+
|
|
12
|
+
export { getPlacementDetails, getPlacementSide, isValidPlacement };
|