@bazariodev/scrollback 1.0.0 → 1.0.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.
package/dist/index.js CHANGED
@@ -1100,6 +1100,7 @@ function useTimelineMessageNavigation({
1100
1100
  startScrollCommand,
1101
1101
  onNavigation,
1102
1102
  readingPosition,
1103
+ rowDetach,
1103
1104
  viewport,
1104
1105
  attachVersion = 0,
1105
1106
  }) {
@@ -1151,16 +1152,36 @@ function useTimelineMessageNavigation({
1151
1152
  revealCachedItemRef,
1152
1153
  scrollToLoadedMessage,
1153
1154
  });
1155
+ // When a row renders again, React calls its previous inline ref with null.
1156
+ // takeRecords() clears the removal records. Check once per commit to
1157
+ // prevent a later ref from forcing layout after a removal.
1158
+ const didPrepareDetachRef = useRef(false);
1159
+ useLayoutEffect(() => {
1160
+ didPrepareDetachRef.current = false;
1161
+ rowDetach.finish();
1162
+ });
1163
+
1154
1164
  const registerItemElement = useCallback(
1155
1165
  (id, element) => {
1166
+ const elements = messageElementsRef.current;
1156
1167
  if (element) {
1157
- messageElementsRef.current.set(id, element);
1168
+ // A child can replace rows without rendering this hook. React
1169
+ // attaches the new refs after it removes the old rows.
1170
+ if (didPrepareDetachRef.current) {
1171
+ didPrepareDetachRef.current = false;
1172
+ rowDetach.finish();
1173
+ }
1174
+ elements.set(id, element);
1158
1175
  notifyRegistered();
1159
1176
  return;
1160
1177
  }
1161
- messageElementsRef.current.delete(id);
1178
+ if (!didPrepareDetachRef.current) {
1179
+ const row = elements.get(id);
1180
+ if (row) didPrepareDetachRef.current = rowDetach.prepare(row);
1181
+ }
1182
+ elements.delete(id);
1162
1183
  },
1163
- [messageElementsRef, notifyRegistered],
1184
+ [messageElementsRef, notifyRegistered, rowDetach],
1164
1185
  );
1165
1186
 
1166
1187
  // Takes the scroll command token. The returned check stays true until a
@@ -1359,6 +1380,8 @@ function useTimelineViewportStability({
1359
1380
  ]);
1360
1381
  }
1361
1382
 
