@zag-js/rect-utils 0.1.7 → 0.2.0
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 +68 -5
- package/dist/index.mjs +491 -0
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
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
|
+
|
|
1
62
|
// src/align.ts
|
|
2
63
|
function hAlign(a, ref, h) {
|
|
3
64
|
let x = ref.minX;
|
|
@@ -42,7 +103,7 @@ function alignRect(a, ref, options) {
|
|
|
42
103
|
return vAlign(hAlign(a, ref, h), ref, v);
|
|
43
104
|
}
|
|
44
105
|
|
|
45
|
-
// ../core/dist/index.
|
|
106
|
+
// ../core/dist/index.mjs
|
|
46
107
|
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
47
108
|
|
|
48
109
|
// src/rect.ts
|
|
@@ -206,19 +267,20 @@ function contains(r, v) {
|
|
|
206
267
|
return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
|
|
207
268
|
}
|
|
208
269
|
|
|
209
|
-
// ../dom/dist/index.
|
|
270
|
+
// ../dom/dist/index.mjs
|
|
210
271
|
function getCache() {
|
|
211
272
|
const g = globalThis;
|
|
212
273
|
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
|
|
213
274
|
return g.__styleCache;
|
|
214
275
|
}
|
|
215
276
|
function getComputedStyle(el) {
|
|
277
|
+
var _a;
|
|
216
278
|
if (!el)
|
|
217
279
|
return {};
|
|
218
280
|
const cache = getCache();
|
|
219
281
|
let style = cache.get(el);
|
|
220
282
|
if (!style) {
|
|
221
|
-
const win = (el == null ? void 0 : el.ownerDocument.defaultView)
|
|
283
|
+
const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
222
284
|
style = win.getComputedStyle(el);
|
|
223
285
|
cache.set(el, style);
|
|
224
286
|
}
|
|
@@ -450,7 +512,8 @@ function debugPolygon(polygon) {
|
|
|
450
512
|
el.remove();
|
|
451
513
|
};
|
|
452
514
|
}
|
|
453
|
-
export
|
|
515
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
516
|
+
0 && (module.exports = {
|
|
454
517
|
alignRect,
|
|
455
518
|
closest,
|
|
456
519
|
closestSideToPoint,
|
|
@@ -487,4 +550,4 @@ export {
|
|
|
487
550
|
shrink,
|
|
488
551
|
toRad,
|
|
489
552
|
union
|
|
490
|
-
};
|
|
553
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
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
|
+
var _a;
|
|
217
|
+
if (!el)
|
|
218
|
+
return {};
|
|
219
|
+
const cache = getCache();
|
|
220
|
+
let style = cache.get(el);
|
|
221
|
+
if (!style) {
|
|
222
|
+
const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
223
|
+
style = win.getComputedStyle(el);
|
|
224
|
+
cache.set(el, style);
|
|
225
|
+
}
|
|
226
|
+
return style;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/from-element.ts
|
|
230
|
+
function getElementRect(el, opts = {}) {
|
|
231
|
+
return createRect(getClientRect(el, opts));
|
|
232
|
+
}
|
|
233
|
+
function getClientRect(el, opts = {}) {
|
|
234
|
+
const { excludeScrollbar = false, excludeBorders = false } = opts;
|
|
235
|
+
const { x, y, width, height } = el.getBoundingClientRect();
|
|
236
|
+
const r = { x, y, width, height };
|
|
237
|
+
const style = getComputedStyle(el);
|
|
238
|
+
const { borderLeftWidth, borderTopWidth, borderRightWidth, borderBottomWidth } = style;
|
|
239
|
+
const borderXWidth = sum(borderLeftWidth, borderRightWidth);
|
|
240
|
+
const borderYWidth = sum(borderTopWidth, borderBottomWidth);
|
|
241
|
+
if (excludeBorders) {
|
|
242
|
+
r.width -= borderXWidth;
|
|
243
|
+
r.height -= borderYWidth;
|
|
244
|
+
r.x += px(borderLeftWidth);
|
|
245
|
+
r.y += px(borderTopWidth);
|
|
246
|
+
}
|
|
247
|
+
if (excludeScrollbar) {
|
|
248
|
+
const scrollbarWidth = el.offsetWidth - el.clientWidth - borderXWidth;
|
|
249
|
+
const scrollbarHeight = el.offsetHeight - el.clientHeight - borderYWidth;
|
|
250
|
+
r.width -= scrollbarWidth;
|
|
251
|
+
r.height -= scrollbarHeight;
|
|
252
|
+
}
|
|
253
|
+
return r;
|
|
254
|
+
}
|
|
255
|
+
var px = (v) => parseFloat(v.replace("px", ""));
|
|
256
|
+
var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
|
|
257
|
+
|
|
258
|
+
// src/from-points.ts
|
|
259
|
+
function getRectFromPoints(...pts) {
|
|
260
|
+
const xs = pts.map((p) => p.x);
|
|
261
|
+
const ys = pts.map((p) => p.y);
|
|
262
|
+
const x = Math.min(...xs);
|
|
263
|
+
const y = Math.min(...ys);
|
|
264
|
+
const width = Math.max(...xs) - x;
|
|
265
|
+
const height = Math.max(...ys) - y;
|
|
266
|
+
return createRect({ x, y, width, height });
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/union.ts
|
|
270
|
+
var { min, max } = Math;
|
|
271
|
+
function union(...rs) {
|
|
272
|
+
const pMin = {
|
|
273
|
+
x: min.apply(
|
|
274
|
+
Math,
|
|
275
|
+
rs.map((r) => r.minX)
|
|
276
|
+
),
|
|
277
|
+
y: min.apply(
|
|
278
|
+
Math,
|
|
279
|
+
rs.map((r) => r.minY)
|
|
280
|
+
)
|
|
281
|
+
};
|
|
282
|
+
const pMax = {
|
|
283
|
+
x: max.apply(
|
|
284
|
+
Math,
|
|
285
|
+
rs.map((r) => r.maxX)
|
|
286
|
+
),
|
|
287
|
+
y: max.apply(
|
|
288
|
+
Math,
|
|
289
|
+
rs.map((r) => r.maxY)
|
|
290
|
+
)
|
|
291
|
+
};
|
|
292
|
+
return getRectFromPoints(pMin, pMax);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/from-range.ts
|
|
296
|
+
function fromRange(range) {
|
|
297
|
+
let rs = [];
|
|
298
|
+
const rects = Array.from(range.getClientRects());
|
|
299
|
+
if (rects.length) {
|
|
300
|
+
rs = rs.concat(rects.map(createRect));
|
|
301
|
+
return union.apply(void 0, rs);
|
|
302
|
+
}
|
|
303
|
+
let start = range.startContainer;
|
|
304
|
+
if (start.nodeType === Node.TEXT_NODE) {
|
|
305
|
+
start = start.parentNode;
|
|
306
|
+
}
|
|
307
|
+
if (start instanceof HTMLElement) {
|
|
308
|
+
const r = getElementRect(start);
|
|
309
|
+
rs.push({ ...r, x: r.maxX, width: 0 });
|
|
310
|
+
}
|
|
311
|
+
return union.apply(void 0, rs);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/from-rotation.ts
|
|
315
|
+
function toRad(d) {
|
|
316
|
+
return d % 360 * Math.PI / 180;
|
|
317
|
+
}
|
|
318
|
+
function rotate(a, d, c) {
|
|
319
|
+
const r = toRad(d);
|
|
320
|
+
const sin = Math.sin(r);
|
|
321
|
+
const cos = Math.cos(r);
|
|
322
|
+
const x = a.x - c.x;
|
|
323
|
+
const y = a.y - c.y;
|
|
324
|
+
return {
|
|
325
|
+
x: c.x + x * cos - y * sin,
|
|
326
|
+
y: c.y + x * sin + y * cos
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function getRotationRect(r, deg) {
|
|
330
|
+
const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
|
|
331
|
+
const xs = rr.map((p) => p.x);
|
|
332
|
+
const ys = rr.map((p) => p.y);
|
|
333
|
+
const minX = Math.min(...xs);
|
|
334
|
+
const minY = Math.min(...ys);
|
|
335
|
+
const maxX = Math.max(...xs);
|
|
336
|
+
const maxY = Math.max(...ys);
|
|
337
|
+
return createRect({
|
|
338
|
+
x: minX,
|
|
339
|
+
y: minY,
|
|
340
|
+
width: maxX - minX,
|
|
341
|
+
height: maxY - minY
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/from-window.ts
|
|
346
|
+
function getWindowRect(win, opts = {}) {
|
|
347
|
+
return createRect(getViewportRect(win, opts));
|
|
348
|
+
}
|
|
349
|
+
function getViewportRect(win, opts) {
|
|
350
|
+
const { excludeScrollbar = false } = opts;
|
|
351
|
+
const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
|
|
352
|
+
const width = (visualViewport == null ? void 0 : visualViewport.width) || innerWidth2;
|
|
353
|
+
const height = (visualViewport == null ? void 0 : visualViewport.height) || innerHeight2;
|
|
354
|
+
const rect = { x: 0, y: 0, width, height };
|
|
355
|
+
if (excludeScrollbar) {
|
|
356
|
+
const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
|
|
357
|
+
const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight;
|
|
358
|
+
rect.width -= scrollbarWidth;
|
|
359
|
+
rect.height -= scrollbarHeight;
|
|
360
|
+
}
|
|
361
|
+
return rect;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/get-polygon.ts
|
|
365
|
+
function getElementPolygon(rectValue, placement) {
|
|
366
|
+
const rect = createRect(rectValue);
|
|
367
|
+
const { top, right, left, bottom } = getRectCorners(rect);
|
|
368
|
+
const [base] = placement.split("-");
|
|
369
|
+
return {
|
|
370
|
+
top: [left, top, right, bottom],
|
|
371
|
+
right: [top, right, bottom, left],
|
|
372
|
+
bottom: [top, left, bottom, right],
|
|
373
|
+
left: [right, top, left, bottom]
|
|
374
|
+
}[base];
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/operations.ts
|
|
378
|
+
var isSymmetric = (v) => "dx" in v || "dy" in v;
|
|
379
|
+
function inset(r, i) {
|
|
380
|
+
const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
|
|
381
|
+
const { top = 0, right = 0, bottom = 0, left = 0 } = v;
|
|
382
|
+
return createRect({
|
|
383
|
+
x: r.x + left,
|
|
384
|
+
y: r.y + top,
|
|
385
|
+
width: r.width - left - right,
|
|
386
|
+
height: r.height - top - bottom
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
function expand(r, v) {
|
|
390
|
+
const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
|
|
391
|
+
return inset(r, value);
|
|
392
|
+
}
|
|
393
|
+
function shrink(r, v) {
|
|
394
|
+
const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
|
|
395
|
+
return inset(r, value);
|
|
396
|
+
}
|
|
397
|
+
function shift(r, o) {
|
|
398
|
+
const { x = 0, y = 0 } = o;
|
|
399
|
+
return createRect({
|
|
400
|
+
x: r.x + x,
|
|
401
|
+
y: r.y + y,
|
|
402
|
+
width: r.width,
|
|
403
|
+
height: r.height
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// src/polygon.ts
|
|
408
|
+
function isPointInPolygon(polygon, point2) {
|
|
409
|
+
const { x, y } = point2;
|
|
410
|
+
let c = false;
|
|
411
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
412
|
+
const xi = polygon[i].x;
|
|
413
|
+
const yi = polygon[i].y;
|
|
414
|
+
const xj = polygon[j].x;
|
|
415
|
+
const yj = polygon[j].y;
|
|
416
|
+
if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
|
|
417
|
+
c = !c;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return c;
|
|
421
|
+
}
|
|
422
|
+
function createPolygonElement() {
|
|
423
|
+
const id = "debug-polygon";
|
|
424
|
+
const existingPolygon = document.getElementById(id);
|
|
425
|
+
if (existingPolygon) {
|
|
426
|
+
return existingPolygon;
|
|
427
|
+
}
|
|
428
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
429
|
+
Object.assign(svg.style, {
|
|
430
|
+
top: "0",
|
|
431
|
+
left: "0",
|
|
432
|
+
width: "100%",
|
|
433
|
+
height: "100%",
|
|
434
|
+
opacity: "0.15",
|
|
435
|
+
position: "fixed",
|
|
436
|
+
pointerEvents: "none",
|
|
437
|
+
fill: "red"
|
|
438
|
+
});
|
|
439
|
+
const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
|
|
440
|
+
polygon.setAttribute("id", id);
|
|
441
|
+
polygon.setAttribute("points", "0,0 0,0");
|
|
442
|
+
svg.appendChild(polygon);
|
|
443
|
+
document.body.appendChild(svg);
|
|
444
|
+
return polygon;
|
|
445
|
+
}
|
|
446
|
+
function debugPolygon(polygon) {
|
|
447
|
+
const el = createPolygonElement();
|
|
448
|
+
const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
|
|
449
|
+
el.setAttribute("points", points);
|
|
450
|
+
return () => {
|
|
451
|
+
el.remove();
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
export {
|
|
455
|
+
alignRect,
|
|
456
|
+
closest,
|
|
457
|
+
closestSideToPoint,
|
|
458
|
+
closestSideToRect,
|
|
459
|
+
collisions,
|
|
460
|
+
contains,
|
|
461
|
+
containsPoint,
|
|
462
|
+
containsRect,
|
|
463
|
+
createRect,
|
|
464
|
+
debugPolygon,
|
|
465
|
+
distance,
|
|
466
|
+
distanceBtwEdges,
|
|
467
|
+
distanceFromPoint,
|
|
468
|
+
distanceFromRect,
|
|
469
|
+
expand,
|
|
470
|
+
fromRange,
|
|
471
|
+
getElementPolygon,
|
|
472
|
+
getElementRect,
|
|
473
|
+
getRectCenters,
|
|
474
|
+
getRectCorners,
|
|
475
|
+
getRectEdges,
|
|
476
|
+
getRectFromPoints,
|
|
477
|
+
getRotationRect,
|
|
478
|
+
getViewportRect,
|
|
479
|
+
getWindowRect,
|
|
480
|
+
inset,
|
|
481
|
+
intersection,
|
|
482
|
+
intersects,
|
|
483
|
+
isPointInPolygon,
|
|
484
|
+
isRect,
|
|
485
|
+
isSymmetric,
|
|
486
|
+
rotate,
|
|
487
|
+
shift,
|
|
488
|
+
shrink,
|
|
489
|
+
toRad,
|
|
490
|
+
union
|
|
491
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/rect-utils",
|
|
4
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"description": "",
|
|
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/rect",
|
|
17
17
|
"sideEffects": false,
|
|
18
18
|
"files": [
|
|
@@ -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.
|
|
29
|
-
"@zag-js/utils": "0.
|
|
28
|
+
"@zag-js/dom-utils": "0.2.0",
|
|
29
|
+
"@zag-js/utils": "0.2.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"build-fast": "tsup src/index.ts --format=esm",
|
|
32
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
33
33
|
"start": "pnpm build --watch",
|
|
34
|
-
"build": "tsup src/index.ts --format=esm --dts",
|
|
34
|
+
"build": "tsup src/index.ts --format=esm,cjs --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",
|