@codefast/ui 0.7.0 → 0.9.0

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +2 -2
  3. package/dist/components/sidebar.d.ts +1 -1
  4. package/dist/components/sidebar.js +1 -1
  5. package/dist/hooks/use-animated-value.d.ts.map +1 -1
  6. package/dist/hooks/use-animated-value.js +14 -10
  7. package/dist/hooks/use-animated-value.js.map +1 -1
  8. package/dist/hooks/use-has-hydrated.d.ts +12 -0
  9. package/dist/hooks/use-has-hydrated.d.ts.map +1 -0
  10. package/dist/hooks/use-has-hydrated.js +16 -0
  11. package/dist/hooks/use-has-hydrated.js.map +1 -0
  12. package/dist/hooks/use-latest.d.ts +13 -0
  13. package/dist/hooks/use-latest.d.ts.map +1 -0
  14. package/dist/hooks/use-latest.js +19 -0
  15. package/dist/hooks/use-latest.js.map +1 -0
  16. package/dist/hooks/use-message-scroller-commands.d.ts +3 -1
  17. package/dist/hooks/use-message-scroller-commands.d.ts.map +1 -1
  18. package/dist/hooks/use-message-scroller-commands.js +32 -3
  19. package/dist/hooks/use-message-scroller-commands.js.map +1 -1
  20. package/dist/hooks/use-message-scroller-controller.d.ts.map +1 -1
  21. package/dist/hooks/use-message-scroller-controller.js +75 -11
  22. package/dist/hooks/use-message-scroller-controller.js.map +1 -1
  23. package/dist/hooks/use-message-scroller-refs.d.ts +2 -1
  24. package/dist/hooks/use-message-scroller-refs.d.ts.map +1 -1
  25. package/dist/hooks/use-message-scroller-refs.js +16 -22
  26. package/dist/hooks/use-message-scroller-refs.js.map +1 -1
  27. package/dist/hooks/use-pagination.d.ts +2 -0
  28. package/dist/hooks/use-pagination.d.ts.map +1 -1
  29. package/dist/hooks/use-pagination.js.map +1 -1
  30. package/dist/lib/message-scroller/geometry.d.ts +2 -2
  31. package/dist/lib/message-scroller/geometry.d.ts.map +1 -1
  32. package/dist/lib/message-scroller/stores.js +1 -1
  33. package/dist/lib/message-scroller/stores.js.map +1 -1
  34. package/dist/lib/message-scroller/types.d.ts +14 -5
  35. package/dist/lib/message-scroller/types.d.ts.map +1 -1
  36. package/dist/lib/message-scroller/types.js +1 -0
  37. package/dist/lib/message-scroller/types.js.map +1 -1
  38. package/dist/primitives/message-scroller.d.ts.map +1 -1
  39. package/dist/primitives/message-scroller.js +22 -12
  40. package/dist/primitives/message-scroller.js.map +1 -1
  41. package/package.json +20 -15
  42. package/src/components/sidebar.tsx +1 -1
  43. package/src/hooks/use-animated-value.ts +16 -12
  44. package/src/hooks/use-has-hydrated.ts +22 -0
  45. package/src/hooks/use-latest.ts +23 -0
  46. package/src/hooks/use-message-scroller-commands.ts +48 -9
  47. package/src/hooks/use-message-scroller-controller.ts +93 -11
  48. package/src/hooks/use-message-scroller-refs.ts +19 -25
  49. package/src/hooks/use-pagination.ts +2 -0
  50. package/src/lib/message-scroller/geometry.ts +2 -2
  51. package/src/lib/message-scroller/stores.ts +1 -1
  52. package/src/lib/message-scroller/types.ts +17 -4
  53. package/src/primitives/message-scroller.tsx +23 -12
  54. package/dist/lib/message-scroller/utils.d.ts +0 -6
  55. package/dist/lib/message-scroller/utils.d.ts.map +0 -1
  56. package/dist/lib/message-scroller/utils.js +0 -11
  57. package/dist/lib/message-scroller/utils.js.map +0 -1
  58. package/src/lib/message-scroller/utils.ts +0 -14
