@react-stately/virtualizer 3.7.2-nightly.4649 → 3.7.2-nightly.4656

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 (55) hide show
  1. package/dist/Layout.main.js +2 -41
  2. package/dist/Layout.main.js.map +1 -1
  3. package/dist/Layout.mjs +2 -41
  4. package/dist/Layout.module.js +2 -41
  5. package/dist/Layout.module.js.map +1 -1
  6. package/dist/LayoutInfo.main.js.map +1 -1
  7. package/dist/LayoutInfo.module.js.map +1 -1
  8. package/dist/OverscanManager.main.js +8 -43
  9. package/dist/OverscanManager.main.js.map +1 -1
  10. package/dist/OverscanManager.mjs +8 -43
  11. package/dist/OverscanManager.module.js +8 -43
  12. package/dist/OverscanManager.module.js.map +1 -1
  13. package/dist/ReusableView.main.js +23 -0
  14. package/dist/ReusableView.main.js.map +1 -1
  15. package/dist/ReusableView.mjs +23 -0
  16. package/dist/ReusableView.module.js +23 -0
  17. package/dist/ReusableView.module.js.map +1 -1
  18. package/dist/Virtualizer.main.js +123 -710
  19. package/dist/Virtualizer.main.js.map +1 -1
  20. package/dist/Virtualizer.mjs +124 -711
  21. package/dist/Virtualizer.module.js +124 -711
  22. package/dist/Virtualizer.module.js.map +1 -1
  23. package/dist/types.d.ts +64 -225
  24. package/dist/types.d.ts.map +1 -1
  25. package/dist/useVirtualizerState.main.js +39 -40
  26. package/dist/useVirtualizerState.main.js.map +1 -1
  27. package/dist/useVirtualizerState.mjs +40 -41
  28. package/dist/useVirtualizerState.module.js +40 -41
  29. package/dist/useVirtualizerState.module.js.map +1 -1
  30. package/dist/utils.main.js +1 -27
  31. package/dist/utils.main.js.map +1 -1
  32. package/dist/utils.mjs +2 -26
  33. package/dist/utils.module.js +2 -26
  34. package/dist/utils.module.js.map +1 -1
  35. package/package.json +4 -4
  36. package/src/Layout.ts +10 -55
  37. package/src/LayoutInfo.ts +2 -2
  38. package/src/OverscanManager.ts +10 -47
  39. package/src/ReusableView.ts +36 -7
  40. package/src/Virtualizer.ts +163 -1058
  41. package/src/types.ts +16 -38
  42. package/src/useVirtualizerState.ts +40 -39
  43. package/src/utils.ts +0 -52
  44. package/dist/Transaction.main.js +0 -32
  45. package/dist/Transaction.main.js.map +0 -1
  46. package/dist/Transaction.mjs +0 -27
  47. package/dist/Transaction.module.js +0 -27
  48. package/dist/Transaction.module.js.map +0 -1
  49. package/dist/tween.main.js +0 -67
  50. package/dist/tween.main.js.map +0 -1
  51. package/dist/tween.mjs +0 -61
  52. package/dist/tween.module.js +0 -61
  53. package/dist/tween.module.js.map +0 -1
  54. package/src/Transaction.ts +0 -28
  55. package/src/tween.ts +0 -83
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/virtualizer",
3
- "version": "3.7.2-nightly.4649+02f242f41",
3
+ "version": "3.7.2-nightly.4656+0c17289de",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,8 +22,8 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-aria/utils": "3.0.0-nightly.2937+02f242f41",
26
- "@react-types/shared": "3.0.0-nightly.2937+02f242f41",
25
+ "@react-aria/utils": "3.0.0-nightly.2944+0c17289de",
26
+ "@react-types/shared": "3.0.0-nightly.2944+0c17289de",
27
27
  "@swc/helpers": "^0.5.0"
28
28
  },
29
29
  "peerDependencies": {
@@ -32,5 +32,5 @@
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  },
35
- "gitHead": "02f242f41f0347b3f11e50b019c197127b7d0a08"
35
+ "gitHead": "0c17289de18041e6f8b99df26a5a3ca922cc5145"
36
36
  }
