@zag-js/rect-utils 0.10.5 → 0.11.1

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.
Files changed (53) hide show
  1. package/dist/index.d.mts +186 -0
  2. package/dist/index.d.ts +186 -16
  3. package/dist/index.js +540 -58
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +478 -15
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +2 -2
  8. package/dist/align.d.ts +0 -8
  9. package/dist/align.js +0 -48
  10. package/dist/align.mjs +0 -44
  11. package/dist/closest.d.ts +0 -5
  12. package/dist/closest.js +0 -53
  13. package/dist/closest.mjs +0 -47
  14. package/dist/contains.d.ts +0 -5
  15. package/dist/contains.js +0 -19
  16. package/dist/contains.mjs +0 -13
  17. package/dist/distance.d.ts +0 -9
  18. package/dist/distance.js +0 -48
  19. package/dist/distance.mjs +0 -41
  20. package/dist/from-element.d.ts +0 -12
  21. package/dist/from-element.js +0 -43
  22. package/dist/from-element.mjs +0 -39
  23. package/dist/from-points.d.ts +0 -3
  24. package/dist/from-points.js +0 -17
  25. package/dist/from-points.mjs +0 -13
  26. package/dist/from-range.d.ts +0 -2
  27. package/dist/from-range.js +0 -27
  28. package/dist/from-range.mjs +0 -23
  29. package/dist/from-rotation.d.ts +0 -5
  30. package/dist/from-rotation.js +0 -39
  31. package/dist/from-rotation.mjs +0 -33
  32. package/dist/from-window.d.ts +0 -20
  33. package/dist/from-window.js +0 -26
  34. package/dist/from-window.mjs +0 -21
  35. package/dist/get-polygon.d.ts +0 -5
  36. package/dist/get-polygon.js +0 -19
  37. package/dist/get-polygon.mjs +0 -15
  38. package/dist/intersection.d.ts +0 -14
  39. package/dist/intersection.js +0 -28
  40. package/dist/intersection.mjs +0 -22
  41. package/dist/operations.d.ts +0 -7
  42. package/dist/operations.js +0 -40
  43. package/dist/operations.mjs +0 -32
  44. package/dist/polygon.d.ts +0 -3
  45. package/dist/polygon.js +0 -53
  46. package/dist/polygon.mjs +0 -48
  47. package/dist/rect.d.ts +0 -61
  48. package/dist/rect.js +0 -55
  49. package/dist/rect.mjs +0 -47
  50. package/dist/types.d.ts +0 -30
  51. package/dist/union.d.ts +0 -2
  52. package/dist/union.js +0 -32
  53. package/dist/union.mjs +0 -28
