@zag-js/rect-utils 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.
package/dist/index.js CHANGED
@@ -1,64 +1,3 @@
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
- alignRect: () => alignRect,
24
- closest: () => closest,
25
- closestSideToPoint: () => closestSideToPoint,
26
- closestSideToRect: () => closestSideToRect,
27
- collisions: () => collisions,
28
- contains: () => contains,
29
- containsPoint: () => containsPoint,
30
- containsRect: () => containsRect,
31
- createRect: () => createRect,
32
- debugPolygon: () => debugPolygon,
33
- distance: () => distance,
34
- distanceBtwEdges: () => distanceBtwEdges,
35
- distanceFromPoint: () => distanceFromPoint,
36
- distanceFromRect: () => distanceFromRect,
37
- expand: () => expand,
38
- fromRange: () => fromRange,
39
- getElementPolygon: () => getElementPolygon,
40
- getElementRect: () => getElementRect,
41
- getRectCenters: () => getRectCenters,
42
- getRectCorners: () => getRectCorners,
43
- getRectEdges: () => getRectEdges,
44
- getRectFromPoints: () => getRectFromPoints,
45
- getRotationRect: () => getRotationRect,
46
- getViewportRect: () => getViewportRect,
47
- getWindowRect: () => getWindowRect,
48
- inset: () => inset,
49
- intersection: () => intersection,
50
- intersects: () => intersects,
51
- isPointInPolygon: () => isPointInPolygon,
52
- isRect: () => isRect,
53
- isSymmetric: () => isSymmetric,
54
- rotate: () => rotate,
55
- shift: () => shift,
56
- shrink: () => shrink,
57
- toRad: () => toRad,
58
- union: () => union
59
- });
60
- module.exports = __toCommonJS(src_exports);
61
-
62
1
  // src/align.ts
