@react-stately/layout 4.6.0 → 4.7.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/src/GridLayout.ts DELETED
@@ -1,381 +0,0 @@
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, ItemDropTarget, Key, Node} from '@react-types/shared';
14
- import {InvalidationContext, Layout, LayoutInfo, Rect, Size} from '@react-stately/virtualizer';
15
-
16
- export interface GridLayoutOptions {
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
- * Whether to preserve the aspect ratio of the `minItemSize`.
29
- * By default, grid rows may have variable heights. When `preserveAspectRatio`
30
- * is true, all rows will have equal heights.
31
- * @default false
32
- */
33
- preserveAspectRatio?: boolean,
34
- /**
35
- * The minimum space required between items.
36
- * @default 18 x 18
37
- */
38
- minSpace?: Size,
39
- /**
40
- * The maximum allowed horizontal space between items.
41
- * @default Infinity
42
- */
43
- maxHorizontalSpace?: number,
44
- /**
45
- * The maximum number of columns.
46
- * @default Infinity
47
- */
48
- maxColumns?: number,
49
- /**
50
- * The thickness of the drop indicator.
51
- * @default 2
52
- */
53
- dropIndicatorThickness?: number,
54
- /**
55
- * The fixed height of a loader element in px. This loader is specifically for
56
- * "load more" elements rendered when loading more rows at the root level or inside nested row/sections.
57
- * @default 48
58
- */
59
- loaderHeight?: number
60
- }
61
-
62
- const DEFAULT_OPTIONS = {
63
- minItemSize: new Size(200, 200),
64
- maxItemSize: new Size(Infinity, Infinity),
65
- preserveAspectRatio: false,
66
- minSpace: new Size(18, 18),
67
- maxSpace: Infinity,
68
- maxColumns: Infinity,
69
- dropIndicatorThickness: 2,
70
- loaderHeight: 48
71
- };
72
-
73
- /**
74
- * GridLayout is a virtualizer Layout implementation
75
- * that arranges its items in a grid.
76
- * The items are sized between a minimum and maximum size
77
- * depending on the width of the container.
78
- */
79
- export class GridLayout<T, O extends GridLayoutOptions = GridLayoutOptions> extends Layout<Node<T>, O> implements DropTargetDelegate {
80
- protected gap: Size = DEFAULT_OPTIONS.minSpace;
81
- protected dropIndicatorThickness = 2;
82
- protected numColumns: number = 0;
83
- private contentSize: Size = new Size();
84
- private layoutInfos: Map<Key, LayoutInfo> = new Map();
85
- private margin: number = 0;
86
-
87
- shouldInvalidateLayoutOptions(newOptions: O, oldOptions: O): boolean {
88
- return newOptions.maxColumns !== oldOptions.maxColumns
89
- || newOptions.dropIndicatorThickness !== oldOptions.dropIndicatorThickness
90
- || newOptions.preserveAspectRatio !== oldOptions.preserveAspectRatio
91
- || (!(newOptions.minItemSize || DEFAULT_OPTIONS.minItemSize).equals(oldOptions.minItemSize || DEFAULT_OPTIONS.minItemSize))
92
- || (!(newOptions.maxItemSize || DEFAULT_OPTIONS.maxItemSize).equals(oldOptions.maxItemSize || DEFAULT_OPTIONS.maxItemSize))
93
- || (!(newOptions.minSpace || DEFAULT_OPTIONS.minSpace).equals(oldOptions.minSpace || DEFAULT_OPTIONS.minSpace))
94
- || newOptions.maxHorizontalSpace !== oldOptions.maxHorizontalSpace
95
- || newOptions.loaderHeight !== oldOptions.loaderHeight;
96
- }
97
-
98
- update(invalidationContext: InvalidationContext<O>): void {
99
- let {
100
- minItemSize = DEFAULT_OPTIONS.minItemSize,
101
- maxItemSize = DEFAULT_OPTIONS.maxItemSize,
102
- preserveAspectRatio = DEFAULT_OPTIONS.preserveAspectRatio,
103
- minSpace = DEFAULT_OPTIONS.minSpace,
104
- maxHorizontalSpace = DEFAULT_OPTIONS.maxSpace,
105
- maxColumns = DEFAULT_OPTIONS.maxColumns,
106
- dropIndicatorThickness = DEFAULT_OPTIONS.dropIndicatorThickness,
107
- loaderHeight = DEFAULT_OPTIONS.loaderHeight
108
- } = invalidationContext.layoutOptions || {};
109
- this.dropIndicatorThickness = dropIndicatorThickness;
110
-
111
- let visibleWidth = this.virtualizer!.visibleRect.width;
112
-
113
- // The max item width is always the entire viewport.
114
- // If the max item height is infinity, scale in proportion to the max width.
115
- let maxItemWidth = Math.min(maxItemSize.width, visibleWidth);
116
- let maxItemHeight = Number.isFinite(maxItemSize.height)
117
- ? maxItemSize.height
118
- : Math.floor((minItemSize.height / minItemSize.width) * maxItemWidth);
119
-
120
- // Compute the number of rows and columns needed to display the content
121
- let columns = Math.floor(visibleWidth / (minItemSize.width + minSpace.width));
122
- let numColumns = Math.max(1, Math.min(maxColumns, columns));
123
- this.numColumns = numColumns;
124
-
125
- // Compute the available width (minus the space between items)
126
- let width = visibleWidth - (minSpace.width * Math.max(0, numColumns));
127
-
128
- // Compute the item width based on the space available
129
- let itemWidth = Math.floor(width / numColumns);
130
- itemWidth = Math.max(minItemSize.width, Math.min(maxItemWidth, itemWidth));
131
-
132
- // Compute the item height, which is proportional to the item width
133
- let t = ((itemWidth - minItemSize.width) / Math.max(1, maxItemWidth - minItemSize.width));
134
- let itemHeight = minItemSize.height + Math.floor((maxItemHeight - minItemSize.height) * t);
135
- itemHeight = Math.max(minItemSize.height, Math.min(maxItemHeight, itemHeight));
136
-
137
- // Compute the horizontal spacing, content height and horizontal margin
138
- let horizontalSpacing = Math.min(Math.max(maxHorizontalSpace, minSpace.width), Math.floor((visibleWidth - numColumns * itemWidth) / (numColumns + 1)));
139
- this.gap = new Size(horizontalSpacing, minSpace.height);
140
- this.margin = Math.floor((visibleWidth - numColumns * itemWidth - horizontalSpacing * (numColumns + 1)) / 2);
141
-
142
- // If there is a skeleton loader within the last 2 items in the collection, increment the collection size
143
- // so that an additional row is added for the skeletons.
144
- let collection = this.virtualizer!.collection;
145
- let collectionSize = collection.size;
146
- let lastKey = collection.getLastKey();
147
- for (let i = 0; i < 2 && lastKey != null; i++) {
148
- let item = collection.getItem(lastKey);
149
- if (item?.type === 'skeleton') {
150
- collectionSize++;
151
- break;
152
- }
153
- lastKey = collection.getKeyBefore(lastKey);
154
- }
155
-
156
- let rows = Math.ceil(collectionSize / numColumns);
157
- let iterator = collection[Symbol.iterator]();
158
- let y = rows > 0 ? minSpace.height : 0;
159
- let newLayoutInfos = new Map();
160
- let skeleton: Node<T> | null = null;
161
- let skeletonCount = 0;
162
- for (let row = 0; row < rows; row++) {
163
- let maxHeight = 0;
164
- let rowLayoutInfos: LayoutInfo[] = [];
165
- for (let col = 0; col < numColumns; col++) {
166
- // Repeat skeleton until the end of the current row.
167
- let node = skeleton || iterator.next().value;
168
- if (!node) {
169
- break;
170
- }
171
-
172
- // We will add the loader after the skeletons so skip here
173
- if (node.type === 'loader') {
174
- continue;
175
- }
176
-
177
- if (node.type === 'skeleton') {
178
- skeleton = node;
179
- }
180
-
181
- let key = skeleton ? `${skeleton.key}-${skeletonCount++}` : node.key;
182
- let oldLayoutInfo = this.layoutInfos.get(key);
183
- let content = node;
184
- if (skeleton) {
185
- content = oldLayoutInfo && oldLayoutInfo.content.key === key ? oldLayoutInfo.content : {...skeleton, key};
186
- }
187
- let x = horizontalSpacing + col * (itemWidth + horizontalSpacing) + this.margin;
188
- let height = itemHeight;
189
- let estimatedSize = !preserveAspectRatio;
190
- if (oldLayoutInfo && estimatedSize) {
191
- height = oldLayoutInfo.rect.height;
192
- estimatedSize = invalidationContext.layoutOptionsChanged || invalidationContext.sizeChanged || oldLayoutInfo.estimatedSize || (oldLayoutInfo.content !== content);
193
- }
194
-
195
- let rect = new Rect(x, y, itemWidth, height);
196
- let layoutInfo = new LayoutInfo(node.type, key, rect);
197
- layoutInfo.estimatedSize = estimatedSize;
198
- layoutInfo.allowOverflow = true;
199
- layoutInfo.content = content;
200
- newLayoutInfos.set(key, layoutInfo);
201
- rowLayoutInfos.push(layoutInfo);
202
-
203
- maxHeight = Math.max(maxHeight, layoutInfo.rect.height);
204
- }
205
-
206
- for (let layoutInfo of rowLayoutInfos) {
207
- layoutInfo.rect.height = maxHeight;
208
- }
209
-
210
- y += maxHeight + minSpace.height;
211
-
212
- // Keep adding skeleton rows until we fill the viewport
213
- if (skeleton && row === rows - 1 && y < this.virtualizer!.visibleRect.height) {
214
- rows++;
215
- }
216
- }
217
-
218
- // Always add the loader sentinel if present in the collection so we can make sure it is never virtualized out.
219
- let lastNode = collection.getItem(collection.getLastKey()!);
220
- if (lastNode?.type === 'loader') {
221
- if (skeletonCount > 0 || !lastNode.props.isLoading) {
222
- loaderHeight = 0;
223
- }
224
- const loaderWidth = visibleWidth - horizontalSpacing * 2;
225
- // Note that if the user provides isLoading to their sentinel during a case where they only want to render the emptyState, this will reserve
226
- // room for the loader alongside rendering the emptyState
227
- let rect = new Rect(horizontalSpacing, y, loaderWidth, loaderHeight);
228
- let layoutInfo = new LayoutInfo('loader', lastNode.key, rect);
229
- newLayoutInfos.set(lastNode.key, layoutInfo);
230
- y = layoutInfo.rect.maxY;
231
- }
232
-
233
- this.layoutInfos = newLayoutInfos;
234
- this.contentSize = new Size(this.virtualizer!.visibleRect.width, y);
235
- }
236
-
237
- getLayoutInfo(key: Key): LayoutInfo | null {
238
- return this.layoutInfos.get(key) || null;
239
- }
240
-
241
- getContentSize(): Size {
242
- return this.contentSize;
243
- }
244
-
245
- getVisibleLayoutInfos(rect: Rect): LayoutInfo[] {
246
- let layoutInfos: LayoutInfo[] = [];
247
- for (let layoutInfo of this.layoutInfos.values()) {
248
- if (layoutInfo.rect.intersects(rect) || this.virtualizer!.isPersistedKey(layoutInfo.key) || layoutInfo.type === 'loader') {
249
- layoutInfos.push(layoutInfo);
250
- }
251
- }
252
- return layoutInfos;
253
- }
254
-
255
- updateItemSize(key: Key, size: Size): boolean {
256
- let layoutInfo = this.layoutInfos.get(key);
257
- if (!size || !layoutInfo) {
258
- return false;
259
- }
260
-
261
- if (size.height !== layoutInfo.rect.height) {
262
- let newLayoutInfo = layoutInfo.copy();
263
- newLayoutInfo.rect.height = size.height;
264
- newLayoutInfo.estimatedSize = false;
265
- this.layoutInfos.set(key, newLayoutInfo);
266
- return true;
267
- }
268
-
269
- return false;
270
- }
271
-
272
- getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget {
273
- if (this.layoutInfos.size === 0) {
274
- return {type: 'root'};
275
- }
276
-
277
- x += this.virtualizer!.visibleRect.x;
278
- y += this.virtualizer!.visibleRect.y;
279
-
280
- // Find the closest item within on either side of the point using the gap width.
281
- let key: Key | null = null;
282
- if (this.numColumns === 1) {
283
- let searchRect = new Rect(x, Math.max(0, y - this.gap.height), 1, Math.max(1, this.gap.height * 2));
284
- let candidates = this.getVisibleLayoutInfos(searchRect);
285
- let minDistance = Infinity;
286
- for (let candidate of candidates) {
287
- // Ignore items outside the search rect, e.g. persisted keys.
288
- if (!candidate.rect.intersects(searchRect)) {
289
- continue;
290
- }
291
-
292
- let yDist = Math.abs(candidate.rect.y - y);
293
- let maxYDist = Math.abs(candidate.rect.maxY - y);
294
- let dist = Math.min(yDist, maxYDist);
295
- if (dist < minDistance) {
296
- minDistance = dist;
297
- key = candidate.key;
298
- }
299
- }
300
- } else {
301
- let searchRect = new Rect(Math.max(0, x - this.gap.width), y, this.gap.width * 2, 1);
302
- let candidates = this.getVisibleLayoutInfos(searchRect);
303
- let minDistance = Infinity;
304
- for (let candidate of candidates) {
305
- // Ignore items outside the search rect, e.g. persisted keys.
306
- if (!candidate.rect.intersects(searchRect)) {
307
- continue;
308
- }
309
-
310
- let xDist = Math.abs(candidate.rect.x - x);
311
- let maxXDist = Math.abs(candidate.rect.maxX - x);
312
- let dist = Math.min(xDist, maxXDist);
313
- if (dist < minDistance) {
314
- minDistance = dist;
315
- key = candidate.key;
316
- }
317
- }
318
- }
319
-
320
- let layoutInfo = key != null ? this.getLayoutInfo(key) : null;
321
- if (!layoutInfo) {
322
- return {type: 'root'};
323
- }
324
-
325
- let target: DropTarget = {
326
- type: 'item',
327
- key: layoutInfo.key,
328
- dropPosition: 'on'
329
- };
330
-
331
- let pos = this.numColumns === 1 ? y : x;
332
- let layoutInfoPos = this.numColumns === 1 ? layoutInfo.rect.y : layoutInfo.rect.x;
333
- let size = this.numColumns === 1 ? layoutInfo.rect.height : layoutInfo.rect.width;
334
- if (isValidDropTarget(target)) {
335
- // If dropping on the item is accepted, try the before/after positions
336
- // if within 5px of the start or end of the item.
337
- if (pos < layoutInfoPos + 5) {
338
- target.dropPosition = 'before';
339
- } else if (pos > layoutInfoPos + size - 5) {
340
- target.dropPosition = 'after';
341
- }
342
- } else {
343
- // If dropping on the item isn't accepted, try the target before or after depending on the position.
344
- let mid = layoutInfoPos + size / 2;
345
- if (pos <= mid && isValidDropTarget({...target, dropPosition: 'before'})) {
346
- target.dropPosition = 'before';
347
- } else if (pos >= mid && isValidDropTarget({...target, dropPosition: 'after'})) {
348
- target.dropPosition = 'after';
349
- }
350
- }
351
-
352
- return target;
353
- }
354
-
355
- getDropTargetLayoutInfo(target: ItemDropTarget): LayoutInfo {
356
- let layoutInfo = this.getLayoutInfo(target.key)!;
357
- let rect: Rect;
358
- if (this.numColumns === 1) {
359
- // Flip from vertical to horizontal if only one column is visible.
360
- rect = new Rect(
361
- layoutInfo.rect.x,
362
- target.dropPosition === 'before'
363
- ? layoutInfo.rect.y - this.gap.height / 2 - this.dropIndicatorThickness / 2
364
- : layoutInfo.rect.maxY + this.gap.height / 2 - this.dropIndicatorThickness / 2,
365
- layoutInfo.rect.width,
366
- this.dropIndicatorThickness
367
- );
368
- } else {
369
- rect = new Rect(
370
- target.dropPosition === 'before'
371
- ? layoutInfo.rect.x - this.gap.width / 2 - this.dropIndicatorThickness / 2
372
- : layoutInfo.rect.maxX + this.gap.width / 2 - this.dropIndicatorThickness / 2,
373
- layoutInfo.rect.y,
374
- this.dropIndicatorThickness,
375
- layoutInfo.rect.height
376
- );
377
- }
378
-
379
- return new LayoutInfo('dropIndicator', target.key + ':' + target.dropPosition, rect);
380
- }
381
- }