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