@tempots/ui 2.8.0 → 2.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempots/ui",
3
- "version": "2.8.0",
3
+ "version": "2.10.0",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -1,4 +1,4 @@
1
- import { Renderable, Signal, TNode } from '@tempots/dom';
1
+ import { Renderable, Signal, TNode, Value } from '@tempots/dom';
2
2
  import { AsyncResource, ResourceLoadOptions } from '../utils/resource';
3
3
  /**
4
4
  * Options for displaying the different states of an asynchronous resource.
@@ -41,7 +41,7 @@ export declare const ResourceDisplay: <V, E>(resource: AsyncResource<V, E>, opti
41
41
  * @public
42
42
  */
43
43
  export declare const Resource: <R, V, E = unknown>({ request, load, mapError, }: {
44
- request: Signal<R>;
44
+ request: Value<R>;
45
45
  load: (options: ResourceLoadOptions<R, V, E>) => Promise<V>;
46
46
  mapError?: (error: unknown) => E;
47
47
  }) => ((displayOptions: ResourceDisplayOptions<V, E>) => Renderable);
@@ -1,12 +1,218 @@
1
1
  import { DOMContext, Signal, TNode, Size } from '@tempots/dom';
2
+ /**
3
+ * Represents a rectangle with position and dimensions.
4
+ *
5
+ * A Rect defines a rectangular area in 2D space with a position (left, top)
6
+ * and dimensions (width, height). It provides convenient methods for accessing
7
+ * computed properties like right, bottom, center, and size.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // Create a rectangle using the constructor
12
+ * const rect1 = new Rect(10, 20, 100, 50);
13
+ *
14
+ * // Create a rectangle using the static factory method
15
+ * const rect2 = Rect.of({ left: 10, top: 20, width: 100, height: 50 });
16
+ *
17
+ * // Access computed properties
18
+ * console.log(rect1.right); // 110 (left + width)
19
+ * console.log(rect1.bottom); // 70 (top + height)
20
+ * console.log(rect1.center); // { x: 60, y: 45 }
21
+ *
22
+ * // Compare rectangles
23
+ * const areEqual = rect1.equals(rect2); // true
24
+ * ```
25
+ *
26
+ * @public
27
+ */
28
+ export declare class Rect {
29
+ /** The x-coordinate of the left edge of the rectangle */
30
+ readonly left: number;
31
+ /** The y-coordinate of the top edge of the rectangle */
32
+ readonly top: number;
33
+ /** The width of the rectangle */
34
+ readonly width: number;
35
+ /** The height of the rectangle */
36
+ readonly height: number;
37
+ /**
38
+ * Creates a new Rect instance using an object with optional properties.
39
+ *
40
+ * This factory method provides a convenient way to create rectangles with
41
+ * default values for any unspecified properties.
42
+ *
43
+ * @param options - Object containing rectangle properties
44
+ * @param options.left - The x-coordinate of the left edge (default: 0)
45
+ * @param options.top - The y-coordinate of the top edge (default: 0)
46
+ * @param options.width - The width of the rectangle (default: 0)
47
+ * @param options.height - The height of the rectangle (default: 0)
48
+ * @returns A new Rect instance
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * // Create a rectangle at origin with no size
53
+ * const emptyRect = Rect.of({});
54
+ *
55
+ * // Create a rectangle with only position
56
+ * const positioned = Rect.of({ left: 10, top: 20 });
57
+ *
58
+ * // Create a rectangle with all properties
59
+ * const fullRect = Rect.of({ left: 10, top: 20, width: 100, height: 50 });
60
+ * ```
61
+ */
62
+ static of({ left, top, width, height, }: {
63
+ left?: number;
64
+ top?: number;
65
+ width?: number;
66
+ height?: number;
67
+ }): Rect;
68
+ /**
69
+ * Creates a new Rect instance.
70
+ *
71
+ * @param left - The x-coordinate of the left edge
72
+ * @param top - The y-coordinate of the top edge
73
+ * @param width - The width of the rectangle
74
+ * @param height - The height of the rectangle
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const rect = new Rect(10, 20, 100, 50);
79
+ * console.log(rect.left); // 10
80
+ * console.log(rect.top); // 20
81
+ * console.log(rect.width); // 100
82
+ * console.log(rect.height); // 50
83
+ * ```
84
+ */
85
+ constructor(
86
+ /** The x-coordinate of the left edge of the rectangle */
87
+ left: number,
88
+ /** The y-coordinate of the top edge of the rectangle */
89
+ top: number,
90
+ /** The width of the rectangle */
91
+ width: number,
92
+ /** The height of the rectangle */
93
+ height: number);
94
+ /**
95
+ * Gets the x-coordinate of the right edge of the rectangle.
96
+ *
97
+ * @returns The right edge position (left + width)
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const rect = new Rect(10, 20, 100, 50);
102
+ * console.log(rect.right); // 110
103
+ * ```
104
+ */
105
+ get right(): number;
106
+ /**
107
+ * Gets the y-coordinate of the bottom edge of the rectangle.
108
+ *
109
+ * @returns The bottom edge position (top + height)
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const rect = new Rect(10, 20, 100, 50);
114
+ * console.log(rect.bottom); // 70
115
+ * ```
116
+ */
117
+ get bottom(): number;
118
+ /**
119
+ * Gets the center point of the rectangle.
120
+ *
121
+ * @returns An object with x and y coordinates of the center point
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * const rect = new Rect(10, 20, 100, 50);
126
+ * const center = rect.center;
127
+ * console.log(center.x); // 60 (left + width/2)
128
+ * console.log(center.y); // 45 (top + height/2)
129
+ * ```
130
+ */
131
+ get center(): {
132
+ x: number;
133
+ y: number;
134
+ };
135
+ /**
136
+ * Gets the size dimensions of the rectangle.
137
+ *
138
+ * @returns An object containing width and height properties
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * const rect = new Rect(10, 20, 100, 50);
143
+ * const size = rect.size;
144
+ * console.log(size.width); // 100
145
+ * console.log(size.height); // 50
146
+ * ```
147
+ */
148
+ get size(): {
149
+ width: number;
150
+ height: number;
151
+ };
152
+ /**
153
+ * Compares this rectangle with another rectangle for equality.
154
+ *
155
+ * Uses near-equality comparison for floating-point precision tolerance.
156
+ * Two rectangles are considered equal if all their corresponding properties
157
+ * (left, top, width, height) are nearly equal within a small tolerance.
158
+ *
159
+ * @param other - The rectangle to compare with
160
+ * @returns True if the rectangles are equal, false otherwise
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * const rect1 = new Rect(10, 20, 100, 50);
165
+ * const rect2 = new Rect(10, 20, 100, 50);
166
+ * const rect3 = new Rect(10.00000001, 20, 100, 50); // Very close values
167
+ *
168
+ * console.log(rect1.equals(rect2)); // true
169
+ * console.log(rect1.equals(rect3)); // true (within tolerance)
170
+ * ```
171
+ */
172
+ readonly equals: (other: Rect) => boolean;
173
+ }
174
+ /**
175
+ * Gets the absolute rectangle of an element relative to the document.
176
+ *
177
+ * This function calculates the element's position relative to the entire document
178
+ * (including scroll offset), rather than just the viewport. It combines the element's
179
+ * bounding client rectangle with the current scroll position to provide absolute
180
+ * coordinates.
181
+ *
182
+ * @param el - The DOM element to get the absolute rectangle for
183
+ * @returns A Rect instance representing the element's absolute position and size
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * const element = document.getElementById('myElement');
188
+ * const absoluteRect = getAbsoluteRect(element);
189
+ *
190
+ * console.log(absoluteRect.left); // Absolute x position in document
191
+ * console.log(absoluteRect.top); // Absolute y position in document
192
+ * console.log(absoluteRect.width); // Element width
193
+ * console.log(absoluteRect.height); // Element height
194
+ * ```
195
+ *
196
+ * @public
197
+ */
198
+ export declare function getAbsoluteRect(el: Element): Rect;
199
+ /**
200
+ * Creates a renderable function that monitors the size of an element and provides it as a signal.
201
+ *
202
+ * @param fn - The renderable function that receives the size signal and returns a TNode.
203
+ * @returns A function that takes a DOMContext and returns a renderable function.
204
+ * @public
205
+ */
206
+ export declare const ElementRect: (fn: (rect: Signal<Rect>) => TNode) => import('@tempots/dom').Renderable;
2
207
  /**
3
208
  * Creates a renderable function that monitors the size of an element and provides it as a signal.
4
209
  *
5
210
  * @param fn - The renderable function that receives the size signal and returns a TNode.
6
211
  * @returns A function that takes a DOMContext and returns a renderable function.
212
+ * @deprecated use ElementRect instead
7
213
  * @public
8
214
  */
