@zag-js/rect-utils 0.0.0-dev-20220704151659 → 0.0.0-dev-20220707125149

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.
@@ -1,3 +1,3 @@
1
- import type { Point } from "./types";
2
1
  import { Rect } from "./rect";
2
+ import type { Point } from "./types";
3
3
  export declare function getRectFromPoints(...pts: Point[]): Rect;
@@ -1,5 +1,5 @@
1
- import type { Point } from "./types";
2
1
  import { Rect } from "./rect";
2
+ import type { Point } from "./types";
3
3
  export declare function toRad(d: number): number;
4
4
  export declare function rotate(a: Point, d: number, c: Point): Point;
5
5
  export declare function getRotationRect(r: Rect, deg: number): Rect;
@@ -0,0 +1,5 @@
1
+ import type { RectValue } from "./types";
2
+ export declare function getElementPolygon(rectValue: RectValue, placement: string): {
3
+ x: number;
4
+ y: number;
5
+ }[];
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from "./from-points";
7
7
  export * from "./from-range";
8
8
  export * from "./from-rotation";
9
9
  export * from "./from-window";
10
+ export * from "./get-polygon";
10
11
  export * from "./intersection";
11
12
  export * from "./operations";
12
13
  export * from "./polygon";
package/dist/index.js CHANGED
@@ -1,8 +1,25 @@
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;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
6
23
  var __export = (target, all) => {
7
24
  for (var name in all)
8
25
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -20,7 +37,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
37
  // src/index.ts
21
38
  var src_exports = {};
22
39
  __export(src_exports, {
23
- Rect: () => Rect,
24
40
  alignRect: () => alignRect,
25
41
  closest: () => closest,
26
42
  closestSideToPoint: () => closestSideToPoint,
@@ -29,6 +45,7 @@ __export(src_exports, {
29
45
  contains: () => contains,
30
46
  containsPoint: () => containsPoint,
31
47
  containsRect: () => containsRect,
48
+ createRect: () => createRect,
32
49
  debugPolygon: () => debugPolygon,
33
50
  distance: () => distance,
34
51
  distanceBtwEdges: () => distanceBtwEdges,
@@ -36,7 +53,11 @@ __export(src_exports, {
36
53
  distanceFromRect: () => distanceFromRect,
37
54
  expand: () => expand,
38
55
  fromRange: () => fromRange,
56
+ getElementPolygon: () => getElementPolygon,
39
57
  getElementRect: () => getElementRect,
58
+ getRectCenters: () => getRectCenters,
59
+ getRectCorners: () => getRectCorners,
60
+ getRectEdges: () => getRectEdges,
40
61
  getRectFromPoints: () => getRectFromPoints,
41
62
  getRotationRect: () => getRotationRect,
42
63
  getViewportRect: () => getViewportRect,
@@ -44,121 +65,109 @@ __export(src_exports, {
44
65
  inset: () => inset,
45
66
  intersection: () => intersection,
46
67
  intersects: () => intersects,
68
+ isPointInPolygon: () => isPointInPolygon,
69
+ isRect: () => isRect,
47
70
  isSymmetric: () => isSymmetric,
48
71
  rotate: () => rotate,
49
72
  shift: () => shift,
50
73
  shrink: () => shrink,
51
74
  toRad: () => toRad,
52
- union: () => union,
53
- withinPolygon: () => withinPolygon
75
+ union: () => union
54
76
  });
55
77
  module.exports = __toCommonJS(src_exports);
56
78
 
57
79
  // src/align.ts
58
80
  function hAlign(a, ref, h) {
59
81
  let x = ref.minX;
60
- if (h === "left-inside")
82
+ if (h === "left-inside") {
61
83
  x = ref.minX;
62
- if (h === "left-outside")
84
+ }
85
+ if (h === "left-outside") {
63
86
  x = ref.minX - ref.width;
64
- if (h === "right-inside")
87
+ }
88
+ if (h === "right-inside") {
65
89
  x = ref.maxX - ref.width;
66
- if (h === "right-outside")
90
+ }
91
+ if (h === "right-outside") {
67
92
  x = ref.maxX;
68
- if (h === "center")
93
+ }
94
+ if (h === "center") {
69
95
  x = ref.midX - ref.width / 2;
70
- return a.clone().set({ x });
96
+ }
97
+ return __spreadProps(__spreadValues({}, a), { x });
71
98
  }
72
99
  function vAlign(a, ref, v) {
73
100
  let y = ref.minY;
74
- if (v === "top-inside")
101
+ if (v === "top-inside") {
75
102
  y = ref.minY;
76
- if (v === "top-outside")
103
+ }
104
+ if (v === "top-outside") {
77
105
  y = ref.minY - a.height;
78
- if (v === "bottom-inside")
106
+ }
107
+ if (v === "bottom-inside") {
79
108
  y = ref.maxY - a.height;
80
- if (v === "bottom-outside")
109
+ }
110
+ if (v === "bottom-outside") {
81
111
  y = ref.maxY;
82
- if (v === "center")
112
+ }
113
+ if (v === "center") {
83
114
  y = ref.midY - a.height / 2;
84
- return a.clone().set({ y });
115
+ }
116
+ return __spreadProps(__spreadValues({}, a), { y });
85
117
  }
86
118
  function alignRect(a, ref, options) {
87
119
  const { h, v } = options;
88
120
  return vAlign(hAlign(a, ref, h), ref, v);
89
121
  }
90
122
 
123
+ // ../core/dist/index.mjs
124
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
125
+
91
126
  // src/rect.ts
92
127
  var point = (x, y) => ({ x, y });
93
- var Rect = class {
94
- constructor(v) {
95
- this.v = v;
96
- }
97
- static create(v) {
98
- return new Rect(v);
99
- }
100
- set(n) {
101
- return new Rect(Object.assign({}, this.v, n));
102
- }
103
- clone() {
104
- return new Rect(this.v);
105
- }
106
- get x() {
107
- return this.v.x;
108
- }
109
- get y() {
110
- return this.v.y;
111
- }
112
- get width() {
113
- return this.v.width;
114
- }
115
- get height() {
116
- return this.v.height;
117
- }
118
- get minX() {
119
- return this.v.x;
120
- }
121
- get midX() {
122
- return this.v.x + this.v.width / 2;
123
- }
124
- get maxX() {
125
- return this.v.x + this.v.width;
126
- }
127
- get minY() {
128
- return this.v.y;
129
- }
130
- get midY() {
131
- return this.v.y + this.v.height / 2;
132
- }
133
- get maxY() {
134
- return this.v.y + this.v.height;
135
- }
136
- get center() {
137
- return point(this.midX, this.midY);
138
- }
139
- get centers() {
140
- const top = point(this.midX, this.minY);
141
- const right = point(this.maxX, this.midY);
142
- const bottom = point(this.midX, this.maxY);
143
- const left = point(this.minX, this.midY);
144
- return { top, right, bottom, left };
145
- }
146
- get corners() {
147
- const top = point(this.minX, this.minY);
148
- const right = point(this.maxX, this.minY);
149
- const bottom = point(this.maxX, this.maxY);
150
- const left = point(this.minX, this.maxY);
151
- return { top, right, bottom, left };
152
- }
153
- get edges() {
154
- const c = this.corners;
155
- const top = [c.top, c.right];
156
- const right = [c.right, c.bottom];
157
- const bottom = [c.left, c.bottom];
158
- const left = [c.top, c.left];
159
- return { top, right, bottom, left };
160
- }
161
- };
128
+ function createRect(r) {
129
+ const { x, y, width, height } = r;
130
+ const midX = x + width / 2;
131
+ const midY = y + height / 2;
132
+ return {
133
+ x,
134
+ y,
135
+ width,
136
+ height,
137
+ minX: x,
138
+ minY: y,
139
+ maxX: x + width,
140
+ maxY: y + height,
141
+ midX,
142
+ midY,
143
+ center: point(midX, midY)
144
+ };
145
+ }
146
+ function isRect(v) {
147
+ return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
148
+ }
149
+ function getRectCenters(v) {
150
+ const top = point(v.midX, v.minY);
151
+ const right = point(v.maxX, v.midY);
152
+ const bottom = point(v.midX, v.maxY);
153
+ const left = point(v.minX, v.midY);
154
+ return { top, right, bottom, left };
155
+ }
156
+ function getRectCorners(v) {
157
+ const top = point(v.minX, v.minY);
158
+ const right = point(v.maxX, v.minY);
159
+ const bottom = point(v.maxX, v.maxY);
160
+ const left = point(v.minX, v.maxY);
161
+ return { top, right, bottom, left };
162
+ }
163
+ function getRectEdges(v) {
164
+ const c = getRectCorners(v);
165
+ const top = [c.top, c.right];
166
+ const right = [c.right, c.bottom];
167
+ const bottom = [c.left, c.bottom];
168
+ const left = [c.top, c.left];
169
+ return { top, right, bottom, left };
170
+ }
162
171
 
163
172
  // src/intersection.ts
164
173
  function intersects(a, b) {
@@ -169,7 +178,7 @@ function intersection(a, b) {
169
178
  const y = Math.max(a.y, b.y);
170
179
  const x2 = Math.min(a.x + a.width, b.x + b.width);
171
180
  const y2 = Math.min(a.y + a.height, b.y + b.height);
172
- return Rect.create({ x, y, width: x2 - x, height: y2 - y });
181
+ return createRect({ x, y, width: x2 - x, height: y2 - y });
173
182
  }
174
183
  function collisions(a, b) {
175
184
  return {
@@ -228,14 +237,18 @@ function closest(...pts) {
228
237
  };
229
238
  }
230
239
  function closestSideToRect(ref, r) {
231
- if (r.maxX <= ref.minX)
240
+ if (r.maxX <= ref.minX) {
232
241
  return "left";
233
- if (r.minX >= ref.maxX)
242
+ }
243
+ if (r.minX >= ref.maxX) {
234
244
  return "right";
235
- if (r.maxY <= ref.minY)
245
+ }
246
+ if (r.maxY <= ref.minY) {
236
247
  return "top";
237
- if (r.minY >= ref.maxY)
248
+ }
249
+ if (r.minY >= ref.maxY) {
238
250
  return "bottom";
251
+ }
239
252
  return "left";
240
253
  }
241
254
  function closestSideToPoint(ref, p) {
@@ -265,17 +278,17 @@ function containsPoint(r, p) {
265
278
  return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
266
279
  }
267
280
  function containsRect(a, b) {
268
- return Object.values(b.corners).every((c) => containsPoint(a, c));
281
+ return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
269
282
  }
270
283
  function contains(r, v) {
271
- return v instanceof Rect ? containsRect(r, v) : containsPoint(r, v);
284
+ return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
272
285
  }
273
286
 
274
287
  // ../dom/dist/index.mjs
275
288
  function getCache() {
276
289
  const g = globalThis;
277
- g.__styleCache__ = g.__styleCache__ || /* @__PURE__ */ new WeakMap();
278
- return g.__styleCache__;
290
+ g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
291
+ return g.__styleCache;
279
292
  }
280
293
  function getComputedStyle(el) {
281
294
  var _a;
@@ -293,7 +306,7 @@ function getComputedStyle(el) {
293
306
 
294
307
  // src/from-element.ts
295
308
  function getElementRect(el, opts = {}) {
296
- return Rect.create(getClientRect(el, opts));
309
+ return createRect(getClientRect(el, opts));
297
310
  }
298
311
  function getClientRect(el, opts = {}) {
299
312
  const { excludeScrollbar = false, excludeBorders = false } = opts;
@@ -328,7 +341,7 @@ function getRectFromPoints(...pts) {
328
341
  const y = Math.min(...ys);
329
342
  const width = Math.max(...xs) - x;
330
343
  const height = Math.max(...ys) - y;
331
- return Rect.create({ x, y, width, height });
344
+ return createRect({ x, y, width, height });
332
345
  }
333
346
 
334
347
  // src/union.ts
@@ -350,17 +363,16 @@ function fromRange(range) {
350
363
  let rs = [];
351
364
  const rects = Array.from(range.getClientRects());
352
365
  if (rects.length) {
353
- rs = rs.concat(rects.map((r) => Rect.create(r)));
354
- } else {
355
- let start = range.startContainer;
356
- if (start.nodeType === Node.TEXT_NODE) {
357
- start = start.parentNode;
358
- }
359
- if (start instanceof HTMLElement) {
360
- let r = getElementRect(start);
361
- r.clone().set({ x: r.maxX, width: 0 });
362
- rs.push(r);
363
- }
366
+ rs = rs.concat(rects.map(createRect));
367
+ return union.apply(void 0, rs);
368
+ }
369
+ let start = range.startContainer;
370
+ if (start.nodeType === Node.TEXT_NODE) {
371
+ start = start.parentNode;
372
+ }
373
+ if (start instanceof HTMLElement) {
374
+ const r = getElementRect(start);
375
+ rs.push(__spreadProps(__spreadValues({}, r), { x: r.maxX, width: 0 }));
364
376
  }
365
377
  return union.apply(void 0, rs);
366
378
  }
@@ -375,22 +387,30 @@ function rotate(a, d, c) {
375
387
  const cos = Math.cos(r);
376
388
  const x = a.x - c.x;
377
389
  const y = a.y - c.y;
378
- return { x: c.x + x * cos - y * sin, y: c.y + x * sin + y * cos };
390
+ return {
391
+ x: c.x + x * cos - y * sin,
392
+ y: c.y + x * sin + y * cos
393
+ };
379
394
  }
380
395
  function getRotationRect(r, deg) {
381
- const rr = Object.values(r.corners).map((p) => rotate(p, deg, r.center));
396
+ const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
382
397
  const xs = rr.map((p) => p.x);
383
398
  const ys = rr.map((p) => p.y);
384
399
  const minX = Math.min(...xs);
385
400
  const minY = Math.min(...ys);
386
401
  const maxX = Math.max(...xs);
387
402
  const maxY = Math.max(...ys);
388
- return Rect.create({ x: minX, y: minY, width: maxX - minX, height: maxY - minY });
403
+ return createRect({
404
+ x: minX,
405
+ y: minY,
406
+ width: maxX - minX,
407
+ height: maxY - minY
408
+ });
389
409
  }
390
410
 
391
411
  // src/from-window.ts
392
412
  function getWindowRect(win, opts = {}) {
393
- return Rect.create(getViewportRect(win, opts));
413
+ return createRect(getViewportRect(win, opts));
394
414
  }
395
415
  function getViewportRect(win, opts) {
396
416
  const { excludeScrollbar = false } = opts;
@@ -407,12 +427,30 @@ function getViewportRect(win, opts) {
407
427
  return rect;
408
428
  }
409
429
 
430
+ // src/get-polygon.ts
431
+ function getElementPolygon(rectValue, placement) {
432
+ const rect = createRect(rectValue);
433
+ const { top, right, left, bottom } = getRectCorners(rect);
434
+ const [base] = placement.split("-");
435
+ return {
436
+ top: [left, top, right, bottom],
437
+ right: [top, right, bottom, left],
438
+ bottom: [top, left, bottom, right],
439
+ left: [right, top, left, bottom]
440
+ }[base];
441
+ }
442
+
410
443
  // src/operations.ts
411
444
  var isSymmetric = (v) => "dx" in v || "dy" in v;
412
445
  function inset(r, i) {
413
446
  const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
414
447
  const { top = 0, right = 0, bottom = 0, left = 0 } = v;
415
- return Rect.create({ x: r.x + left, y: r.y + top, width: r.width - left - right, height: r.height - top - bottom });
448
+ return createRect({
449
+ x: r.x + left,
450
+ y: r.y + top,
451
+ width: r.width - left - right,
452
+ height: r.height - top - bottom
453
+ });
416
454
  }
417
455
  function expand(r, v) {
418
456
  const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
@@ -424,11 +462,16 @@ function shrink(r, v) {
424
462
  }
425
463
  function shift(r, o) {
426
464
  const { x = 0, y = 0 } = o;
427
- return Rect.create({ x: r.x + x, y: r.y + y, width: r.width, height: r.height });
465
+ return createRect({
466
+ x: r.x + x,
467
+ y: r.y + y,
468
+ width: r.width,
469
+ height: r.height
470
+ });
428
471
  }
429
472
 
430
473
  // src/polygon.ts
431
- function withinPolygon(polygon, point2) {
474
+ function isPointInPolygon(polygon, point2) {
432
475
  const { x, y } = point2;
433
476
  let c = false;
434
477
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
@@ -470,3 +513,7 @@ function debugPolygon(polygon) {
470
513
  const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
471
514
  el.setAttribute("points", points);
472
515
  }
516
+ debugPolygon.remove = () => {
517
+ const el = document.getElementById("debug-polygon");
518
+ el == null ? void 0 : el.remove();
519
+ };
package/dist/index.mjs CHANGED
@@ -1,108 +1,115 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
1
21
  // src/align.ts
2
22
  function hAlign(a, ref, h) {
3
23
  let x = ref.minX;
4
- if (h === "left-inside")
24
+ if (h === "left-inside") {
5
25
  x = ref.minX;
6
- if (h === "left-outside")
26
+ }
27
+ if (h === "left-outside") {
7
28
  x = ref.minX - ref.width;
8
- if (h === "right-inside")
29
+ }
30
+ if (h === "right-inside") {
9
31
  x = ref.maxX - ref.width;
10
- if (h === "right-outside")
32
+ }
33
+ if (h === "right-outside") {
11
34
  x = ref.maxX;
12
- if (h === "center")
35
+ }
36
+ if (h === "center") {
13
37
  x = ref.midX - ref.width / 2;
14
- return a.clone().set({ x });
38
+ }
39
+ return __spreadProps(__spreadValues({}, a), { x });
15
40
  }
16
41
  function vAlign(a, ref, v) {
17
42
  let y = ref.minY;
18
- if (v === "top-inside")
43
+ if (v === "top-inside") {
19
44
  y = ref.minY;
20
- if (v === "top-outside")
45
+ }
46
+ if (v === "top-outside") {
21
47
  y = ref.minY - a.height;
22
- if (v === "bottom-inside")
48
+ }
49
+ if (v === "bottom-inside") {
23
50
  y = ref.maxY - a.height;
24
- if (v === "bottom-outside")
51
+ }
52
+ if (v === "bottom-outside") {
25
53
  y = ref.maxY;
26
- if (v === "center")
54
+ }
55
+ if (v === "center") {
27
56
  y = ref.midY - a.height / 2;
28
- return a.clone().set({ y });
57
+ }
58
+ return __spreadProps(__spreadValues({}, a), { y });
29
59
  }
30
60
  function alignRect(a, ref, options) {
31
61
  const { h, v } = options;
32
62
  return vAlign(hAlign(a, ref, h), ref, v);
33
63
  }
34
64
 
65
+ // ../core/dist/index.mjs
66
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
67
+
35
68
  // src/rect.ts
36
69
  var point = (x, y) => ({ x, y });
37
- var Rect = class {
38
- constructor(v) {
39
- this.v = v;
40
- }
41
- static create(v) {
42
- return new Rect(v);
43
- }
44
- set(n) {
45
- return new Rect(Object.assign({}, this.v, n));
46
- }
47
- clone() {
48
- return new Rect(this.v);
49
- }
50
- get x() {
51
- return this.v.x;
52
- }
53
- get y() {
54
- return this.v.y;
55
- }
56
- get width() {
57
- return this.v.width;
58
- }
59
- get height() {
60
- return this.v.height;
61
- }
62
- get minX() {
63
- return this.v.x;
64
- }
65
- get midX() {
66
- return this.v.x + this.v.width / 2;
67
- }
68
- get maxX() {
69
- return this.v.x + this.v.width;
70
- }
71
- get minY() {
72
- return this.v.y;
73
- }
74
- get midY() {
75
- return this.v.y + this.v.height / 2;
76
- }
77
- get maxY() {
78
- return this.v.y + this.v.height;
79
- }
80
- get center() {
81
- return point(this.midX, this.midY);
82
- }
83
- get centers() {
84
- const top = point(this.midX, this.minY);
85
- const right = point(this.maxX, this.midY);
86
- const bottom = point(this.midX, this.maxY);
87
- const left = point(this.minX, this.midY);
88
- return { top, right, bottom, left };
89
- }
90
- get corners() {
91
- const top = point(this.minX, this.minY);
92
- const right = point(this.maxX, this.minY);
93
- const bottom = point(this.maxX, this.maxY);
94
- const left = point(this.minX, this.maxY);
95
- return { top, right, bottom, left };
96
- }
97
- get edges() {
98
- const c = this.corners;
99
- const top = [c.top, c.right];
100
- const right = [c.right, c.bottom];
101
- const bottom = [c.left, c.bottom];
102
- const left = [c.top, c.left];
103
- return { top, right, bottom, left };
104
- }
105
- };
70
+ function createRect(r) {
71
+ const { x, y, width, height } = r;
72
+ const midX = x + width / 2;
73
+ const midY = y + height / 2;
74
+ return {
75
+ x,
76
+ y,
77
+ width,
78
+ height,
79
+ minX: x,
80
+ minY: y,
81
+ maxX: x + width,
82
+ maxY: y + height,
83
+ midX,
84
+ midY,
85
+ center: point(midX, midY)
86
+ };
87
+ }
88
+ function isRect(v) {
89
+ return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
90
+ }
91
+ function getRectCenters(v) {
92
+ const top = point(v.midX, v.minY);
93
+ const right = point(v.maxX, v.midY);
94
+ const bottom = point(v.midX, v.maxY);
95
+ const left = point(v.minX, v.midY);
96
+ return { top, right, bottom, left };
97
+ }
98
+ function getRectCorners(v) {
99
+ const top = point(v.minX, v.minY);
100
+ const right = point(v.maxX, v.minY);
101
+ const bottom = point(v.maxX, v.maxY);
102
+ const left = point(v.minX, v.maxY);
103
+ return { top, right, bottom, left };
104
+ }
105
+ function getRectEdges(v) {
106
+ const c = getRectCorners(v);
107
+ const top = [c.top, c.right];
108
+ const right = [c.right, c.bottom];
109
+ const bottom = [c.left, c.bottom];
110
+ const left = [c.top, c.left];
111
+ return { top, right, bottom, left };
112
+ }
106
113
 
107
114
  // src/intersection.ts
108
115
  function intersects(a, b) {
@@ -113,7 +120,7 @@ function intersection(a, b) {
113
120
  const y = Math.max(a.y, b.y);
114
121
  const x2 = Math.min(a.x + a.width, b.x + b.width);
115
122
  const y2 = Math.min(a.y + a.height, b.y + b.height);
116
- return Rect.create({ x, y, width: x2 - x, height: y2 - y });
123
+ return createRect({ x, y, width: x2 - x, height: y2 - y });
117
124
  }
118
125
  function collisions(a, b) {
119
126
  return {
@@ -172,14 +179,18 @@ function closest(...pts) {
172
179
  };
173
180
  }
174
181
  function closestSideToRect(ref, r) {
175
- if (r.maxX <= ref.minX)
182
+ if (r.maxX <= ref.minX) {
176
183
  return "left";
177
- if (r.minX >= ref.maxX)
184
+ }
185
+ if (r.minX >= ref.maxX) {
178
186
  return "right";
179
- if (r.maxY <= ref.minY)
187
+ }
188
+ if (r.maxY <= ref.minY) {
180
189
  return "top";
181
- if (r.minY >= ref.maxY)
190
+ }
191
+ if (r.minY >= ref.maxY) {
182
192
  return "bottom";
193
+ }
183
194
  return "left";
184
195
  }
185
196
  function closestSideToPoint(ref, p) {
@@ -209,17 +220,17 @@ function containsPoint(r, p) {
209
220
  return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
210
221
  }
211
222
  function containsRect(a, b) {
212
- return Object.values(b.corners).every((c) => containsPoint(a, c));
223
+ return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
213
224
  }
214
225
  function contains(r, v) {
215
- return v instanceof Rect ? containsRect(r, v) : containsPoint(r, v);
226
+ return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
216
227
  }
217
228
 
218
229
  // ../dom/dist/index.mjs
219
230
  function getCache() {
220
231
  const g = globalThis;
221
- g.__styleCache__ = g.__styleCache__ || /* @__PURE__ */ new WeakMap();
222
- return g.__styleCache__;
232
+ g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
233
+ return g.__styleCache;
223
234
  }
224
235
  function getComputedStyle(el) {
225
236
  var _a;
@@ -237,7 +248,7 @@ function getComputedStyle(el) {
237
248
 
238
249
  // src/from-element.ts
239
250
  function getElementRect(el, opts = {}) {
240
- return Rect.create(getClientRect(el, opts));
251
+ return createRect(getClientRect(el, opts));
241
252
  }
242
253
  function getClientRect(el, opts = {}) {
243
254
  const { excludeScrollbar = false, excludeBorders = false } = opts;
@@ -272,7 +283,7 @@ function getRectFromPoints(...pts) {
272
283
  const y = Math.min(...ys);
273
284
  const width = Math.max(...xs) - x;
274
285
  const height = Math.max(...ys) - y;
275
- return Rect.create({ x, y, width, height });
286
+ return createRect({ x, y, width, height });
276
287
  }
277
288
 
278
289
  // src/union.ts
@@ -294,17 +305,16 @@ function fromRange(range) {
294
305
  let rs = [];
295
306
  const rects = Array.from(range.getClientRects());
296
307
  if (rects.length) {
297
- rs = rs.concat(rects.map((r) => Rect.create(r)));
298
- } else {
299
- let start = range.startContainer;
300
- if (start.nodeType === Node.TEXT_NODE) {
301
- start = start.parentNode;
302
- }
303
- if (start instanceof HTMLElement) {
304
- let r = getElementRect(start);
305
- r.clone().set({ x: r.maxX, width: 0 });
306
- rs.push(r);
307
- }
308
+ rs = rs.concat(rects.map(createRect));
309
+ return union.apply(void 0, rs);
310
+ }
311
+ let start = range.startContainer;
312
+ if (start.nodeType === Node.TEXT_NODE) {
313
+ start = start.parentNode;
314
+ }
315
+ if (start instanceof HTMLElement) {
316
+ const r = getElementRect(start);
317
+ rs.push(__spreadProps(__spreadValues({}, r), { x: r.maxX, width: 0 }));
308
318
  }
309
319
  return union.apply(void 0, rs);
310
320
  }
@@ -319,22 +329,30 @@ function rotate(a, d, c) {
319
329
  const cos = Math.cos(r);
320
330
  const x = a.x - c.x;
321
331
  const y = a.y - c.y;
322
- return { x: c.x + x * cos - y * sin, y: c.y + x * sin + y * cos };
332
+ return {
333
+ x: c.x + x * cos - y * sin,
334
+ y: c.y + x * sin + y * cos
335
+ };
323
336
  }
324
337
  function getRotationRect(r, deg) {
325
- const rr = Object.values(r.corners).map((p) => rotate(p, deg, r.center));
338
+ const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
326
339
  const xs = rr.map((p) => p.x);
327
340
  const ys = rr.map((p) => p.y);
328
341
  const minX = Math.min(...xs);
329
342
  const minY = Math.min(...ys);
330
343
  const maxX = Math.max(...xs);
331
344
  const maxY = Math.max(...ys);
332
- return Rect.create({ x: minX, y: minY, width: maxX - minX, height: maxY - minY });
345
+ return createRect({
346
+ x: minX,
347
+ y: minY,
348
+ width: maxX - minX,
349
+ height: maxY - minY
350
+ });
333
351
  }
334
352
 
335
353
  // src/from-window.ts
336
354
  function getWindowRect(win, opts = {}) {
337
- return Rect.create(getViewportRect(win, opts));
355
+ return createRect(getViewportRect(win, opts));
338
356
  }
339
357
  function getViewportRect(win, opts) {
340
358
  const { excludeScrollbar = false } = opts;
@@ -351,12 +369,30 @@ function getViewportRect(win, opts) {
351
369
  return rect;
352
370
  }
353
371
 
372
+ // src/get-polygon.ts
373
+ function getElementPolygon(rectValue, placement) {
374
+ const rect = createRect(rectValue);
375
+ const { top, right, left, bottom } = getRectCorners(rect);
376
+ const [base] = placement.split("-");
377
+ return {
378
+ top: [left, top, right, bottom],
379
+ right: [top, right, bottom, left],
380
+ bottom: [top, left, bottom, right],
381
+ left: [right, top, left, bottom]
382
+ }[base];
383
+ }
384
+
354
385
  // src/operations.ts
355
386
  var isSymmetric = (v) => "dx" in v || "dy" in v;
356
387
  function inset(r, i) {
357
388
  const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
358
389
  const { top = 0, right = 0, bottom = 0, left = 0 } = v;
359
- return Rect.create({ x: r.x + left, y: r.y + top, width: r.width - left - right, height: r.height - top - bottom });
390
+ return createRect({
391
+ x: r.x + left,
392
+ y: r.y + top,
393
+ width: r.width - left - right,
394
+ height: r.height - top - bottom
395
+ });
360
396
  }
361
397
  function expand(r, v) {
362
398
  const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
@@ -368,11 +404,16 @@ function shrink(r, v) {
368
404
  }
369
405
  function shift(r, o) {
370
406
  const { x = 0, y = 0 } = o;
371
- return Rect.create({ x: r.x + x, y: r.y + y, width: r.width, height: r.height });
407
+ return createRect({
408
+ x: r.x + x,
409
+ y: r.y + y,
410
+ width: r.width,
411
+ height: r.height
412
+ });
372
413
  }
373
414
 
374
415
  // src/polygon.ts
375
- function withinPolygon(polygon, point2) {
416
+ function isPointInPolygon(polygon, point2) {
376
417
  const { x, y } = point2;
377
418
  let c = false;
378
419
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
@@ -414,8 +455,11 @@ function debugPolygon(polygon) {
414
455
  const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
415
456
  el.setAttribute("points", points);
416
457
  }
458
+ debugPolygon.remove = () => {
459
+ const el = document.getElementById("debug-polygon");
460
+ el == null ? void 0 : el.remove();
461
+ };
417
462
  export {
418
- Rect,
419
463
  alignRect,
420
464
  closest,
421
465
  closestSideToPoint,
@@ -424,6 +468,7 @@ export {
424
468
  contains,
425
469
  containsPoint,
426
470
  containsRect,
471
+ createRect,
427
472
  debugPolygon,
428
473
  distance,
429
474
  distanceBtwEdges,
@@ -431,7 +476,11 @@ export {
431
476
  distanceFromRect,
432
477
  expand,
433
478
  fromRange,
479
+ getElementPolygon,
434
480
  getElementRect,
481
+ getRectCenters,
482
+ getRectCorners,
483
+ getRectEdges,
435
484
  getRectFromPoints,
436
485
  getRotationRect,
437
486
  getViewportRect,
@@ -439,11 +488,12 @@ export {
439
488
  inset,
440
489
  intersection,
441
490
  intersects,
491
+ isPointInPolygon,
492
+ isRect,
442
493
  isSymmetric,
443
494
  rotate,
444
495
  shift,
445
496
  shrink,
446
497
  toRad,
447
- union,
448
- withinPolygon
498
+ union
449
499
  };
package/dist/polygon.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import type { Point } from "./types";
2
- export declare function withinPolygon(polygon: Point[], point: Point): boolean;
2
+ export declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
3
3
  export declare function debugPolygon(polygon: Point[]): void;
4
+ export declare namespace debugPolygon {
5
+ var remove: () => void;
6
+ }
package/dist/rect.d.ts CHANGED
@@ -1,64 +1,61 @@
1
1
  import type { RectEdge, RectValue } from "./types";
2
- export declare class Rect {
3
- private v;
4
- static create(v: RectValue): Rect;
5
- protected constructor(v: RectValue);
6
- set(n: Partial<RectValue>): Rect;
7
- clone(): Rect;
8
- get x(): number;
9
- get y(): number;
10
- get width(): number;
11
- get height(): number;
12
- get minX(): number;
13
- get midX(): number;
14
- get maxX(): number;
15
- get minY(): number;
16
- get midY(): number;
17
- get maxY(): number;
18
- get center(): {
2
+ export declare function createRect(r: RectValue): {
3
+ x: number;
4
+ y: number;
5
+ width: number;
6
+ height: number;
7
+ minX: number;
8
+ minY: number;
9
+ maxX: number;
10
+ maxY: number;
11
+ midX: number;
12
+ midY: number;
13
+ center: {
19
14
  x: number;
20
15
  y: number;
21
16
  };
22
- get centers(): {
23
- top: {
24
- x: number;
25
- y: number;
26
- };
27
- right: {
28
- x: number;
29
- y: number;
30
- };
31
- bottom: {
32
- x: number;
33
- y: number;
34
- };
35
- left: {
36
- x: number;
37
- y: number;
38
- };
17
+ };
18
+ export declare type Rect = ReturnType<typeof createRect>;
19
+ export declare function isRect(v: any): v is Rect;
20
+ export declare function getRectCenters(v: Rect): {
21
+ top: {
22
+ x: number;
23
+ y: number;
39
24
  };
40
- get corners(): {
41
- top: {
42
- x: number;
43
- y: number;
44
- };
45
- right: {
46
- x: number;
47
- y: number;
48
- };
49
- bottom: {
50
- x: number;
51
- y: number;
52
- };
53
- left: {
54
- x: number;
55
- y: number;
56
- };
25
+ right: {
26
+ x: number;
27
+ y: number;
57
28
  };
58
- get edges(): {
59
- top: RectEdge;
60
- right: RectEdge;
61
- bottom: RectEdge;
62
- left: RectEdge;
29
+ bottom: {
30
+ x: number;
31
+ y: number;
32
+ };
33
+ left: {
34
+ x: number;
35
+ y: number;
36
+ };
37
+ };
38
+ export declare function getRectCorners(v: Rect): {
39
+ top: {
40
+ x: number;
41
+ y: number;
42
+ };
43
+ right: {
44
+ x: number;
45
+ y: number;
46
+ };
47
+ bottom: {
48
+ x: number;
49
+ y: number;
50
+ };
51
+ left: {
52
+ x: number;
53
+ y: number;
63
54
  };
64
- }
55
+ };
56
+ export declare function getRectEdges(v: Rect): {
57
+ top: RectEdge;
58
+ right: RectEdge;
59
+ bottom: RectEdge;
60
+ left: RectEdge;
61
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/rect-utils",
3
- "version": "0.0.0-dev-20220704151659",
3
+ "version": "0.0.0-dev-20220707125149",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/chakra-ui/zag/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@zag-js/dom-utils": "0.0.0-dev-20220704151659",
28
+ "@zag-js/dom-utils": "0.0.0-dev-20220707125149",
29
29
  "@zag-js/utils": "0.1.2"
30
30
  },
31
31
  "scripts": {