@@ -222,17 +222,56 @@ function useMessageScrollerCommands({
222
222
  [scheduleVisibilitySync, scrollToPosition, setTailSpacerHeight],
223
223
  );
224
224
 
225
- const reanchorToAnchoredMessage = useCallback(() => {
226
- const element = streamingTurnRef.current;
225
+ const reanchorToAnchoredMessage = useCallback(
226
+ ({ followOnSpacerConsumed = false }: { followOnSpacerConsumed?: boolean } = {}) => {
227
+ if (modeRef.current !== "anchored-to-message") {
228
+ return false;
229
+ }
227
230
 
228
- if (!element || !element.isConnected || modeRef.current !== "anchored-to-message") {
229
- return false;
230
- }
231
+ const element = streamingTurnRef.current;
231
232
 
232
- // Re-run the placement so the tail spacer is recomputed for the new content
233
- // height and the turn is held at the reading line.
234
- return scrollToElement(element, { align: "start" }, { keepPreviousPeek: true });
235
- }, [scrollToElement]);
233
+ if (!element || !element.isConnected) {
234
+ // An unmounted anchor can never re-place; drop the hold so the follow
235
+ // arm is not blocked by a mode nothing else exits.
236
+ streamingTurnRef.current = null;
237
+ modeRef.current = "free-scrolling";
238
+
239
+ return false;
240
+ }
241
+
242
+ const content = contentRef.current;
243
+ const viewport = viewportRef.current;
244
+
245
+ // The reply streaming below the anchor consumes the tail spacer as it
246
+ // grows; once the placement would need none, the reader is at the live
247
+ // edge and the hold hands off to follow-bottom in one scroll command.
248
+ if (
249
+ followOnSpacerConsumed &&
250
+ autoScrollRef.current &&
251
+ spacerHeightRef.current > 0 &&
252
+ content &&
253
+ viewport &&
254
+ content.contains(element)
255
+ ) {
256
+ const scrollTop = getElementScrollTop({
257
+ align: "start",
258
+ element,
259
+ scrollMargin: scrollMarginRef.current + scrollPreviousItemPeekRef.current,
260
+ spacer: spacerRef.current,
261
+ viewport,
262
+ });
263
+
264
+ if (getTailSpacerHeight({ content, scrollTop, spacer: spacerRef.current, viewport }) <= 0) {
265
+ return scrollToEnd({ behavior: "auto" });
266
+ }
267
+ }
268
+
269
+ // Re-run the placement so the tail spacer is recomputed for the new content
270
+ // height and the turn is held at the reading line.
271
+ return scrollToElement(element, { align: "start" }, { keepPreviousPeek: true });
272
+ },
273
+ [scrollToElement, scrollToEnd],
274
+ );
236
275
 
237
276
  // The target row may not be mounted yet (e.g. an async-loaded transcript).
238
277
  // When it is missing the request is queued in pendingScrollToMessageRef and
@@ -31,6 +31,7 @@ import type {
31
31
  MessageScrollerContextValue,
32
32
  MessageScrollerProviderProps,
33
33
  MessageScrollerRegisterMessage,
34
+ MessageScrollerScrollEdges,
34
35
  MessageScrollerScrollable,
35
36
  } from "#/lib/message-scroller/types";
36
37
 
@@ -78,6 +79,7 @@ function useMessageScrollerController({
78
79
  defaultScrollPositionAppliedRef,
79
80
  firstItemRef,
80
81
  itemCountRef,
82
+ lastScrollTopRef,
81
83
  messageElementsRef,
82
84
  modeRef,
83
85
  pendingScrollFrameRef,
@@ -89,6 +91,7 @@ function useMessageScrollerController({
89
91
  scrollMarginRef,
90
92
  scrollPreviousItemPeekRef,
91
93
  spacerGapRef,
94
+ spacerHeightRef,
92
95
  spacerRef,
93
96
  stateFrameRef,
94
97
  stateStore,
@@ -101,6 +104,8 @@ function useMessageScrollerController({
101
104
  } = refs;
102
105
 
103
106
  const previousDefaultScrollPositionRef = useRef(defaultScrollPosition);
107
+ const previousViewportHeightRef = useRef(0);
108
+ const resizeFrameRef = useRef<number | null>(null);
104
109
 
105
110
  if (previousDefaultScrollPositionRef.current !== defaultScrollPosition) {
106
111
  previousDefaultScrollPositionRef.current = defaultScrollPosition;
@@ -125,29 +130,70 @@ function useMessageScrollerController({
125
130
  }
126
131
 
127
132
  element.toggleAttribute("data-autoscrolling", autoScrolling);
133
+ element.toggleAttribute("data-following", state.following);
128
134
  }
129
135
  }, []);
130
136
 
131
- // Owns the one follow-bottom transition: arm at the bottom, release on any
132
- // scroll away (including a scrollbar drag), suppressed during a programmatic
133
- // scroll so the auto-scroll animation cannot release itself.
134
- const reconcileFollowMode = useCallback((scrollable: MessageScrollerScrollable) => {
135
- if (autoScrollRef.current && !scrollable.end && modeRef.current !== "settling-jump") {
137
+ // Owns the one follow-bottom transition: arm at the bottom, release on a
138
+ // reader's real scroll up, and drop the mode whenever autoScroll is off so
139
+ // the published follow state can never outlive the prop.
140
+ const reconcileFollowMode = useCallback((scrollable: MessageScrollerScrollEdges) => {
141
+ const scrollTop = viewportRef.current?.scrollTop ?? 0;
142
+ // Content growing past the live edge also reads as "not at the end", but
143
+ // only a reader moves scrollTop up — releasing on growth would drop follow
144
+ // before the frame-coalesced resize handler has caught it up.
145
+ const scrolledUp = scrollTop < lastScrollTopRef.current - SCROLL_POSITION_EPSILON;
146
+
147
+ if (!autoScrollRef.current) {
148
+ if (modeRef.current === "following-bottom") {
149
+ modeRef.current = "free-scrolling";
150
+ }
151
+
152
+ lastScrollTopRef.current = scrollTop;
153
+ return;
154
+ }
155
+
156
+ // The tail spacer makes a freshly anchored turn read as "at the end";
157
+ // arming there would let the first streamed chunk yank the reader off the
158
+ // anchor, so the hold hands off to following in the resize pass instead.
159
+ if (!scrollable.end && modeRef.current !== "settling-jump" && modeRef.current !== "anchored-to-message") {
136
160
  modeRef.current = "following-bottom";
137
- } else if (modeRef.current === "following-bottom" && scrollable.end && !autoscrollingRef.current) {
161
+ lastScrollTopRef.current = scrollTop;
162
+ return;
163
+ }
164
+
165
+ if (modeRef.current === "following-bottom" && scrollable.end && scrolledUp && !autoscrollingRef.current) {
138
166
  modeRef.current = "free-scrolling";
167
+ lastScrollTopRef.current = scrollTop;
168
+ return;
169
+ }
170
+
171
+ // The baseline advances only outside autoscroll suppression and only
172
+ // downward, so a suppressed or sub-epsilon upward drag keeps accumulating
173
+ // toward a release instead of being consumed commit by commit.
174
+ if (!autoscrollingRef.current && scrollTop > lastScrollTopRef.current) {
175
+ lastScrollTopRef.current = scrollTop;
139
176
  }
140
177
  }, []);
141
178
 
142
179
  const commitScrollState = useCallback(() => {
143
- const nextState = getMessageScrollerScrollable({
180
+ const nextEdges = getMessageScrollerScrollable({
144
181
  content: contentRef.current,
145
182
  scrollEdgeThreshold: scrollEdgeThresholdRef.current,
146
183
  spacer: spacerRef.current,
147
184
  viewport: viewportRef.current,
148
185
  });
149
186
 
150
- reconcileFollowMode(nextState);
187
+ reconcileFollowMode(nextEdges);
188
+
189
+ // The edges publish raw geometry; follow-output rides alongside so a
190
+ // consumer (the end button) can stay quiet while the gap is already being
191
+ // closed, without masking the real end state.
192
+ const nextState: MessageScrollerScrollable = {
193
+ ...nextEdges,
194
+ following: modeRef.current === "following-bottom",
195
+ };
196
+
151
197
  writeStateAttributes(nextState);
152
198
  stateStore.setSnapshot(nextState);
153
199
  }, [reconcileFollowMode, stateStore, writeStateAttributes]);
@@ -422,6 +468,13 @@ function useMessageScrollerController({
422
468
  ]);
423
469
 
424
470
  const handleResize = useCallback(() => {
471
+ const viewportHeight = viewportRef.current?.clientHeight ?? 0;
472
+ // A shrinking viewport consumes tail spacer without any reply growth, so
473
+ // the anchor hold must survive it instead of handing off to the live edge.
474
+ const viewportShrank = viewportHeight < previousViewportHeightRef.current;
475
+
476
+ previousViewportHeightRef.current = viewportHeight;
477
+
425
478
  if (modeRef.current === "following-bottom" && autoScrollRef.current) {
426
479
  scrollToEnd({ behavior: "auto" });
427
480
  return;
@@ -430,7 +483,7 @@ function useMessageScrollerController({
430
483
  // Hold the anchored turn in place as content below it resizes (a reply
431
484
  // streaming in, or a transient marker collapsing) — otherwise the shrinking
432
485
  // content lets the browser clamp scrollTop and the turn drops.
433
- if (reanchorToAnchoredMessage()) {
486
+ if (reanchorToAnchoredMessage({ followOnSpacerConsumed: !viewportShrank })) {
434
487
  return;
435
488
  }
436
489
 
@@ -438,6 +491,30 @@ function useMessageScrollerController({
438
491
  scheduleVisibilitySync();
439
492
  }, [reanchorToAnchoredMessage, scheduleStateCommit, scheduleVisibilitySync, scrollToEnd]);
440
493
 
494
+ // Runs the resize pass synchronously only when no spacer write can occur;
495
+ // otherwise it coalesces onto one shared frame, since resizing the observed
496
+ // spacer during ResizeObserver delivery fires the loop-completed error.
497
+ const scheduleResize = useCallback(() => {
498
+ if (modeRef.current !== "anchored-to-message" && spacerHeightRef.current === 0) {
499
+ if (resizeFrameRef.current !== null) {
500
+ window.cancelAnimationFrame(resizeFrameRef.current);
501
+ resizeFrameRef.current = null;
502
+ }
503
+
504
+ handleResize();
505
+ return;
506
+ }
507
+
508
+ if (resizeFrameRef.current !== null) {
509
+ return;
510
+ }
511
+
512
+ resizeFrameRef.current = window.requestAnimationFrame(() => {
513
+ resizeFrameRef.current = null;
514
+ handleResize();
515
+ });
516
+ }, [handleResize]);
517
+
441
518
  const observeVisibility = useCallback(() => {
442
519
  const viewport = viewportRef.current;
443
520
 
@@ -562,9 +639,9 @@ function useMessageScrollerController({
562
639
  const context = useMemo<MessageScrollerContextValue>(
563
640
  () => ({
564
641
  handleContentChange,
565
- handleResize,
566
642
  observeVisibility,
567
643
  preserveScrollOnPrependRef,
644
+ scheduleResize,
568
645
  scrollToEnd,
569
646
  scrollToMessage,
570
647
  scrollToStart,
@@ -581,8 +658,8 @@ function useMessageScrollerController({
581
658
  }),
582
659
  [
583
660
  handleContentChange,
584
- handleResize,
585
661
  observeVisibility,
662
+ scheduleResize,
586
663
  scrollToEnd,
587
664
  scrollToMessage,
588
665
  scrollToStart,
@@ -627,6 +704,11 @@ function useMessageScrollerController({
627
704
  pendingScrollFrameRef.current = null;
628
705
  }
629
706
 
707
+ if (resizeFrameRef.current !== null) {
708
+ window.cancelAnimationFrame(resizeFrameRef.current);
709
+ resizeFrameRef.current = null;
710
+ }
711
+
630
712
  visibilityObserverRef.current?.disconnect();
631
713
  visibilityObserverRef.current = null;
632
714
  };
@@ -1,6 +1,7 @@
1
- import { useRef } from "react";
1
+ import { useRef, useState } from "react";
2
2
  import type { RefObject } from "react";
3
3
 
4
+ import { useLatest } from "#/hooks/use-latest";
4
5
  import {
5
6
  areScrollStatesEqual,
6
7
  createMessageScrollerStore,
@@ -31,6 +32,7 @@ type MessageScrollerRefs = {
31
32
  defaultScrollPositionAppliedRef: RefObject<boolean>;
32
33
  firstItemRef: RefObject<HTMLElement | null>;
33
34
  itemCountRef: RefObject<number>;
35
+ lastScrollTopRef: RefObject<number>;
34
36
  messageElementsRef: RefObject<Map<string, HTMLElement>>;
35
37
  modeRef: RefObject<MessageScrollerMode>;
36
38
  pendingScrollFrameRef: RefObject<number | null>;
@@ -62,7 +64,7 @@ type MessageScrollerRefs = {
62
64
 
63
65
  /**
64
66
  * Builds the per-instance ref bag: the two external stores constructed once, and
65
- * the latest prop values mirrored onto refs each render so callbacks stay stable.
67
+ * the latest prop values mirrored onto refs at commit so callbacks stay stable.
66
68
  *
67
69
  * @since 0.5.0-canary.3
68
70
  */
@@ -77,13 +79,16 @@ function useMessageScrollerRefs({
77
79
  scrollMargin: number;
78
80
  scrollPreviousItemPeek: number;
79
81
  }): MessageScrollerRefs {
80
- const autoScrollRef = useRef(autoScroll);
82
+ const autoScrollRef = useLatest(autoScroll);
81
83
  const autoscrollingRef = useRef(false);
82
84
  const contentRef = useRef<HTMLDivElement | null>(null);
83
85
  const defaultScrollPositionAppliedRef = useRef(false);
84
- const scrollEdgeThresholdRef = useRef(scrollEdgeThreshold);
86
+ const scrollEdgeThresholdRef = useLatest(scrollEdgeThreshold);
85
87
  const itemCountRef = useRef(0);
86
88
  const firstItemRef = useRef<HTMLElement | null>(null);
89
+ // The scrollTop seen by the previous state commit, so follow-release can tell
90
+ // a reader scrolling up from content growing past the live edge.
91
+ const lastScrollTopRef = useRef(0);
87
92
  const modeRef = useRef<MessageScrollerMode>(autoScroll ? "following-bottom" : "free-scrolling");
88
93
  const messageElementsRef = useRef(new Map<string, HTMLElement>());
89
94
  const pendingScrollToMessageRef = useRef<{
@@ -99,39 +104,27 @@ function useMessageScrollerRefs({
99
104
  // The turn held at the reading line so a reply streaming in below it can re-pin
100
105
  // it instead of letting scrollTop clamp it loose.
101
106
  const streamingTurnRef = useRef<HTMLElement | null>(null);
102
- const scrollPreviousItemPeekRef = useRef(scrollPreviousItemPeek);
107
+ const scrollPreviousItemPeekRef = useLatest(scrollPreviousItemPeek);
103
108
  const preserveScrollOnPrependRef = useRef(true);
104
109
  const rootRef = useRef<HTMLDivElement | null>(null);
105
- const scrollMarginRef = useRef(scrollMargin);
110
+ const scrollMarginRef = useLatest(scrollMargin);
106
111
  const pendingScrollFrameRef = useRef<number | null>(null);
107
112
  const spacerGapRef = useRef(0);
108
113
  const spacerHeightRef = useRef(0);
109
114
  const spacerRef = useRef<HTMLDivElement | null>(null);
110
115
  const stateFrameRef = useRef<number | null>(null);
111
- const stateStoreRef = useRef<MessageScrollerStore<MessageScrollerScrollable> | null>(null);
116
+ // Stores are created once via lazy state init — never reassigned, so they outlive every render.
117
+ const [stateStore] = useState<MessageScrollerStore<MessageScrollerScrollable>>(() =>
118
+ createMessageScrollerStore(EMPTY_MESSAGE_SCROLLER_SCROLLABLE, areScrollStatesEqual),
119
+ );
112
120
  const autoscrollingTimeoutRef = useRef<number | null>(null);
113
121
  const viewportRef = useRef<HTMLDivElement | null>(null);
114
122
  const visibilityFrameRef = useRef<number | null>(null);
115
123
  const visibilityObserverRef = useRef<IntersectionObserver | null>(null);
116
- const visibilityStoreRef = useRef<MessageScrollerVisibilityStore | null>(null);
124
+ const [visibilityStore] = useState<MessageScrollerVisibilityStore>(() => createMessageScrollerVisibilityStore());
117
125
  const visibleMessageIdsRef = useRef(new Set<string>());
118
126
  const handledScrollAnchorsRef = useRef(new WeakSet<HTMLElement>());
119
127
 
120
- if (stateStoreRef.current === null) {
121
- stateStoreRef.current = createMessageScrollerStore(EMPTY_MESSAGE_SCROLLER_SCROLLABLE, areScrollStatesEqual);
122
- }
123
-
124
- if (visibilityStoreRef.current === null) {
125
- visibilityStoreRef.current = createMessageScrollerVisibilityStore();
126
- }
127
-
128
- // Track the latest prop values on every render so callbacks read fresh values
129
- // without being recreated (the useLatest pattern).
130
- autoScrollRef.current = autoScroll;
131
- scrollEdgeThresholdRef.current = scrollEdgeThreshold;
132
- scrollMarginRef.current = scrollMargin;
133
- scrollPreviousItemPeekRef.current = scrollPreviousItemPeek;
134
-
135
128
  return {
136
129
  autoScrollRef,
137
130
  autoscrollingRef,
@@ -141,6 +134,7 @@ function useMessageScrollerRefs({
141
134
  defaultScrollPositionAppliedRef,
142
135
  firstItemRef,
143
136
  itemCountRef,
137
+ lastScrollTopRef,
144
138
  messageElementsRef,
145
139
  modeRef,
146
140
  pendingScrollFrameRef,
@@ -155,11 +149,11 @@ function useMessageScrollerRefs({
155
149
  spacerHeightRef,
156
150
  spacerRef,
157
151
  stateFrameRef,
158
- stateStore: stateStoreRef.current,
152
+ stateStore,
159
153
  viewportRef,
160
154
  visibilityFrameRef,
161
155
  visibilityObserverRef,
162
- visibilityStore: visibilityStoreRef.current,
156
+ visibilityStore,
163
157
  visibleMessageIdsRef,
164
158
  handledScrollAnchorsRef,
165
159
  };
@@ -1,6 +1,8 @@
1
1
  import { useMemo } from "react";
2
2
 
3
3
  /**
4
+ * Options for {@link usePagination}.
5
+ *
4
6
  * @since 0.3.16-canary.0
5
7
  */
6
8
  export interface UsePaginationOptions {
@@ -4,8 +4,8 @@ import {
4
4
  SCROLL_POSITION_EPSILON,
5
5
  } from "#/lib/message-scroller/types";
6
6
  import type {
7
- MessageScrollerScrollable,
8
7
  MessageScrollerScrollAlign,
8
+ MessageScrollerScrollEdges,
9
9
  MessageScrollerVisibilityState,
10
10
  } from "#/lib/message-scroller/types";
11
11
 
@@ -22,7 +22,7 @@ function getMessageScrollerScrollable({
22
22
  scrollEdgeThreshold: number;
23
23
  spacer: HTMLElement | null;
24
24
  viewport: HTMLElement | null;
25
- }): MessageScrollerScrollable {
25
+ }): MessageScrollerScrollEdges {
26
26
  if (!viewport || !content) {
27
27
  return EMPTY_MESSAGE_SCROLLER_SCROLLABLE;
28
28
  }
@@ -62,7 +62,7 @@ function createMessageScrollerVisibilityStore(): MessageScrollerVisibilityStore
62
62
  * @since 0.5.0-canary.3
63
63
  */
64
64
  function areScrollStatesEqual(current: MessageScrollerScrollable, next: MessageScrollerScrollable) {
65
- return current.start === next.start && current.end === next.end;
65
+ return current.start === next.start && current.end === next.end && current.following === next.following;
66
66
  }
67
67
 
68
68
  /**
@@ -104,18 +104,29 @@ type MessageScrollerScrollOptions = {
104
104
  };
105
105
 
106
106
  /**
107
- * Scroll snapshot from useMessageScrollerScrollable: which edges the viewport can
107
+ * The raw geometry half of the scroll snapshot: which edges the viewport can
108
108
  * still scroll toward.
109
109
  *
110
- * @since 0.5.0-canary.3
110
+ * @since 0.9.0
111
111
  */
112
- type MessageScrollerScrollable = {
112
+ type MessageScrollerScrollEdges = {
113
113
  // The viewport can scroll toward the start (content is hidden above).
114
114
  start: boolean;
115
115
  // The viewport can scroll toward the end (content is hidden below).
116
116
  end: boolean;
117
117
  };
118
118
 
119
+ /**
120
+ * Scroll snapshot from useMessageScrollerScrollable: the reachable edges plus
121
+ * whether follow-output is engaged.
122
+ *
123
+ * @since 0.5.0-canary.3
124
+ */
125
+ type MessageScrollerScrollable = MessageScrollerScrollEdges & {
126
+ // Follow-output is engaged and already closing any gap toward the end.
127
+ following: boolean;
128
+ };
129
+
119
130
  /**
120
131
  * Visibility snapshot from useMessageScrollerVisibility.
121
132
  *
@@ -256,9 +267,9 @@ type MessageScrollerRegisterMessage = (
256
267
  */
257
268
  type MessageScrollerContextValue = {
258
269
  handleContentChange: () => void;
259
- handleResize: () => void;
260
270
  observeVisibility: () => void;
261
271
  preserveScrollOnPrependRef: RefObject<boolean>;
272
+ scheduleResize: () => void;
262
273
  scrollToEnd: (options?: MessageScrollerScrollOptions) => boolean;
263
274
  scrollToMessage: (messageId: string, options?: MessageScrollerScrollOptions) => boolean;
264
275
  scrollToStart: (options?: MessageScrollerScrollOptions) => boolean;
@@ -283,6 +294,7 @@ type MessageScrollerContextValue = {
283
294
  const EMPTY_MESSAGE_SCROLLER_SCROLLABLE: MessageScrollerScrollable = {
284
295
  start: false,
285
296
  end: false,
297
+ following: false,
286
298
  };
287
299
 
288
300
  /**
@@ -327,6 +339,7 @@ export type {
327
339
  MessageScrollerProviderProps,
328
340
  MessageScrollerRegisterMessage,
329
341
  MessageScrollerScrollAlign,
342
+ MessageScrollerScrollEdges,
330
343
  MessageScrollerScrollOptions,
331
344
  MessageScrollerScrollable,
332
345
  MessageScrollerStore,
@@ -22,7 +22,6 @@ import type {
22
22
  MessageScrollerRegisterMessage,
23
23
  MessageScrollerViewportProps,
24
24
  } from "#/lib/message-scroller/types";
25
- import { useLatest } from "#/lib/message-scroller/utils";
26
25
  import { composeRefs, mergeProps, useRender } from "#/lib/use-render";
27
26
 
28
27
  const MessageScrollerContext = createContext<MessageScrollerContextValue | null>(null);
@@ -142,15 +141,18 @@ function MessageScrollerViewport({
142
141
  ...props
143
142
  }: MessageScrollerViewportProps) {
144
143
  const {
145
- handleResize,
146
144
  preserveScrollOnPrependRef,
145
+ scheduleResize,
147
146
  setViewportElement,
148
147
  syncAfterScroll,
149
148
  userScrollIntent,
150
149
  viewportRef,
151
150
  } = useMessageScrollerContext();
152
151
 
153
- preserveScrollOnPrependRef.current = preserveScrollOnPrepend;
152
+ // Layout effect, not passive: the MutationObserver reads this ref in the same commit's microtask.
153
+ useLayoutEffect(() => {
154
+ preserveScrollOnPrependRef.current = preserveScrollOnPrepend;
155
+ }, [preserveScrollOnPrependRef, preserveScrollOnPrepend]);
154
156
 
155
157
  const setViewportRef = useCallback(
156
158
  (element: HTMLDivElement | null) => {
@@ -190,12 +192,14 @@ function MessageScrollerViewport({
190
192
  return;
191
193
  }
192
194
 
193
- const observer = new ResizeObserver(handleResize);
195
+ // scheduleResize self-coalesces, deferring any pass that would resize the
196
+ // observed spacer out of ResizeObserver delivery.
197
+ const observer = new ResizeObserver(scheduleResize);
194
198
 
195
199
  observer.observe(viewport);
196
200
 
197
201
  return () => observer.disconnect();
198
- }, [handleResize, viewportRef]);
202
+ }, [scheduleResize, viewportRef]);
199
203
 
200
204
  return (
201
205
  // The viewport defaults to role="region"; the scroll/key handlers back that landmark.
@@ -227,7 +231,7 @@ function MessageScrollerContent({
227
231
  spacerClassName,
228
232
  ...props
229
233
  }: MessageScrollerContentProps) {
230
- const { handleContentChange, handleResize, setContentElement, setSpacerElement } = useMessageScrollerContext();
234
+ const { handleContentChange, scheduleResize, setContentElement, setSpacerElement } = useMessageScrollerContext();
231
235
  const contentRef = useRef<HTMLDivElement | null>(null);
232
236
 
233
237
  const setContentRef = useCallback(
@@ -268,12 +272,14 @@ function MessageScrollerContent({
268
272
  return;
269
273
  }
270
274
 
271
- const observer = new ResizeObserver(handleResize);
275
+ // scheduleResize self-coalesces, deferring any pass that would resize the
276
+ // spacer inside this observed element out of ResizeObserver delivery.
277
+ const observer = new ResizeObserver(scheduleResize);
272
278
 
273
279
  observer.observe(content);
274
280
 
275
281
  return () => observer.disconnect();
276
- }, [handleResize]);
282
+ }, [scheduleResize]);
277
283
 
278
284
  return (
279
285
  <div ref={setContentRef} role={role ?? "log"} aria-relevant={ariaRelevant ?? "additions"} {...props}>
@@ -335,12 +341,17 @@ function MessageScrollerButton({
335
341
  ...props
336
342
  }: MessageScrollerButtonProps) {
337
343
  const { scrollToEnd, scrollToStart, stateStore } = useMessageScrollerContext();
338
- const onClickRef = useLatest(onClick);
339
344
  const subscribe = useCallback((listener: () => void) => stateStore.subscribe(listener), [stateStore]);
340
345
  const getSnapshot = useCallback(() => {
341
346
  const state = stateStore.getSnapshot();
342
347
 
343
- return direction === "start" ? state.start : state.end;
348
+ if (direction === "start") {
349
+ return state.start;
350
+ }
351
+
352
+ // Follow-output is already closing any end gap; advertising it would
353
+ // strobe the button once per streamed chunk.
354
+ return state.end && !state.following;
344
355
  }, [direction, stateStore]);
345
356
  const isActive = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
346
357
 
@@ -350,7 +361,7 @@ function MessageScrollerButton({
350
361
  return;
351
362
  }
352
363
 
353
- onClickRef.current?.(event);
364
+ onClick?.(event);
354
365
 
355
366
  if (!event.defaultPrevented) {
356
367
  event.currentTarget.blur();
@@ -362,7 +373,7 @@ function MessageScrollerButton({
362
373
  }
363
374
  }
364
375
  },
365
- [behavior, direction, isActive, onClickRef, scrollToEnd, scrollToStart],
376
+ [behavior, direction, isActive, onClick, scrollToEnd, scrollToStart],
366
377
  );
367
378
 
368
379
  return useRender({
@@ -1,6 +0,0 @@
1
- /**
2
- * @since 0.5.0-canary.3
3
- */
4
- declare function useLatest<T>(value: T): import("react").RefObject<T>;
5
- export { useLatest };
6
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/lib/message-scroller/utils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,iBAAS,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,gCAM7B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1,11 +0,0 @@
1
- import { useRef } from "react";
2
- /**
3
- * @since 0.5.0-canary.3
4
- */
5
- function useLatest(value) {
6
- const ref = useRef(value);
7
- ref.current = value;
8
- return ref;
9
- }
10
- export { useLatest };
11
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/message-scroller/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B;;GAEG;AACH,SAAS,SAAS,CAAI,KAAQ;IAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAE1B,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC;IAEpB,OAAO,GAAG,CAAC;AACb,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1,14 +0,0 @@
1
- import { useRef } from "react";
2
-
3
- /**
4
- * @since 0.5.0-canary.3
5
- */
6
- function useLatest<T>(value: T) {
7
- const ref = useRef(value);
8
-
9
- ref.current = value;
10
-
11
- return ref;
12
- }
13
-
14
- export { useLatest };