virtua 0.41.0 → 0.41.1

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,4 +1,4 @@
1
- import { mergeProps, createEffect, onCleanup, createMemo, createSignal, onMount, createComputed, on, For, untrack } from 'solid-js';
1
+ import { mergeProps, createEffect, onCleanup, createMemo, createSignal, onMount, createComputed, on, untrack, For, splitProps } from 'solid-js';
2
2
  import { Dynamic } from 'solid-js/web';
3
3
 
4
4
  /** @internal */
@@ -1272,7 +1272,7 @@ const ListItem = (props) => {
1272
1272
  }
1273
1273
  return style;
1274
1274
  });
1275
- return (<Dynamic component={props._as} ref={elementRef} style={style()}>
1275
+ return (<Dynamic component={props._as} index={props._index} ref={elementRef} style={style()}>
1276
1276
  {props._children}
1277
1277
  </Dynamic>);
1278
1278
  };
@@ -1372,8 +1372,46 @@ const Virtualizer = (props) => {
1372
1372
  }));
1373
1373
  const dataSlice = createMemo(() => {
1374
1374
  const [start, end] = range();
1375
- return end >= 0 ? props.data.slice(start, end + 1) : [];
1375
+ const items = end >= 0 ? props.data.slice(start, end + 1) : [];
1376
+ const indexes = items.map((_, index) => start + index);
1377
+ if (props.keepMounted) {
1378
+ const startItems = [];
1379
+ const startIndexes = [];
1380
+ const endItems = [];
1381
+ const endIndexes = [];
1382
+ sort(props.keepMounted).forEach((index) => {
1383
+ if (index < 0 || index >= props.data.length)
1384
+ return;
1385
+ if (index < start) {
1386
+ startItems.push(props.data[index]);
1387
+ startIndexes.push(index);
1388
+ }
1389
+ if (index > end) {
1390
+ endItems.push(props.data[index]);
1391
+ endIndexes.push(index);
1392
+ }
1393
+ });
1394
+ items.unshift(...startItems);
1395
+ indexes.unshift(...startIndexes);
1396
+ items.push(...endItems);
1397
+ indexes.push(...endIndexes);
1398
+ }
1399
+ return { _items: items, _indexes: indexes };
1376
1400
  });
1401
+ const renderItem = (data, index) => {
1402
+ const offset = createMemo(() => {
1403
+ stateVersion();
1404
+ return store.$getItemOffset(index());
1405
+ });
1406
+ const hide = createMemo(() => {
1407
+ stateVersion();
1408
+ return store.$isUnmeasuredItem(index());
1409
+ });
1410
+ const children = createMemo(() => {
1411
+ return untrack(() => props.children(data, index));
1412
+ });
1413
+ return (<ListItem _as={props.item} _index={index()} _resizer={resizer.$observeItem} _offset={offset()} _hide={hide()} _children={children()} _isHorizontal={horizontal}/>);
1414
+ };
1377
1415
  return (<Dynamic component={props.as} ref={containerRef} style={{
1378
1416
  // contain: "content",
1379
1417
  "overflow-anchor": "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
@@ -1384,41 +1422,48 @@ const Virtualizer = (props) => {
1384
1422
  height: horizontal ? "100%" : totalSize() + "px",
1385
1423
  "pointer-events": isScrolling() ? "none" : undefined,
1386
1424
  }}>
1387
- <For each={dataSlice()}>
1425
+ <For each={dataSlice()._items}>
1388
1426
  {(data, index) => {
1389
- const itemIndex = createMemo(() => range()[0] + index());
1390
- const offset = createMemo(() => {
1391
- stateVersion();
1392
- return store.$getItemOffset(itemIndex());
1393
- });
1394
- const hide = createMemo(() => {
1395
- stateVersion();
1396
- return store.$isUnmeasuredItem(itemIndex());
1397
- });
1398
- const children = createMemo(() => {
1399
- return untrack(() => props.children(data, itemIndex));
1400
- });
1401
- return (<ListItem _as={props.item} _index={itemIndex()} _resizer={resizer.$observeItem} _offset={offset()} _hide={hide()} _children={children()} _isHorizontal={horizontal}/>);
1427
+ const itemIndex = createMemo(() => dataSlice()._indexes[index()]);
1428
+ // eslint-disable-next-line solid/reactivity
1429
+ return renderItem(data, itemIndex);
1402
1430
  }}
1403
1431
  </For>
1404
1432
  </Dynamic>);
1405
1433
  };
1406
1434
 
1435
+ /**
1436
+ * @jsxImportSource solid-js
1437
+ */
1407
1438
  /**
1408
1439
  * Virtualized list component. See {@link VListProps} and {@link VListHandle}.
1409
1440
  */
1410
1441
  const VList = (props) => {
1411
- const { ref, data, children, overscan, itemSize, shift, horizontal, cache, onScroll, onScrollEnd, style, ...attrs } = props;
1412
- return (<div {...attrs} style={{
1413
- display: horizontal ? "inline-block" : "block",
1414
- [horizontal ? "overflow-x" : "overflow-y"]: "auto",
1442
+ const [local, others] = splitProps(props, [
1443
+ "ref",
1444
+ "data",
1445
+ "children",
1446
+ "overscan",
1447
+ "itemSize",
1448
+ "shift",
1449
+ "horizontal",
1450
+ "keepMounted",
1451
+ "cache",
1452
+ "item",
1453
+ "onScroll",
1454
+ "onScrollEnd",
1455
+ "style",
1456
+ ]);
1457
+ return (<div {...others} style={{
1458
+ display: local.horizontal ? "inline-block" : "block",
1459
+ [local.horizontal ? "overflow-x" : "overflow-y"]: "auto",
1415
1460
  contain: "strict",
1416
1461
  width: "100%",
1417
1462
  height: "100%",
1418
- ...props.style,
1463
+ ...local.style,
1419
1464
  }}>
1420
- <Virtualizer ref={props.ref} data={props.data} overscan={props.overscan} itemSize={props.itemSize} shift={props.shift} horizontal={horizontal} cache={cache} onScroll={props.onScroll} onScrollEnd={props.onScrollEnd}>
1421
- {props.children}
1465
+ <Virtualizer ref={local.ref} data={local.data} overscan={local.overscan} itemSize={local.itemSize} shift={local.shift} horizontal={local.horizontal} keepMounted={local.keepMounted} cache={local.cache} item={local.item} onScroll={local.onScroll} onScrollEnd={local.onScrollEnd}>
1466
+ {local.children}
1422
1467
  </Virtualizer>
1423
1468
  </div>);
1424
1469
  };