@zag-js/popper 0.1.12 → 0.1.13

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,7 +1,35 @@
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/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ getBasePlacement: () => getBasePlacement,
24
+ getPlacement: () => getPlacement,
25
+ getPlacementStyles: () => getPlacementStyles
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
1
29
  // src/get-placement.ts
2
- import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
30
+ var import_dom2 = require("@floating-ui/dom");
3
31
 
4
- // ../core/dist/index.js
32
+ // ../core/dist/index.mjs
5
33
  var callAll = (...fns) => (...a) => {
6
34
  fns.forEach(function(fn) {
7
35
  fn == null ? void 0 : fn(...a);
@@ -10,9 +38,9 @@ var callAll = (...fns) => (...a) => {
10
38
  var isBoolean = (v) => v === true || v === false;
11
39
 
12
40
  // src/auto-update.ts
13
- import { getOverflowAncestors } from "@floating-ui/dom";
41
+ var import_dom = require("@floating-ui/dom");
14
42
 
15
- // ../dom/dist/index.js
43
+ // ../dom/dist/index.mjs
16
44
  var runIfFn = (v, ...a) => {
17
45
  const res = typeof v === "function" ? v(...a) : v;
18
46
  return res ?? void 0;
@@ -96,7 +124,7 @@ function autoUpdate(reference, floating, update, options = false) {
96
124
  const useAncestors = ancestorScroll || ancestorResize;
97
125
  const ancestors = [];
98
126
  if (useAncestors && isHTMLElement(reference)) {
99
- ancestors.push(...getOverflowAncestors(reference));
127
+ ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
100
128
  }
101
129
  function addResizeListeners() {
102
130
  let cleanups = [observeElementRect(floating, update)];
@@ -182,7 +210,7 @@ function getPlacement(reference, floating, opts = {}) {
182
210
  const middleware = [];
183
211
  if (options.flip) {
184
212
  middleware.push(
185
- flip({
213
+ (0, import_dom2.flip)({
186
214
  boundary: options.boundary,
187
215
  padding: options.overflowPadding
188
216
  })
@@ -193,10 +221,10 @@ function getPlacement(reference, floating, opts = {}) {
193
221
  const data = options.gutter ? { mainAxis: options.gutter } : options.offset;
194
222
  if ((data == null ? void 0 : data.mainAxis) != null)
195
223
  data.mainAxis += arrowOffset;
196
- middleware.push(offset(data));
224
+ middleware.push((0, import_dom2.offset)(data));
197
225
  }
198
226
  middleware.push(
199
- shift({
227
+ (0, import_dom2.shift)({
200
228
  boundary: options.boundary,
201
229
  crossAxis: options.overlap,
202
230
  padding: options.overflowPadding
@@ -204,13 +232,13 @@ function getPlacement(reference, floating, opts = {}) {
204
232
  );
205
233
  if (arrowEl) {
206
234
  middleware.push(
207
- arrow({ element: arrowEl, padding: 8 }),
235
+ (0, import_dom2.arrow)({ element: arrowEl, padding: 8 }),
208
236
  shiftArrow({ element: arrowEl })
209
237
  );
210
238
  }
211
239
  middleware.push(transformOrigin);
212
240
  middleware.push(
213
- size({
241
+ (0, import_dom2.size)({
214
242
  padding: options.overflowPadding,
215
243
  apply({ rects, availableHeight, availableWidth }) {
216
244
  const referenceWidth = Math.round(rects.reference.width);
@@ -236,7 +264,7 @@ function getPlacement(reference, floating, opts = {}) {
236
264
  if (!reference || !floating)
237
265
  return;
238
266
  const { placement, strategy } = options;
239
- computePosition(reference, floating, {
267
+ (0, import_dom2.computePosition)(reference, floating, {
240
268
  placement,
241
269
  middleware,
242
270
  strategy,
@@ -307,8 +335,9 @@ function getPlacementStyles(options) {
307
335
  }
308
336
  };
309
337
  }
310
- export {
338
+ // Annotate the CommonJS export names for ESM import in node:
339
+ 0 && (module.exports = {
311
340
  getBasePlacement,
312
341
  getPlacement,
313
342
  getPlacementStyles
314
- };
343
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,314 @@
1
+ // src/get-placement.ts
2
+ import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
3
+
4
+ // ../core/dist/index.mjs
5
+ var callAll = (...fns) => (...a) => {
6
+ fns.forEach(function(fn) {
7
+ fn == null ? void 0 : fn(...a);
8
+ });
9
+ };
10
+ var isBoolean = (v) => v === true || v === false;
11
+
12
+ // src/auto-update.ts
13
+ import { getOverflowAncestors } from "@floating-ui/dom";
14
+
15
+ // ../dom/dist/index.mjs
16
+ var runIfFn = (v, ...a) => {
17
+ const res = typeof v === "function" ? v(...a) : v;
18
+ return res ?? void 0;
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
+ }
24
+ var isRef = (v) => hasProp(v, "current");
25
+ function addDomEvent(target, eventName, handler, options) {
26
+ const node = isRef(target) ? target.current : runIfFn(target);
27
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
28
+ return () => {
29
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
30
+ };
31
+ }
32
+ function getObservedElements() {
33
+ ;
34
+ globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
35
+ return globalThis.__rectObserverMap__;
36
+ }
37
+ function observeElementRect(el, fn) {
38
+ const observedElements = getObservedElements();
39
+ const data = observedElements.get(el);
40
+ if (!data) {
41
+ observedElements.set(el, { rect: {}, callbacks: [fn] });
42
+ if (observedElements.size === 1) {
43
+ rafId = requestAnimationFrame(runLoop);
44
+ }
45
+ } else {
46
+ data.callbacks.push(fn);
47
+ fn(el.getBoundingClientRect());
48
+ }
49
+ return function unobserve() {
50
+ const data2 = observedElements.get(el);
51
+ if (!data2)
52
+ return;
53
+ const index = data2.callbacks.indexOf(fn);
54
+ if (index > -1) {
55
+ data2.callbacks.splice(index, 1);
56
+ }
57
+ if (data2.callbacks.length === 0) {
58
+ observedElements.delete(el);
59
+ if (observedElements.size === 0) {
60
+ cancelAnimationFrame(rafId);
61
+ }
62
+ }
63
+ };
64
+ }
65
+ var rafId;
66
+ function runLoop() {
67
+ const observedElements = getObservedElements();
68
+ const changedRectsData = [];
69
+ observedElements.forEach((data, element) => {
70
+ const newRect = element.getBoundingClientRect();
71
+ if (!isEqual(data.rect, newRect)) {
72
+ data.rect = newRect;
73
+ changedRectsData.push(data);
74
+ }
75
+ });
76
+ changedRectsData.forEach((data) => {
77
+ data.callbacks.forEach((callback) => callback(data.rect));
78
+ });
79
+ rafId = requestAnimationFrame(runLoop);
80
+ }
81
+ function isEqual(rect1, rect2) {
82
+ return rect1.width === rect2.width && rect1.height === rect2.height && rect1.top === rect2.top && rect1.right === rect2.right && rect1.bottom === rect2.bottom && rect1.left === rect2.left;
83
+ }
84
+
85
+ // src/auto-update.ts
86
+ function resolveOptions(option) {
87
+ const bool = isBoolean(option);
88
+ return {
89
+ ancestorResize: bool ? option : option.ancestorResize ?? true,
90
+ ancestorScroll: bool ? option : option.ancestorScroll ?? true,
91
+ referenceResize: bool ? option : option.referenceResize ?? true
92
+ };
93
+ }
94
+ function autoUpdate(reference, floating, update, options = false) {
95
+ const { ancestorScroll, ancestorResize, referenceResize } = resolveOptions(options);
96
+ const useAncestors = ancestorScroll || ancestorResize;
97
+ const ancestors = [];
98
+ if (useAncestors && isHTMLElement(reference)) {
99
+ ancestors.push(...getOverflowAncestors(reference));
100
+ }
101
+ function addResizeListeners() {
102
+ let cleanups = [observeElementRect(floating, update)];
103
+ if (referenceResize && isHTMLElement(reference)) {
104
+ cleanups.push(observeElementRect(reference, update));
105
+ }
106
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
107
+ return () => cleanups.forEach((fn) => fn());
108
+ }
109
+ function addScrollListeners() {
110
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
111
+ }
112
+ return callAll(addResizeListeners(), addScrollListeners());
113
+ }
114
+
115
+ // src/middleware.ts
116
+ var toVar = (value) => ({ variable: value, reference: `var(${value})` });
117
+ var cssVars = {
118
+ arrowSize: toVar("--arrow-size"),
119
+ arrowSizeHalf: toVar("--arrow-size-half"),
120
+ arrowBg: toVar("--arrow-background"),
121
+ transformOrigin: toVar("--transform-origin"),
122
+ arrowOffset: toVar("--arrow-offset")
123
+ };
124
+ var getTransformOrigin = (arrow2) => ({
125
+ top: "bottom center",
126
+ "top-start": arrow2 ? `${arrow2.x}px bottom` : "left bottom",
127
+ "top-end": arrow2 ? `${arrow2.x}px bottom` : "right bottom",
128
+ bottom: "top center",
129
+ "bottom-start": arrow2 ? `${arrow2.x}px top` : "top left",
130
+ "bottom-end": arrow2 ? `${arrow2.x}px top` : "top right",
131
+ left: "right center",
132
+ "left-start": arrow2 ? `right ${arrow2.y}px` : "right top",
133
+ "left-end": arrow2 ? `right ${arrow2.y}px` : "right bottom",
134
+ right: "left center",
135
+ "right-start": arrow2 ? `left ${arrow2.y}px` : "left top",
136
+ "right-end": arrow2 ? `left ${arrow2.y}px` : "left bottom"
137
+ });
138
+ var transformOrigin = {
139
+ name: "transformOrigin",
140
+ fn({ placement, elements, middlewareData }) {
141
+ const { arrow: arrow2 } = middlewareData;
142
+ const transformOrigin2 = getTransformOrigin(arrow2)[placement];
143
+ const { floating } = elements;
144
+ floating.style.setProperty(cssVars.transformOrigin.variable, transformOrigin2);
145
+ return {
146
+ data: { transformOrigin: transformOrigin2 }
147
+ };
148
+ }
149
+ };
150
+ var shiftArrow = (opts) => ({
151
+ name: "shiftArrow",
152
+ fn({ placement, middlewareData }) {
153
+ const { element: arrow2 } = opts;
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
+ }
163
+ return {};
164
+ }
165
+ });
166
+
167
+ // src/get-placement.ts
168
+ var defaultOptions = {
169
+ strategy: "absolute",
170
+ placement: "bottom",
171
+ listeners: true,
172
+ gutter: 8,
173
+ flip: true,
174
+ sameWidth: false,
175
+ overflowPadding: 8
176
+ };
177
+ function getPlacement(reference, floating, opts = {}) {
178
+ if (!floating || !reference)
179
+ return;
180
+ const options = Object.assign({}, defaultOptions, opts);
181
+ const arrowEl = floating.querySelector("[data-part=arrow]");
182
+ const middleware = [];
183
+ if (options.flip) {
184
+ middleware.push(
185
+ flip({
186
+ boundary: options.boundary,
187
+ padding: options.overflowPadding
188
+ })
189
+ );
190
+ }
191
+ if (options.gutter || options.offset) {
192
+ const arrowOffset = arrowEl ? arrowEl.offsetHeight / 2 : 0;
193
+ const data = options.gutter ? { mainAxis: options.gutter } : options.offset;
194
+ if ((data == null ? void 0 : data.mainAxis) != null)
195
+ data.mainAxis += arrowOffset;
196
+ middleware.push(offset(data));
197
+ }
198
+ middleware.push(
199
+ shift({
200
+ boundary: options.boundary,
201
+ crossAxis: options.overlap,
202
+ padding: options.overflowPadding
203
+ })
204
+ );
205
+ if (arrowEl) {
206
+ middleware.push(
207
+ arrow({ element: arrowEl, padding: 8 }),
208
+ shiftArrow({ element: arrowEl })
209
+ );
210
+ }
211
+ middleware.push(transformOrigin);
212
+ middleware.push(
213
+ size({
214
+ padding: options.overflowPadding,
215
+ apply({ rects, availableHeight, availableWidth }) {
216
+ const referenceWidth = Math.round(rects.reference.width);
217
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
218
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
219
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
220
+ if (options.sameWidth) {
221
+ Object.assign(floating.style, {
222
+ width: `${referenceWidth}px`,
223
+ minWidth: "unset"
224
+ });
225
+ }
226
+ if (options.fitViewport) {
227
+ Object.assign(floating.style, {
228
+ maxWidth: `${availableWidth}px`,
229
+ maxHeight: `${availableHeight}px`
230
+ });
231
+ }
232
+ }
233
+ })
234
+ );
235
+ function compute(config = {}) {
236
+ if (!reference || !floating)
237
+ return;
238
+ const { placement, strategy } = options;
239
+ computePosition(reference, floating, {
240
+ placement,
241
+ middleware,
242
+ strategy,
243
+ ...config
244
+ }).then((data) => {
245
+ var _a;
246
+ const x = Math.round(data.x);
247
+ const y = Math.round(data.y);
248
+ Object.assign(floating.style, {
249
+ position: data.strategy,
250
+ top: "0",
251
+ left: "0",
252
+ transform: `translate3d(${x}px, ${y}px, 0)`
253
+ });
254
+ (_a = options.onComplete) == null ? void 0 : _a.call(options, { ...data, compute });
255
+ });
256
+ }
257
+ compute();
258
+ return callAll(
259
+ options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0,
260
+ options.onCleanup
261
+ );
262
+ }
263
+ function getBasePlacement(placement) {
264
+ return placement.split("-")[0];
265
+ }
266
+
267
+ // src/get-styles.ts
268
+ var UNMEASURED_FLOATING_STYLE = {
269
+ position: "fixed",
270
+ top: 0,
271
+ left: 0,
272
+ opacity: 0,
273
+ transform: "translate3d(0, -200%, 0)",
274
+ pointerEvents: "none"
275
+ };
276
+ var ARROW_FLOATING_STYLE = {
277
+ bottom: "rotate(45deg)",
278
+ left: "rotate(135deg)",
279
+ top: "rotate(225deg)",
280
+ right: "rotate(315deg)"
281
+ };
282
+ function getPlacementStyles(options) {
283
+ const { measured, strategy = "absolute", placement = "bottom" } = options;
284
+ return {
285
+ arrow: {
286
+ position: "absolute",
287
+ width: cssVars.arrowSize.reference,
288
+ height: cssVars.arrowSize.reference,
289
+ [cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
290
+ [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`,
291
+ opacity: !measured ? 0 : void 0
292
+ },
293
+ innerArrow: {
294
+ transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
295
+ background: cssVars.arrowBg.reference,
296
+ top: "0",
297
+ left: "0",
298
+ width: "100%",
299
+ height: "100%",
300
+ position: "absolute",
301
+ zIndex: "inherit"
302
+ },
303
+ floating: {
304
+ position: strategy,
305
+ minWidth: "max-content",
306
+ ...!measured && UNMEASURED_FLOATING_STYLE
307
+ }
308
+ };
309
+ }
310
+ export {
311
+ getBasePlacement,
312
+ getPlacement,
313
+ getPlacementStyles
314
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
- "type": "module",
3
2
  "name": "@zag-js/popper",
4
- "version": "0.1.12",
3
+ "version": "0.1.13",
5
4
  "description": "Dynamic positioning logic for ui machines",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
6
8
  "keywords": [
7
9
  "js",
8
10
  "utils",
@@ -11,8 +13,6 @@
11
13
  "author": "Segun Adebayo <sage@adebayosegun.com>",
12
14
  "homepage": "https://github.com/chakra-ui/zag#readme",
13
15
  "license": "MIT",
14
- "main": "dist/index.js",
15
- "types": "dist/index.d.ts",
16
16
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/popper",
17
17
  "sideEffects": false,
18
18
  "files": [
@@ -28,13 +28,13 @@
28
28
  "@floating-ui/dom": "1.0.1"
29
29
  },
30
30
  "devDependencies": {
31
- "@zag-js/dom-utils": "0.1.12",
32
- "@zag-js/utils": "0.1.5"
31
+ "@zag-js/dom-utils": "0.1.13",
32
+ "@zag-js/utils": "0.1.6"
33
33
  },
34
34
  "scripts": {
35
- "build-fast": "tsup src/index.ts --format=esm",
35
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
36
36
  "start": "pnpm build --watch",
37
- "build": "tsup src/index.ts --format=esm --dts",
37
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
38
38
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
39
39
  "lint": "eslint src --ext .ts,.tsx",
40
40
  "test-ci": "pnpm test --ci --runInBand",