@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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 287c2a9: Keep the timeline pinned in WebKit when React replaces registered rows at the end.
8
+ This includes a grouped row and the last two rows in one commit. An upward scroll before
9
+ the replacement still ends follow mode.
10
+
11
+ The view can still move away from the end if React removes an unregistered element first
12
+ while layout changes are pending.
13
+
3
14
  ## 1.0.0
4
15
 
5
16
  ### Major Changes
package/README.md CHANGED
@@ -69,6 +69,8 @@ which part of `items` to render and when to call `older.load`.
69
69
  at the end, the view stays in place. `followsAppend` can force a follow for a new last item. Use it
70
70
  for an item that the reader created.
71
71
  - A change of `layout.bottomInset` keeps a pinned tail in view.
72
+ - In WebKit, the view can move away from the end if React removes an unregistered element,
73
+ such as a date divider, before it replaces the last row while layout changes are pending.
72
74
  - The engine reports the scroll state through `memory.onChange`. Give a stored value to
73
75
  `memory.initial` to restore it. The engine restores the anchor item and its offset when the reader
74
76
  was not at the end. Otherwise it scrolls to the end.
package/dist/index.cjs CHANGED
@@ -1102,6 +1102,7 @@ function useTimelineMessageNavigation({
1102
1102
  startScrollCommand,
1103
1103
  onNavigation,
1104
1104
  readingPosition,
1105
+ rowDetach,
1105
1106
  viewport,
1106
1107
  attachVersion = 0,
1107
1108
  }) {
@@ -1153,16 +1154,36 @@ function useTimelineMessageNavigation({
1153
1154
  revealCachedItemRef,
1154
1155
  scrollToLoadedMessage,
1155
1156
  });
1157
+ // When a row renders again, React calls its previous inline ref with null.
1158
+ // takeRecords() clears the removal records. Check once per commit to
1159
+ // prevent a later ref from forcing layout after a removal.
1160
+ const didPrepareDetachRef = react.useRef(false);
1161
+ react.useLayoutEffect(() => {
1162
+ didPrepareDetachRef.current = false;
1163
+ rowDetach.finish();
1164
+ });
1165
+
1156
1166
  const registerItemElement = react.useCallback(
1157
1167
  (id, element) => {
1168
+ const elements = messageElementsRef.current;
1158
1169
  if (element) {
1159
- messageElementsRef.current.set(id, element);
1170
+ // A child can replace rows without rendering this hook. React
1171
+ // attaches the new refs after it removes the old rows.
1172
+ if (didPrepareDetachRef.current) {
1173
+ didPrepareDetachRef.current = false;
1174
+ rowDetach.finish();
1175
+ }
1176
+ elements.set(id, element);
1160
1177
  notifyRegistered();
1161
1178
  return;
1162
1179
  }
1163
- messageElementsRef.current.delete(id);
1180
+ if (!didPrepareDetachRef.current) {
1181
+ const row = elements.get(id);
1182
+ if (row) didPrepareDetachRef.current = rowDetach.prepare(row);
1183
+ }
1184
+ elements.delete(id);
1164
1185
  },
1165
- [messageElementsRef, notifyRegistered],
1186
+ [messageElementsRef, notifyRegistered, rowDetach],
1166
1187
  );
1167
1188
 
1168
1189
  // Takes the scroll command token. The returned check stays true until a
@@ -1361,6 +1382,8 @@ function useTimelineViewportStability({
1361
1382
  ]);
1362
1383
  }
1363
1384
 
