@zag-js/popper 0.1.6 → 0.1.9

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,109 @@
1
- export { getPlacement } from "./get-placement";
2
- export { getPlacementStyles } from "./get-styles";
3
- export type { Placement, PositioningOptions } from "./types";
1
+ import { Placement, Boundary, ComputePositionReturn, ComputePositionConfig, VirtualElement } from '@floating-ui/dom';
2
+ export { Placement } from '@floating-ui/dom';
3
+
4
+ declare type AutoUpdateOptions = {
5
+ ancestorScroll?: boolean;
6
+ ancestorResize?: boolean;
7
+ referenceResize?: boolean;
8
+ };
9
+
10
+ declare type PositioningOptions = {
11
+ /**
12
+ * The strategy to use for positioning
13
+ */
14
+ strategy?: "absolute" | "fixed";
15
+ /**
16
+ * The initial placement of the floating element
17
+ */
18
+ placement?: Placement;
19
+ /**
20
+ * The offset of the floating element
21
+ */
22
+ offset?: {
23
+ mainAxis?: number;
24
+ crossAxis?: number;
25
+ };
26
+ /**
27
+ * The main axis offset or gap between the reference and floating elements
28
+ */
29
+ gutter?: number;
30
+ /**
31
+ * The virtual padding around the viewport edges to check for overflow
32
+ */
33
+ overflowPadding?: number;
34
+ /**
35
+ * Whether to flip the placement
36
+ */
37
+ flip?: boolean;
38
+ /**
39
+ * Whether the floating element can overlap the reference element
40
+ * @default false
41
+ */
42
+ overlap?: boolean;
43
+ /**
44
+ * Whether to make the floating element same width as the reference element
45
+ */
46
+ sameWidth?: boolean;
47
+ /**
48
+ * Whether the popover should fit the viewport.
49
+ */
50
+ fitViewport?: boolean;
51
+ /**
52
+ * The overflow boundary of the reference element
53
+ */
54
+ boundary?: Boundary;
55
+ /**
56
+ * Options to activate auto-update listeners
57
+ */
58
+ listeners?: boolean | AutoUpdateOptions;
59
+ /**
60
+ * Function called when the placement is computed
61
+ */
62
+ onComplete?(data: ComputePositionReturn & {
63
+ compute: (config?: Omit<ComputePositionConfig, "platform">) => void;
64
+ }): void;
65
+ /**
66
+ * Function called on cleanup of all listeners
67
+ */
68
+ onCleanup?: VoidFunction;
69
+ };
70
+ declare type BasePlacement = "top" | "right" | "bottom" | "left";
71
+
72
+ declare function getPlacement(reference: HTMLElement | VirtualElement | null, floating: HTMLElement | null, opts?: PositioningOptions): (() => void) | undefined;
73
+ declare function getBasePlacement(placement: Placement): BasePlacement;
74
+
75
+ declare type Options = {
76
+ measured: boolean;
77
+ strategy?: "absolute" | "fixed";
78
+ placement?: Placement;
79
+ };
80
+ declare function getPlacementStyles(options: Options): {
81
+ arrow: {
82
+ readonly [x: string]: string | 0 | undefined;
83
+ readonly position: "absolute";
84
+ readonly width: string;
85
+ readonly height: string;
86
+ readonly opacity: 0 | undefined;
87
+ };
88
+ innerArrow: {
89
+ readonly transform: any;
90
+ readonly background: string;
91
+ readonly top: "0";
92
+ readonly left: "0";
93
+ readonly width: "100%";
94
+ readonly height: "100%";
95
+ readonly position: "absolute";
96
+ readonly zIndex: "inherit";
97
+ };
98
+ floating: {
99
+ position: "absolute" | "fixed";
100
+ readonly top?: 0 | undefined;
101
+ readonly left?: 0 | undefined;
102
+ readonly opacity?: 0 | undefined;
103
+ readonly transform?: "translate3d(0, -200%, 0)" | undefined;
104
+ readonly pointerEvents?: "none" | undefined;
105
+ readonly minWidth: "max-content";
106
+ };
107
+ };
108
+
109
+ export { PositioningOptions, getBasePlacement, getPlacement, getPlacementStyles };
package/dist/index.js CHANGED
@@ -2,21 +2,7 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __spreadValues = (a, b) => {
10
- for (var prop in b || (b = {}))
11
- if (__hasOwnProp.call(b, prop))
12
- __defNormalProp(a, prop, b[prop]);
13
- if (__getOwnPropSymbols)
14
- for (var prop of __getOwnPropSymbols(b)) {
15
- if (__propIsEnum.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- }
18
- return a;
19
- };
20
6
  var __export = (target, all) => {
21
7
  for (var name in all)
22
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -34,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
34
20
  // src/index.ts
35
21
  var src_exports = {};
36
22
  __export(src_exports, {
23
+ getBasePlacement: () => getBasePlacement,
37
24
  getPlacement: () => getPlacement,
38
25
  getPlacementStyles: () => getPlacementStyles
39
26
  });
@@ -43,20 +30,25 @@ module.exports = __toCommonJS(src_exports);
43
30
  var import_dom2 = require("@floating-ui/dom");
44
31
 
45
32
  // ../core/dist/index.mjs
46
- var isBoolean = (v) => v === true || v === false;
47
- var noop = () => {
33
+ var callAll = (...fns) => (...a) => {
34
+ fns.forEach(function(fn) {
35
+ fn == null ? void 0 : fn(...a);
36
+ });
48
37
  };
49
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
38
+ var isBoolean = (v) => v === true || v === false;
50
39
 
51
40
  // src/auto-update.ts
52
41
  var import_dom = require("@floating-ui/dom");
53
42
 
54
43
  // ../dom/dist/index.mjs
55
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
56
44
  var runIfFn = (v, ...a) => {
57
45
  const res = typeof v === "function" ? v(...a) : v;
58
- return res != null ? res : void 0;
46
+ return res ?? void 0;
59
47
  };
48
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
49
+ function isHTMLElement(v) {
50
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
51
+ }
60
52
  var isRef = (v) => hasProp(v, "current");
61
53
  function addDomEvent(target, eventName, handler, options) {
62
54
  const node = isRef(target) ? target.current : runIfFn(target);
@@ -65,9 +57,6 @@ function addDomEvent(target, eventName, handler, options) {
65
57
  node == null ? void 0 : node.removeEventListener(eventName, handler, options);
66
58
  };
67
59
  }
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
60
  function getObservedElements() {
72
61
  ;
73
62
  globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
@@ -123,12 +112,11 @@ function isEqual(rect1, rect2) {
123
112
 
124
113
  // src/auto-update.ts
125
114
  function resolveOptions(option) {
126
- var _a, _b, _c;
127
115
  const bool = isBoolean(option);
128
116
  return {
129
- ancestorResize: bool ? option : (_a = option.ancestorResize) != null ? _a : true,
130
- ancestorScroll: bool ? option : (_b = option.ancestorScroll) != null ? _b : true,
131
- referenceResize: bool ? option : (_c = option.referenceResize) != null ? _c : true
117
+ ancestorResize: bool ? option : option.ancestorResize ?? true,
118
+ ancestorScroll: bool ? option : option.ancestorScroll ?? true,
119
+ referenceResize: bool ? option : option.referenceResize ?? true
132
120
  };
133
121
  }
134
122
  function autoUpdate(reference, floating, update, options = false) {
@@ -143,13 +131,13 @@ function autoUpdate(reference, floating, update, options = false) {
143
131
  if (referenceResize && isHTMLElement(reference)) {
144
132
  cleanups.push(observeElementRect(reference, update));
145
133
  }
146
- cleanups.push(pipe(...ancestors.map((el) => addDomEvent(el, "resize", update))));
134
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
147
135
  return () => cleanups.forEach((fn) => fn());
148
136
  }
149
137
  function addScrollListeners() {
150
- return pipe(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
138
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
151
139
  }
152
- return pipe(addResizeListeners(), addScrollListeners());
140
+ return callAll(addResizeListeners(), addScrollListeners());
153
141
  }
154
142
 
155
143
  // src/middleware.ts
@@ -190,20 +178,16 @@ var transformOrigin = {
190
178
  var shiftArrow = (opts) => ({
191
179
  name: "shiftArrow",
192
180
  fn({ placement, middlewareData }) {
193
- var _a;
194
181
  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
- });
182
+ if (middlewareData.arrow) {
183
+ const { x, y } = middlewareData.arrow;
184
+ const dir = placement.split("-")[0];
185
+ Object.assign(arrow2.style, {
186
+ left: x != null ? `${x}px` : "",
187
+ top: y != null ? `${y}px` : "",
188
+ [dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
189
+ });
190
+ }
207
191
  return {};
208
192
  }
209
193
  });
@@ -218,11 +202,10 @@ var defaultOptions = {
218
202
  sameWidth: false,
219
203
  overflowPadding: 8
220
204
  };
221
- function getPlacement(reference, floating, options = {}) {
222
- var _a;
223
- if (reference == null || floating == null)
224
- return noop;
225
- options = Object.assign({}, defaultOptions, options);
205
+ function getPlacement(reference, floating, opts = {}) {
206
+ if (!floating || !reference)
207
+ return;
208
+ const options = Object.assign({}, defaultOptions, opts);
226
209
  const arrowEl = floating.querySelector("[data-part=arrow]");
227
210
  const middleware = [];
228
211
  if (options.flip) {
@@ -247,34 +230,38 @@ function getPlacement(reference, floating, options = {}) {
247
230
  middleware.push((0, import_dom2.arrow)({ element: arrowEl, padding: 8 }), shiftArrow({ element: arrowEl }));
248
231
  }
249
232
  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
- }
233
+ middleware.push((0, import_dom2.size)({
234
+ padding: options.overflowPadding,
235
+ apply({ rects, availableHeight, availableWidth }) {
236
+ const referenceWidth = Math.round(rects.reference.width);
237
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
238
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
239
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
240
+ if (options.sameWidth) {
241
+ Object.assign(floating.style, {
242
+ width: `${referenceWidth}px`,
243
+ minWidth: "unset"
244
+ });
266
245
  }
267
- }));
268
- }
269
- function compute() {
270
- if (reference == null || floating == null)
246
+ if (options.fitViewport) {
247
+ Object.assign(floating.style, {
248
+ maxWidth: `${availableWidth}px`,
249
+ maxHeight: `${availableHeight}px`
250
+ });
251
+ }
252
+ }
253
+ }));
254
+ function compute(config = {}) {
255
+ if (!reference || !floating)
271
256
  return;
272
257
  const { placement, strategy } = options;
273
258
  (0, import_dom2.computePosition)(reference, floating, {
274
259
  placement,
275
260
  middleware,
276
- strategy
261
+ strategy,
262
+ ...config
277
263
  }).then((data) => {
264
+ var _a;
278
265
  const x = Math.round(data.x);
279
266
  const y = Math.round(data.y);
280
267
  Object.assign(floating.style, {
@@ -283,14 +270,14 @@ function getPlacement(reference, floating, options = {}) {
283
270
  left: "0",
284
271
  transform: `translate3d(${x}px, ${y}px, 0)`
285
272
  });
286
- return data;
287
- }).then((data) => {
288
- var _a2;
289
- (_a2 = options.onComplete) == null ? void 0 : _a2.call(options, data);
273
+ (_a = options.onComplete) == null ? void 0 : _a.call(options, { ...data, compute });
290
274
  });
291
275
  }
292
276
  compute();
293
- return pipe(autoUpdate(reference, floating, compute, options.listeners), (_a = options.onCleanup) != null ? _a : noop);
277
+ return callAll(options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0, options.onCleanup);
278
+ }
279
+ function getBasePlacement(placement) {
280
+ return placement.split("-")[0];
294
281
  }
295
282
 
296
283
  // src/get-styles.ts
@@ -329,9 +316,16 @@ function getPlacementStyles(options) {
329
316
  position: "absolute",
330
317
  zIndex: "inherit"
331
318
  },
332
- floating: __spreadValues({
319
+ floating: {
333
320
  position: strategy,
334
- minWidth: "max-content"
335
- }, !measured && UNMEASURED_FLOATING_STYLE)
321
+ minWidth: "max-content",
322
+ ...!measured && UNMEASURED_FLOATING_STYLE
323
+ }
336
324
  };
337
325
  }
326
+ // Annotate the CommonJS export names for ESM import in node:
327
+ 0 && (module.exports = {
328
+ getBasePlacement,
329
+ getPlacement,
330
+ getPlacementStyles
331
+ });
package/dist/index.mjs CHANGED
@@ -1,39 +1,26 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __spreadValues = (a, b) => {
8
- for (var prop in b || (b = {}))
9
- if (__hasOwnProp.call(b, prop))
10
- __defNormalProp(a, prop, b[prop]);
11
- if (__getOwnPropSymbols)
12
- for (var prop of __getOwnPropSymbols(b)) {
13
- if (__propIsEnum.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- }
16
- return a;
17
- };
18
-
19
1
  // src/get-placement.ts
20
2
  import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
21
3
 
22
4
  // ../core/dist/index.mjs
23
- var isBoolean = (v) => v === true || v === false;
24
- var noop = () => {
5
+ var callAll = (...fns) => (...a) => {
6
+ fns.forEach(function(fn) {
7
+ fn == null ? void 0 : fn(...a);
8
+ });
25
9
  };
26
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
10
+ var isBoolean = (v) => v === true || v === false;
27
11
 
28
12
  // src/auto-update.ts
29
13
  import { getOverflowAncestors } from "@floating-ui/dom";
30
14
 
31
15
  // ../dom/dist/index.mjs
32
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
33
16
  var runIfFn = (v, ...a) => {
34
17
  const res = typeof v === "function" ? v(...a) : v;
35
- return res != null ? res : void 0;
18
+ return res ?? void 0;
36
19
  };
20
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
21
+ function isHTMLElement(v) {
22
+ return typeof v === "object" && (v == null ? void 0 : v.nodeType) === Node.ELEMENT_NODE && typeof (v == null ? void 0 : v.nodeName) === "string";
23
+ }
37
24
  var isRef = (v) => hasProp(v, "current");
38
25
  function addDomEvent(target, eventName, handler, options) {
39
26
  const node = isRef(target) ? target.current : runIfFn(target);
@@ -42,9 +29,6 @@ function addDomEvent(target, eventName, handler, options) {
42
29
  node == null ? void 0 : node.removeEventListener(eventName, handler, options);
43
30
  };
44
31
  }
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
32
  function getObservedElements() {
49
33
  ;
50
34
  globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
@@ -100,12 +84,11 @@ function isEqual(rect1, rect2) {
100
84
 
101
85
  // src/auto-update.ts
102
86
  function resolveOptions(option) {
103
- var _a, _b, _c;
104
87
  const bool = isBoolean(option);
105
88
  return {
106
- ancestorResize: bool ? option : (_a = option.ancestorResize) != null ? _a : true,
107
- ancestorScroll: bool ? option : (_b = option.ancestorScroll) != null ? _b : true,
108
- referenceResize: bool ? option : (_c = option.referenceResize) != null ? _c : true
89
+ ancestorResize: bool ? option : option.ancestorResize ?? true,
90
+ ancestorScroll: bool ? option : option.ancestorScroll ?? true,
91
+ referenceResize: bool ? option : option.referenceResize ?? true
109
92
  };
110
93
  }
111
94
  function autoUpdate(reference, floating, update, options = false) {
@@ -120,13 +103,13 @@ function autoUpdate(reference, floating, update, options = false) {
120
103
  if (referenceResize && isHTMLElement(reference)) {
121
104
  cleanups.push(observeElementRect(reference, update));
122
105
  }
123
- cleanups.push(pipe(...ancestors.map((el) => addDomEvent(el, "resize", update))));
106
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
124
107
  return () => cleanups.forEach((fn) => fn());
125
108
  }
126
109
  function addScrollListeners() {
127
- return pipe(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
110
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
128
111
  }
129
- return pipe(addResizeListeners(), addScrollListeners());
112
+ return callAll(addResizeListeners(), addScrollListeners());
130
113
  }
131
114
 
132
115
  // src/middleware.ts
@@ -167,20 +150,16 @@ var transformOrigin = {
167
150
  var shiftArrow = (opts) => ({
168
151
  name: "shiftArrow",
169
152
  fn({ placement, middlewareData }) {
170
- var _a;
171
153
  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
- });
154
+ if (middlewareData.arrow) {
155
+ const { x, y } = middlewareData.arrow;
156
+ const dir = placement.split("-")[0];
157
+ Object.assign(arrow2.style, {
158
+ left: x != null ? `${x}px` : "",
159
+ top: y != null ? `${y}px` : "",
160
+ [dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
161
+ });
162
+ }
184
163
  return {};
185
164
  }
186
165
  });
@@ -195,11 +174,10 @@ var defaultOptions = {
195
174
  sameWidth: false,
196
175
  overflowPadding: 8
197
176
  };
198
- function getPlacement(reference, floating, options = {}) {
199
- var _a;
200
- if (reference == null || floating == null)
201
- return noop;
202
- options = Object.assign({}, defaultOptions, options);
177
+ function getPlacement(reference, floating, opts = {}) {
178
+ if (!floating || !reference)
179
+ return;
180
+ const options = Object.assign({}, defaultOptions, opts);
203
181
  const arrowEl = floating.querySelector("[data-part=arrow]");
204
182
  const middleware = [];
205
183
  if (options.flip) {
@@ -224,34 +202,38 @@ function getPlacement(reference, floating, options = {}) {
224
202
  middleware.push(arrow({ element: arrowEl, padding: 8 }), shiftArrow({ element: arrowEl }));
225
203
  }
226
204
  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
- }
205
+ middleware.push(size({
206
+ padding: options.overflowPadding,
207
+ apply({ rects, availableHeight, availableWidth }) {
208
+ const referenceWidth = Math.round(rects.reference.width);
209
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
210
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
211
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
212
+ if (options.sameWidth) {
213
+ Object.assign(floating.style, {
214
+ width: `${referenceWidth}px`,
215
+ minWidth: "unset"
216
+ });
243
217
  }
244
- }));
245
- }
246
- function compute() {
247
- if (reference == null || floating == null)
218
+ if (options.fitViewport) {
219
+ Object.assign(floating.style, {
220
+ maxWidth: `${availableWidth}px`,
221
+ maxHeight: `${availableHeight}px`
222
+ });
223
+ }
224
+ }
225
+ }));
226
+ function compute(config = {}) {
227
+ if (!reference || !floating)
248
228
  return;
249
229
  const { placement, strategy } = options;
250
230
  computePosition(reference, floating, {
251
231
  placement,
252
232
  middleware,
253
- strategy
233
+ strategy,
234
+ ...config
254
235
  }).then((data) => {
236
+ var _a;
255
237
  const x = Math.round(data.x);
256
238
  const y = Math.round(data.y);
257
239
  Object.assign(floating.style, {
@@ -260,14 +242,14 @@ function getPlacement(reference, floating, options = {}) {
260
242
  left: "0",
261
243
  transform: `translate3d(${x}px, ${y}px, 0)`
262
244
  });
263
- return data;
264
- }).then((data) => {
265
- var _a2;
266
- (_a2 = options.onComplete) == null ? void 0 : _a2.call(options, data);
245
+ (_a = options.onComplete) == null ? void 0 : _a.call(options, { ...data, compute });
267
246
  });
268
247
  }
269
248
  compute();
270
- return pipe(autoUpdate(reference, floating, compute, options.listeners), (_a = options.onCleanup) != null ? _a : noop);
249
+ return callAll(options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0, options.onCleanup);
250
+ }
251
+ function getBasePlacement(placement) {
252
+ return placement.split("-")[0];
271
253
  }
272
254
 
273
255
  // src/get-styles.ts
@@ -306,13 +288,15 @@ function getPlacementStyles(options) {
306
288
  position: "absolute",
307
289
  zIndex: "inherit"
308
290
  },
309
- floating: __spreadValues({
291
+ floating: {
310
292
  position: strategy,
311
- minWidth: "max-content"
312
- }, !measured && UNMEASURED_FLOATING_STYLE)
293
+ minWidth: "max-content",
294
+ ...!measured && UNMEASURED_FLOATING_STYLE
295
+ }
313
296
  };
314
297
  }
315
298
  export {
299
+ getBasePlacement,
316
300
  getPlacement,
317
301
  getPlacementStyles
318
302
  };
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.9",
4
4
  "description": "Dynamic positioning logic for ui machines",
5
5
  "keywords": [
6
6
  "js",
@@ -25,17 +25,20 @@
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",
30
- "@zag-js/utils": "0.1.2"
28
+ "@floating-ui/dom": "0.5.4"
29
+ },
30
+ "devDependencies": {
31
+ "@zag-js/dom-utils": "0.1.8",
32
+ "@zag-js/utils": "0.1.3"
31
33
  },
32
34
  "scripts": {
33
- "build:fast": "zag build",
34
- "start": "zag build --watch",
35
- "build": "zag build --prod",
35
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
36
+ "start": "pnpm build --watch",
37
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
36
38
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
37
39
  "lint": "eslint src --ext .ts,.tsx",
38
- "test:ci": "yarn test --ci --runInBand",
39
- "test:watch": "yarn test --watch --updateSnapshot"
40
+ "test-ci": "pnpm test --ci --runInBand",
41
+ "test-watch": "pnpm test --watch -u",
42
+ "typecheck": "tsc --noEmit"
40
43
  }
41
- }
44
+ }
@@ -1,8 +0,0 @@
1
- import type { Placement, ReferenceElement } from "@floating-ui/dom";
2
- export type { Placement };
3
- export declare type AutoUpdateOptions = {
4
- ancestorScroll?: boolean;
5
- ancestorResize?: boolean;
6
- referenceResize?: boolean;
7
- };
8
- export declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): (v: void) => void;
@@ -1,3 +0,0 @@
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,35 +0,0 @@
1
- import { Placement } from "@floating-ui/dom";
2
- declare type Options = {
3
- measured: boolean;
4
- strategy?: "absolute" | "fixed";
5
- placement?: Placement;
6
- };
7
- export declare function getPlacementStyles(options: Options): {
8
- arrow: {
9
- readonly [x: string]: string | 0;
10
- readonly position: "absolute";
11
- readonly width: string;
12
- readonly height: string;
13
- readonly opacity: 0;
14
- };
15
- innerArrow: {
16
- readonly transform: any;
17
- readonly background: string;
18
- readonly top: "0";
19
- readonly left: "0";
20
- readonly width: "100%";
21
- readonly height: "100%";
22
- readonly position: "absolute";
23
- readonly zIndex: "inherit";
24
- };
25
- floating: {
26
- readonly position: "fixed";
27
- readonly top: 0;
28
- readonly left: 0;
29
- readonly opacity: 0;
30
- readonly transform: "translate3d(0, -200%, 0)";
31
- readonly pointerEvents: "none";
32
- readonly minWidth: "max-content";
33
- };
34
- };
35
- export {};
@@ -1,29 +0,0 @@
1
- import { Middleware } from "@floating-ui/dom";
2
- export declare const cssVars: {
3
- arrowSize: {
4
- variable: string;
5
- reference: string;
6
- };
7
- arrowSizeHalf: {
8
- variable: string;
9
- reference: string;
10
- };
11
- arrowBg: {
12
- variable: string;
13
- reference: string;
14
- };
15
- transformOrigin: {
16
- variable: string;
17
- reference: string;
18
- };
19
- arrowOffset: {
20
- variable: string;
21
- reference: string;
22
- };
23
- };
24
- export declare const transformOrigin: Middleware;
25
- declare type ArrowOptions = {
26
- element: HTMLElement;
27
- };
28
- export declare const shiftArrow: (opts: ArrowOptions) => Middleware;
29
- export {};
package/dist/types.d.ts DELETED
@@ -1,61 +0,0 @@
1
- import type { Boundary, Placement, ComputePositionReturn } from "@floating-ui/dom";
2
- import { AutoUpdateOptions } from "./auto-update";
3
- export type { Placement };
4
- export declare type PositioningOptions = {
5
- /**
6
- * The strategy to use for positioning
7
- */
8
- strategy?: "absolute" | "fixed";
9
- /**
10
- * The initial placement of the floating element
11
- */
12
- placement?: Placement;
13
- /**
14
- * The offset of the floating element
15
- */
16
- offset?: {
17
- mainAxis?: number;
18
- crossAxis?: number;
19
- };
20
- /**
21
- * The main axis offset or gap between the reference and floating elements
22
- */
23
- gutter?: number;
24
- /**
25
- * The virtual padding around the viewport edges to check for overflow
26
- */
27
- overflowPadding?: number;
28
- /**
29
- * Whether to flip the placement
30
- */
31
- flip?: boolean;
32
- /**
33
- * Whether the floating element can overlap the reference element
34
- * @default false
35
- */
36
- overlap?: boolean;
37
- /**
38
- * Whether to make the floating element same width as the reference element
39
- */
40
- sameWidth?: boolean;
41
- /**
42
- * Whether the popover should fit the viewport.
43
- */
44
- fitViewport?: boolean;
45
- /**
46
- * The overflow boundary of the reference element
47
- */
48
- boundary?: Boundary;
49
- /**
50
- * Options to activate auto-update listeners
51
- */
52
- listeners?: boolean | AutoUpdateOptions;
53
- /**
54
- * Function called when the placement is computed
55
- */
56
- onComplete?(data: ComputePositionReturn): void;
57
- /**
58
- * Function called on cleanup of all listeners
59
- */
60
- onCleanup?: VoidFunction;
61
- };