react-window 2.0.0-alpha.3 → 2.0.0-alpha.4

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.
@@ -1,7 +1,7 @@
1
1
  import { jsx as j } from "react/jsx-runtime";
2
- import { useLayoutEffect as J, useEffect as W, useMemo as H, useState as L, useCallback as P, memo as V, useImperativeHandle as F, createElement as N, useRef as Y } from "react";
3
- const q = typeof window < "u" ? J : W;
4
- function B(t) {
2
+ import { useLayoutEffect as D, useEffect as W, useMemo as k, useState as L, useRef as B, useCallback as P, memo as N, useImperativeHandle as Y, createElement as q } from "react";
3
+ const U = typeof window < "u" ? D : W;
4
+ function V(t) {
5
5
  if (t !== void 0)
6
6
  switch (typeof t) {
7
7
  case "number":
@@ -13,143 +13,143 @@ function B(t) {
13
13
  }
14
14
  }
15
15
  }
16
- function K({
16
+ function J({
17
17
  box: t,
18
- defaultHeight: r,
19
- defaultWidth: l,
18
+ defaultHeight: o,
19
+ defaultWidth: n,
20
20
  disabled: s,
21
21
  element: e,
22
22
  mode: f,
23
- style: n
23
+ style: l
24
24
  }) {
25
- const { styleHeight: c, styleWidth: o } = H(
25
+ const { styleHeight: c, styleWidth: r } = k(
26
26
  () => ({
27
- styleHeight: B(n?.height),
28
- styleWidth: B(n?.width)
27
+ styleHeight: V(l?.height),
28
+ styleWidth: V(l?.width)
29
29
  }),
30
- [n?.height, n?.width]
30
+ [l?.height, l?.width]
31
31
  ), [u, w] = L({
32
- height: r,
33
- width: l
34
- }), I = s || f === "only-height" && c !== void 0 || f === "only-width" && o !== void 0 || c !== void 0 && o !== void 0;
35
- return q(() => {
32
+ height: o,
33
+ width: n
34
+ }), I = s || f === "only-height" && c !== void 0 || f === "only-width" && r !== void 0 || c !== void 0 && r !== void 0;
35
+ return U(() => {
36
36
  if (e === null || I)
37
37
  return;
38
38
  const O = new ResizeObserver((S) => {
39
39
  for (const m of S) {
40
- const { contentRect: d, target: b } = m;
41
- e === b && w((a) => a.height === d.height && a.width === d.width ? a : {
42
- height: d.height,
43
- width: d.width
40
+ const { contentRect: a, target: b } = m;
41
+ e === b && w((d) => d.height === a.height && d.width === a.width ? d : {
42
+ height: a.height,
43
+ width: a.width
44
44
  });
45
45
  }
46
46
  });
47
47
  return O.observe(e, { box: t }), () => {
48
48
  O?.unobserve(e);
49
49
  };
50
- }, [t, I, e, c, o]), H(
50
+ }, [t, I, e, c, r]), k(
51
51
  () => ({
52
52
  height: c ?? u.height,
53
- width: o ?? u.width
53
+ width: r ?? u.width
54
54
  }),
55
- [u, c, o]
55
+ [u, c, r]
56
56
  );
57
57
  }
58
- function $(t, r = "Assertion error") {
58
+ function $(t, o = "Assertion error") {
59
59
  if (!t)
60
- throw console.error(r), Error(r);
60
+ throw console.error(o), Error(o);
61
61
  }
62
- function Q({
62
+ function K({
63
63
  cachedBounds: t,
64
- itemCount: r,
65
- itemSize: l
64
+ itemCount: o,
65
+ itemSize: n
66
66
  }) {
67
- if (typeof l == "number")
68
- return r * l;
67
+ if (typeof n == "number")
68
+ return o * n;
69
69
  if (t.size > 0) {
70
70
  const s = t.get(t.size - 1);
71
71
  $(s !== void 0, "Unexpected bounds cache miss");
72
72
  const e = (s.scrollOffset + s.size) / t.size;
73
- return r * e;
73
+ return o * e;
74
74
  } else
75
75
  return 0;
76
76
  }
77
- function X({
77
+ function Q({
78
78
  align: t,
79
- cachedBounds: r,
80
- index: l,
79
+ cachedBounds: o,
80
+ index: n,
81
81
  itemCount: s,
82
82
  containerScrollOffset: e,
83
83
  containerSize: f
84
84
  }) {
85
- const n = r.get(s - 1).scrollOffset, c = n + n - f, o = Math.max(
85
+ const l = o.get(s - 1).scrollOffset, c = l + l - f, r = Math.max(
86
86
  0,
87
- r.get(l + 1).scrollOffset - f
87
+ o.get(n + 1).scrollOffset - f
88
88
  ), u = Math.min(
89
- n,
90
- r.get(l).scrollOffset
89
+ l,
90
+ o.get(n).scrollOffset
91
91
  );
92
- switch (t === "smart" && (e >= o && e <= u ? t = "auto" : t = "center"), t) {
92
+ switch (t === "smart" && (e >= r && e <= u ? t = "auto" : t = "center"), t) {
93
93
  case "start":
94
94
  return Math.min(c, u);
95
95
  case "end":
96
- return o;
96
+ return r;
97
97
  case "center": {
98
98
  const w = Math.round(
99
- o + (u - o) / 2
99
+ r + (u - r) / 2
100
100
  );
101
101
  return w < Math.ceil(f / 2) ? 0 : w > c ? c : w;
102
102
  }
103
103
  case "auto":
104
104
  default:
105
- return e >= o && e <= u ? e : e < o ? o : u;
105
+ return e >= r && e <= u ? e : e < r ? r : u;
106
106
  }
107
107
  }
108
- function Z({
108
+ function X({
109
109
  cachedBounds: t,
110
- containerScrollOffset: r,
111
- containerSize: l,
110
+ containerScrollOffset: o,
111
+ containerSize: n,
112
112
  itemCount: s,
113
113
  overscanCount: e
114
114
  }) {
115
115
  const f = s - 1;
116
- let n = 0, c = -1, o = 0;
117
- for (; o < f; ) {
118
- const u = t.get(o);
119
- if (u.scrollOffset + u.size > r)
116
+ let l = 0, c = -1, r = 0;
117
+ for (; r < f; ) {
118
+ const u = t.get(r);
119
+ if (u.scrollOffset + u.size > o)
120
120
  break;
121
- o++;
121
+ r++;
122
122
  }
123
- for (n = o, n = Math.max(0, n - e); o < f; ) {
124
- const u = t.get(o);
125
- if (u.scrollOffset + u.size >= r + l)
123
+ for (l = r, l = Math.max(0, l - e); r < f; ) {
124
+ const u = t.get(r);
125
+ if (u.scrollOffset + u.size >= o + n)
126
126
  break;
127
- o++;
127
+ r++;
128
128
  }
129
- return c = Math.min(f, o), c = Math.min(s - 1, c + e), n < 0 ? [0, -1] : [n, c];
129
+ return c = Math.min(f, r), c = Math.min(s - 1, c + e), l < 0 ? [0, -1] : [l, c];
130
130
  }
131
- function C({
131
+ function Z({
132
132
  itemCount: t,
133
- itemProps: r,
134
- itemSize: l
133
+ itemProps: o,
134
+ itemSize: n
135
135
  }) {
136
136
  const s = /* @__PURE__ */ new Map();
137
137
  return {
138
138
  get(e) {
139
139
  for ($(e < t, `Invalid index ${e}`); s.size - 1 < e; ) {
140
- const n = s.size, c = typeof l == "number" ? l : l(n, r);
141
- if (n === 0)
142
- s.set(n, {
140
+ const l = s.size, c = typeof n == "number" ? n : n(l, o);
141
+ if (l === 0)
142
+ s.set(l, {
143
143
  size: c,
144
144
  scrollOffset: 0
145
145
  });
146
146
  else {
147
- const o = s.get(n - 1);
147
+ const r = s.get(l - 1);
148
148
  $(
149
- o !== void 0,
149
+ r !== void 0,
150
150
  `Unexpected bounds cache miss for index ${e}`
151
- ), s.set(n, {
152
- scrollOffset: o.scrollOffset + o.size,
151
+ ), s.set(l, {
152
+ scrollOffset: r.scrollOffset + r.size,
153
153
  size: c
154
154
  });
155
155
  }
@@ -170,75 +170,83 @@ function C({
170
170
  }
171
171
  function tt({
172
172
  itemCount: t,
173
- itemProps: r,
174
- itemSize: l
173
+ itemProps: o,
174
+ itemSize: n
175
175
  }) {
176
- return H(
177
- () => C({
176
+ return k(
177
+ () => Z({
178
178
  itemCount: t,
179
- itemProps: r,
180
- itemSize: l
179
+ itemProps: o,
180
+ itemSize: n
181
181
  }),
182
- [t, r, l]
182
+ [t, o, n]
183
183
  );
184
184
  }
185
+ function et(t) {
186
+ const o = B(() => {
187
+ throw new Error("Cannot call an event handler while rendering.");
188
+ });
189
+ return U(() => {
190
+ o.current = t;
191
+ }, [t]), P((n) => o.current?.(n), [o]);
192
+ }
185
193
  function G({
186
194
  containerElement: t,
187
- containerStyle: r,
188
- defaultContainerSize: l = 0,
195
+ containerStyle: o,
196
+ defaultContainerSize: n = 0,
189
197
  direction: s,
190
198
  itemCount: e,
191
199
  itemProps: f,
192
- itemSize: n,
200
+ itemSize: l,
193
201
  overscanCount: c
194
202
  }) {
195
- const [o, u] = L([0, -1]), [w, I] = [
196
- Math.min(e - 1, o[0]),
197
- Math.min(e - 1, o[1])
198
- ], { height: O = l, width: S = l } = K({
199
- defaultHeight: s === "vertical" ? l : void 0,
200
- defaultWidth: s === "horizontal" ? l : void 0,
203
+ const [r, u] = L([0, -1]), [w, I] = [
204
+ Math.min(e - 1, r[0]),
205
+ Math.min(e - 1, r[1])
206
+ ], { height: O = n, width: S = n } = J({
207
+ defaultHeight: s === "vertical" ? n : void 0,
208
+ defaultWidth: s === "horizontal" ? n : void 0,
201
209
  element: t,
202
210
  mode: s === "vertical" ? "only-height" : "only-width",
203
- style: r
204
- }), m = s === "vertical" ? O : S, d = tt({
211
+ style: o
212
+ }), m = s === "vertical" ? O : S, a = tt({
205
213
  itemCount: e,
206
214
  itemProps: f,
207
- itemSize: n
215
+ itemSize: l
208
216
  }), b = P(
209
- (i) => d.get(i),
210
- [d]
211
- ), a = P(
212
- () => Q({
213
- cachedBounds: d,
217
+ (i) => a.get(i),
218
+ [a]
219
+ ), d = P(
220
+ () => K({
221
+ cachedBounds: a,
214
222
  itemCount: e,
215
- itemSize: n
223
+ itemSize: l
216
224
  }),
217
- [d, e, n]
218
- ), M = P(
219
- (i) => Z({
220
- cachedBounds: d,
225
+ [a, e, l]
226
+ ), E = P(
227
+ (i) => X({
228
+ cachedBounds: a,
221
229
  containerScrollOffset: i,
222
230
  containerSize: m,
223
231
  itemCount: e,
224
232
  overscanCount: c
225
233
  }),
226
- [d, m, e, c]
234
+ [a, m, e, c]
227
235
  );
228
- q(() => {
236
+ U(() => {
229
237
  const i = (s === "vertical" ? t?.scrollTop : t?.scrollLeft) ?? 0;
230
- u(M(i));
231
- }, [t, s, M]);
232
- const g = P(
238
+ u(E(i));
239
+ }, [t, s, E]);
240
+ const g = et(
233
241
  ({
234
242
  align: i = "auto",
235
243
  behavior: p = "auto",
236
244
  containerScrollOffset: z,
237
245
  index: x
238
246
  }) => {
239
- const h = X({
247
+ const h = Q({
240
248
  align: i,
241
- cachedBounds: d,
249
+ cachedBounds: a,
242
250
  containerScrollOffset: z,
243
251
  containerSize: m,
244
252
  index: x,
@@ -251,14 +259,13 @@ function G({
251
259
  behavior: p || void 0,
252
260
  top: h
253
261
  });
254
- },
255
- [d, t, m, s, e]
262
+ }
256
263
  );
257
264
  return {
258
265
  getCellBounds: b,
259
- getEstimatedHeight: a,
266
+ getEstimatedHeight: d,
260
267
  onScroll: () => {
261
- const i = M(
268
+ const i = E(
262
269
  (s === "vertical" ? t?.scrollTop : t?.scrollLeft) ?? 0
263
270
  );
264
271
  (i[0] !== w || i[1] !== I) && u(i);
@@ -269,83 +276,83 @@ function G({
269
276
  };
270
277
  }
271
278
  function A(t) {
272
- return H(() => t, Object.values(t));
279
+ return k(() => t, Object.values(t));
273
280
  }
274
- function U(t, r) {
275
- if (t === r)
281
+ function F(t, o) {
282
+ if (t === o)
276
283
  return !0;
277
- if (!!t != !!r || ($(t !== void 0), $(r !== void 0), Object.keys(t).length !== Object.keys(r).length))
284
+ if (!!t != !!o || ($(t !== void 0), $(o !== void 0), Object.keys(t).length !== Object.keys(o).length))
278
285
  return !1;
279
- for (const l in t)
280
- if (!Object.is(r[l], t[l]))
286
+ for (const n in t)
287
+ if (!Object.is(o[n], t[n]))
281
288
  return !1;
282
289
  return !0;
283
290
  }
284
- function _(t, r) {
285
- const { style: l, ...s } = t, { style: e, ...f } = r;
286
- return U(l, e) && U(s, f);
291
+ function _(t, o) {
292
+ const { style: n, ...s } = t, { style: e, ...f } = o;
293
+ return F(n, e) && F(s, f);
287
294
  }
288
- const D = () => {
295
+ const C = () => {
289
296
  };
290
- function ot({
297
+ function rt({
291
298
  cellComponent: t,
292
- cellProps: r,
293
- className: l,
299
+ cellProps: o,
300
+ className: n,
294
301
  columnCount: s,
295
302
  columnWidth: e,
296
303
  defaultHeight: f = 0,
297
- defaultWidth: n = 0,
304
+ defaultWidth: l = 0,
298
305
  gridRef: c,
299
- onCellsRendered: o,
300
- onScroll: u = D,
306
+ onCellsRendered: r,
307
+ onScroll: u = C,
301
308
  overscanCount: w = 3,
302
309
  rowCount: I,
303
310
  rowHeight: O,
304
311
  style: S,
305
312
  ...m
306
313
  }) {
307
- const d = A(r), b = H(
308
- () => V(t, _),
314
+ const a = A(o), b = k(
315
+ () => N(t, _),
309
316
  [t]
310
- ), [a, M] = L(null), g = G({
311
- containerElement: a,
312
- defaultContainerSize: n,
317
+ ), [d, E] = L(null), g = G({
318
+ containerElement: d,
319
+ defaultContainerSize: l,
313
320
  direction: "horizontal",
314
321
  itemCount: s,
315
- itemProps: d,
322
+ itemProps: a,
316
323
  itemSize: e,
317
324
  overscanCount: w
318
325
  }), i = G({
319
- containerElement: a,
326
+ containerElement: d,
320
327
  defaultContainerSize: f,
321
328
  direction: "vertical",
322
329
  itemCount: I,
323
- itemProps: d,
330
+ itemProps: a,
324
331
  itemSize: O,
325
332
  overscanCount: w
326
333
  }), { startIndex: p, stopIndex: z } = g, { startIndex: x, stopIndex: h } = i;
327
- F(
334
+ Y(
328
335
  c,
329
336
  () => ({
330
337
  get element() {
331
- return a;
338
+ return d;
332
339
  },
333
340
  scrollToCell({
334
341
  behavior: v = "auto",
335
342
  columnAlign: y = "auto",
336
343
  columnIndex: T,
337
- rowAlign: k = "auto",
344
+ rowAlign: H = "auto",
338
345
  rowIndex: R
339
346
  }) {
340
347
  i?.scrollToIndex({
341
- align: k,
348
+ align: H,
342
349
  behavior: v,
343
- containerScrollOffset: a?.scrollTop ?? 0,
350
+ containerScrollOffset: d?.scrollTop ?? 0,
344
351
  index: R
345
352
  }), g?.scrollToIndex({
346
353
  align: y,
347
354
  behavior: v,
348
- containerScrollOffset: a?.scrollLeft ?? 0,
355
+ containerScrollOffset: d?.scrollLeft ?? 0,
349
356
  index: T
350
357
  });
351
358
  },
@@ -357,7 +364,7 @@ function ot({
357
364
  g?.scrollToIndex({
358
365
  align: v,
359
366
  behavior: y,
360
- containerScrollOffset: a?.scrollLeft ?? 0,
367
+ containerScrollOffset: d?.scrollLeft ?? 0,
361
368
  index: T
362
369
  });
363
370
  },
@@ -369,40 +376,40 @@ function ot({
369
376
  i?.scrollToIndex({
370
377
  align: v,
371
378
  behavior: y,
372
- containerScrollOffset: a?.scrollTop ?? 0,
379
+ containerScrollOffset: d?.scrollTop ?? 0,
373
380
  index: T
374
381
  });
375
382
  }
376
383
  }),
377
- [g, a, i]
384
+ [g, d, i]
378
385
  ), W(() => {
379
- p >= 0 && z >= 0 && x >= 0 && h >= 0 && o && o({
386
+ p >= 0 && z >= 0 && x >= 0 && h >= 0 && r && r({
380
387
  columnStartIndex: p,
381
388
  columnStopIndex: z,
382
389
  rowStartIndex: x,
383
390
  rowStopIndex: h
384
391
  });
385
392
  }, [
386
- o,
393
+ r,
387
394
  p,
388
395
  z,
389
396
  x,
390
397
  h
391
398
  ]);
392
- const E = H(() => {
399
+ const M = k(() => {
393
400
  const v = [];
394
401
  if (g && s > 0 && i && I > 0)
395
402
  for (let y = x; y <= h; y++) {
396
403
  const T = i.getCellBounds(y);
397
- for (let k = p; k <= z; k++) {
398
- const R = g.getCellBounds(k);
404
+ for (let H = p; H <= z; H++) {
405
+ const R = g.getCellBounds(H);
399
406
  v.push(
400
- /* @__PURE__ */ N(
407
+ /* @__PURE__ */ q(
401
408
  b,
402
409
  {
403
- ...d,
404
- columnIndex: k,
405
- key: `${y}-${k}`,
410
+ ...a,
411
+ columnIndex: H,
412
+ key: `${y}-${H}`,
406
413
  rowIndex: y,
407
414
  style: {
408
415
  position: "absolute",
@@ -419,7 +426,7 @@ function ot({
419
426
  return v;
420
427
  }, [
421
428
  b,
422
- d,
429
+ a,
423
430
  s,
424
431
  p,
425
432
  z,
@@ -434,11 +441,11 @@ function ot({
434
441
  {
435
442
  role: "grid",
436
443
  ...m,
437
- className: l,
444
+ className: n,
438
445
  onScroll: (v) => {
439
446
  g.onScroll(), i.onScroll(), u(v);
440
447
  },
441
- ref: M,
448
+ ref: E,
442
449
  style: {
443
450
  ...S,
444
451
  maxHeight: "100%",
@@ -449,54 +456,54 @@ function ot({
449
456
  children: /* @__PURE__ */ j(
450
457
  "div",
451
458
  {
452
- className: l,
459
+ className: n,
453
460
  style: {
454
461
  position: "relative",
455
462
  height: i?.getEstimatedHeight(),
456
463
  width: g?.getEstimatedHeight()
457
464
  },
458
- children: E
465
+ children: M
459
466
  }
460
467
  )
461
468
  }
462
469
  );
463
470
  }
464
- const rt = L, lt = Y;
465
- function nt({
471
+ const nt = L, lt = B;
472
+ function it({
466
473
  className: t,
467
- defaultHeight: r = 0,
468
- listRef: l,
474
+ defaultHeight: o = 0,
475
+ listRef: n,
469
476
  onRowsRendered: s,
470
- onScroll: e = D,
477
+ onScroll: e = C,
471
478
  overscanCount: f = 3,
472
- rowComponent: n,
479
+ rowComponent: l,
473
480
  rowCount: c,
474
- rowHeight: o,
481
+ rowHeight: r,
475
482
  rowProps: u,
476
483
  style: w,
477
484
  ...I
478
485
  }) {
479
- const O = A(u), S = H(
480
- () => V(n, _),
481
- [n]
482
- ), [m, d] = L(null), {
486
+ const O = A(u), S = k(
487
+ () => N(l, _),
488
+ [l]
489
+ ), [m, a] = L(null), {
483
490
  getCellBounds: b,
484
- getEstimatedHeight: a,
485
- onScroll: M,
491
+ getEstimatedHeight: d,
492
+ onScroll: E,
486
493
  scrollToIndex: g,
487
494
  startIndex: i,
488
495
  stopIndex: p
489
496
  } = G({
490
497
  containerElement: m,
491
- defaultContainerSize: r,
498
+ defaultContainerSize: o,
492
499
  direction: "vertical",
493
500
  itemCount: c,
494
501
  itemProps: O,
495
- itemSize: o,
502
+ itemSize: r,
496
503
  overscanCount: f
497
504
  });
498
- F(
499
- l,
505
+ Y(
506
+ n,
500
507
  () => ({
501
508
  get element() {
502
509
  return m;
@@ -504,13 +511,13 @@ function nt({
504
511
  scrollToRow({
505
512
  align: x = "auto",
506
513
  behavior: h = "auto",
507
- index: E
514
+ index: M
508
515
  }) {
509
516
  g({
510
517
  align: x,
511
518
  behavior: h,
512
519
  containerScrollOffset: m?.scrollTop ?? 0,
513
- index: E
520
+ index: M
514
521
  });
515
522
  }
516
523
  }),
@@ -521,13 +528,13 @@ function nt({
521
528
  stopIndex: p
522
529
  });
523
530
  }, [s, i, p]);
524
- const z = H(() => {
531
+ const z = k(() => {
525
532
  const x = [];
526
533
  if (c > 0)
527
534
  for (let h = i; h <= p; h++) {
528
- const E = b(h);
535
+ const M = b(h);
529
536
  x.push(
530
- /* @__PURE__ */ N(
537
+ /* @__PURE__ */ q(
531
538
  S,
532
539
  {
533
540
  ...O,
@@ -536,8 +543,8 @@ function nt({
536
543
  style: {
537
544
  position: "absolute",
538
545
  left: 0,
539
- transform: `translateY(${E.scrollOffset}px)`,
540
- height: E.size,
546
+ transform: `translateY(${M.scrollOffset}px)`,
547
+ height: M.size,
541
548
  width: "100%"
542
549
  }
543
550
  }
@@ -553,9 +560,9 @@ function nt({
553
560
  ...I,
554
561
  className: t,
555
562
  onScroll: (x) => {
556
- M(), e(x);
563
+ E(), e(x);
557
564
  },
558
- ref: d,
565
+ ref: a,
559
566
  style: {
560
567
  ...w,
561
568
  maxHeight: "100%",
@@ -567,7 +574,7 @@ function nt({
567
574
  {
568
575
  className: t,
569
576
  style: {
570
- height: a(),
577
+ height: d(),
571
578
  position: "relative",
572
579
  width: "100%"
573
580
  },
@@ -577,12 +584,12 @@ function nt({
577
584
  }
578
585
  );
579
586
  }
580
- const it = L, ct = Y;
587
+ const ct = L, ft = B;
581
588
  export {
582
- ot as Grid,
583
- nt as List,
584
- rt as useGridCallbackRef,
589
+ rt as Grid,
590
+ it as List,
591
+ nt as useGridCallbackRef,
585
592
  lt as useGridRef,
586
- it as useListCallbackRef,
587
- ct as useListRef
593
+ ct as useListCallbackRef,
594
+ ft as useListRef
588
595
  };