9
- export declare const ElementSize: (fn: (size: Signal<Size>) => TNode) => import('@tempots/dom').Renderable;
215
+ export declare const ElementSize: (fn: (size: Signal<Rect>) => TNode) => import('@tempots/dom').Renderable;
10
216
  /**
11
217
  * Creates a renderable function that monitors the window size and invokes the provided function with the current size.
12
218
  * @param fn - The function to be invoked with the current window size.
@@ -1,4 +1,4 @@
1
- import { Signal } from '@tempots/dom';
1
+ import { Signal, Value } from '@tempots/dom';
2
2
  import { AsyncResult } from '@tempots/std';
3
3
  /**
4
4
  * Represents an asynchronous resource with its current status, value, error, and loading state.
@@ -51,4 +51,4 @@ export interface ResourceLoadOptions<R, V, E> {
51
51
  * @returns The created asynchronous resource.
52
52
  * @public
53
53
  */
54
- export declare const makeResource: <R, V, E>(request: Signal<R>, load: (options: ResourceLoadOptions<R, V, E>) => Promise<V>, convertError: (error: unknown) => E) => AsyncResource<V, E>;
54
+ export declare const makeResource: <R, V, E>(request: Value<R>, load: (options: ResourceLoadOptions<R, V, E>) => Promise<V>, convertError: (error: unknown) => E) => AsyncResource<V, E>;