package/src/Layout.ts CHANGED
@@ -13,17 +13,15 @@
13
13
  import {InvalidationContext} from './types';
14
14
  import {Key} from '@react-types/shared';
15
15
  import {LayoutInfo} from './LayoutInfo';
16
- // import {Point} from './Point';
17
16
  import {Rect} from './Rect';
18
17
  import {Size} from './Size';
19
18
  import {Virtualizer} from './Virtualizer';
20
- // import { DragTarget, DropTarget } from '@react-types/shared';
21
19
 
22
20
  /**
23
- * [CollectionView]{@link CollectionView} supports arbitrary layout objects, which compute what views are visible, and how
21
+ * [Virtualizer]{@link Virtualizer} supports arbitrary layout objects, which compute what views are visible, and how
24
22
  * to position and style them. However, layouts do not create the views themselves directly. Instead,
25
23
  * layouts produce lightweight {@link LayoutInfo} objects which describe various properties of a view,
26
- * such as its position and size. The {@link CollectionView} is then responsible for creating the actual
24
+ * such as its position and size. The {@link Virtualizer} is then responsible for creating the actual
27
25
  * views as needed, based on this layout information.
28
26
  *
29
27
  * Every layout extends from the {@link Layout} abstract base class. Layouts must implement a minimum of the
@@ -32,14 +30,14 @@ import {Virtualizer} from './Virtualizer';
32
30
  * @see {@link getVisibleLayoutInfos}
33
31
  * @see {@link getLayoutInfo}
34
32
  */