1385
+ const WEBKIT_CSS_SUPPORT_QUERY = '(-webkit-backdrop-filter: none)';
1386
+
1364
1387
  function useTimelineMessageLayout({
1365
1388
  attachVersion = 0,
1366
1389
  containerAttachVersion = 0,
@@ -1389,6 +1412,61 @@ function useTimelineMessageLayout({
1389
1412
  const didRestoreRef = react.useRef(false);
1390
1413
  const previousSourceLastIdRef = react.useRef(undefined);
1391
1414
  const previousContainerRef = react.useRef(null);
1415
+ const rowDetachObserverRef = react.useRef(null);
1416
+ // biome-ignore lint/correctness/useExhaustiveDependencies: containerAttachVersion tracks a new container element
1417
+ react.useLayoutEffect(() => {
1418
+ const element = scrollContainerRef.current;
1419
+ const view = element?.ownerDocument.defaultView;
1420
+ if (
1421
+ !view?.MutationObserver ||
1422
+ !view.CSS?.supports(WEBKIT_CSS_SUPPORT_QUERY)
1423
+ ) {
1424
+ return;
1425
+ }
1426
+
1427
+ // Read pending removals during ref cleanup. The callback needs no records.
1428
+ const observer = new view.MutationObserver(() => {});
1429
+ observer.observe(element, { childList: true, subtree: true });
1430
+ rowDetachObserverRef.current = observer;
1431
+ return () => {
1432
+ rowDetachObserverRef.current = null;
1433
+ observer.disconnect();
1434
+ };
1435
+ }, [containerAttachVersion, scrollContainerRef]);
1436
+
1437
+ const rowDetach = react.useMemo(
1438
+ () => ({
1439
+ prepare(row) {
1440
+ const observer = rowDetachObserverRef.current;
1441
+ const element = scrollContainerRef.current;
1442
+ if (
1443
+ !observer ||
1444
+ !didRestoreRef.current ||
1445
+ !wasAtBottomRef.current ||
1446
+ !element ||
1447
+ element !== previousContainerRef.current ||
1448
+ !element.contains(row) ||
1449
+ scrollCorrection.hasPendingScrollCorrection()
1450
+ ) {
1451
+ return false;
1452
+ }
1453
+
1454
+ // WebKit can clamp the offset while React replaces rows. Read before
1455
+ // removal to update layout. A read after removal can cause a clamp.
1456
+ const records = observer.takeRecords();
1457
+ if (!records.some(({ removedNodes }) => removedNodes.length)) {
1458
+ void element.scrollTop;
1459
+ }
1460
+ return true;
1461
+ },
1462
+
1463
+ finish() {
1464
+ rowDetachObserverRef.current?.takeRecords();
1465
+ },
1466
+ }),
1467
+ [scrollContainerRef, scrollCorrection, wasAtBottomRef],
1468
+ );
1469
+
1392
1470
  const applyInitialPosition = react.useCallback(
1393
1471
  (element) => {
1394
1472
  didRestoreRef.current = true;
@@ -1530,7 +1608,7 @@ function useTimelineMessageLayout({
1530
1608
  viewport,
1531
1609
  });
1532
1610
 
1533
- return { didRestoreRef, scrollToBottom };
1611
+ return { didRestoreRef, rowDetach, scrollToBottom };
1534
1612
  }
1535
1613
 
1536
1614
  const NO_PAGE_COMMIT = Object.freeze({
@@ -3323,32 +3401,35 @@ function useTimelineScroll({
3323
3401
  scrollContainerRef,
3324
3402
  updateScrollState,
3325
3403
  });
3326
- const { didRestoreRef, scrollToBottom: scrollRenderedToBottom } =
3327
- useTimelineMessageLayout({
3328
- attachVersion,
3329
- containerAttachVersion,
3330
- bottomInset,
3331
- commitRender,
3332
- followsAppend,
3333
- initialMessageId,
3334
- isLoading,
3335
- messages,
3336
- sourceMessages,
3337
- itemListRef,
3338
- olderPageVersion,
3339
- navigationVersion,
3340
- onAnchorLost: pauseAutoPaging,
3341
- platform,
3342
- previousScrollTopRef,
3343
- readingPosition,
3344
- scrollContainerRef,
3345
- scrollCorrection,
3346
- scrollMemory,
3347
- updateScrollState,
3348
- updateScrollPosition,
3349
- viewport,
3350
- wasAtBottomRef,
3351
- });
3404
+ const {
3405
+ didRestoreRef,
3406
+ rowDetach,
3407
+ scrollToBottom: scrollRenderedToBottom,
3408
+ } = useTimelineMessageLayout({
3409
+ attachVersion,
3410
+ containerAttachVersion,
3411
+ bottomInset,
3412
+ commitRender,
3413
+ followsAppend,
3414
+ initialMessageId,
3415
+ isLoading,
3416
+ messages,
3417
+ sourceMessages,
3418
+ itemListRef,
3419
+ olderPageVersion,
3420
+ navigationVersion,
3421
+ onAnchorLost: pauseAutoPaging,
3422
+ platform,
3423
+ previousScrollTopRef,
3424
+ readingPosition,
3425
+ scrollContainerRef,
3426
+ scrollCorrection,
3427
+ scrollMemory,
3428
+ updateScrollState,
3429
+ updateScrollPosition,
3430
+ viewport,
3431
+ wasAtBottomRef,
3432
+ });
3352
3433
  const { isMessageReached: isReadTargetReached, updateMessageReach } =
3353
3434
  useTimelineMessageReach({
3354
3435
  isMessageBeforeWindow: readTargetBeforeWindow,
@@ -3390,6 +3471,7 @@ function useTimelineScroll({
3390
3471
  startScrollCommand,
3391
3472
  onNavigation,
3392
3473
  readingPosition,
3474
+ rowDetach,
3393
3475
  viewport,
3394
3476
  });
3395
3477
  const { checkPagePrefetch } = useTimelinePagePrefetch({