@zag-js/rect-utils 0.1.5 → 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();
@@ -330,12 +269,24 @@ function getRectFromPoints(...pts) {
330
269
  var { min, max } = Math;
331
270
  function union(...rs) {
332
271
  const pMin = {
333
- x: min.apply(Math, rs.map((r) => r.minX)),
334
- y: min.apply(Math, rs.map((r) => r.minY))
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
+ )
335
280
  };
336
281
  const pMax = {
337
- x: max.apply(Math, rs.map((r) => r.maxX)),
338
- y: max.apply(Math, rs.map((r) => r.maxY))
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
+ )
339
290
  };
340
291
  return getRectFromPoints(pMin, pMax);
341
292
  }
@@ -397,8 +348,8 @@ function getWindowRect(win, opts = {}) {
397
348
  function getViewportRect(win, opts) {
398
349
  const { excludeScrollbar = false } = opts;
399
350
  const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
400
- const width = visualViewport.width || innerWidth2;
401
- 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;
402
353
  const rect = { x: 0, y: 0, width, height };
403
354
  if (excludeScrollbar) {
404
355
  const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
@@ -499,8 +450,7 @@ function debugPolygon(polygon) {
499
450
  el.remove();
500
451
  };
501
452
  }
502
- // Annotate the CommonJS export names for ESM import in node:
503
- 0 && (module.exports = {
453
+ export {
504
454
  alignRect,
505
455
  closest,
506
456
  closestSideToPoint,
@@ -537,4 +487,4 @@ function debugPolygon(polygon) {
537
487
  shrink,
538
488
  toRad,
539
489
  union
540
- });
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.5",
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.8",
29
- "@zag-js/utils": "0.1.3"
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,478 +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(Math, rs.map((r) => r.minX)),
273
- y: min.apply(Math, rs.map((r) => r.minY))
274
- };
275
- const pMax = {
276
- x: max.apply(Math, rs.map((r) => r.maxX)),
277
- y: max.apply(Math, rs.map((r) => r.maxY))
278
- };
279
- return getRectFromPoints(pMin, pMax);
280
- }
281
-
282
- // src/from-range.ts
283
- function fromRange(range) {
284
- let rs = [];
285
- const rects = Array.from(range.getClientRects());
286
- if (rects.length) {
287
- rs = rs.concat(rects.map(createRect));
288
- return union.apply(void 0, rs);
289
- }
290
- let start = range.startContainer;
291
- if (start.nodeType === Node.TEXT_NODE) {
292
- start = start.parentNode;
293
- }
294
- if (start instanceof HTMLElement) {
295
- const r = getElementRect(start);
296
- rs.push({ ...r, x: r.maxX, width: 0 });
297
- }
298
- return union.apply(void 0, rs);
299
- }
300
-
301
- // src/from-rotation.ts
302
- function toRad(d) {
303
- return d % 360 * Math.PI / 180;
304
- }
305
- function rotate(a, d, c) {
306
- const r = toRad(d);
307
- const sin = Math.sin(r);
308
- const cos = Math.cos(r);
309
- const x = a.x - c.x;
310
- const y = a.y - c.y;
311
- return {
312
- x: c.x + x * cos - y * sin,
313
- y: c.y + x * sin + y * cos
314
- };
315
- }
316
- function getRotationRect(r, deg) {
317
- const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
318
- const xs = rr.map((p) => p.x);
319
- const ys = rr.map((p) => p.y);
320
- const minX = Math.min(...xs);
321
- const minY = Math.min(...ys);
322
- const maxX = Math.max(...xs);
323
- const maxY = Math.max(...ys);
324
- return createRect({
325
- x: minX,
326
- y: minY,
327
- width: maxX - minX,
328
- height: maxY - minY
329
- });
330
- }
331
-
332
- // src/from-window.ts
333
- function getWindowRect(win, opts = {}) {
334
- return createRect(getViewportRect(win, opts));
335
- }
336
- function getViewportRect(win, opts) {
337
- const { excludeScrollbar = false } = opts;
338
- const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
339
- const width = visualViewport.width || innerWidth2;
340
- const height = visualViewport.height || innerHeight2;
341
- const rect = { x: 0, y: 0, width, height };
342
- if (excludeScrollbar) {
343
- const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
344
- const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight;
345
- rect.width -= scrollbarWidth;
346
- rect.height -= scrollbarHeight;
347
- }
348
- return rect;
349
- }
350
-
351
- // src/get-polygon.ts
352
- function getElementPolygon(rectValue, placement) {
353
- const rect = createRect(rectValue);
354
- const { top, right, left, bottom } = getRectCorners(rect);
355
- const [base] = placement.split("-");
356
- return {
357
- top: [left, top, right, bottom],
358
- right: [top, right, bottom, left],
359
- bottom: [top, left, bottom, right],
360
- left: [right, top, left, bottom]
361
- }[base];
362
- }
363
-
364
- // src/operations.ts
365
- var isSymmetric = (v) => "dx" in v || "dy" in v;
366
- function inset(r, i) {
367
- const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
368
- const { top = 0, right = 0, bottom = 0, left = 0 } = v;
369
- return createRect({
370
- x: r.x + left,
371
- y: r.y + top,
372
- width: r.width - left - right,
373
- height: r.height - top - bottom
374
- });
375
- }
376
- function expand(r, v) {
377
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
378
- return inset(r, value);
379
- }
380
- function shrink(r, v) {
381
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
382
- return inset(r, value);
383
- }
384
- function shift(r, o) {
385
- const { x = 0, y = 0 } = o;
386
- return createRect({
387
- x: r.x + x,
388
- y: r.y + y,
389
- width: r.width,
390
- height: r.height
391
- });
392
- }
393
-
394
- // src/polygon.ts
395
- function isPointInPolygon(polygon, point2) {
396
- const { x, y } = point2;
397
- let c = false;
398
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
399
- const xi = polygon[i].x;
400
- const yi = polygon[i].y;
401
- const xj = polygon[j].x;
402
- const yj = polygon[j].y;
403
- if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
404
- c = !c;
405
- }
406
- }
407
- return c;
408
- }
409
- function createPolygonElement() {
410
- const id = "debug-polygon";
411
- const existingPolygon = document.getElementById(id);
412
- if (existingPolygon) {
413
- return existingPolygon;
414
- }
415
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
416
- Object.assign(svg.style, {
417
- top: "0",
418
- left: "0",
419
- width: "100%",
420
- height: "100%",
421
- opacity: "0.15",
422
- position: "fixed",
423
- pointerEvents: "none",
424
- fill: "red"
425
- });
426
- const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
427
- polygon.setAttribute("id", id);
428
- polygon.setAttribute("points", "0,0 0,0");
429
- svg.appendChild(polygon);
430
- document.body.appendChild(svg);
431
- return polygon;
432
- }
433
- function debugPolygon(polygon) {
434
- const el = createPolygonElement();
435
- const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
436
- el.setAttribute("points", points);
437
- return () => {
438
- el.remove();
439
- };
440
- }
441
- export {
442
- alignRect,
443
- closest,
444
- closestSideToPoint,
445
- closestSideToRect,
446
- collisions,
447
- contains,
448
- containsPoint,
449
- containsRect,
450
- createRect,
451
- debugPolygon,
452
- distance,
453
- distanceBtwEdges,
454
- distanceFromPoint,
455
- distanceFromRect,
456
- expand,
457
- fromRange,
458
- getElementPolygon,
459
- getElementRect,
460
- getRectCenters,
461
- getRectCorners,
462
- getRectEdges,
463
- getRectFromPoints,
464
- getRotationRect,
465
- getViewportRect,
466
- getWindowRect,
467
- inset,
468
- intersection,
469
- intersects,
470
- isPointInPolygon,
471
- isRect,
472
- isSymmetric,
473
- rotate,
474
- shift,
475
- shrink,
476
- toRad,
477
- union
478
- };