@zag-js/rect-utils
Advanced tools
Comparing version 0.0.0-dev-20220605192656 to 0.0.0-dev-20220627213111
@@ -13,2 +13,1 @@ import { Rect } from "./rect"; | ||
}; | ||
//# sourceMappingURL=from-element.d.ts.map |
@@ -1,7 +0,15 @@ | ||
export * from "./rect"; | ||
export * from "./types"; | ||
export * from "./align"; | ||
export * from "./closest"; | ||
export * from "./contains"; | ||
export * from "./distance"; | ||
export * from "./from-element"; | ||
export * from "./point"; | ||
export * from "./from-points"; | ||
export * from "./from-range"; | ||
export * from "./from-rotation"; | ||
export * from "./from-window"; | ||
export * from "./intersection"; | ||
export * from "./operations"; | ||
export * from "./polygon"; | ||
//# sourceMappingURL=index.d.ts.map | ||
export * from "./rect"; | ||
export * from "./types"; | ||
export * from "./union"; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
@@ -23,13 +24,31 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
Rect: () => Rect, | ||
alignRect: () => alignRect, | ||
closest: () => closest, | ||
closestSideToPoint: () => closestSideToPoint, | ||
closestSideToRect: () => closestSideToRect, | ||
collisions: () => collisions, | ||
contains: () => contains, | ||
containsPoint: () => containsPoint, | ||
containsRect: () => containsRect, | ||
debugPolygon: () => debugPolygon, | ||
distance: () => distance, | ||
distanceBtwEdges: () => distanceBtwEdges, | ||
distanceFromPoint: () => distanceFromPoint, | ||
distanceFromRect: () => distanceFromRect, | ||
expand: () => expand, | ||
fromRange: () => fromRange, | ||
getElementRect: () => getElementRect, | ||
getEventPoint: () => getEventPoint, | ||
getRectFromPoints: () => getRectFromPoints, | ||
getRotationRect: () => getRotationRect, | ||
getViewportRect: () => getViewportRect, | ||
getWindowRect: () => getWindowRect, | ||
inset: () => inset, | ||
intersection: () => intersection, | ||
intersects: () => intersects, | ||
isSymmetric: () => isSymmetric, | ||
relativeToNode: () => relativeToNode, | ||
rotate: () => rotate, | ||
shift: () => shift, | ||
shrink: () => shrink, | ||
toRad: () => toRad, | ||
union: () => union, | ||
withinPolygon: () => withinPolygon | ||
@@ -39,2 +58,36 @@ }); | ||
// src/align.ts | ||
function hAlign(a, ref, h) { | ||
let x = ref.minX; | ||
if (h === "left-inside") | ||
x = ref.minX; | ||
if (h === "left-outside") | ||
x = ref.minX - ref.width; | ||
if (h === "right-inside") | ||
x = ref.maxX - ref.width; | ||
if (h === "right-outside") | ||
x = ref.maxX; | ||
if (h === "center") | ||
x = ref.midX - ref.width / 2; | ||
return a.clone().set({ x }); | ||
} | ||
function vAlign(a, ref, v) { | ||
let y = ref.minY; | ||
if (v === "top-inside") | ||
y = ref.minY; | ||
if (v === "top-outside") | ||
y = ref.minY - a.height; | ||
if (v === "bottom-inside") | ||
y = ref.maxY - a.height; | ||
if (v === "bottom-outside") | ||
y = ref.maxY; | ||
if (v === "center") | ||
y = ref.midY - a.height / 2; | ||
return a.clone().set({ y }); | ||
} | ||
function alignRect(a, ref, options) { | ||
const { h, v } = options; | ||
return vAlign(hAlign(a, ref, h), ref, v); | ||
} | ||
// src/rect.ts | ||
@@ -112,4 +165,119 @@ var point = (x, y) => ({ x, y }); | ||
// src/computed-style.ts | ||
var styleCache = /* @__PURE__ */ new WeakMap(); | ||
// src/intersection.ts | ||
function intersects(a, b) { | ||
return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y; | ||
} | ||
function intersection(a, b) { | ||
const x = Math.max(a.x, b.x); | ||
const y = Math.max(a.y, b.y); | ||
const x2 = Math.min(a.x + a.width, b.x + b.width); | ||
const y2 = Math.min(a.y + a.height, b.y + b.height); | ||
return Rect.create({ x, y, width: x2 - x, height: y2 - y }); | ||
} | ||
function collisions(a, b) { | ||
return { | ||
top: a.minY <= b.minY, | ||
right: a.maxX >= b.maxX, | ||
bottom: a.maxY >= b.maxY, | ||
left: a.minX <= b.minX | ||
}; | ||
} | ||
// src/distance.ts | ||
function distance(a, b = { x: 0, y: 0 }) { | ||
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)); | ||
} | ||
function distanceFromPoint(r, p) { | ||
let x = 0; | ||
let y = 0; | ||
if (p.x < r.x) | ||
x = r.x - p.x; | ||
else if (p.x > r.maxX) | ||
x = p.x - r.maxX; | ||
if (p.y < r.y) | ||
y = r.y - p.y; | ||
else if (p.y > r.maxY) | ||
y = p.y - r.maxY; | ||
return { x, y, value: distance({ x, y }) }; | ||
} | ||
function distanceFromRect(a, b) { | ||
if (intersects(a, b)) | ||
return { x: 0, y: 0, value: 0 }; | ||
const left = a.x < b.x ? a : b; | ||
const right = b.x < a.x ? a : b; | ||
const upper = a.y < b.y ? a : b; | ||
const lower = b.y < a.y ? a : b; | ||
let x = left.x === right.x ? 0 : right.x - left.maxX; | ||
x = Math.max(0, x); | ||
let y = upper.y === lower.y ? 0 : lower.y - upper.maxY; | ||
y = Math.max(0, y); | ||
return { x, y, value: distance({ x, y }) }; | ||
} | ||
function distanceBtwEdges(a, b) { | ||
return { | ||
left: b.x - a.x, | ||
top: b.y - a.y, | ||
right: a.maxX - b.maxX, | ||
bottom: a.maxY - b.maxY | ||
}; | ||
} | ||
// src/closest.ts | ||
function closest(...pts) { | ||
return (a) => { | ||
const ds = pts.map((b) => distance(b, a)); | ||
const c = Math.min.apply(Math, ds); | ||
return pts[ds.indexOf(c)]; | ||
}; | ||
} | ||
function closestSideToRect(ref, r) { | ||
if (r.maxX <= ref.minX) | ||
return "left"; | ||
if (r.minX >= ref.maxX) | ||
return "right"; | ||
if (r.maxY <= ref.minY) | ||
return "top"; | ||
if (r.minY >= ref.maxY) | ||
return "bottom"; | ||
return "left"; | ||
} | ||
function closestSideToPoint(ref, p) { | ||
const { x, y } = p; | ||
const dl = x - ref.minX; | ||
const dr = ref.maxX - x; | ||
const dt = y - ref.minY; | ||
const db = ref.maxY - y; | ||
let closest2 = dl; | ||
let side = "left"; | ||
if (dr < closest2) { | ||
closest2 = dr; | ||
side = "right"; | ||
} | ||
if (dt < closest2) { | ||
closest2 = dt; | ||
side = "top"; | ||
} | ||
if (db < closest2) { | ||
side = "bottom"; | ||
} | ||
return side; | ||
} | ||
// src/contains.ts | ||
function containsPoint(r, p) { | ||
return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY; | ||
} | ||
function containsRect(a, b) { | ||
return Object.values(b.corners).every((c) => containsPoint(a, c)); | ||
} | ||
function contains(r, v) { | ||
return v instanceof Rect ? containsRect(r, v) : containsPoint(r, v); | ||
} | ||
// ../dom/dist/index.mjs | ||
function getCache() { | ||
const g = globalThis; | ||
g.__styleCache__ = g.__styleCache__ || /* @__PURE__ */ new WeakMap(); | ||
return g.__styleCache__; | ||
} | ||
function getComputedStyle(el) { | ||
@@ -119,7 +287,8 @@ var _a; | ||
return {}; | ||
let style = styleCache.get(el); | ||
const cache = getCache(); | ||
let style = cache.get(el); | ||
if (!style) { | ||
const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window; | ||
style = win.getComputedStyle(el); | ||
styleCache.set(el, style); | ||
cache.set(el, style); | ||
} | ||
@@ -158,35 +327,89 @@ return style; | ||
// ../core/dist/index.mjs | ||
var isDom = () => typeof window !== "undefined"; | ||
var isArray = (v) => Array.isArray(v); | ||
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v)); | ||
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); | ||
var isTouchDevice = isDom() && !!navigator.maxTouchPoints; | ||
var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches"); | ||
// src/from-points.ts | ||
function getRectFromPoints(...pts) { | ||
const xs = pts.map((p) => p.x); | ||
const ys = pts.map((p) => p.y); | ||
const x = Math.min(...xs); | ||
const y = Math.min(...ys); | ||
const width = Math.max(...xs) - x; | ||
const height = Math.max(...ys) - y; | ||
return Rect.create({ x, y, width, height }); | ||
} | ||
// src/point.ts | ||
function distance(a, b = { x: 0, y: 0 }) { | ||
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)); | ||
} | ||
function closest(...pts) { | ||
return (a) => { | ||
const ds = pts.map((b) => distance(b, a)); | ||
const c = Math.min.apply(Math, ds); | ||
return pts[ds.indexOf(c)]; | ||
// src/union.ts | ||
var { min, max } = Math; | ||
function union(...rs) { | ||
const pMin = { | ||
x: min.apply(Math, rs.map((r) => r.minX)), | ||
y: min.apply(Math, rs.map((r) => r.minY)) | ||
}; | ||
const pMax = { | ||
x: max.apply(Math, rs.map((r) => r.maxX)), | ||
y: max.apply(Math, rs.map((r) => r.maxY)) | ||
}; | ||
return getRectFromPoints(pMin, pMax); | ||
} | ||
var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 }; | ||
function getEventPoint(e, t = "page") { | ||
const p = isTouchEvent(e) ? e.touches[0] || e.changedTouches[0] || fallback : e; | ||
return { x: p[`${t}X`], y: p[`${t}Y`] }; | ||
// src/from-range.ts | ||
function fromRange(range) { | ||
let rs = []; | ||
const rects = Array.from(range.getClientRects()); | ||
if (rects.length) { | ||
rs = rs.concat(rects.map((r) => Rect.create(r))); | ||
} else { | ||
let start = range.startContainer; | ||
if (start.nodeType === Node.TEXT_NODE) { | ||
start = start.parentNode; | ||
} | ||
if (start instanceof HTMLElement) { | ||
let r = getElementRect(start); | ||
r.clone().set({ x: r.maxX, width: 0 }); | ||
rs.push(r); | ||
} | ||
} | ||
return union.apply(void 0, rs); | ||
} | ||
function relativeToNode(p, el) { | ||
const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft; | ||
const dy = p.y - el.offsetTop - el.clientTop + el.scrollTop; | ||
return { | ||
point: { x: dx, y: dy }, | ||
progress: { x: dx / el.offsetWidth, y: dy / el.offsetHeight } | ||
}; | ||
// src/from-rotation.ts | ||
function toRad(d) { | ||
return d % 360 * Math.PI / 180; | ||
} | ||
function rotate(a, d, c) { | ||
const r = toRad(d); | ||
const sin = Math.sin(r); | ||
const cos = Math.cos(r); | ||
const x = a.x - c.x; | ||
const y = a.y - c.y; | ||
return { x: c.x + x * cos - y * sin, y: c.y + x * sin + y * cos }; | ||
} | ||
function getRotationRect(r, deg) { | ||
const rr = Object.values(r.corners).map((p) => rotate(p, deg, r.center)); | ||
const xs = rr.map((p) => p.x); | ||
const ys = rr.map((p) => p.y); | ||
const minX = Math.min(...xs); | ||
const minY = Math.min(...ys); | ||
const maxX = Math.max(...xs); | ||
const maxY = Math.max(...ys); | ||
return Rect.create({ x: minX, y: minY, width: maxX - minX, height: maxY - minY }); | ||
} | ||
// src/from-window.ts | ||
function getWindowRect(win, opts = {}) { | ||
return Rect.create(getViewportRect(win, opts)); | ||
} | ||
function getViewportRect(win, opts) { | ||
const { excludeScrollbar = false } = opts; | ||
const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win; | ||
const width = visualViewport.width || innerWidth2; | ||
const height = visualViewport.height || innerHeight2; | ||
const rect = { x: 0, y: 0, width, height }; | ||
if (excludeScrollbar) { | ||
const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth; | ||
const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight; | ||
rect.width -= scrollbarWidth; | ||
rect.height -= scrollbarHeight; | ||
} | ||
return rect; | ||
} | ||
// src/operations.ts | ||
@@ -255,2 +478,1 @@ var isSymmetric = (v) => "dx" in v || "dy" in v; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -8,2 +8,1 @@ import { Rect } from "./rect"; | ||
export declare function shift(r: Rect, o: Partial<Point>): Rect; | ||
//# sourceMappingURL=operations.d.ts.map |
import { Point } from "./types"; | ||
export declare function withinPolygon(polygon: Point[], point: Point): boolean; | ||
export declare function debugPolygon(polygon: Point[]): void; | ||
//# sourceMappingURL=polygon.d.ts.map |
@@ -65,2 +65,1 @@ import { RectEdge, RectValue } from "./types"; | ||
} | ||
//# sourceMappingURL=rect.d.ts.map |
@@ -31,2 +31,1 @@ export declare type Point = { | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@zag-js/rect-utils", | ||
"version": "0.0.0-dev-20220605192656", | ||
"version": "0.0.0-dev-20220627213111", | ||
"description": "", | ||
@@ -28,3 +28,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@zag-js/utils": "0.0.0-dev-20220605192656" | ||
"@zag-js/dom-utils": "0.0.0-dev-20220627213111", | ||
"@zag-js/utils": "0.1.2" | ||
}, | ||
@@ -31,0 +32,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
1096
33284
2
20
+ Added@types/prop-types@15.7.14(transitive)
+ Added@types/react@18.0.14(transitive)
+ Added@types/scheduler@0.23.0(transitive)
+ Added@zag-js/dom-utils@0.0.0-dev-20220627213111(transitive)
+ Added@zag-js/utils@0.1.2(transitive)
+ Addedcsstype@3.1.3(transitive)
- Removed@zag-js/utils@0.0.0-dev-20220605192656(transitive)
Updated@zag-js/utils@0.1.2