react-window 1.8.2 → 1.8.6

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/dist/index.esm.js CHANGED
@@ -60,7 +60,7 @@ var cachedRTLResult = null; // TRICKY According to the spec, scrollLeft should b
60
60
  // and then verify that the subsequent "scroll" event matches the negative offset.
61
61
  // If it does not match, then we can assume a non-standard RTL scroll implementation.
62
62
 
63
- function isRTLOffsetNegative(recalculate) {
63
+ function getRTLOffsetType(recalculate) {
64
64
  if (recalculate === void 0) {
65
65
  recalculate = false;
66
66
  }
@@ -78,8 +78,19 @@ function isRTLOffsetNegative(recalculate) {
78
78
  innerStyle.height = '100px';
79
79
  outerDiv.appendChild(innerDiv);
80
80
  document.body.appendChild(outerDiv);
81
- outerDiv.scrollLeft = -10;
82
- cachedRTLResult = outerDiv.scrollLeft === -10;
81
+
82
+ if (outerDiv.scrollLeft > 0) {
83
+ cachedRTLResult = 'positive-descending';
84
+ } else {
85
+ outerDiv.scrollLeft = 1;
86
+
87
+ if (outerDiv.scrollLeft === 0) {
88
+ cachedRTLResult = 'negative';
89
+ } else {
90
+ cachedRTLResult = 'positive-ascending';
91
+ }
92
+ }
93
+
83
94
  document.body.removeChild(outerDiv);
84
95
  return cachedRTLResult;
85
96
  }
@@ -197,11 +208,17 @@ function createGridComponent(_ref2) {
197
208
  if (itemStyleCache.hasOwnProperty(key)) {
198
209
  style = itemStyleCache[key];
199
210
  } else {
200
- var _style;
201
-
202
- itemStyleCache[key] = style = (_style = {
203
- position: 'absolute'
204
- }, _style[direction === 'rtl' ? 'right' : 'left'] = getColumnOffset(_this.props, columnIndex, _this._instanceProps), _style.top = getRowOffset(_this.props, rowIndex, _this._instanceProps), _style.height = getRowHeight(_this.props, rowIndex, _this._instanceProps), _style.width = getColumnWidth(_this.props, columnIndex, _this._instanceProps), _style);
211
+ var _offset = getColumnOffset(_this.props, columnIndex, _this._instanceProps);
212
+
213
+ var isRtl = direction === 'rtl';
214
+ itemStyleCache[key] = style = {
215
+ position: 'absolute',
216
+ left: isRtl ? undefined : _offset,
217
+ right: isRtl ? _offset : undefined,
218
+ top: getRowOffset(_this.props, rowIndex, _this._instanceProps),
219
+ height: getRowHeight(_this.props, rowIndex, _this._instanceProps),
220
+ width: getColumnWidth(_this.props, columnIndex, _this._instanceProps)
221
+ };
205
222
  }
206
223
 
207
224
  return style;
@@ -229,19 +246,22 @@ function createGridComponent(_ref2) {
229
246
  return null;
230
247
  }
231
248
 
232
- var direction = _this.props.direction;
249
+ var direction = _this.props.direction; // TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
250
+ // This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
251
+ // It's also easier for this component if we convert offsets to the same format as they would be in for ltr.
252
+ // So the simplest solution is to determine which browser behavior we're dealing with, and convert based on it.
253
+
233
254
  var calculatedScrollLeft = scrollLeft;
234
255
 
235
256
  if (direction === 'rtl') {
236
- var isNegative = isRTLOffsetNegative(); // TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
237
- // This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
238
- // It's also easier for this component if we convert offsets to the same format as they would be in for ltr.
239
- // So the simplest solution is to determine which browser behavior we're dealing with, and convert based on it.
240
-
241
- if (isNegative) {
242
- calculatedScrollLeft = -scrollLeft;
243
- } else {
244
- calculatedScrollLeft = scrollWidth - clientWidth - scrollLeft;
257
+ switch (getRTLOffsetType()) {
258
+ case 'negative':
259
+ calculatedScrollLeft = -scrollLeft;
260
+ break;
261
+
262
+ case 'positive-descending':
263
+ calculatedScrollLeft = scrollWidth - clientWidth - scrollLeft;
264
+ break;
245
265
  }
246
266
  } // Prevent Safari's elastic scrolling from causing visual shaking when scrolling past bounds.
247
267
 
@@ -406,14 +426,20 @@ function createGridComponent(_ref2) {
406
426
  var outerRef = this._outerRef;
407
427
 
408
428
  if (direction === 'rtl') {
409
- var isNegative = isRTLOffsetNegative();
429
+ switch (getRTLOffsetType()) {
430
+ case 'negative':
431
+ outerRef.scrollLeft = -scrollLeft;
432
+ break;
410
433
 
411
- if (isNegative) {
412
- outerRef.scrollLeft = -scrollLeft;
413
- } else {
414
- var clientWidth = outerRef.clientWidth,
415
- scrollWidth = outerRef.scrollWidth;
416
- outerRef.scrollLeft = scrollWidth - clientWidth - scrollLeft;
434
+ case 'positive-ascending':
435
+ outerRef.scrollLeft = scrollLeft;
436
+ break;
437
+
438
+ default:
439
+ var clientWidth = outerRef.clientWidth,
440
+ scrollWidth = outerRef.scrollWidth;
441
+ outerRef.scrollLeft = scrollWidth - clientWidth - scrollLeft;
442
+ break;
417
443
  }
418
444
  } else {
419
445
  outerRef.scrollLeft = Math.max(0, scrollLeft);
@@ -842,7 +868,11 @@ var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(itemT
842
868
  default:
843
869
  if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
844
870
  return scrollOffset;
845
- } else if (scrollOffset - minOffset < maxOffset - scrollOffset) {
871
+ } else if (minOffset > maxOffset) {
872
+ // Because we only take into account the scrollbar size when calculating minOffset
873
+ // this value can be larger than maxOffset when at the end of the list
874
+ return minOffset;
875
+ } else if (scrollOffset < minOffset) {
846
876
  return minOffset;
847
877
  } else {
848
878
  return maxOffset;
@@ -1075,16 +1105,21 @@ function createListComponent(_ref) {
1075
1105
  if (itemStyleCache.hasOwnProperty(index)) {
1076
1106
  style = itemStyleCache[index];
1077
1107
  } else {
1078
- var _style;
1079
-
1080
1108
  var _offset = getItemOffset(_this.props, index, _this._instanceProps);
1081
1109
 
1082
1110
  var size = getItemSize(_this.props, index, _this._instanceProps); // TODO Deprecate direction "horizontal"
1083
1111
 
1084
1112
  var isHorizontal = direction === 'horizontal' || layout === 'horizontal';
1085
- itemStyleCache[index] = style = (_style = {
1086
- position: 'absolute'
1087
- }, _style[direction === 'rtl' ? 'right' : 'left'] = isHorizontal ? _offset : 0, _style.top = !isHorizontal ? _offset : 0, _style.height = !isHorizontal ? size : '100%', _style.width = isHorizontal ? size : '100%', _style);
1113
+ var isRtl = direction === 'rtl';
1114
+ var offsetHorizontal = isHorizontal ? _offset : 0;
1115
+ itemStyleCache[index] = style = {
1116
+ position: 'absolute',
1117
+ left: isRtl ? undefined : offsetHorizontal,
1118
+ right: isRtl ? offsetHorizontal : undefined,
1119
+ top: !isHorizontal ? _offset : 0,
1120
+ height: !isHorizontal ? size : '100%',
1121
+ width: isHorizontal ? size : '100%'
1122
+ };
1088
1123
  }
1089
1124
 
1090
1125
  return style;
@@ -1113,15 +1148,18 @@ function createListComponent(_ref) {
1113
1148
  var scrollOffset = scrollLeft;
1114
1149
 
1115
1150
  if (direction === 'rtl') {
1116
- var isNegative = isRTLOffsetNegative(); // TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
1151
+ // TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
1117
1152
  // This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
1118
1153
  // It's also easier for this component if we convert offsets to the same format as they would be in for ltr.
1119
1154
  // So the simplest solution is to determine which browser behavior we're dealing with, and convert based on it.
1120
-
1121
- if (isNegative) {
1122
- scrollOffset = -scrollLeft;
1123
- } else {
1124
- scrollOffset = scrollWidth - clientWidth - scrollLeft;
1155
+ switch (getRTLOffsetType()) {
1156
+ case 'negative':
1157
+ scrollOffset = -scrollLeft;
1158
+ break;
1159
+
1160
+ case 'positive-descending':
1161
+ scrollOffset = scrollWidth - clientWidth - scrollLeft;
1162
+ break;
1125
1163
  }
1126
1164
  } // Prevent Safari's elastic scrolling from causing visual shaking when scrolling past bounds.
1127
1165
 
@@ -1264,14 +1302,20 @@ function createListComponent(_ref) {
1264
1302
  // TRICKY According to the spec, scrollLeft should be negative for RTL aligned elements.
1265
1303
  // This is not the case for all browsers though (e.g. Chrome reports values as positive, measured relative to the left).
1266
1304
  // So we need to determine which browser behavior we're dealing with, and mimic it.
1267
- var isNegative = isRTLOffsetNegative();
1268
-
1269
- if (isNegative) {
1270
- outerRef.scrollLeft = -scrollOffset;
1271
- } else {
1272
- var clientWidth = outerRef.clientWidth,
1273
- scrollWidth = outerRef.scrollWidth;
1274
- outerRef.scrollLeft = scrollWidth - clientWidth - scrollOffset;
1305
+ switch (getRTLOffsetType()) {
1306
+ case 'negative':
1307
+ outerRef.scrollLeft = -scrollOffset;
1308
+ break;
1309
+
1310
+ case 'positive-ascending':
1311
+ outerRef.scrollLeft = scrollOffset;
1312
+ break;
1313
+
1314
+ default:
1315
+ var clientWidth = outerRef.clientWidth,
1316
+ scrollWidth = outerRef.scrollWidth;
1317
+ outerRef.scrollLeft = scrollWidth - clientWidth - scrollOffset;
1318
+ break;
1275
1319
  }
1276
1320
  } else {
1277
1321
  outerRef.scrollLeft = scrollOffset;
@@ -1635,7 +1679,7 @@ createListComponent({
1635
1679
  default:
1636
1680
  if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
1637
1681
  return scrollOffset;
1638
- } else if (scrollOffset - minOffset < maxOffset - scrollOffset) {
1682
+ } else if (scrollOffset < minOffset) {
1639
1683
  return minOffset;
1640
1684
  } else {
1641
1685
  return maxOffset;
@@ -1740,7 +1784,8 @@ createGridComponent({
1740
1784
  var columnCount = _ref7.columnCount,
1741
1785
  columnWidth = _ref7.columnWidth,
1742
1786
  width = _ref7.width;
1743
- var maxOffset = Math.max(0, Math.min(columnCount * columnWidth - width, columnIndex * columnWidth));
1787
+ var lastColumnOffset = Math.max(0, columnCount * columnWidth - width);
1788
+ var maxOffset = Math.min(lastColumnOffset, columnIndex * columnWidth);
1744
1789
  var minOffset = Math.max(0, columnIndex * columnWidth - width + scrollbarSize + columnWidth);
1745
1790
 
1746
1791
  if (align === 'smart') {
@@ -1759,13 +1804,27 @@ createGridComponent({
1759
1804
  return minOffset;
1760
1805
 
1761
1806
  case 'center':
1762
- return Math.round(minOffset + (maxOffset - minOffset) / 2);
1807
+ // "Centered" offset is usually the average of the min and max.
1808
+ // But near the edges of the list, this doesn't hold true.
1809
+ var middleOffset = Math.round(minOffset + (maxOffset - minOffset) / 2);
1810
+
1811
+ if (middleOffset < Math.ceil(width / 2)) {
1812
+ return 0; // near the beginning
1813
+ } else if (middleOffset > lastColumnOffset + Math.floor(width / 2)) {
1814
+ return lastColumnOffset; // near the end
1815
+ } else {
1816
+ return middleOffset;
1817
+ }
1763
1818
 
1764
1819
  case 'auto':
1765
1820
  default:
1766
1821
  if (scrollLeft >= minOffset && scrollLeft <= maxOffset) {
1767
1822
  return scrollLeft;
1768
- } else if (scrollLeft - minOffset < maxOffset - scrollLeft) {
1823
+ } else if (minOffset > maxOffset) {
1824
+ // Because we only take into account the scrollbar size when calculating minOffset
1825
+ // this value can be larger than maxOffset when at the end of the list
1826
+ return minOffset;
1827
+ } else if (scrollLeft < minOffset) {
1769
1828
  return minOffset;
1770
1829
  } else {
1771
1830
  return maxOffset;
@@ -1777,7 +1836,8 @@ createGridComponent({
1777
1836
  var rowHeight = _ref8.rowHeight,
1778
1837
  height = _ref8.height,
1779
1838
  rowCount = _ref8.rowCount;
1780
- var maxOffset = Math.max(0, Math.min(rowCount * rowHeight - height, rowIndex * rowHeight));
1839
+ var lastRowOffset = Math.max(0, rowCount * rowHeight - height);
1840
+ var maxOffset = Math.min(lastRowOffset, rowIndex * rowHeight);
1781
1841
  var minOffset = Math.max(0, rowIndex * rowHeight - height + scrollbarSize + rowHeight);
1782
1842
 
1783
1843
  if (align === 'smart') {
@@ -1796,13 +1856,27 @@ createGridComponent({
1796
1856
  return minOffset;
1797
1857
 
1798
1858
  case 'center':
1799
- return Math.round(minOffset + (maxOffset - minOffset) / 2);
1859
+ // "Centered" offset is usually the average of the min and max.
1860
+ // But near the edges of the list, this doesn't hold true.
1861
+ var middleOffset = Math.round(minOffset + (maxOffset - minOffset) / 2);
1862
+
1863
+ if (middleOffset < Math.ceil(height / 2)) {
1864
+ return 0; // near the beginning
1865
+ } else if (middleOffset > lastRowOffset + Math.floor(height / 2)) {
1866
+ return lastRowOffset; // near the end
1867
+ } else {
1868
+ return middleOffset;
1869
+ }
1800
1870
 
1801
1871
  case 'auto':
1802
1872
  default:
1803
1873
  if (scrollTop >= minOffset && scrollTop <= maxOffset) {
1804
1874
  return scrollTop;
1805
- } else if (scrollTop - minOffset < maxOffset - scrollTop) {
1875
+ } else if (minOffset > maxOffset) {
1876
+ // Because we only take into account the scrollbar size when calculating minOffset
1877
+ // this value can be larger than maxOffset when at the end of the list
1878
+ return minOffset;
1879
+ } else if (scrollTop < minOffset) {
1806
1880
  return minOffset;
1807
1881
  } else {
1808
1882
  return maxOffset;
@@ -1820,7 +1894,9 @@ createGridComponent({
1820
1894
  columnCount = _ref10.columnCount,
1821
1895
  width = _ref10.width;
1822
1896
  var left = startIndex * columnWidth;
1823
- return Math.max(0, Math.min(columnCount - 1, startIndex + Math.floor((width + (scrollLeft - left)) / columnWidth)));
1897
+ var numVisibleColumns = Math.ceil((width + scrollLeft - left) / columnWidth);
1898
+ return Math.max(0, Math.min(columnCount - 1, startIndex + numVisibleColumns - 1 // -1 is because stop index is inclusive
1899
+ ));
1824
1900
  },
1825
1901
  getRowStartIndexForOffset: function getRowStartIndexForOffset(_ref11, scrollTop) {
1826
1902
  var rowHeight = _ref11.rowHeight,
@@ -1831,8 +1907,10 @@ createGridComponent({
1831
1907
  var rowHeight = _ref12.rowHeight,
1832
1908
  rowCount = _ref12.rowCount,
1833
1909
  height = _ref12.height;
1834
- var left = startIndex * rowHeight;
1835
- return Math.max(0, Math.min(rowCount - 1, startIndex + Math.floor((height + (scrollTop - left)) / rowHeight)));
1910
+ var top = startIndex * rowHeight;
1911
+ var numVisibleRows = Math.ceil((height + scrollTop - top) / rowHeight);
1912
+ return Math.max(0, Math.min(rowCount - 1, startIndex + numVisibleRows - 1 // -1 is because stop index is inclusive
1913
+ ));
1836
1914
  },
1837
1915
  initInstanceProps: function initInstanceProps(props) {// Noop
1838
1916
  },
@@ -1857,13 +1935,11 @@ var FixedSizeList =
1857
1935
  /*#__PURE__*/
1858
1936
  createListComponent({
1859
1937
  getItemOffset: function getItemOffset(_ref, index) {
1860
- var itemSize = _ref.itemSize,
1861
- size = _ref.size;
1938
+ var itemSize = _ref.itemSize;
1862
1939
  return index * itemSize;
1863
1940
  },
1864
1941
  getItemSize: function getItemSize(_ref2, index) {
1865
- var itemSize = _ref2.itemSize,
1866
- size = _ref2.size;
1942
+ var itemSize = _ref2.itemSize;
1867
1943
  return itemSize;
1868
1944
  },
1869
1945
  getEstimatedTotalSize: function getEstimatedTotalSize(_ref3) {
@@ -1881,7 +1957,8 @@ createListComponent({
1881
1957
  // TODO Deprecate direction "horizontal"
1882
1958
  var isHorizontal = direction === 'horizontal' || layout === 'horizontal';
1883
1959
  var size = isHorizontal ? width : height;
1884
- var maxOffset = Math.max(0, Math.min(itemCount * itemSize - size, index * itemSize));
1960
+ var lastItemOffset = Math.max(0, itemCount * itemSize - size);
1961
+ var maxOffset = Math.min(lastItemOffset, index * itemSize);
1885
1962
  var minOffset = Math.max(0, index * itemSize - size + itemSize);
1886
1963
 
1887
1964
  if (align === 'smart') {
@@ -1900,13 +1977,25 @@ createListComponent({
1900
1977
  return minOffset;
1901
1978
 
1902
1979
  case 'center':
1903
- return Math.round(minOffset + (maxOffset - minOffset) / 2);
1980
+ {
1981
+ // "Centered" offset is usually the average of the min and max.
1982
+ // But near the edges of the list, this doesn't hold true.
1983
+ var middleOffset = Math.round(minOffset + (maxOffset - minOffset) / 2);
1984
+
1985
+ if (middleOffset < Math.ceil(size / 2)) {
1986
+ return 0; // near the beginning
1987
+ } else if (middleOffset > lastItemOffset + Math.floor(size / 2)) {
1988
+ return lastItemOffset; // near the end
1989
+ } else {
1990
+ return middleOffset;
1991
+ }
1992
+ }
1904
1993
 
1905
1994
  case 'auto':
1906
1995
  default:
1907
1996
  if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
1908
1997
  return scrollOffset;
1909
- } else if (scrollOffset - minOffset < maxOffset - scrollOffset) {
1998
+ } else if (scrollOffset < minOffset) {
1910
1999
  return minOffset;
1911
2000
  } else {
1912
2001
  return maxOffset;
@@ -1930,7 +2019,9 @@ createListComponent({
1930
2019
  var isHorizontal = direction === 'horizontal' || layout === 'horizontal';
1931
2020
  var offset = startIndex * itemSize;
1932
2021
  var size = isHorizontal ? width : height;
1933
- return Math.max(0, Math.min(itemCount - 1, startIndex + Math.floor((size + (scrollOffset - offset)) / itemSize)));
2022
+ var numVisibleItems = Math.ceil((size + scrollOffset - offset) / itemSize);
2023
+ return Math.max(0, Math.min(itemCount - 1, startIndex + numVisibleItems - 1 // -1 is because stop index is inclusive
2024
+ ));
1934
2025
  },
1935
2026
  initInstanceProps: function initInstanceProps(props) {// Noop
1936
2027
  },
@@ -1985,3 +2076,4 @@ function shouldComponentUpdate(nextProps, nextState) {
1985
2076
  }
1986
2077
 
1987
2078
  export { VariableSizeGrid, VariableSizeList, FixedSizeGrid, FixedSizeList, areEqual, shouldComponentUpdate };
2079
+ //# sourceMappingURL=index.esm.js.map