@@ -0,0 +1,186 @@
1
+ type Point = {
2
+ x: number;
3
+ y: number;
4
+ };
5
+ type RectValue = {
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ };
11
+ type RectSide = "top" | "right" | "bottom" | "left";
12
+ type RectPoint = "top-left" | "top-center" | "top-right" | "right-center" | "left-center" | "bottom-left" | "bottom-right" | "bottom-center" | "center";
13
+ type RectEdge = [Point, Point];
14
+ type RectPoints = [Point, Point, Point, Point];
15
+ type RectEdges = Record<RectSide, RectEdge> & {
16
+ value: RectEdge[];
17
+ };
18
+ type RectCorner = "topLeft" | "topRight" | "bottomLeft" | "bottomRight";
19
+ type RectCorners = Record<RectCorner, Point> & {
20
+ value: RectPoints;
21
+ };
22
+ type RectCenter = "topCenter" | "rightCenter" | "leftCenter" | "bottomCenter";
23
+ type RectCenters = Record<RectCenter, Point> & {
24
+ value: RectPoints;
25
+ };
26
+ type RectInset = Partial<Record<RectSide, number>>;
27
+ type SymmetricRectInset = {
28
+ dx?: number;
29
+ dy?: number;
30
+ };
31
+
32
+ declare function createRect(r: RectValue): {
33
+ x: number;
34
+ y: number;
35
+ width: number;
36
+ height: number;
37
+ minX: number;
38
+ minY: number;
39
+ maxX: number;
40
+ maxY: number;
41
+ midX: number;
42
+ midY: number;
43
+ center: {
44
+ x: number;
45
+ y: number;
46
+ };
47
+ };
48
+ type Rect = ReturnType<typeof createRect>;
49
+ declare function isRect(v: any): v is Rect;
50
+ declare function getRectCenters(v: Rect): {
51
+ top: {
52
+ x: number;
53
+ y: number;
54
+ };
55
+ right: {
56
+ x: number;
57
+ y: number;
58
+ };
59
+ bottom: {
60
+ x: number;
61
+ y: number;
62
+ };
63
+ left: {
64
+ x: number;
65
+ y: number;
66
+ };
67
+ };
68
+ declare function getRectCorners(v: Rect): {
69
+ top: {
70
+ x: number;
71
+ y: number;
72
+ };
73
+ right: {
74
+ x: number;
75
+ y: number;
76
+ };
77
+ bottom: {
78
+ x: number;
79
+ y: number;
80
+ };
81
+ left: {
82
+ x: number;
83
+ y: number;
84
+ };
85
+ };
86
+ declare function getRectEdges(v: Rect): {
87
+ top: RectEdge;
88
+ right: RectEdge;
89
+ bottom: RectEdge;
90
+ left: RectEdge;
91
+ };
92
+
93
+ declare function alignRect(a: Rect, ref: Rect, options: AlignOptions): Rect;
94
+ type AlignOptions = {
95
+ h: HAlign;
96
+ v: VAlign;
97
+ };
98
+ type HAlign = "left-inside" | "left-outside" | "center" | "right-inside" | "right-outside";
99
+ type VAlign = "top-inside" | "top-outside" | "center" | "bottom-inside" | "bottom-outside";
100
+
101
+ declare function closest(...pts: Point[]): (a: Point) => Point;
102
+ declare function closestSideToRect(ref: Rect, r: Rect): RectSide;
103
+ declare function closestSideToPoint(ref: Rect, p: Point): RectSide;
104
+
105
+ declare function containsPoint(r: Rect, p: Point): boolean;
106
+ declare function containsRect(a: Rect, b: Rect): boolean;
107
+ declare function contains(r: Rect, v: Rect | Point): boolean;
108
+
109
+ type DistanceValue = Point & {
110
+ value: number;
111
+ };
112
+ declare function distance(a: Point, b?: Point): number;
113
+ declare function distanceFromPoint(r: Rect, p: Point): DistanceValue;
114
+ declare function distanceFromRect(a: Rect, b: Rect): DistanceValue;
115
+ declare function distanceBtwEdges(a: Rect, b: Rect): Record<RectSide, number>;
116
+
117
+ declare function getElementRect(el: HTMLElement, opts?: ElementRectOptions): Rect;
118
+ type ElementRectOptions = {
119
+ /**
120
+ * Whether to exclude the element's scrollbar size from the calculation.
121
+ */
122
+ excludeScrollbar?: boolean;
123
+ /**
124
+ * Whether to exclude the element's borders from the calculation.
125
+ */
126
+ excludeBorders?: boolean;
127
+ };
128
+
129
+ declare function getRectFromPoints(...pts: Point[]): Rect;
130
+
131
+ declare function fromRange(range: Range): Rect;
132
+
133
+ declare function toRad(d: number): number;
134
+ declare function rotate(a: Point, d: number, c: Point): Point;
135
+ declare function getRotationRect(r: Rect, deg: number): Rect;
136
+
137
+ type WindowRectOptions = {
138
+ /**
139
+ * Whether to exclude the element's scrollbar size from the calculation.
140
+ */
141
+ excludeScrollbar?: boolean;
142
+ };
143
+ /**
144
+ * Creates a rectange from window object
145
+ */
146
+ declare function getWindowRect(win: Window, opts?: WindowRectOptions): Rect;
147
+ /**
148
+ * Get the rect of the window with the option to exclude the scrollbar
149
+ */
150
+ declare function getViewportRect(win: Window, opts: WindowRectOptions): {
151
+ x: number;
152
+ y: number;
153
+ width: number;
154
+ height: number;
155
+ };
156
+
157
+ declare function getElementPolygon(rectValue: RectValue, placement: string): {
158
+ x: number;
159
+ y: number;
160
+ }[] | undefined;
161
+
162
+ /**
163
+ * Checks if a Rect intersects another Rect
164
+ */
165
+ declare function intersects(a: Rect, b: Rect): boolean;
166
+ /**
167
+ * Returns a new Rect that represents the intersection between two Rects
168
+ */
169
+ declare function intersection(a: Rect, b: Rect): Rect;
170
+ /**
171
+ * Returns whether two rects collide along each edge
172
+ */
173
+ declare function collisions(a: Rect, b: Rect): Record<RectSide, boolean>;
174
+
175
+ declare const isSymmetric: (v: any) => v is SymmetricRectInset;
176
+ declare function inset(r: Rect, i: RectInset | SymmetricRectInset): Rect;
177
+ declare function expand(r: Rect, v: number | SymmetricRectInset): Rect;
178
+ declare function shrink(r: Rect, v: number | SymmetricRectInset): Rect;
179
+ declare function shift(r: Rect, o: Partial<Point>): Rect;
180
+
181
+ declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
182
+ declare function debugPolygon(polygon: Point[]): () => void;
183
+
184
+ declare function union(...rs: Rect[]): Rect;
185
+
186
+ export { AlignOptions, DistanceValue, ElementRectOptions, HAlign, Point, Rect, RectCenter, RectCenters, RectCorner, RectCorners, RectEdge, RectEdges, RectInset, RectPoint, RectPoints, RectSide, RectValue, SymmetricRectInset, VAlign, WindowRectOptions, alignRect, closest, closestSideToPoint, closestSideToRect, collisions, contains, containsPoint, containsRect, createRect, debugPolygon, distance, distanceBtwEdges, distanceFromPoint, distanceFromRect, expand, fromRange, getElementPolygon, getElementRect, getRectCenters, getRectCorners, getRectEdges, getRectFromPoints, getRotationRect, getViewportRect, getWindowRect, inset, intersection, intersects, isPointInPolygon, isRect, isSymmetric, rotate, shift, shrink, toRad, union };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,186 @@
1
- export * from "./align";
2
- export * from "./closest";
3
- export * from "./contains";
4
- export * from "./distance";
5
- export * from "./from-element";
6
- export * from "./from-points";
7
- export * from "./from-range";
8
- export * from "./from-rotation";
9
- export * from "./from-window";
10
- export * from "./get-polygon";
11
- export * from "./intersection";
12
- export * from "./operations";
13
- export * from "./polygon";
14
- export * from "./rect";
15
- export * from "./types";
16
- export * from "./union";
1
+ type Point = {
2
+ x: number;
3
+ y: number;
4
+ };
5
+ type RectValue = {
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ };
11
+ type RectSide = "top" | "right" | "bottom" | "left";
12
+ type RectPoint = "top-left" | "top-center" | "top-right" | "right-center" | "left-center" | "bottom-left" | "bottom-right" | "bottom-center" | "center";
13
+ type RectEdge = [Point, Point];
14
+ type RectPoints = [Point, Point, Point, Point];
15
+ type RectEdges = Record<RectSide, RectEdge> & {
16
+ value: RectEdge[];
17
+ };
18
+ type RectCorner = "topLeft" | "topRight" | "bottomLeft" | "bottomRight";
19
+ type RectCorners = Record<RectCorner, Point> & {
20
+ value: RectPoints;
21
+ };
22
+ type RectCenter = "topCenter" | "rightCenter" | "leftCenter" | "bottomCenter";
23
+ type RectCenters = Record<RectCenter, Point> & {
24
+ value: RectPoints;
25
+ };
26
+ type RectInset = Partial<Record<RectSide, number>>;
27
+ type SymmetricRectInset = {
28
+ dx?: number;
29
+ dy?: number;
30
+ };
31
+
32
+ declare function createRect(r: RectValue): {
33
+ x: number;
34
+ y: number;
35
+ width: number;
36
+ height: number;
37
+ minX: number;
38
+ minY: number;
39
+ maxX: number;
40
+ maxY: number;
41
+ midX: number;
42
+ midY: number;
43
+ center: {
44
+ x: number;
45
+ y: number;
46
+ };
47
+ };
48
+ type Rect = ReturnType<typeof createRect>;
49
+ declare function isRect(v: any): v is Rect;
50
+ declare function getRectCenters(v: Rect): {
51
+ top: {
52
+ x: number;
53
+ y: number;
54
+ };
55
+ right: {
56
+ x: number;
57
+ y: number;
58
+ };
59
+ bottom: {
60
+ x: number;
61
+ y: number;
62
+ };
63
+ left: {
64
+ x: number;
65
+ y: number;
66
+ };
67
+ };
68
+ declare function getRectCorners(v: Rect): {
69
+ top: {
70
+ x: number;
71
+ y: number;
72
+ };
73
+ right: {
74
+ x: number;
75
+ y: number;
76
+ };
77
+ bottom: {
78
+ x: number;
79
+ y: number;
80
+ };
81
+ left: {
82
+ x: number;
83
+ y: number;
84
+ };
85
+ };
86
+ declare function getRectEdges(v: Rect): {
87
+ top: RectEdge;
88
+ right: RectEdge;
89
+ bottom: RectEdge;
90
+ left: RectEdge;
91
+ };
92
+
93
+ declare function alignRect(a: Rect, ref: Rect, options: AlignOptions): Rect;
94
+ type AlignOptions = {
95
+ h: HAlign;
96
+ v: VAlign;
97
+ };
98
+ type HAlign = "left-inside" | "left-outside" | "center" | "right-inside" | "right-outside";
99
+ type VAlign = "top-inside" | "top-outside" | "center" | "bottom-inside" | "bottom-outside";
100
+
101
+ declare function closest(...pts: Point[]): (a: Point) => Point;
102
+ declare function closestSideToRect(ref: Rect, r: Rect): RectSide;
103
+ declare function closestSideToPoint(ref: Rect, p: Point): RectSide;
104
+
105
+ declare function containsPoint(r: Rect, p: Point): boolean;
106
+ declare function containsRect(a: Rect, b: Rect): boolean;
107
+ declare function contains(r: Rect, v: Rect | Point): boolean;
108
+
109
+ type DistanceValue = Point & {
110
+ value: number;
111
+ };
112
+ declare function distance(a: Point, b?: Point): number;
113
+ declare function distanceFromPoint(r: Rect, p: Point): DistanceValue;
114
+ declare function distanceFromRect(a: Rect, b: Rect): DistanceValue;
115
+ declare function distanceBtwEdges(a: Rect, b: Rect): Record<RectSide, number>;
116
+
117
+ declare function getElementRect(el: HTMLElement, opts?: ElementRectOptions): Rect;
118
+ type ElementRectOptions = {
119
+ /**
120
+ * Whether to exclude the element's scrollbar size from the calculation.
121
+ */
122
+ excludeScrollbar?: boolean;
123
+ /**
124
+ * Whether to exclude the element's borders from the calculation.
125
+ */
126
+ excludeBorders?: boolean;
127
+ };
128
+
129
+ declare function getRectFromPoints(...pts: Point[]): Rect;
130
+
131
+ declare function fromRange(range: Range): Rect;
132
+
133
+ declare function toRad(d: number): number;
134
+ declare function rotate(a: Point, d: number, c: Point): Point;
135
+ declare function getRotationRect(r: Rect, deg: number): Rect;
136
+
137
+ type WindowRectOptions = {
138
+ /**
139
+ * Whether to exclude the element's scrollbar size from the calculation.
140
+ */
141
+ excludeScrollbar?: boolean;
142
+ };
143
+ /**
144
+ * Creates a rectange from window object
145
+ */
146
+ declare function getWindowRect(win: Window, opts?: WindowRectOptions): Rect;
147
+ /**
148
+ * Get the rect of the window with the option to exclude the scrollbar
149
+ */
150
+ declare function getViewportRect(win: Window, opts: WindowRectOptions): {
151
+ x: number;
152
+ y: number;
153
+ width: number;
154
+ height: number;
155
+ };
156
+
157
+ declare function getElementPolygon(rectValue: RectValue, placement: string): {
158
+ x: number;
159
+ y: number;
160
+ }[] | undefined;
161
+
162
+ /**
163
+ * Checks if a Rect intersects another Rect
164
+ */
165
+ declare function intersects(a: Rect, b: Rect): boolean;
166
+ /**
167
+ * Returns a new Rect that represents the intersection between two Rects
168
+ */
169
+ declare function intersection(a: Rect, b: Rect): Rect;
170
+ /**
171
+ * Returns whether two rects collide along each edge
172
+ */
173
+ declare function collisions(a: Rect, b: Rect): Record<RectSide, boolean>;
174
+
175
+ declare const isSymmetric: (v: any) => v is SymmetricRectInset;
176
+ declare function inset(r: Rect, i: RectInset | SymmetricRectInset): Rect;
177
+ declare function expand(r: Rect, v: number | SymmetricRectInset): Rect;
178
+ declare function shrink(r: Rect, v: number | SymmetricRectInset): Rect;
179
+ declare function shift(r: Rect, o: Partial<Point>): Rect;
180
+
181
+ declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
182
+ declare function debugPolygon(polygon: Point[]): () => void;
183
+
184
+ declare function union(...rs: Rect[]): Rect;
185
+
186
+ export { AlignOptions, DistanceValue, ElementRectOptions, HAlign, Point, Rect, RectCenter, RectCenters, RectCorner, RectCorners, RectEdge, RectEdges, RectInset, RectPoint, RectPoints, RectSide, RectValue, SymmetricRectInset, VAlign, WindowRectOptions, alignRect, closest, closestSideToPoint, closestSideToRect, collisions, contains, containsPoint, containsRect, createRect, debugPolygon, distance, distanceBtwEdges, distanceFromPoint, distanceFromRect, expand, fromRange, getElementPolygon, getElementRect, getRectCenters, getRectCorners, getRectEdges, getRectFromPoints, getRotationRect, getViewportRect, getWindowRect, inset, intersection, intersects, isPointInPolygon, isRect, isSymmetric, rotate, shift, shrink, toRad, union };