@zag-js/popper 0.2.5 → 0.8.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.
@@ -49,9 +49,9 @@ function autoUpdate(reference, floating, update, options = false) {
49
49
  ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
50
50
  }
51
51
  function addResizeListeners() {
52
- let cleanups = [(0, import_element_rect.trackElementRect)(floating, update)];
52
+ let cleanups = [(0, import_element_rect.trackElementRect)(floating, { scope: "size", onChange: update })];
53
53
  if (referenceResize && isHTMLElement(reference)) {
54
- cleanups.push((0, import_element_rect.trackElementRect)(reference, update));
54
+ cleanups.push((0, import_element_rect.trackElementRect)(reference, { onChange: update }));
55
55
  }
56
56
  cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
57
57
  return () => cleanups.forEach((fn) => fn());
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  autoUpdate
3
- } from "./chunk-KMOWWEFH.mjs";
3
+ } from "./chunk-MLNMIWN3.mjs";
4
4
  export {
5
5
  autoUpdate
6
6
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  autoUpdate
3
- } from "./chunk-KMOWWEFH.mjs";
3
+ } from "./chunk-MLNMIWN3.mjs";
4
4
  import {
5
5
  shiftArrow,
6
6
  transformOrigin
@@ -8,6 +8,7 @@ import {
8
8
 
9
9
  // src/get-placement.ts
10
10
  import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
11
+ import { raf } from "@zag-js/dom-query";
11
12
  import { callAll } from "@zag-js/utils";
12
13
  var defaultOptions = {
13
14
  strategy: "absolute",
@@ -18,7 +19,7 @@ var defaultOptions = {
18
19
  sameWidth: false,
19
20
  overflowPadding: 8
20
21
  };
21
- function getPlacement(reference, floating, opts = {}) {
22
+ function getPlacementImpl(reference, floating, opts = {}) {
22
23
  if (!floating || !reference)
23
24
  return;
24
25
  const options = Object.assign({}, defaultOptions, opts);
@@ -80,7 +81,7 @@ function getPlacement(reference, floating, opts = {}) {
80
81
  function compute(config = {}) {
81
82
  if (!reference || !floating)
82
83
  return;
83
- const { placement, strategy } = options;
84
+ const { placement, strategy, onComplete } = options;
84
85
  computePosition(reference, floating, {
85
86
  placement,
86
87
  middleware,
@@ -91,11 +92,11 @@ function getPlacement(reference, floating, opts = {}) {
91
92
  const y = Math.round(data.y);
92
93
  Object.assign(floating.style, {
93
94
  position: data.strategy,
94
- top: "0",
95
- left: "0",
95
+ top: "0px",
96
+ left: "0px",
96
97
  transform: `translate3d(${x}px, ${y}px, 0)`
97
98
  });
98
- options.onComplete?.({ ...data, compute });
99
+ onComplete?.(data);
99
100
  });
100
101
  }
101
102
  compute();
@@ -107,8 +108,23 @@ function getPlacement(reference, floating, opts = {}) {
107
108
  function getBasePlacement(placement) {
108
109
  return placement.split("-")[0];
109
110
  }
111
+ function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
112
+ const { defer, ...restOptions } = opts;
113
+ const func = defer ? raf : (v) => v();
114
+ const cleanups = [];
115
+ cleanups.push(
116
+ func(() => {
117
+ const reference = typeof referenceOrFn === "function" ? referenceOrFn() : referenceOrFn;
118
+ const floating = typeof floatingOrFn === "function" ? floatingOrFn() : floatingOrFn;
119
+ cleanups.push(getPlacementImpl(reference, floating, restOptions));
120
+ })
121
+ );
122
+ return () => {
123
+ cleanups.forEach((fn) => fn?.());
124
+ };
125
+ }
110
126
 
111
127
  export {
112
- getPlacement,
113
- getBasePlacement
128
+ getBasePlacement,
129
+ getPlacement
114
130
  };
@@ -25,9 +25,9 @@ function autoUpdate(reference, floating, update, options = false) {
25
25
  ancestors.push(...getOverflowAncestors(reference));
26
26
  }
27
27
  function addResizeListeners() {
28
- let cleanups = [trackElementRect(floating, update)];
28
+ let cleanups = [trackElementRect(floating, { scope: "size", onChange: update })];
29
29
  if (referenceResize && isHTMLElement(reference)) {
30
- cleanups.push(trackElementRect(reference, update));
30
+ cleanups.push(trackElementRect(reference, { onChange: update }));
31
31
  }
32
32
  cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
33
33
  return () => cleanups.forEach((fn) => fn());
@@ -3,14 +3,6 @@ import {
3
3
  } from "./chunk-X5LLREVI.mjs";
4
4
 
5
5
  // src/get-styles.ts
6
- var UNMEASURED_FLOATING_STYLE = {
7
- position: "fixed",
8
- top: 0,
9
- left: 0,
10
- opacity: 0,
11
- transform: "translate3d(0, -200%, 0)",
12
- pointerEvents: "none"
13
- };
14
6
  var ARROW_FLOATING_STYLE = {
15
7
  bottom: "rotate(45deg)",
16
8
  left: "rotate(135deg)",
@@ -18,15 +10,14 @@ var ARROW_FLOATING_STYLE = {
18
10
  right: "rotate(315deg)"
19
11
  };
20
12
  function getPlacementStyles(options) {
21
- const { measured, strategy = "absolute", placement = "bottom" } = options;
13
+ const { placement = "bottom" } = options;
22
14
  return {
23
15
  arrow: {
24
16
  position: "absolute",
25
17
  width: cssVars.arrowSize.reference,
26
18
  height: cssVars.arrowSize.reference,
27
19
  [cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
28
- [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`,
29
- opacity: !measured ? 0 : void 0
20
+ [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
30
21
  },
31
22
  arrowTip: {
32
23
  transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
@@ -39,9 +30,10 @@ function getPlacementStyles(options) {
39
30
  zIndex: "inherit"
40
31
  },
41
32
  floating: {
42
- position: strategy,
33
+ position: "absolute",
43
34
  minWidth: "max-content",
44
- ...!measured && UNMEASURED_FLOATING_STYLE
35
+ top: "0px",
36
+ left: "0px"
45
37
  }
46
38
  };
47
39
  }
@@ -1,8 +1,13 @@
1
- import { VirtualElement, Placement } from '@floating-ui/dom';
2
- import { PositioningOptions, BasePlacement } from './types.js';
1
+ import { Placement, VirtualElement } from '@floating-ui/dom';
2
+ import { BasePlacement, PositioningOptions } from './types.js';
3
3
  import './auto-update.js';
4
4
 
5
- declare function getPlacement(reference: HTMLElement | VirtualElement | null, floating: HTMLElement | null, opts?: PositioningOptions): (() => void) | undefined;
5
+ type MaybeRectElement = HTMLElement | VirtualElement | null;
6
+ type MaybeElement = HTMLElement | null;
7
+ type MaybeFn<T> = T | (() => T);
6
8
  declare function getBasePlacement(placement: Placement): BasePlacement;
9
+ declare function getPlacement(referenceOrFn: MaybeFn<MaybeRectElement>, floatingOrFn: MaybeFn<MaybeElement>, opts?: PositioningOptions & {
10
+ defer?: boolean;
11
+ }): () => void;
7
12
 
8
13
  export { getBasePlacement, getPlacement };
@@ -25,6 +25,7 @@ __export(get_placement_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(get_placement_exports);
27
27
  var import_dom2 = require("@floating-ui/dom");
28
+ var import_dom_query = require("@zag-js/dom-query");
28
29
  var import_utils = require("@zag-js/utils");
29
30
 
30
31
  // src/auto-update.ts
@@ -54,9 +55,9 @@ function autoUpdate(reference, floating, update, options = false) {
54
55
  ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
55
56
  }
56
57
  function addResizeListeners() {
57
- let cleanups = [(0, import_element_rect.trackElementRect)(floating, update)];
58
+ let cleanups = [(0, import_element_rect.trackElementRect)(floating, { scope: "size", onChange: update })];
58
59
  if (referenceResize && isHTMLElement(reference)) {
59
- cleanups.push((0, import_element_rect.trackElementRect)(reference, update));
60
+ cleanups.push((0, import_element_rect.trackElementRect)(reference, { onChange: update }));
60
61
  }
61
62
  cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
62
63
  return () => cleanups.forEach((fn) => fn());
@@ -129,7 +130,7 @@ var defaultOptions = {
129
130
  sameWidth: false,
130
131
  overflowPadding: 8
131
132
  };
132
- function getPlacement(reference, floating, opts = {}) {
133
+ function getPlacementImpl(reference, floating, opts = {}) {
133
134
  if (!floating || !reference)
134
135
  return;
135
136
  const options = Object.assign({}, defaultOptions, opts);
@@ -191,7 +192,7 @@ function getPlacement(reference, floating, opts = {}) {
191
192
  function compute(config = {}) {
192
193
  if (!reference || !floating)
193
194
  return;
194
- const { placement, strategy } = options;
195
+ const { placement, strategy, onComplete } = options;
195
196
  (0, import_dom2.computePosition)(reference, floating, {
196
197
  placement,
197
198
  middleware,
@@ -202,11 +203,11 @@ function getPlacement(reference, floating, opts = {}) {
202
203
  const y = Math.round(data.y);
203
204
  Object.assign(floating.style, {
204
205
  position: data.strategy,
205
- top: "0",
206
- left: "0",
206
+ top: "0px",
207
+ left: "0px",
207
208
  transform: `translate3d(${x}px, ${y}px, 0)`
208
209
  });
209
- options.onComplete?.({ ...data, compute });
210
+ onComplete?.(data);
210
211
  });
211
212
  }
212
213
  compute();
@@ -218,6 +219,21 @@ function getPlacement(reference, floating, opts = {}) {
218
219
  function getBasePlacement(placement) {
219
220
  return placement.split("-")[0];
220
221
  }
222
+ function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
223
+ const { defer, ...restOptions } = opts;
224
+ const func = defer ? import_dom_query.raf : (v) => v();
225
+ const cleanups = [];
226
+ cleanups.push(
227
+ func(() => {
228
+ const reference = typeof referenceOrFn === "function" ? referenceOrFn() : referenceOrFn;
229
+ const floating = typeof floatingOrFn === "function" ? floatingOrFn() : floatingOrFn;
230
+ cleanups.push(getPlacementImpl(reference, floating, restOptions));
231
+ })
232
+ );
233
+ return () => {
234
+ cleanups.forEach((fn) => fn?.());
235
+ };
236
+ }
221
237
  // Annotate the CommonJS export names for ESM import in node:
222
238
  0 && (module.exports = {
223
239
  getBasePlacement,
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  getBasePlacement,
3
3
  getPlacement
4
- } from "./chunk-K6E3OR2A.mjs";
5
- import "./chunk-KMOWWEFH.mjs";
4
+ } from "./chunk-EC2KMZAZ.mjs";
5
+ import "./chunk-MLNMIWN3.mjs";
6
6
  import "./chunk-X5LLREVI.mjs";
7
7
  export {
8
8
  getBasePlacement,
@@ -1,17 +1,14 @@
1
1
  import { Placement } from '@floating-ui/dom';
2
2
 
3
3
  type Options = {
4
- measured: boolean;
5
- strategy?: "absolute" | "fixed";
6
4
  placement?: Placement;
7
5
  };
8
6
  declare function getPlacementStyles(options: Options): {
9
7
  arrow: {
10
- readonly [x: string]: string | 0 | undefined;
8
+ readonly [x: string]: string;
11
9
  readonly position: "absolute";
12
10
  readonly width: string;
13
11
  readonly height: string;
14
- readonly opacity: 0 | undefined;
15
12
  };
16
13
  arrowTip: {
17
14
  readonly transform: any;
@@ -24,13 +21,10 @@ declare function getPlacementStyles(options: Options): {
24
21
  readonly zIndex: "inherit";
25
22
  };
26
23
  floating: {
27
- position: "absolute" | "fixed";
28
- readonly top?: 0 | undefined;
29
- readonly left?: 0 | undefined;
30
- readonly opacity?: 0 | undefined;
31
- readonly transform?: "translate3d(0, -200%, 0)" | undefined;
32
- readonly pointerEvents?: "none" | undefined;
24
+ readonly position: "absolute";
33
25
  readonly minWidth: "max-content";
26
+ readonly top: "0px";
27
+ readonly left: "0px";
34
28
  };
35
29
  };
36
30
 
@@ -35,14 +35,6 @@ var cssVars = {
35
35
  };
36
36
 
37
37
  // src/get-styles.ts
38
- var UNMEASURED_FLOATING_STYLE = {
39
- position: "fixed",
40
- top: 0,
41
- left: 0,
42
- opacity: 0,
43
- transform: "translate3d(0, -200%, 0)",
44
- pointerEvents: "none"
45
- };
46
38
  var ARROW_FLOATING_STYLE = {
47
39
  bottom: "rotate(45deg)",
48
40
  left: "rotate(135deg)",
@@ -50,15 +42,14 @@ var ARROW_FLOATING_STYLE = {
50
42
  right: "rotate(315deg)"
51
43
  };
52
44
  function getPlacementStyles(options) {
53
- const { measured, strategy = "absolute", placement = "bottom" } = options;
45
+ const { placement = "bottom" } = options;
54
46
  return {
55
47
  arrow: {
56
48
  position: "absolute",
57
49
  width: cssVars.arrowSize.reference,
58
50
  height: cssVars.arrowSize.reference,
59
51
  [cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
60
- [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`,
61
- opacity: !measured ? 0 : void 0
52
+ [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
62
53
  },
63
54
  arrowTip: {
64
55
  transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
@@ -71,9 +62,10 @@ function getPlacementStyles(options) {
71
62
  zIndex: "inherit"
72
63
  },
73
64
  floating: {
74
- position: strategy,
65
+ position: "absolute",
75
66
  minWidth: "max-content",
76
- ...!measured && UNMEASURED_FLOATING_STYLE
67
+ top: "0px",
68
+ left: "0px"
77
69
  }
78
70
  };
79
71
  }
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getPlacementStyles
3
- } from "./chunk-I46LTIWB.mjs";
3
+ } from "./chunk-V4C4UQPN.mjs";
4
4
  import "./chunk-X5LLREVI.mjs";
5
5
  export {
6
6
  getPlacementStyles
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ module.exports = __toCommonJS(src_exports);
28
28
 
29
29
  // src/get-placement.ts
30
30
  var import_dom2 = require("@floating-ui/dom");
31
+ var import_dom_query = require("@zag-js/dom-query");
31
32
  var import_utils = require("@zag-js/utils");
32
33
 
33
34
  // src/auto-update.ts
@@ -57,9 +58,9 @@ function autoUpdate(reference, floating, update, options = false) {
57
58
  ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
58
59
  }
59
60
  function addResizeListeners() {
60
- let cleanups = [(0, import_element_rect.trackElementRect)(floating, update)];
61
+ let cleanups = [(0, import_element_rect.trackElementRect)(floating, { scope: "size", onChange: update })];
61
62
  if (referenceResize && isHTMLElement(reference)) {
62
- cleanups.push((0, import_element_rect.trackElementRect)(reference, update));
63
+ cleanups.push((0, import_element_rect.trackElementRect)(reference, { onChange: update }));
63
64
  }
64
65
  cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
65
66
  return () => cleanups.forEach((fn) => fn());
@@ -132,7 +133,7 @@ var defaultOptions = {
132
133
  sameWidth: false,
133
134
  overflowPadding: 8
134
135
  };
135
- function getPlacement(reference, floating, opts = {}) {
136
+ function getPlacementImpl(reference, floating, opts = {}) {
136
137
  if (!floating || !reference)
137
138
  return;
138
139
  const options = Object.assign({}, defaultOptions, opts);
@@ -194,7 +195,7 @@ function getPlacement(reference, floating, opts = {}) {
194
195
  function compute(config = {}) {
195
196
  if (!reference || !floating)
196
197
  return;
197
- const { placement, strategy } = options;
198
+ const { placement, strategy, onComplete } = options;
198
199
  (0, import_dom2.computePosition)(reference, floating, {
199
200
  placement,
200
201
  middleware,
@@ -205,11 +206,11 @@ function getPlacement(reference, floating, opts = {}) {
205
206
  const y = Math.round(data.y);
206
207
  Object.assign(floating.style, {
207
208
  position: data.strategy,
208
- top: "0",
209
- left: "0",
209
+ top: "0px",
210
+ left: "0px",
210
211
  transform: `translate3d(${x}px, ${y}px, 0)`
211
212
  });
212
- options.onComplete?.({ ...data, compute });
213
+ onComplete?.(data);
213
214
  });
214
215
  }
215
216
  compute();
@@ -221,16 +222,23 @@ function getPlacement(reference, floating, opts = {}) {
221
222
  function getBasePlacement(placement) {
222
223
  return placement.split("-")[0];
223
224
  }
225
+ function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
226
+ const { defer, ...restOptions } = opts;
227
+ const func = defer ? import_dom_query.raf : (v) => v();
228
+ const cleanups = [];
229
+ cleanups.push(
230
+ func(() => {
231
+ const reference = typeof referenceOrFn === "function" ? referenceOrFn() : referenceOrFn;
232
+ const floating = typeof floatingOrFn === "function" ? floatingOrFn() : floatingOrFn;
233
+ cleanups.push(getPlacementImpl(reference, floating, restOptions));
234
+ })
235
+ );
236
+ return () => {
237
+ cleanups.forEach((fn) => fn?.());
238
+ };
239
+ }
224
240
 
225
241
  // src/get-styles.ts
226
- var UNMEASURED_FLOATING_STYLE = {
227
- position: "fixed",
228
- top: 0,
229
- left: 0,
230
- opacity: 0,
231
- transform: "translate3d(0, -200%, 0)",
232
- pointerEvents: "none"
233
- };
234
242
  var ARROW_FLOATING_STYLE = {
235
243
  bottom: "rotate(45deg)",
236
244
  left: "rotate(135deg)",
@@ -238,15 +246,14 @@ var ARROW_FLOATING_STYLE = {
238
246
  right: "rotate(315deg)"
239
247
  };
240
248
  function getPlacementStyles(options) {
241
- const { measured, strategy = "absolute", placement = "bottom" } = options;
249
+ const { placement = "bottom" } = options;
242
250
  return {
243
251
  arrow: {
244
252
  position: "absolute",
245
253
  width: cssVars.arrowSize.reference,
246
254
  height: cssVars.arrowSize.reference,
247
255
  [cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
248
- [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`,
249
- opacity: !measured ? 0 : void 0
256
+ [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
250
257
  },
251
258
  arrowTip: {
252
259
  transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
@@ -259,9 +266,10 @@ function getPlacementStyles(options) {
259
266
  zIndex: "inherit"
260
267
  },
261
268
  floating: {
262
- position: strategy,
269
+ position: "absolute",
263
270
  minWidth: "max-content",
264
- ...!measured && UNMEASURED_FLOATING_STYLE
271
+ top: "0px",
272
+ left: "0px"
265
273
  }
266
274
  };
267
275
  }
package/dist/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  getBasePlacement,
3
3
  getPlacement
4
- } from "./chunk-K6E3OR2A.mjs";
5
- import "./chunk-KMOWWEFH.mjs";
4
+ } from "./chunk-EC2KMZAZ.mjs";
5
+ import "./chunk-MLNMIWN3.mjs";
6
6
  import {
7
7
  getPlacementStyles
8
- } from "./chunk-I46LTIWB.mjs";
8
+ } from "./chunk-V4C4UQPN.mjs";
9
9
  import "./chunk-X5LLREVI.mjs";
10
10
  export {
11
11
  getBasePlacement,
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Placement, Boundary, ComputePositionReturn, ComputePositionConfig } from '@floating-ui/dom';
1
+ import { Placement, Boundary, ComputePositionReturn } from '@floating-ui/dom';
2
2
  export { Placement } from '@floating-ui/dom';
3
3
  import { AutoUpdateOptions } from './auto-update.js';
4
4
 
@@ -54,9 +54,7 @@ type PositioningOptions = {
54
54
  /**
55
55
  * Function called when the placement is computed
56
56
  */
57
- onComplete?(data: ComputePositionReturn & {
58
- compute: (config?: Omit<ComputePositionConfig, "platform">) => void;
59
- }): void;
57
+ onComplete?(data: ComputePositionReturn): void;
60
58
  /**
61
59
  * Function called on cleanup of all listeners
62
60
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/popper",
3
- "version": "0.2.5",
3
+ "version": "0.8.0",
4
4
  "description": "Dynamic positioning logic for ui machines",
5
5
  "keywords": [
6
6
  "js",
@@ -22,10 +22,10 @@
22
22
  "url": "https://github.com/chakra-ui/zag/issues"
23
23
  },
24
24
  "dependencies": {
25
- "@floating-ui/dom": "1.2.1",
25
+ "@floating-ui/dom": "1.2.6",
26
26
  "@zag-js/dom-query": "0.1.4",
27
- "@zag-js/element-rect": "0.2.2",
28
- "@zag-js/utils": "0.3.3"
27
+ "@zag-js/element-rect": "0.8.0",
28
+ "@zag-js/utils": "0.3.4"
29
29
  },
30
30
  "devDependencies": {
31
31
  "clean-package": "2.2.0"