63
2
  function hAlign(a, ref, h) {
64
3
  let x = ref.minX;
@@ -103,7 +42,7 @@ function alignRect(a, ref, options) {
103
42
  return vAlign(hAlign(a, ref, h), ref, v);
104
43
  }
105
44
 
106
- // ../core/dist/index.mjs
45
+ // ../core/dist/index.js
107
46
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
108
47
 
109
48
  // src/rect.ts
@@ -267,7 +206,7 @@ function contains(r, v) {
267
206
  return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
268
207
  }
269
208
 
270
- // ../dom/dist/index.mjs
209
+ // ../dom/dist/index.js
271
210
  function getCache() {
272
211
  const g = globalThis;
273
212
  g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
@@ -409,8 +348,8 @@ function getWindowRect(win, opts = {}) {
409
348
  function getViewportRect(win, opts) {
410
349
  const { excludeScrollbar = false } = opts;
411
350
  const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
412
- const width = visualViewport.width || innerWidth2;
413
- const height = visualViewport.height || innerHeight2;
351
+ const width = (visualViewport == null ? void 0 : visualViewport.width) || innerWidth2;
352
+ const height = (visualViewport == null ? void 0 : visualViewport.height) || innerHeight2;
414
353
  const rect = { x: 0, y: 0, width, height };
415
354
  if (excludeScrollbar) {
416
355
  const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
@@ -511,8 +450,7 @@ function debugPolygon(polygon) {
511
450
  el.remove();
512
451
  };
513
452
  }
514
- // Annotate the CommonJS export names for ESM import in node:
515
- 0 && (module.exports = {
453
+ export {
516
454
  alignRect,
517
455
  closest,
518
456
  closestSideToPoint,
@@ -549,4 +487,4 @@ function debugPolygon(polygon) {
549
487
  shrink,
550
488
  toRad,
551
489
  union
552
- });
490
+ };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
+ "type": "module",
2
3
  "name": "@zag-js/rect-utils",
3
- "version": "0.1.6",
4
+ "version": "0.1.7",
4
5
  "description": "",
5
6
  "keywords": [
6
7
  "js",
@@ -11,7 +12,6 @@
11
12
  "homepage": "https://github.com/chakra-ui/zag#readme",
12
13
  "license": "MIT",
13
14
  "main": "dist/index.js",
14
- "module": "dist/index.mjs",
15
15
  "types": "dist/index.d.ts",
16
16
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/rect",
17
17
  "sideEffects": false,
@@ -25,13 +25,13 @@
25
25
  "url": "https://github.com/chakra-ui/zag/issues"
26
26
  },
27
27
  "devDependencies": {
28
- "@zag-js/dom-utils": "0.1.11",
29
- "@zag-js/utils": "0.1.4"
28
+ "@zag-js/dom-utils": "0.1.12",
29
+ "@zag-js/utils": "0.1.5"
30
30
  },
31
31
  "scripts": {
32
- "build-fast": "tsup src/index.ts --format=esm,cjs",
32
+ "build-fast": "tsup src/index.ts --format=esm",
33
33
  "start": "pnpm build --watch",
34
- "build": "tsup src/index.ts --format=esm,cjs --dts",
34
+ "build": "tsup src/index.ts --format=esm --dts",
35
35
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
36
36
  "lint": "eslint src --ext .ts,.tsx",
37
37
  "test-ci": "pnpm test --ci --runInBand",
package/dist/index.mjs DELETED
@@ -1,490 +0,0 @@
1
- // src/align.ts
2
- function hAlign(a, ref, h) {
3
- let x = ref.minX;
4
- if (h === "left-inside") {
5
- x = ref.minX;
6
- }
7
- if (h === "left-outside") {
8
- x = ref.minX - ref.width;
9
- }
10
- if (h === "right-inside") {
11
- x = ref.maxX - ref.width;
12
- }
13
- if (h === "right-outside") {
14
- x = ref.maxX;
15
- }
16
- if (h === "center") {
17
- x = ref.midX - ref.width / 2;
18
- }
19
- return { ...a, x };
20
- }
21
- function vAlign(a, ref, v) {
22
- let y = ref.minY;
23
- if (v === "top-inside") {
24
- y = ref.minY;
25
- }
26
- if (v === "top-outside") {
27
- y = ref.minY - a.height;
28
- }
29
- if (v === "bottom-inside") {
30
- y = ref.maxY - a.height;
31
- }
32
- if (v === "bottom-outside") {
33
- y = ref.maxY;
34
- }
35
- if (v === "center") {
36
- y = ref.midY - a.height / 2;
37
- }
38
- return { ...a, y };
39
- }
40
- function alignRect(a, ref, options) {
41
- const { h, v } = options;
42
- return vAlign(hAlign(a, ref, h), ref, v);
43
- }
44
-
45
- // ../core/dist/index.mjs
46
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
47
-
48
- // src/rect.ts
49
- var point = (x, y) => ({ x, y });
50
- function createRect(r) {
51
- const { x, y, width, height } = r;
52
- const midX = x + width / 2;
53
- const midY = y + height / 2;
54
- return {
55
- x,
56
- y,
57
- width,
58
- height,
59
- minX: x,
60
- minY: y,
61
- maxX: x + width,
62
- maxY: y + height,
63
- midX,
64
- midY,
65
- center: point(midX, midY)
66
- };
67
- }
68
- function isRect(v) {
69
- return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
70
- }
71
- function getRectCenters(v) {
72
- const top = point(v.midX, v.minY);
73
- const right = point(v.maxX, v.midY);
74
- const bottom = point(v.midX, v.maxY);
75
- const left = point(v.minX, v.midY);
76
- return { top, right, bottom, left };
77
- }
78
- function getRectCorners(v) {
79
- const top = point(v.minX, v.minY);
80
- const right = point(v.maxX, v.minY);
81
- const bottom = point(v.maxX, v.maxY);
82
- const left = point(v.minX, v.maxY);
83
- return { top, right, bottom, left };
84
- }
85
- function getRectEdges(v) {
86
- const c = getRectCorners(v);
87
- const top = [c.top, c.right];
88
- const right = [c.right, c.bottom];
89
- const bottom = [c.left, c.bottom];
90
- const left = [c.top, c.left];
91
- return { top, right, bottom, left };
92
- }
93
-
94
- // src/intersection.ts
95
- function intersects(a, b) {
96
- return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
97
- }
98
- function intersection(a, b) {
99
- const x = Math.max(a.x, b.x);
100
- const y = Math.max(a.y, b.y);
101
- const x2 = Math.min(a.x + a.width, b.x + b.width);
102
- const y2 = Math.min(a.y + a.height, b.y + b.height);
103
- return createRect({ x, y, width: x2 - x, height: y2 - y });
104
- }
105
- function collisions(a, b) {
106
- return {
107
- top: a.minY <= b.minY,
108
- right: a.maxX >= b.maxX,
109
- bottom: a.maxY >= b.maxY,
110
- left: a.minX <= b.minX
111
- };
112
- }
113
-
114
- // src/distance.ts
115
- function distance(a, b = { x: 0, y: 0 }) {
116
- return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
117
- }
118
- function distanceFromPoint(r, p) {
119
- let x = 0;
120
- let y = 0;
121
- if (p.x < r.x)
122
- x = r.x - p.x;
123
- else if (p.x > r.maxX)
124
- x = p.x - r.maxX;
125
- if (p.y < r.y)
126
- y = r.y - p.y;
127
- else if (p.y > r.maxY)
128
- y = p.y - r.maxY;
129
- return { x, y, value: distance({ x, y }) };
130
- }
131
- function distanceFromRect(a, b) {
132
- if (intersects(a, b))
133
- return { x: 0, y: 0, value: 0 };
134
- const left = a.x < b.x ? a : b;
135
- const right = b.x < a.x ? a : b;
136
- const upper = a.y < b.y ? a : b;
137
- const lower = b.y < a.y ? a : b;
138
- let x = left.x === right.x ? 0 : right.x - left.maxX;
139
- x = Math.max(0, x);
140
- let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
141
- y = Math.max(0, y);
142
- return { x, y, value: distance({ x, y }) };
143
- }
144
- function distanceBtwEdges(a, b) {
145
- return {
146
- left: b.x - a.x,
147
- top: b.y - a.y,
148
- right: a.maxX - b.maxX,
149
- bottom: a.maxY - b.maxY
150
- };
151
- }
152
-
153
- // src/closest.ts
154
- function closest(...pts) {
155
- return (a) => {
156
- const ds = pts.map((b) => distance(b, a));
157
- const c = Math.min.apply(Math, ds);
158
- return pts[ds.indexOf(c)];
159
- };
160
- }
161
- function closestSideToRect(ref, r) {
162
- if (r.maxX <= ref.minX) {
163
- return "left";
164
- }
165
- if (r.minX >= ref.maxX) {
166
- return "right";
167
- }
168
- if (r.maxY <= ref.minY) {
169
- return "top";
170
- }
171
- if (r.minY >= ref.maxY) {
172
- return "bottom";
173
- }
174
- return "left";
175
- }
176
- function closestSideToPoint(ref, p) {
177
- const { x, y } = p;
178
- const dl = x - ref.minX;
179
- const dr = ref.maxX - x;
180
- const dt = y - ref.minY;
181
- const db = ref.maxY - y;
182
- let closest2 = dl;
183
- let side = "left";
184
- if (dr < closest2) {
185
- closest2 = dr;
186
- side = "right";
187
- }
188
- if (dt < closest2) {
189
- closest2 = dt;
190
- side = "top";
191
- }
192
- if (db < closest2) {
193
- side = "bottom";
194
- }
195
- return side;
196
- }
197
-
198
- // src/contains.ts
199
- function containsPoint(r, p) {
200
- return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
201
- }
202
- function containsRect(a, b) {
203
- return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
204
- }
205
- function contains(r, v) {
206
- return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
207
- }
208
-
209
- // ../dom/dist/index.mjs
210
- function getCache() {
211
- const g = globalThis;
212
- g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
213
- return g.__styleCache;
214
- }
215
- function getComputedStyle(el) {
216
- if (!el)
217
- return {};
218
- const cache = getCache();
219
- let style = cache.get(el);
220
- if (!style) {
221
- const win = (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
222
- style = win.getComputedStyle(el);
223
- cache.set(el, style);
224
- }
225
- return style;
226
- }
227
-
228
- // src/from-element.ts
229
- function getElementRect(el, opts = {}) {
230
- return createRect(getClientRect(el, opts));
231
- }
232
- function getClientRect(el, opts = {}) {
233
- const { excludeScrollbar = false, excludeBorders = false } = opts;
234
- const { x, y, width, height } = el.getBoundingClientRect();
235
- const r = { x, y, width, height };
236
- const style = getComputedStyle(el);
237
- const { borderLeftWidth, borderTopWidth, borderRightWidth, borderBottomWidth } = style;
238
- const borderXWidth = sum(borderLeftWidth, borderRightWidth);
239
- const borderYWidth = sum(borderTopWidth, borderBottomWidth);
240
- if (excludeBorders) {
241
- r.width -= borderXWidth;
242
- r.height -= borderYWidth;
243
- r.x += px(borderLeftWidth);
244
- r.y += px(borderTopWidth);
245
- }
246
- if (excludeScrollbar) {
247
- const scrollbarWidth = el.offsetWidth - el.clientWidth - borderXWidth;
248
- const scrollbarHeight = el.offsetHeight - el.clientHeight - borderYWidth;
249
- r.width -= scrollbarWidth;
250
- r.height -= scrollbarHeight;
251
- }
252
- return r;
253
- }
254
- var px = (v) => parseFloat(v.replace("px", ""));
255
- var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
256
-
257
- // src/from-points.ts
258
- function getRectFromPoints(...pts) {
259
- const xs = pts.map((p) => p.x);
260
- const ys = pts.map((p) => p.y);
261
- const x = Math.min(...xs);
262
- const y = Math.min(...ys);
263
- const width = Math.max(...xs) - x;
264
- const height = Math.max(...ys) - y;
265
- return createRect({ x, y, width, height });
266
- }
267
-
268
- // src/union.ts
269
- var { min, max } = Math;
270
- function union(...rs) {
271
- const pMin = {
272
- x: min.apply(
273
- Math,
274
- rs.map((r) => r.minX)
275
- ),
276
- y: min.apply(
277
- Math,
278
- rs.map((r) => r.minY)
279
- )
280
- };
281
- const pMax = {
282
- x: max.apply(
283
- Math,
284
- rs.map((r) => r.maxX)
285
- ),
286
- y: max.apply(
287
- Math,
288
- rs.map((r) => r.maxY)
289
- )
290
- };
291
- return getRectFromPoints(pMin, pMax);
292
- }
293
-
294
- // src/from-range.ts
295
- function fromRange(range) {
296
- let rs = [];
297
- const rects = Array.from(range.getClientRects());
298
- if (rects.length) {
299
- rs = rs.concat(rects.map(createRect));
300
- return union.apply(void 0, rs);
301
- }
302
- let start = range.startContainer;
303
- if (start.nodeType === Node.TEXT_NODE) {
304
- start = start.parentNode;
305
- }
306
- if (start instanceof HTMLElement) {
307
- const r = getElementRect(start);
308
- rs.push({ ...r, x: r.maxX, width: 0 });
309
- }
310
- return union.apply(void 0, rs);
311
- }
312
-
313
- // src/from-rotation.ts
314
- function toRad(d) {
315
- return d % 360 * Math.PI / 180;
316
- }
317
- function rotate(a, d, c) {
318
- const r = toRad(d);
319
- const sin = Math.sin(r);
320
- const cos = Math.cos(r);
321
- const x = a.x - c.x;
322
- const y = a.y - c.y;
323
- return {
324
- x: c.x + x * cos - y * sin,
325
- y: c.y + x * sin + y * cos
326
- };
327
- }
328
- function getRotationRect(r, deg) {
329
- const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
330
- const xs = rr.map((p) => p.x);
331
- const ys = rr.map((p) => p.y);
332
- const minX = Math.min(...xs);
333
- const minY = Math.min(...ys);
334
- const maxX = Math.max(...xs);
335
- const maxY = Math.max(...ys);
336
- return createRect({
337
- x: minX,
338
- y: minY,
339
- width: maxX - minX,
340
- height: maxY - minY
341
- });
342
- }
343
-
344
- // src/from-window.ts
345
- function getWindowRect(win, opts = {}) {
346
- return createRect(getViewportRect(win, opts));
347
- }
348
- function getViewportRect(win, opts) {
349
- const { excludeScrollbar = false } = opts;
350
- const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
351
- const width = visualViewport.width || innerWidth2;
352
- const height = visualViewport.height || innerHeight2;
353
- const rect = { x: 0, y: 0, width, height };
354
- if (excludeScrollbar) {
355
- const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
356
- const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight;
357
- rect.width -= scrollbarWidth;
358
- rect.height -= scrollbarHeight;
359
- }
360
- return rect;
361
- }
362
-
363
- // src/get-polygon.ts
364
- function getElementPolygon(rectValue, placement) {
365
- const rect = createRect(rectValue);
366
- const { top, right, left, bottom } = getRectCorners(rect);
367
- const [base] = placement.split("-");
368
- return {
369
- top: [left, top, right, bottom],
370
- right: [top, right, bottom, left],
371
- bottom: [top, left, bottom, right],
372
- left: [right, top, left, bottom]
373
- }[base];
374
- }
375
-
376
- // src/operations.ts
377
- var isSymmetric = (v) => "dx" in v || "dy" in v;
378
- function inset(r, i) {
379
- const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
380
- const { top = 0, right = 0, bottom = 0, left = 0 } = v;
381
- return createRect({
382
- x: r.x + left,
383
- y: r.y + top,
384
- width: r.width - left - right,
385
- height: r.height - top - bottom
386
- });
387
- }
388
- function expand(r, v) {
389
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
390
- return inset(r, value);
391
- }
392
- function shrink(r, v) {
393
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
394
- return inset(r, value);
395
- }
396
- function shift(r, o) {
397
- const { x = 0, y = 0 } = o;
398
- return createRect({
399
- x: r.x + x,
400
- y: r.y + y,
401
- width: r.width,
402
- height: r.height
403
- });
404
- }
405
-
406
- // src/polygon.ts
407
- function isPointInPolygon(polygon, point2) {
408
- const { x, y } = point2;
409
- let c = false;
410
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
411
- const xi = polygon[i].x;
412
- const yi = polygon[i].y;
413
- const xj = polygon[j].x;
414
- const yj = polygon[j].y;
415
- if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
416
- c = !c;
417
- }
418
- }
419
- return c;
420
- }
421
- function createPolygonElement() {
422
- const id = "debug-polygon";
423
- const existingPolygon = document.getElementById(id);
424
- if (existingPolygon) {
425
- return existingPolygon;
426
- }
427
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
428
- Object.assign(svg.style, {
429
- top: "0",
430
- left: "0",
431
- width: "100%",
432
- height: "100%",
433
- opacity: "0.15",
434
- position: "fixed",
435
- pointerEvents: "none",
436
- fill: "red"
437
- });
438
- const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
439
- polygon.setAttribute("id", id);
440
- polygon.setAttribute("points", "0,0 0,0");
441
- svg.appendChild(polygon);
442
- document.body.appendChild(svg);
443
- return polygon;
444
- }
445
- function debugPolygon(polygon) {
446
- const el = createPolygonElement();
447
- const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
448
- el.setAttribute("points", points);
449
- return () => {
450
- el.remove();
451
- };
452
- }
453
- export {
454
- alignRect,
455
- closest,
456
- closestSideToPoint,
457
- closestSideToRect,
458
- collisions,
459
- contains,
460
- containsPoint,
461
- containsRect,
462
- createRect,
463
- debugPolygon,
464
- distance,
465
- distanceBtwEdges,
466
- distanceFromPoint,
467
- distanceFromRect,
468
- expand,
469
- fromRange,
470
- getElementPolygon,
471
- getElementRect,
472
- getRectCenters,
473
- getRectCorners,
474
- getRectEdges,
475
- getRectFromPoints,
476
- getRotationRect,
477
- getViewportRect,
478
- getWindowRect,
479
- inset,
480
- intersection,
481
- intersects,
482
- isPointInPolygon,
483
- isRect,
484
- isSymmetric,
485
- rotate,
486
- shift,
487
- shrink,
488
- toRad,
489
- union
490
- };