1383
+ const WEBKIT_CSS_SUPPORT_QUERY = '(-webkit-backdrop-filter: none)';
1384
+
1362
1385
  function useTimelineMessageLayout({
1363
1386
  attachVersion = 0,
1364
1387
  containerAttachVersion = 0,
@@ -1387,6 +1410,61 @@ function useTimelineMessageLayout({
1387
1410
  const didRestoreRef = useRef(false);
1388
1411
  const previousSourceLastIdRef = useRef(undefined);
1389
1412
  const previousContainerRef = useRef(null);
1413
+ const rowDetachObserverRef = useRef(null);
1414
+ // biome-ignore lint/correctness/useExhaustiveDependencies: containerAttachVersion tracks a new container element
1415
+ useLayoutEffect(() => {
1416
+ const element = scrollContainerRef.current;
1417
+ const view = element?.ownerDocument.defaultView;
1418
+ if (
1419
+ !view?.MutationObserver ||
1420
+ !view.CSS?.supports(WEBKIT_CSS_SUPPORT_QUERY)
1421
+ ) {
1422
+ return;
1423
+ }
1424
+
1425
+ // Read pending removals during ref cleanup. The callback needs no records.
1426
+ const observer = new view.MutationObserver(() => {});
1427
+ observer.observe(element, { childList: true, subtree: true });
1428
+ rowDetachObserverRef.current = observer;
1429
+ return () => {
1430
+ rowDetachObserverRef.current = null;
1431
+ observer.disconnect();
1432
+ };
1433
+ }, [containerAttachVersion, scrollContainerRef]);
1434
+
1435
+ const rowDetach = useMemo(
1436
+ () => ({
1437
+ prepare(row) {
1438
+ const observer = rowDetachObserverRef.current;
1439
+ const element = scrollContainerRef.current;
1440
+ if (
1441
+ !observer ||
1442
+ !didRestoreRef.current ||
1443
+ !wasAtBottomRef.current ||
1444
+ !element ||
1445
+ element !== previousContainerRef.current ||
1446
+ !element.contains(row) ||
1447
+ scrollCorrection.hasPendingScrollCorrection()
1448
+ ) {
1449
+ return false;
1450
+ }
1451
+
1452
+ // WebKit can clamp the offset while React replaces rows. Read before
1453
+ // removal to update layout. A read after removal can cause a clamp.
1454
+ const records = observer.takeRecords();
1455
+ if (!records.some(({ removedNodes }) => removedNodes.length)) {
1456
+ void element.scrollTop;
1457
+ }
1458
+ return true;
1459
+ },
1460
+
1461
+ finish() {
1462
+ rowDetachObserverRef.current?.takeRecords();
1463
+ },
1464
+ }),
1465
+ [scrollContainerRef, scrollCorrection, wasAtBottomRef],
1466
+ );
1467
+
1390
1468
  const applyInitialPosition = useCallback(
1391
1469
  (element) => {
1392
1470
  didRestoreRef.current = true;
@@ -1528,7 +1606,7 @@ function useTimelineMessageLayout({
1528
1606
  viewport,
1529
1607
  });
1530
1608
 
1531
- return { didRestoreRef, scrollToBottom };
1609
+ return { didRestoreRef, rowDetach, scrollToBottom };
1532
1610
  }
1533
1611
 
1534
1612
  const NO_PAGE_COMMIT = Object.freeze({
@@ -3321,32 +3399,35 @@ function useTimelineScroll({
3321
3399
  scrollContainerRef,
3322
3400
  updateScrollState,
3323
3401
  });
3324
- const { didRestoreRef, scrollToBottom: scrollRenderedToBottom } =
3325
- useTimelineMessageLayout({
3326
- attachVersion,
3327
- containerAttachVersion,
3328
- bottomInset,
3329
- commitRender,
3330
- followsAppend,
3331
- initialMessageId,
3332
- isLoading,
3333
- messages,
3334
- sourceMessages,
3335
- itemListRef,
3336
- olderPageVersion,
3337
- navigationVersion,
3338
- onAnchorLost: pauseAutoPaging,
3339
- platform,
3340
- previousScrollTopRef,
3341
- readingPosition,
3342
- scrollContainerRef,
3343
- scrollCorrection,
3344
- scrollMemory,
3345
- updateScrollState,
3346
- updateScrollPosition,
3347
- viewport,
3348
- wasAtBottomRef,
3349
- });
3402
+ const {
3403
+ didRestoreRef,
3404
+ rowDetach,
3405
+ scrollToBottom: scrollRenderedToBottom,
3406
+ } = useTimelineMessageLayout({
3407
+ attachVersion,
3408
+ containerAttachVersion,
3409
+ bottomInset,
3410
+ commitRender,
3411
+ followsAppend,
3412
+ initialMessageId,
3413
+ isLoading,
3414
+ messages,
3415
+ sourceMessages,
3416
+ itemListRef,
3417
+ olderPageVersion,
3418
+ navigationVersion,
3419
+ onAnchorLost: pauseAutoPaging,
3420
+ platform,
3421
+ previousScrollTopRef,
3422
+ readingPosition,
3423
+ scrollContainerRef,
3424
+ scrollCorrection,
3425
+ scrollMemory,
3426
+ updateScrollState,
3427
+ updateScrollPosition,
3428
+ viewport,
3429
+ wasAtBottomRef,
3430
+ });
3350
3431
  const { isMessageReached: isReadTargetReached, updateMessageReach } =
3351
3432
  useTimelineMessageReach({
3352
3433
  isMessageBeforeWindow: readTargetBeforeWindow,
@@ -3388,6 +3469,7 @@ function useTimelineScroll({
3388
3469
  startScrollCommand,
3389
3470
  onNavigation,
3390
3471
  readingPosition,
3472
+ rowDetach,
3391
3473
  viewport,
3392
3474
  });
3393
3475
  const { checkPagePrefetch } = useTimelinePagePrefetch({