@react-stately/layout 4.1.0 → 4.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.
@@ -0,0 +1,302 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {DropTarget, DropTargetDelegate, Key, LayoutDelegate, Node} from '@react-types/shared';
14
+ import {InvalidationContext, Layout, LayoutInfo, Point, Rect, Size} from '@react-stately/virtualizer';
15
+
16
+ export interface WaterfallLayoutOptions {
17
+ /**
18
+ * The minimum item size.
19
+ * @default 200 x 200
20
+ */
21
+ minItemSize?: Size,
22
+ /**
23
+ * The maximum item size.
24
+ * @default Infinity
25
+ */
26
+ maxItemSize?: Size,
27
+ /**
28
+ * The minimum space required between items.
29
+ * @default 18 x 18
30
+ */
31
+ minSpace?: Size,
32
+ /**
33
+ * The maximum number of columns.
34
+ * @default Infinity
35
+ */
36
+ maxColumns?: number,
37
+ /**
38
+ * The thickness of the drop indicator.
39
+ * @default 2
40
+ */
41
+ dropIndicatorThickness?: number
42
+ }
43
+
44
+ class WaterfallLayoutInfo extends LayoutInfo {
45
+ column = 0;
46
+
47
+ copy(): WaterfallLayoutInfo {
48
+ let res = super.copy() as WaterfallLayoutInfo;
49
+ res.column = this.column;
50
+ return res;
51
+ }
52
+ }
53
+
54
+ const DEFAULT_OPTIONS = {
55
+ minItemSize: new Size(200, 200),
56
+ maxItemSize: new Size(Infinity, Infinity),
57
+ minSpace: new Size(18, 18),
58
+ maxColumns: Infinity,
59
+ dropIndicatorThickness: 2
60
+ };
61
+
62
+ export class WaterfallLayout<T extends object, O extends WaterfallLayoutOptions = WaterfallLayoutOptions> extends Layout<Node<T>, O> implements LayoutDelegate, DropTargetDelegate {
63
+ private contentSize: Size = new Size();
64
+ private layoutInfos: Map<Key, WaterfallLayoutInfo> = new Map();
65
+ protected numColumns = 0;
66
+ protected dropIndicatorThickness = 2;
67
+
68
+ shouldInvalidateLayoutOptions(newOptions: O, oldOptions: O): boolean {
69
+ return newOptions.maxColumns !== oldOptions.maxColumns
70
+ || newOptions.dropIndicatorThickness !== oldOptions.dropIndicatorThickness
71
+ || (!(newOptions.minItemSize || DEFAULT_OPTIONS.minItemSize).equals(oldOptions.minItemSize || DEFAULT_OPTIONS.minItemSize))
72
+ || (!(newOptions.maxItemSize || DEFAULT_OPTIONS.maxItemSize).equals(oldOptions.maxItemSize || DEFAULT_OPTIONS.maxItemSize))
73
+ || (!(newOptions.minSpace || DEFAULT_OPTIONS.minSpace).equals(oldOptions.minSpace || DEFAULT_OPTIONS.minSpace));
74
+ }
75
+
76
+ update(invalidationContext: InvalidationContext<O>): void {
77
+ let {
78
+ minItemSize = DEFAULT_OPTIONS.minItemSize,
79
+ maxItemSize = DEFAULT_OPTIONS.maxItemSize,
80
+ minSpace = DEFAULT_OPTIONS.minSpace,
81
+ maxColumns = DEFAULT_OPTIONS.maxColumns,
82
+ dropIndicatorThickness = DEFAULT_OPTIONS.dropIndicatorThickness
83
+ } = invalidationContext.layoutOptions || {};
84
+ this.dropIndicatorThickness = dropIndicatorThickness;
85
+ let visibleWidth = this.virtualizer!.visibleRect.width;
86
+
87
+ // The max item width is always the entire viewport.
88
+ // If the max item height is infinity, scale in proportion to the max width.
89
+ let maxItemWidth = Math.min(maxItemSize.width, visibleWidth);
90
+ let maxItemHeight = Number.isFinite(maxItemSize.height)
91
+ ? maxItemSize.height
92
+ : Math.floor((minItemSize.height / minItemSize.width) * maxItemWidth);
93
+
94
+ // Compute the number of rows and columns needed to display the content
95
+ let columns = Math.floor(visibleWidth / (minItemSize.width + minSpace.width));
96
+ let numColumns = Math.max(1, Math.min(maxColumns, columns));
97
+
98
+ // Compute the available width (minus the space between items)
99
+ let width = visibleWidth - (minSpace.width * Math.max(0, numColumns));
100
+
101
+ // Compute the item width based on the space available
102
+ let itemWidth = Math.floor(width / numColumns);
103
+ itemWidth = Math.max(minItemSize.width, Math.min(maxItemWidth, itemWidth));
104
+
105
+ // Compute the item height, which is proportional to the item width
106
+ let t = ((itemWidth - minItemSize.width) / Math.max(1, maxItemWidth - minItemSize.width));
107
+ let itemHeight = minItemSize.height + Math.floor((maxItemHeight - minItemSize.height) * t);
108
+ itemHeight = Math.max(minItemSize.height, Math.min(maxItemHeight, itemHeight));
109
+
110
+ // Compute the horizontal spacing and content height
111
+ let horizontalSpacing = Math.floor((visibleWidth - numColumns * itemWidth) / (numColumns + 1));
112
+
113
+ // Setup an array of column heights
114
+ let columnHeights = Array(numColumns).fill(minSpace.height);
115
+ let newLayoutInfos = new Map();
116
+ let addNode = (key: Key, node: Node<T>) => {
117
+ let oldLayoutInfo = this.layoutInfos.get(key);
118
+ let height = itemHeight;
119
+ let estimatedSize = true;
120
+ if (oldLayoutInfo) {
121
+ height = oldLayoutInfo.rect.height;
122
+ estimatedSize = invalidationContext.sizeChanged || oldLayoutInfo.estimatedSize || oldLayoutInfo.content !== node;
123
+ }
124
+
125
+ // Figure out which column to place the item in, and compute its position.
126
+ // Preserve the previous column index so items don't jump around during resizing unless the number of columns changed.
127
+ let prevColumn = numColumns === this.numColumns && oldLayoutInfo && oldLayoutInfo.rect.y < this.virtualizer!.visibleRect.maxY ? oldLayoutInfo.column : undefined;
128
+ let column = prevColumn ?? columnHeights.reduce((minIndex, h, i) => h < columnHeights[minIndex] ? i : minIndex, 0);
129
+ let x = horizontalSpacing + column * (itemWidth + horizontalSpacing);
130
+ let y = columnHeights[column];
131
+
132
+ let rect = new Rect(x, y, itemWidth, height);
133
+ let layoutInfo = new WaterfallLayoutInfo(node.type, key, rect);
134
+ layoutInfo.estimatedSize = estimatedSize;
135
+ layoutInfo.allowOverflow = true;
136
+ layoutInfo.content = node;
137
+ layoutInfo.column = column;
138
+ newLayoutInfos.set(key, layoutInfo);
139
+
140
+ columnHeights[column] += layoutInfo.rect.height + minSpace.height;
141
+ };
142
+
143
+ let skeletonCount = 0;
144
+ for (let node of this.virtualizer!.collection) {
145
+ if (node.type === 'skeleton') {
146
+ // Add skeleton cards until every column has at least one, and we fill the viewport.
147
+ let startingHeights = [...columnHeights];
148
+ while (
149
+ !columnHeights.every((h, i) => h !== startingHeights[i]) ||
150
+ Math.min(...columnHeights) < this.virtualizer!.visibleRect.height
151
+ ) {
152
+ let key = `${node.key}-${skeletonCount++}`;
153
+ let content = this.layoutInfos.get(key)?.content || {...node};
154
+ addNode(key, content);
155
+ }
156
+ break;
157
+ } else {
158
+ addNode(node.key, node);
159
+ }
160
+ }
161
+
162
+ // Reset all columns to the maximum for the next section
163
+ let maxHeight = Math.max(...columnHeights);
164
+ this.contentSize = new Size(this.virtualizer!.visibleRect.width, maxHeight);
165
+ this.layoutInfos = newLayoutInfos;
166
+ this.numColumns = numColumns;
167
+ }
168
+
169
+ getLayoutInfo(key: Key): LayoutInfo {
170
+ return this.layoutInfos.get(key)!;
171
+ }
172
+
173
+ getContentSize(): Size {
174
+ return this.contentSize;
175
+ }
176
+
177
+ getVisibleLayoutInfos(rect: Rect): LayoutInfo[] {
178
+ let layoutInfos: LayoutInfo[] = [];
179
+ for (let layoutInfo of this.layoutInfos.values()) {
180
+ if (layoutInfo.rect.intersects(rect) || this.virtualizer!.isPersistedKey(layoutInfo.key)) {
181
+ layoutInfos.push(layoutInfo);
182
+ }
183
+ }
184
+ return layoutInfos;
185
+ }
186
+
187
+ updateItemSize(key: Key, size: Size) {
188
+ let layoutInfo = this.layoutInfos.get(key);
189
+ if (!size || !layoutInfo) {
190
+ return false;
191
+ }
192
+
193
+ if (size.height !== layoutInfo.rect.height) {
194
+ let newLayoutInfo = layoutInfo.copy();
195
+ newLayoutInfo.rect.height = size.height;
196
+ newLayoutInfo.estimatedSize = false;
197
+ this.layoutInfos.set(key, newLayoutInfo);
198
+ return true;
199
+ }
200
+
201
+ return false;
202
+ }
203
+
204
+ // Override keyboard navigation to work spatially.
205
+ getKeyRightOf(key: Key): Key | null {
206
+ let layoutInfo = this.getLayoutInfo(key);
207
+ if (!layoutInfo) {
208
+ return null;
209
+ }
210
+
211
+ let rect = new Rect(layoutInfo.rect.maxX, layoutInfo.rect.y, this.virtualizer!.visibleRect.maxX - layoutInfo.rect.maxX, layoutInfo.rect.height);
212
+ let layoutInfos = this.getVisibleLayoutInfos(rect);
213
+ let bestKey: Key | null = null;
214
+ let bestDistance = Infinity;
215
+ for (let candidate of layoutInfos) {
216
+ if (candidate.key === key) {
217
+ continue;
218
+ }
219
+
220
+ // Find the closest item in the x direction with the most overlap in the y direction.
221
+ let deltaX = candidate.rect.x - rect.x;
222
+ let overlapY = Math.min(candidate.rect.maxY, rect.maxY) - Math.max(candidate.rect.y, rect.y);
223
+ let distance = deltaX - overlapY;
224
+ if (distance < bestDistance) {
225
+ bestDistance = distance;
226
+ bestKey = candidate.key;
227
+ }
228
+ }
229
+
230
+ return bestKey;
231
+ }
232
+
233
+ getKeyLeftOf(key: Key): Key | null {
234
+ let layoutInfo = this.getLayoutInfo(key);
235
+ if (!layoutInfo) {
236
+ return null;
237
+ }
238
+
239
+ let rect = new Rect(0, layoutInfo.rect.y, layoutInfo.rect.x, layoutInfo.rect.height);
240
+ let layoutInfos = this.getVisibleLayoutInfos(rect);
241
+ let bestKey: Key | null = null;
242
+ let bestDistance = Infinity;
243
+ for (let candidate of layoutInfos) {
244
+ if (candidate.key === key) {
245
+ continue;
246
+ }
247
+
248
+ // Find the closest item in the x direction with the most overlap in the y direction.
249
+ let deltaX = rect.maxX - candidate.rect.maxX;
250
+ let overlapY = Math.min(candidate.rect.maxY, rect.maxY) - Math.max(candidate.rect.y, rect.y);
251
+ let distance = deltaX - overlapY;
252
+ if (distance < bestDistance) {
253
+ bestDistance = distance;
254
+ bestKey = candidate.key;
255
+ }
256
+ }
257
+
258
+ return bestKey;
259
+ }
260
+
261
+ // This overrides the default behavior of shift selection to work spatially
262
+ // rather than following the order of the items in the collection (which may appear unpredictable).
263
+ getKeyRange(from: Key, to: Key): Key[] {
264
+ let fromLayoutInfo = this.getLayoutInfo(from);
265
+ let toLayoutInfo = this.getLayoutInfo(to);
266
+ if (!fromLayoutInfo || !toLayoutInfo) {
267
+ return [];
268
+ }
269
+
270
+ // Find items where half of the area intersects the rectangle
271
+ // formed from the first item to the last item in the range.
272
+ let rect = fromLayoutInfo.rect.union(toLayoutInfo.rect);
273
+ let keys: Key[] = [];
274
+ for (let layoutInfo of this.layoutInfos.values()) {
275
+ if (rect.intersection(layoutInfo.rect).area > layoutInfo.rect.area / 2) {
276
+ keys.push(layoutInfo.key);
277
+ }
278
+ }
279
+ return keys;
280
+ }
281
+
282
+ getDropTargetFromPoint(x: number, y: number): DropTarget {
283
+ if (this.layoutInfos.size === 0) {
284
+ return {type: 'root'};
285
+ }
286
+
287
+ x += this.virtualizer!.visibleRect.x;
288
+ y += this.virtualizer!.visibleRect.y;
289
+ let key = this.virtualizer!.keyAtPoint(new Point(x, y));
290
+ if (key == null) {
291
+ return {type: 'root'};
292
+ }
293
+
294
+ // Only support "on" drop position in waterfall layout.
295
+ // Reordering doesn't make sense because the items don't have a deterministic order.
296
+ return {
297
+ type: 'item',
298
+ key,
299
+ dropPosition: 'on'
300
+ };
301
+ }
302
+ }
package/src/index.ts CHANGED
@@ -12,6 +12,8 @@
12
12
  export type {GridLayoutOptions} from './GridLayout';
13
13
  export type {ListLayoutOptions, LayoutNode} from './ListLayout';
14
14
  export type {TableLayoutProps} from './TableLayout';
15
+ export type {WaterfallLayoutOptions} from './WaterfallLayout';
15
16
  export {GridLayout} from './GridLayout';
16
17
  export {ListLayout} from './ListLayout';
17
18
  export {TableLayout} from './TableLayout';
19
+ export {WaterfallLayout} from './WaterfallLayout';