@zag-js/popper 0.1.6 → 0.1.7

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.
@@ -5,4 +5,4 @@ export declare type AutoUpdateOptions = {
5
5
  ancestorResize?: boolean;
6
6
  referenceResize?: boolean;
7
7
  };
8
- export declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): (v: void) => void;
8
+ export declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): () => void;
@@ -1,3 +1,4 @@
1
- import type { VirtualElement } from "@floating-ui/dom";
2
- import { PositioningOptions } from "./types";
3
- export declare function getPlacement(reference: HTMLElement | VirtualElement | null, floating: HTMLElement | null, options?: PositioningOptions): (v: void) => void;
1
+ import type { Placement, VirtualElement } from "@floating-ui/dom";
2
+ import type { BasePlacement, PositioningOptions } from "./types";
3
+ export declare function getPlacement(reference: HTMLElement | VirtualElement | null, floating: HTMLElement | null, opts?: PositioningOptions): () => void;
4
+ export declare function getBasePlacement(placement: Placement): BasePlacement;
@@ -1,4 +1,4 @@
1
- import { Placement } from "@floating-ui/dom";
1
+ import type { Placement } from "@floating-ui/dom";
2
2
  declare type Options = {
3
3
  measured: boolean;
4
4
  strategy?: "absolute" | "fixed";
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { getPlacement } from "./get-placement";
1
+ export { getPlacement, getBasePlacement } from "./get-placement";
2
2
  export { getPlacementStyles } from "./get-styles";
3
3
  export type { Placement, PositioningOptions } from "./types";
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
7
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
@@ -17,6 +19,7 @@ var __spreadValues = (a, b) => {
17
19
  }
18
20
  return a;
19
21
  };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