35
- export abstract class Layout<T extends object> {
36
- /** The CollectionView the layout is currently attached to. */
33
+ export abstract class Layout<T extends object, O = any> {
34
+ /** The Virtualizer the layout is currently attached to. */
37
35
  virtualizer: Virtualizer<T, any, any>;
38
36
 
39
37
  /**
40
38
  * Returns whether the layout should invalidate in response to
41
39
  * visible rectangle changes. By default, it only invalidates
42
- * when the collection view's size changes. Return true always
40
+ * when the virtualizer's size changes. Return true always
43
41
  * to make the layout invalidate while scrolling (e.g. sticky headers).
44
42
  */
45
43
  shouldInvalidate(newRect: Rect, oldRect: Rect): boolean {
@@ -51,10 +49,10 @@ export abstract class Layout<T extends object> {
51
49
  /**
52
50
  * This method allows the layout to perform any pre-computation
53
51
  * it needs to in order to prepare {@link LayoutInfo}s for retrieval.
54
- * Called by the collection view before {@link getVisibleLayoutInfos}
52
+ * Called by the virtualizer before {@link getVisibleLayoutInfos}
55
53
  * or {@link getLayoutInfo} are called.
56
54
  */
57
- validate(invalidationContext: InvalidationContext<T, any>) {} // eslint-disable-line @typescript-eslint/no-unused-vars
55
+ validate(invalidationContext: InvalidationContext<O>) {} // eslint-disable-line @typescript-eslint/no-unused-vars
58
56
 
59
57
  /**
60
58
  * Returns an array of {@link LayoutInfo} objects which are inside the given rectangle.
@@ -75,51 +73,8 @@ export abstract class Layout<T extends object> {
75
73
  */
76
74
  abstract getContentSize(): Size;
77
75
 
78
- /**
79
- * Returns a {@link DragTarget} describing a view at the given point to be dragged.
80
- * Return `null` to cancel the drag. The default implementation returns the view at the given point.
81
- * @param point The point at which the drag occurred.
76
+ /**
77
+ * Updates the size of the given item.
82
78
  */
83
- // getDragTarget(point: Point): DragTarget | null {
84
- // let target = this.virtualizer.keyAtPoint(point);
85
- // if (!target) {
86
- // return null;
87
- // }
88
-
89
- // return {
90
- // type: 'item',
91
- // key: target
92
- // };
93
- // }
94
-
95
- /**
96
- * Returns a {@link DragTarget} object describing where a drop should occur. Return `null`
97
- * to reject the drop. The dropped items will be inserted before the resulting target.
98
- * @param point The point at which the drop occurred.
99
- */
100
- // getDropTarget(point: Point): DropTarget | null {
101
- // return null;
102
- // }
103
-
104
- /**
105
- * Returns the starting attributes for an animated insertion.
106
- * The view is animated from this {@link LayoutInfo} to the one returned by {@link getLayoutInfo}.
107
- * The default implementation just returns its input.
108
- *
109
- * @param layoutInfo The proposed LayoutInfo for this view.
110
- */
111
- getInitialLayoutInfo(layoutInfo: LayoutInfo): LayoutInfo {
112
- return layoutInfo;
113
- }
114
-
115
- /**
116
- * Returns the ending attributes for an animated removal.
117
- * The view is animated from the {@link LayoutInfo} returned by {@link getLayoutInfo}
118
- * to the one returned by this method. The default implementation returns its input.
119
- *
120
- * @param layoutInfo The original LayoutInfo for this view.
121
- */
122
- getFinalLayoutInfo(layoutInfo: LayoutInfo): LayoutInfo {
123
- return layoutInfo;
124
- }
79
+ updateItemSize?(key: Key, size: Size): boolean;
125
80
  }
package/src/LayoutInfo.ts CHANGED
@@ -15,9 +15,9 @@ import {Rect} from './Rect';
15
15
 
16
16
  /**
17
17
  * Instances of this lightweight class are created by {@link Layout} subclasses
18
- * to represent each view in the {@link CollectionView}. LayoutInfo objects describe
18
+ * to represent each view in the {@link Virtualizer}. LayoutInfo objects describe
19
19
  * various properties of a view, such as its position and size, and style information.
20
- * The collection view uses this information when creating actual views to display.
20
+ * The virtualizer uses this information when creating actual views to display.
21
21
  */
22
22
  export class LayoutInfo {
23
23
  /**
@@ -13,30 +13,14 @@
13
13
  import {Point} from './Point';
14
14
  import {Rect} from './Rect';
15
15
 
16
- class RollingAverage {
17
- private count: number = 0;
18
- value: number = 0;
19
-
20
- addSample(sample: number) {
21
- this.count++;
22
- this.value += (sample - this.value) / this.count;
23
- }
24
- }
25
-
26
16
  export class OverscanManager {
27
17
  private startTime = 0;
28
- private averagePerf = new RollingAverage();
29
- private averageTime = new RollingAverage();
30
- private velocity = new Point(5, 5);
31
- private overscanX = new RollingAverage();
32
- private overscanY = new RollingAverage();
18
+ private velocity = new Point(0, 0);
33
19
  private visibleRect = new Rect();
34
20
 
35
21
  setVisibleRect(rect: Rect) {
36
22
  let time = performance.now() - this.startTime;
37
23
  if (time < 500) {
38
- this.averageTime.addSample(time);
39
-
40
24
  if (rect.x !== this.visibleRect.x && time > 0) {
41
25
  this.velocity.x = (rect.x - this.visibleRect.x) / time;
42
26
  }
@@ -50,42 +34,21 @@ export class OverscanManager {
50
34
  this.visibleRect = rect;
51
35
  }
52
36
 
53
- collectMetrics() {
54
- let time = performance.now() - this.startTime;
55
- if (time < 500) {
56
- this.averagePerf.addSample(time);
57
- }
58
-
59
- if (this.visibleRect.height > 0) {
60
- let o = Math.abs(this.velocity.y * (this.averageTime.value + this.averagePerf.value));
61
- this.overscanY.addSample(o);
62
- }
63
-
64
- if (this.visibleRect.width > 0) {
65
- let o = Math.abs(this.velocity.x * (this.averageTime.value + this.averagePerf.value));
66
- this.overscanX.addSample(o);
67
- }
68
- }
69
-
70
37
  getOverscannedRect() {
71
38
  let overscanned = this.visibleRect.copy();
72
39
 
73
- let overscanY = Math.round(Math.min(this.visibleRect.height * 2, this.overscanY.value) / 100) * 100;
74
- if (this.velocity.y > 0) {
75
- overscanned.y -= overscanY * 0.2;
76
- overscanned.height += overscanY + overscanY * 0.2;
77
- } else {
40
+ let overscanY = this.visibleRect.height / 3;
41
+ overscanned.height += overscanY;
42
+ if (this.velocity.y < 0) {
78
43
  overscanned.y -= overscanY;
79
- overscanned.height += overscanY + overscanY * 0.2;
80
44
  }
81
45
 
82
- let overscanX = Math.round(Math.min(this.visibleRect.width * 2, this.overscanX.value) / 100) * 100;
83
- if (this.velocity.x > 0) {
84
- overscanned.x -= overscanX * 0.2;
85
- overscanned.width += overscanX + overscanX * 0.2;
86
- } else {
87
- overscanned.x -= overscanX;
88
- overscanned.width += overscanX + overscanX * 0.2;
46
+ if (this.velocity.x !== 0) {
47
+ let overscanX = this.visibleRect.width / 3;
48
+ overscanned.width += overscanX;
49
+ if (this.velocity.x < 0) {
50
+ overscanned.x -= overscanX;
51
+ }
89
52
  }
90
53
 
91
54
  return overscanned;
@@ -17,20 +17,17 @@ import {Virtualizer} from './Virtualizer';
17
17
  let KEY = 0;
18
18
 
19
19
  /**
20
- * [CollectionView]{@link CollectionView} creates instances of the [ReusableView]{@link ReusableView} class to
21
- * represent views currently being displayed. ReusableViews manage a DOM node, handle
22
- * applying {@link LayoutInfo} objects to the view, and render content
23
- * as needed. Subclasses must implement the {@link render} method at a
24
- * minimum. Other methods can be overridden to customize behavior.
20
+ * [Virtualizer]{@link Virtualizer} creates instances of the [ReusableView]{@link ReusableView} class to
21
+ * represent views currently being displayed.
25
22
  */
26
23
  export class ReusableView<T extends object, V> {
27
- /** The CollectionVirtualizer this view is a part of. */
24
+ /** The Virtualizer this view is a part of. */
28
25
  virtualizer: Virtualizer<T, V, unknown>;
29
26
 
30
27
  /** The LayoutInfo this view is currently representing. */
31
28
  layoutInfo: LayoutInfo | null;
32
29
 
33
- /** The content currently being displayed by this view, set by the collection view. */
30
+ /** The content currently being displayed by this view, set by the virtualizer. */
34
31
  content: T;
35
32
 
36
33
  rendered: V;
@@ -38,9 +35,16 @@ export class ReusableView<T extends object, V> {
38
35
  viewType: string;
39
36
  key: Key;
40
37
 
38
+ parent: ReusableView<T, V> | null;
39
+ children: Set<ReusableView<T, V>>;
40
+ reusableViews: Map<string, ReusableView<T, V>[]>;
41
+
41
42
  constructor(virtualizer: Virtualizer<T, V, unknown>) {
42
43
  this.virtualizer = virtualizer;
43
44
  this.key = ++KEY;
45
+ this.parent = null;
46
+ this.children = new Set();
47
+ this.reusableViews = new Map();
44
48
  }
45
49
 
46
50
  /**
@@ -51,4 +55,29 @@ export class ReusableView<T extends object, V> {
51
55
  this.rendered = null;
52
56
  this.layoutInfo = null;
53
57
  }
58
+
59
+ getReusableView(reuseType: string) {
60
+ // Reusable view queue should be FIFO so that DOM order remains consistent during scrolling.
61
+ // For example, cells within a row should remain in the same order even if the row changes contents.
62
+ // The cells within a row are removed from their parent in order. If the row is reused, the cells
63
+ // should be reused in the new row in the same order they were before.
64
+ let reusable = this.reusableViews.get(reuseType);
65
+ let view = reusable?.length > 0
66
+ ? reusable.shift()
67
+ : new ReusableView<T, V>(this.virtualizer);
68
+
69
+ view.viewType = reuseType;
70
+ view.parent = this;
71
+ return view;
72
+ }
73
+
74
+ reuseChild(child: ReusableView<T, V>) {
75
+ child.prepareForReuse();
76
+ let reusable = this.reusableViews.get(child.viewType);
77
+ if (!reusable) {
78
+ reusable = [];
79
+ this.reusableViews.set(child.viewType, reusable);
80
+ }
81
+ reusable.push(child);
82
+ }
54
83
  }