23
  var __export = (target, all) => {
21
24
  for (var name in all)
22
25
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -34,6 +37,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
34
37
  // src/index.ts
35
38
  var src_exports = {};
36
39
  __export(src_exports, {
40
+ getBasePlacement: () => getBasePlacement,
37
41
  getPlacement: () => getPlacement,
38
42
  getPlacementStyles: () => getPlacementStyles
39
43
  });
@@ -43,20 +47,25 @@ module.exports = __toCommonJS(src_exports);
43
47
  var import_dom2 = require("@floating-ui/dom");
44
48
 
45
49
  // ../core/dist/index.mjs
46
- var isBoolean = (v) => v === true || v === false;
47
- var noop = () => {
50
+ var callAll = (...fns) => (...a) => {
51
+ fns.forEach(function(fn) {
52
+ fn == null ? void 0 : fn(...a);
53
+ });
48
54
  };
49
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
55
+ var isBoolean = (v) => v === true || v === false;
50
56
 
51
57
  // src/auto-update.ts
52
58
  var import_dom = require("@floating-ui/dom");
53
59
 
54
60
  // ../dom/dist/index.mjs
55
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
56
61
  var runIfFn = (v, ...a) => {
57
62
  const res = typeof v === "function" ? v(...a) : v;
58
63
  return res != null ? res : void 0;
59
64
  };
65
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
66
+ function isHTMLElement(v) {
67
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
68
+ }
60
69
  var isRef = (v) => hasProp(v, "current");
61
70
  function addDomEvent(target, eventName, handler, options) {
62
71
  const node = isRef(target) ? target.current : runIfFn(target);
@@ -65,9 +74,6 @@ function addDomEvent(target, eventName, handler, options) {
65
74
  node == null ? void 0 : node.removeEventListener(eventName, handler, options);
66
75
  };
67
76
  }
68
- function isHTMLElement(v) {
69
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
70
- }
71
77
  function getObservedElements() {
72
78
  ;
73
79
  globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
@@ -143,13 +149,13 @@ function autoUpdate(reference, floating, update, options = false) {
143
149
  if (referenceResize && isHTMLElement(reference)) {
144
150
  cleanups.push(observeElementRect(reference, update));
145
151
  }
146
- cleanups.push(pipe(...ancestors.map((el) => addDomEvent(el, "resize", update))));
152
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
147
153
  return () => cleanups.forEach((fn) => fn());
148
154
  }
149
155
  function addScrollListeners() {
150
- return pipe(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
156
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
151
157
  }
152
- return pipe(addResizeListeners(), addScrollListeners());
158
+ return callAll(addResizeListeners(), addScrollListeners());
153
159
  }
154
160
 
155
161
  // src/middleware.ts
@@ -190,20 +196,16 @@ var transformOrigin = {
190
196
  var shiftArrow = (opts) => ({
191
197
  name: "shiftArrow",
192
198
  fn({ placement, middlewareData }) {
193
- var _a;
194
199
  const { element: arrow2 } = opts;
195
- const { x, y } = (_a = middlewareData.arrow) != null ? _a : { x: 0, y: 0 };
196
- const dir = {
197
- top: "bottom",
198
- right: "left",
199
- bottom: "top",
200
- left: "right"
201
- }[placement.split("-")[0]];
202
- Object.assign(arrow2.style, {
203
- top: `${y}px`,
204
- left: `${x}px`,
205
- [dir]: cssVars.arrowOffset.reference
206
- });
200
+ if (middlewareData.arrow) {
201
+ const { x, y } = middlewareData.arrow;
202
+ const dir = placement.split("-")[0];
203
+ Object.assign(arrow2.style, {
204
+ left: x != null ? `${x}px` : "",
205
+ top: y != null ? `${y}px` : "",
206
+ [dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
207
+ });
208
+ }
207
209
  return {};
208
210
  }
209
211
  });
@@ -218,11 +220,10 @@ var defaultOptions = {
218
220
  sameWidth: false,
219
221
  overflowPadding: 8
220
222
  };
221
- function getPlacement(reference, floating, options = {}) {
222
- var _a;
223
- if (reference == null || floating == null)
224
- return noop;
225
- options = Object.assign({}, defaultOptions, options);
223
+ function getPlacement(reference, floating, opts = {}) {
224
+ if (!floating || !reference)
225
+ return;
226
+ const options = Object.assign({}, defaultOptions, opts);
226
227
  const arrowEl = floating.querySelector("[data-part=arrow]");
227
228
  const middleware = [];
228
229
  if (options.flip) {
@@ -247,34 +248,37 @@ function getPlacement(reference, floating, options = {}) {
247
248
  middleware.push((0, import_dom2.arrow)({ element: arrowEl, padding: 8 }), shiftArrow({ element: arrowEl }));
248
249
  }
249
250
  middleware.push(transformOrigin);
250
- if (options.sameWidth || options.fitViewport) {
251
- middleware.push((0, import_dom2.size)({
252
- padding: options.overflowPadding,
253
- apply(data) {
254
- if (options.sameWidth) {
255
- Object.assign(floating.style, {
256
- width: `${data.rects.reference.width}px`,
257
- minWidth: "unset"
258
- });
259
- }
260
- if (options.fitViewport) {
261
- Object.assign(floating.style, {
262
- maxWidth: `${data.availableWidth}px`,
263
- maxHeight: `${data.availableHeight}px`
264
- });
265
- }
251
+ middleware.push((0, import_dom2.size)({
252
+ padding: options.overflowPadding,
253
+ apply({ rects, availableHeight, availableWidth }) {
254
+ const referenceWidth = Math.round(rects.reference.width);
255
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
256
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
257
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
258
+ if (options.sameWidth) {
259
+ Object.assign(floating.style, {
260
+ width: `${referenceWidth}px`,
261
+ minWidth: "unset"
262
+ });
266
263
  }
267
- }));
268
- }
269
- function compute() {
270
- if (reference == null || floating == null)
264
+ if (options.fitViewport) {
265
+ Object.assign(floating.style, {
266
+ maxWidth: `${availableWidth}px`,
267
+ maxHeight: `${availableHeight}px`
268
+ });
269
+ }
270
+ }
271
+ }));
272
+ function compute(config = {}) {
273
+ if (!reference || !floating)
271
274
  return;
272
275
  const { placement, strategy } = options;
273
- (0, import_dom2.computePosition)(reference, floating, {
276
+ (0, import_dom2.computePosition)(reference, floating, __spreadValues({
274
277
  placement,
275
278
  middleware,
276
279
  strategy
277
- }).then((data) => {
280
+ }, config)).then((data) => {
281
+ var _a;
278
282
  const x = Math.round(data.x);
279
283
  const y = Math.round(data.y);
280
284
  Object.assign(floating.style, {
@@ -283,14 +287,14 @@ function getPlacement(reference, floating, options = {}) {
283
287
  left: "0",
284
288
  transform: `translate3d(${x}px, ${y}px, 0)`
285
289
  });
286
- return data;
287
- }).then((data) => {
288
- var _a2;
289
- (_a2 = options.onComplete) == null ? void 0 : _a2.call(options, data);
290
+ (_a = options.onComplete) == null ? void 0 : _a.call(options, __spreadProps(__spreadValues({}, data), { compute }));
290
291
  });
291
292
  }
292
293
  compute();
293
- return pipe(autoUpdate(reference, floating, compute, options.listeners), (_a = options.onCleanup) != null ? _a : noop);
294
+ return callAll(options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0, options.onCleanup);
295
+ }
296
+ function getBasePlacement(placement) {
297
+ return placement.split("-")[0];
294
298
  }
295
299
 
296
300
  // src/get-styles.ts
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
3
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -15,25 +16,31 @@ var __spreadValues = (a, b) => {
15
16
  }
16
17
  return a;
17
18
  };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
18
20
 
19
21
  // src/get-placement.ts
20
22
  import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
21
23
 
22
24
  // ../core/dist/index.mjs
23
- var isBoolean = (v) => v === true || v === false;
24
- var noop = () => {
25
+ var callAll = (...fns) => (...a) => {
26
+ fns.forEach(function(fn) {
27
+ fn == null ? void 0 : fn(...a);
28
+ });
25
29
  };
26
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
30
+ var isBoolean = (v) => v === true || v === false;
27
31
 
28
32
  // src/auto-update.ts
29
33
  import { getOverflowAncestors } from "@floating-ui/dom";
30
34
 
31
35
  // ../dom/dist/index.mjs
32
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
33
36
  var runIfFn = (v, ...a) => {
34
37
  const res = typeof v === "function" ? v(...a) : v;
35
38
  return res != null ? res : void 0;
36
39
  };
40
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
41
+ function isHTMLElement(v) {
42
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
43
+ }
37
44
  var isRef = (v) => hasProp(v, "current");
38
45
  function addDomEvent(target, eventName, handler, options) {
39
46
  const node = isRef(target) ? target.current : runIfFn(target);
@@ -42,9 +49,6 @@ function addDomEvent(target, eventName, handler, options) {
42
49
  node == null ? void 0 : node.removeEventListener(eventName, handler, options);
43
50
  };
44
51
  }
45
- function isHTMLElement(v) {
46
- return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
47
- }
48
52
  function getObservedElements() {
49
53
  ;
50
54
  globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
@@ -120,13 +124,13 @@ function autoUpdate(reference, floating, update, options = false) {
120
124
  if (referenceResize && isHTMLElement(reference)) {
121
125
  cleanups.push(observeElementRect(reference, update));
122
126
  }
123
- cleanups.push(pipe(...ancestors.map((el) => addDomEvent(el, "resize", update))));
127
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
124
128
  return () => cleanups.forEach((fn) => fn());
125
129
  }
126
130
  function addScrollListeners() {
127
- return pipe(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
131
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
128
132
  }
129
- return pipe(addResizeListeners(), addScrollListeners());
133
+ return callAll(addResizeListeners(), addScrollListeners());
130
134
  }
131
135
 
132
136
  // src/middleware.ts
@@ -167,20 +171,16 @@ var transformOrigin = {
167
171
  var shiftArrow = (opts) => ({
168
172
  name: "shiftArrow",
169
173
  fn({ placement, middlewareData }) {
170
- var _a;
171
174
  const { element: arrow2 } = opts;
172
- const { x, y } = (_a = middlewareData.arrow) != null ? _a : { x: 0, y: 0 };
173
- const dir = {
174
- top: "bottom",
175
- right: "left",
176
- bottom: "top",
177
- left: "right"
178
- }[placement.split("-")[0]];
179
- Object.assign(arrow2.style, {
180
- top: `${y}px`,
181
- left: `${x}px`,
182
- [dir]: cssVars.arrowOffset.reference
183
- });
175
+ if (middlewareData.arrow) {
176
+ const { x, y } = middlewareData.arrow;
177
+ const dir = placement.split("-")[0];
178
+ Object.assign(arrow2.style, {
179
+ left: x != null ? `${x}px` : "",
180
+ top: y != null ? `${y}px` : "",
181
+ [dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
182
+ });
183
+ }
184
184
  return {};
185
185
  }
186
186
  });
@@ -195,11 +195,10 @@ var defaultOptions = {
195
195
  sameWidth: false,
196
196
  overflowPadding: 8
197
197
  };
198
- function getPlacement(reference, floating, options = {}) {
199
- var _a;
200
- if (reference == null || floating == null)
201
- return noop;
202
- options = Object.assign({}, defaultOptions, options);
198
+ function getPlacement(reference, floating, opts = {}) {
199
+ if (!floating || !reference)
200
+ return;
201
+ const options = Object.assign({}, defaultOptions, opts);
203
202
  const arrowEl = floating.querySelector("[data-part=arrow]");
204
203
  const middleware = [];
205
204
  if (options.flip) {
@@ -224,34 +223,37 @@ function getPlacement(reference, floating, options = {}) {
224
223
  middleware.push(arrow({ element: arrowEl, padding: 8 }), shiftArrow({ element: arrowEl }));
225
224
  }
226
225
  middleware.push(transformOrigin);
227
- if (options.sameWidth || options.fitViewport) {
228
- middleware.push(size({
229
- padding: options.overflowPadding,
230
- apply(data) {
231
- if (options.sameWidth) {
232
- Object.assign(floating.style, {
233
- width: `${data.rects.reference.width}px`,
234
- minWidth: "unset"
235
- });
236
- }
237
- if (options.fitViewport) {
238
- Object.assign(floating.style, {
239
- maxWidth: `${data.availableWidth}px`,
240
- maxHeight: `${data.availableHeight}px`
241
- });
242
- }
226
+ middleware.push(size({
227
+ padding: options.overflowPadding,
228
+ apply({ rects, availableHeight, availableWidth }) {
229
+ const referenceWidth = Math.round(rects.reference.width);
230
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
231
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
232
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
233
+ if (options.sameWidth) {
234
+ Object.assign(floating.style, {
235
+ width: `${referenceWidth}px`,
236
+ minWidth: "unset"
237
+ });
243
238
  }
244
- }));
245
- }
246
- function compute() {
247
- if (reference == null || floating == null)
239
+ if (options.fitViewport) {
240
+ Object.assign(floating.style, {
241
+ maxWidth: `${availableWidth}px`,
242
+ maxHeight: `${availableHeight}px`
243
+ });
244
+ }
245
+ }
246
+ }));
247
+ function compute(config = {}) {
248
+ if (!reference || !floating)
248
249
  return;
249
250
  const { placement, strategy } = options;
250
- computePosition(reference, floating, {
251
+ computePosition(reference, floating, __spreadValues({
251
252
  placement,
252
253
  middleware,
253
254
  strategy
254
- }).then((data) => {
255
+ }, config)).then((data) => {
256
+ var _a;
255
257
  const x = Math.round(data.x);
256
258
  const y = Math.round(data.y);
257
259
  Object.assign(floating.style, {
@@ -260,14 +262,14 @@ function getPlacement(reference, floating, options = {}) {
260
262
  left: "0",
261
263
  transform: `translate3d(${x}px, ${y}px, 0)`
262
264
  });
263
- return data;
264
- }).then((data) => {
265
- var _a2;
266
- (_a2 = options.onComplete) == null ? void 0 : _a2.call(options, data);
265
+ (_a = options.onComplete) == null ? void 0 : _a.call(options, __spreadProps(__spreadValues({}, data), { compute }));
267
266
  });
268
267
  }
269
268
  compute();
270
- return pipe(autoUpdate(reference, floating, compute, options.listeners), (_a = options.onCleanup) != null ? _a : noop);
269
+ return callAll(options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0, options.onCleanup);
270
+ }
271
+ function getBasePlacement(placement) {
272
+ return placement.split("-")[0];
271
273
  }
272
274
 
273
275
  // src/get-styles.ts
@@ -313,6 +315,7 @@ function getPlacementStyles(options) {
313
315
  };
314
316
  }
315
317
  export {
318
+ getBasePlacement,
316
319
  getPlacement,
317
320
  getPlacementStyles
318
321
  };
@@ -1,4 +1,4 @@
1
- import { Middleware } from "@floating-ui/dom";
1
+ import type { Middleware } from "@floating-ui/dom";
2
2
  export declare const cssVars: {
3
3
  arrowSize: {
4
4
  variable: string;
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Boundary, Placement, ComputePositionReturn } from "@floating-ui/dom";
2
- import { AutoUpdateOptions } from "./auto-update";
1
+ import type { Boundary, Placement, ComputePositionReturn, ComputePositionConfig } from "@floating-ui/dom";
2
+ import type { AutoUpdateOptions } from "./auto-update";
3
3
  export type { Placement };
4
4
  export declare type PositioningOptions = {
5
5
  /**
@@ -53,9 +53,12 @@ export declare type PositioningOptions = {
53
53
  /**
54
54
  * Function called when the placement is computed
55
55
  */
56
- onComplete?(data: ComputePositionReturn): void;
56
+ onComplete?(data: ComputePositionReturn & {
57
+ compute: (config?: Omit<ComputePositionConfig, "platform">) => void;
58
+ }): void;
57
59
  /**
58
60
  * Function called on cleanup of all listeners
59
61
  */
60
62
  onCleanup?: VoidFunction;
61
63
  };
64
+ export declare type BasePlacement = "top" | "right" | "bottom" | "left";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/popper",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Dynamic positioning logic for ui machines",
5
5
  "keywords": [
6
6
  "js",
@@ -25,8 +25,8 @@
25
25
  "url": "https://github.com/chakra-ui/zag/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@floating-ui/dom": "^0.5.0",
29
- "@zag-js/dom-utils": "0.1.5",
28
+ "@floating-ui/dom": "0.5.4",
29
+ "@zag-js/dom-utils": "0.1.6",
30
30
  "@zag-js/utils": "0.1.2"
31
31
  },
32
32
  "scripts": {