@carbonid1/design-system 5.7.11 → 5.8.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.
- package/dist/MessageScroller/MessageScroller.d.ts +18 -0
- package/dist/MessageScroller/MessageScroller.js +885 -0
- package/dist/MessageScroller/MessageScroller.types.d.ts +47 -0
- package/dist/MessageScroller/MessageScroller.types.js +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { MessageScrollerContentProps, MessageScrollerController, MessageScrollerItemProps, MessageScrollerProviderProps, MessageScrollerRootProps, MessageScrollerStatus, MessageScrollerViewportProps, MessageScrollerVisibility } from './MessageScroller.types';
|
|
2
|
+
declare function MessageScrollerProvider({ autoFollow, children, defaultScrollPosition, scrollEdgeThreshold, scrollMargin, scrollPreviousItemPeek, }: MessageScrollerProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
3
|
+
declare function MessageScrollerRoot({ children, className, ref, ...props }: MessageScrollerRootProps): import("react/jsx-runtime").JSX.Element;
|
|
4
|
+
declare function MessageScrollerViewport({ 'aria-label': ariaLabel, children, className, onKeyDown, onScroll, onTouchCancel, onTouchEnd, onTouchMove, onTouchStart, onWheel, preserveScrollOnPrepend, ref, role, tabIndex, ...props }: MessageScrollerViewportProps): import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
declare function MessageScrollerContent({ 'aria-relevant': ariaRelevant, children, className, ref, role, spacerClassName, ...props }: MessageScrollerContentProps): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
declare function MessageScrollerItem({ children, messageId, ref, scrollAnchor, ...props }: MessageScrollerItemProps): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
/** Headless transcript scroller shared by carbonid1 apps; consumers own row rendering. */
|
|
8
|
+
declare function useMessageScroller(): MessageScrollerController;
|
|
9
|
+
declare function useMessageScrollerStatus(): MessageScrollerStatus;
|
|
10
|
+
declare function useMessageScrollerVisibility(): MessageScrollerVisibility;
|
|
11
|
+
declare const MessageScroller: {
|
|
12
|
+
Content: typeof MessageScrollerContent;
|
|
13
|
+
Item: typeof MessageScrollerItem;
|
|
14
|
+
Provider: typeof MessageScrollerProvider;
|
|
15
|
+
Root: typeof MessageScrollerRoot;
|
|
16
|
+
Viewport: typeof MessageScrollerViewport;
|
|
17
|
+
};
|
|
18
|
+
export { MessageScroller, useMessageScroller, useMessageScrollerStatus, useMessageScrollerVisibility, };
|
|
@@ -0,0 +1,885 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useSyncExternalStore, } from 'react';
|
|
4
|
+
import { cn } from '../helpers/cn/cn';
|
|
5
|
+
const DEFAULT_SCROLL_EDGE_THRESHOLD = 8;
|
|
6
|
+
const DEFAULT_SCROLL_MARGIN = 0;
|
|
7
|
+
const DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK = 64;
|
|
8
|
+
const SCROLL_TOLERANCE = 0.5;
|
|
9
|
+
const USER_SCROLL_INTENT_KEYS = new Set([
|
|
10
|
+
'ArrowDown',
|
|
11
|
+
'ArrowUp',
|
|
12
|
+
'End',
|
|
13
|
+
'Home',
|
|
14
|
+
'PageDown',
|
|
15
|
+
'PageUp',
|
|
16
|
+
' ',
|
|
17
|
+
]);
|
|
18
|
+
const EMPTY_VISIBLE_MESSAGE_IDS = [];
|
|
19
|
+
const DEFAULT_STATUS = {
|
|
20
|
+
canScrollToBottom: false,
|
|
21
|
+
canScrollToTop: false,
|
|
22
|
+
isAtBottom: true,
|
|
23
|
+
isAtTop: true,
|
|
24
|
+
};
|
|
25
|
+
const DEFAULT_VISIBILITY = {
|
|
26
|
+
anchorMessageId: null,
|
|
27
|
+
visibleMessageIds: EMPTY_VISIBLE_MESSAGE_IDS,
|
|
28
|
+
};
|
|
29
|
+
const MessageScrollerContext = createContext(null);
|
|
30
|
+
function createStore(initialSnapshot, isEqual) {
|
|
31
|
+
let snapshot = initialSnapshot;
|
|
32
|
+
const listeners = new Set();
|
|
33
|
+
return {
|
|
34
|
+
getSnapshot: () => snapshot,
|
|
35
|
+
setSnapshot: nextSnapshot => {
|
|
36
|
+
if (isEqual(snapshot, nextSnapshot)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
snapshot = nextSnapshot;
|
|
40
|
+
listeners.forEach(listener => listener());
|
|
41
|
+
},
|
|
42
|
+
subscribe: listener => {
|
|
43
|
+
listeners.add(listener);
|
|
44
|
+
return () => listeners.delete(listener);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function areStatusSnapshotsEqual(currentSnapshot, nextSnapshot) {
|
|
49
|
+
return (currentSnapshot.canScrollToBottom === nextSnapshot.canScrollToBottom &&
|
|
50
|
+
currentSnapshot.canScrollToTop === nextSnapshot.canScrollToTop &&
|
|
51
|
+
currentSnapshot.isAtBottom === nextSnapshot.isAtBottom &&
|
|
52
|
+
currentSnapshot.isAtTop === nextSnapshot.isAtTop);
|
|
53
|
+
}
|
|
54
|
+
function areVisibilitySnapshotsEqual(currentSnapshot, nextSnapshot) {
|
|
55
|
+
return (currentSnapshot.anchorMessageId === nextSnapshot.anchorMessageId &&
|
|
56
|
+
currentSnapshot.visibleMessageIds.length === nextSnapshot.visibleMessageIds.length &&
|
|
57
|
+
currentSnapshot.visibleMessageIds.every((messageId, index) => messageId === nextSnapshot.visibleMessageIds[index]));
|
|
58
|
+
}
|
|
59
|
+
function assignRef(ref, element) {
|
|
60
|
+
if (!ref) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (typeof ref === 'function') {
|
|
64
|
+
ref(element);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
;
|
|
68
|
+
ref.current = element;
|
|
69
|
+
}
|
|
70
|
+
function getMessageElements(contentElement, spacerElement) {
|
|
71
|
+
if (!contentElement) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
return Array.from(contentElement.children).filter((child) => child instanceof HTMLElement && child !== spacerElement);
|
|
75
|
+
}
|
|
76
|
+
function getMaximumScrollTop(viewportElement) {
|
|
77
|
+
return Math.max(0, viewportElement.scrollHeight - viewportElement.clientHeight);
|
|
78
|
+
}
|
|
79
|
+
function canWheelScroll(viewportElement, deltaY) {
|
|
80
|
+
if (deltaY < 0) {
|
|
81
|
+
return viewportElement.scrollTop > SCROLL_TOLERANCE;
|
|
82
|
+
}
|
|
83
|
+
if (deltaY > 0) {
|
|
84
|
+
return getMaximumScrollTop(viewportElement) - viewportElement.scrollTop > SCROLL_TOLERANCE;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function canKeyboardScroll(viewportElement, event) {
|
|
89
|
+
const canScrollToTop = viewportElement.scrollTop > SCROLL_TOLERANCE;
|
|
90
|
+
const canScrollToBottom = getMaximumScrollTop(viewportElement) - viewportElement.scrollTop > SCROLL_TOLERANCE;
|
|
91
|
+
if (event.key === 'ArrowUp' || event.key === 'Home' || event.key === 'PageUp') {
|
|
92
|
+
return canScrollToTop;
|
|
93
|
+
}
|
|
94
|
+
if (event.key === ' ') {
|
|
95
|
+
return event.shiftKey ? canScrollToTop : canScrollToBottom;
|
|
96
|
+
}
|
|
97
|
+
if (event.key === 'ArrowDown' || event.key === 'End' || event.key === 'PageDown') {
|
|
98
|
+
return canScrollToBottom;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function getElementTop(element, viewportElement) {
|
|
103
|
+
const elementRectangle = element.getBoundingClientRect();
|
|
104
|
+
const viewportRectangle = viewportElement.getBoundingClientRect();
|
|
105
|
+
return elementRectangle.top - viewportRectangle.top + viewportElement.scrollTop;
|
|
106
|
+
}
|
|
107
|
+
function getElementViewportTop(element, viewportElement) {
|
|
108
|
+
return element.getBoundingClientRect().top - viewportElement.getBoundingClientRect().top;
|
|
109
|
+
}
|
|
110
|
+
function getTargetScrollTop({ align, element, scrollMargin, viewportElement, }) {
|
|
111
|
+
const elementTop = getElementTop(element, viewportElement);
|
|
112
|
+
const elementHeight = element.getBoundingClientRect().height;
|
|
113
|
+
if (align === 'center') {
|
|
114
|
+
return elementTop - (viewportElement.clientHeight - elementHeight) / 2;
|
|
115
|
+
}
|
|
116
|
+
if (align === 'end') {
|
|
117
|
+
return elementTop - viewportElement.clientHeight + elementHeight + scrollMargin;
|
|
118
|
+
}
|
|
119
|
+
if (align === 'nearest') {
|
|
120
|
+
const elementBottom = elementTop + elementHeight;
|
|
121
|
+
const viewportTop = viewportElement.scrollTop + scrollMargin;
|
|
122
|
+
const viewportBottom = viewportElement.scrollTop + viewportElement.clientHeight - scrollMargin;
|
|
123
|
+
if (elementTop >= viewportTop && elementBottom <= viewportBottom) {
|
|
124
|
+
return viewportElement.scrollTop;
|
|
125
|
+
}
|
|
126
|
+
return elementTop < viewportTop
|
|
127
|
+
? elementTop - scrollMargin
|
|
128
|
+
: elementBottom - viewportElement.clientHeight + scrollMargin;
|
|
129
|
+
}
|
|
130
|
+
return elementTop - scrollMargin;
|
|
131
|
+
}
|
|
132
|
+
function getLastAnchorElement(messageElements) {
|
|
133
|
+
for (let index = messageElements.length - 1; index >= 0; index -= 1) {
|
|
134
|
+
const element = messageElements[index];
|
|
135
|
+
if (element?.dataset.scrollAnchor === 'true') {
|
|
136
|
+
return element;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function getFirstUnhandledAnchorElement(messageElements, handledAnchorElements) {
|
|
142
|
+
return (messageElements.find(element => element.dataset.scrollAnchor === 'true' && !handledAnchorElements.has(element)) ?? null);
|
|
143
|
+
}
|
|
144
|
+
function getGap(element) {
|
|
145
|
+
if (!element) {
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
const styles = window.getComputedStyle(element);
|
|
149
|
+
const gap = styles.rowGap === 'normal' ? styles.gap : styles.rowGap;
|
|
150
|
+
const parsedGap = Number.parseFloat(gap);
|
|
151
|
+
return Number.isFinite(parsedGap) ? parsedGap : 0;
|
|
152
|
+
}
|
|
153
|
+
function useMessageScrollerContext(callerName) {
|
|
154
|
+
const context = useContext(MessageScrollerContext);
|
|
155
|
+
if (!context) {
|
|
156
|
+
throw new Error(`${callerName} must be used within a MessageScroller.Provider.`);
|
|
157
|
+
}
|
|
158
|
+
return context;
|
|
159
|
+
}
|
|
160
|
+
function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdgeThreshold, scrollMargin, scrollPreviousItemPeek, }) {
|
|
161
|
+
const rootElementRef = useRef(null);
|
|
162
|
+
const viewportElementRef = useRef(null);
|
|
163
|
+
const contentElementRef = useRef(null);
|
|
164
|
+
const spacerElementRef = useRef(null);
|
|
165
|
+
const messageElementsRef = useRef(new Map());
|
|
166
|
+
const handledAnchorElementsRef = useRef(new WeakSet());
|
|
167
|
+
const preserveScrollOnPrependRef = useRef(true);
|
|
168
|
+
const autoFollowRef = useRef(autoFollow);
|
|
169
|
+
const defaultScrollPositionRef = useRef(defaultScrollPosition);
|
|
170
|
+
const scrollEdgeThresholdRef = useRef(scrollEdgeThreshold);
|
|
171
|
+
const scrollMarginRef = useRef(scrollMargin);
|
|
172
|
+
const scrollPreviousItemPeekRef = useRef(scrollPreviousItemPeek);
|
|
173
|
+
const modeRef = useRef(autoFollow ? 'following-bottom' : 'free-scrolling');
|
|
174
|
+
const autoscrollingRef = useRef(false);
|
|
175
|
+
// Smooth bottom scrolls can briefly measure as away from bottom while still settling.
|
|
176
|
+
const bottomFollowIntentRef = useRef(autoFollow);
|
|
177
|
+
const ignoreAnchoredBottomResumeFrameRef = useRef(null);
|
|
178
|
+
const ignoreAnchoredBottomResumeRef = useRef(false);
|
|
179
|
+
const pendingDefaultScrollFrameRef = useRef(null);
|
|
180
|
+
const pendingSyncFrameRef = useRef(null);
|
|
181
|
+
const pendingScrollToMessageRef = useRef(null);
|
|
182
|
+
const prependRestoreRef = useRef(null);
|
|
183
|
+
const anchoredElementRef = useRef(null);
|
|
184
|
+
const spacerGapRef = useRef(0);
|
|
185
|
+
const spacerHeightRef = useRef(0);
|
|
186
|
+
const defaultScrollPositionAppliedRef = useRef(false);
|
|
187
|
+
const statusStoreRef = useRef(null);
|
|
188
|
+
const visibilityStoreRef = useRef(null);
|
|
189
|
+
if (!statusStoreRef.current) {
|
|
190
|
+
statusStoreRef.current = createStore(DEFAULT_STATUS, areStatusSnapshotsEqual);
|
|
191
|
+
}
|
|
192
|
+
if (!visibilityStoreRef.current) {
|
|
193
|
+
visibilityStoreRef.current = createStore(DEFAULT_VISIBILITY, areVisibilitySnapshotsEqual);
|
|
194
|
+
}
|
|
195
|
+
const statusStore = statusStoreRef.current;
|
|
196
|
+
const visibilityStore = visibilityStoreRef.current;
|
|
197
|
+
autoFollowRef.current = autoFollow;
|
|
198
|
+
scrollEdgeThresholdRef.current = scrollEdgeThreshold;
|
|
199
|
+
scrollMarginRef.current = scrollMargin;
|
|
200
|
+
scrollPreviousItemPeekRef.current = scrollPreviousItemPeek;
|
|
201
|
+
if (defaultScrollPositionRef.current !== defaultScrollPosition) {
|
|
202
|
+
defaultScrollPositionRef.current = defaultScrollPosition;
|
|
203
|
+
defaultScrollPositionAppliedRef.current = false;
|
|
204
|
+
}
|
|
205
|
+
const applyStatusAttributes = useCallback((status) => {
|
|
206
|
+
const scrollableValue = [status.canScrollToTop && 'top', status.canScrollToBottom && 'bottom']
|
|
207
|
+
.filter(Boolean)
|
|
208
|
+
.join(' ');
|
|
209
|
+
for (const element of [rootElementRef.current, viewportElementRef.current]) {
|
|
210
|
+
if (!element) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (scrollableValue) {
|
|
214
|
+
element.setAttribute('data-scrollable', scrollableValue);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
element.removeAttribute('data-scrollable');
|
|
218
|
+
}
|
|
219
|
+
element.toggleAttribute('data-at-bottom', status.isAtBottom);
|
|
220
|
+
element.toggleAttribute('data-at-top', status.isAtTop);
|
|
221
|
+
}
|
|
222
|
+
}, [rootElementRef, viewportElementRef]);
|
|
223
|
+
const measureStatus = useCallback(() => {
|
|
224
|
+
const viewportElement = viewportElementRef.current;
|
|
225
|
+
if (!viewportElement) {
|
|
226
|
+
return DEFAULT_STATUS;
|
|
227
|
+
}
|
|
228
|
+
const canScrollToTop = viewportElement.scrollTop > scrollEdgeThresholdRef.current;
|
|
229
|
+
const canScrollToBottom = getMaximumScrollTop(viewportElement) - viewportElement.scrollTop >
|
|
230
|
+
scrollEdgeThresholdRef.current;
|
|
231
|
+
return {
|
|
232
|
+
canScrollToBottom,
|
|
233
|
+
canScrollToTop,
|
|
234
|
+
isAtBottom: !canScrollToBottom,
|
|
235
|
+
isAtTop: !canScrollToTop,
|
|
236
|
+
};
|
|
237
|
+
}, []);
|
|
238
|
+
const setSpacerHeight = useCallback((height) => {
|
|
239
|
+
const nextHeight = Math.max(0, Math.ceil(height));
|
|
240
|
+
const spacerElement = spacerElementRef.current;
|
|
241
|
+
spacerHeightRef.current = nextHeight;
|
|
242
|
+
if (!spacerElement) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
spacerElement.hidden = nextHeight === 0;
|
|
246
|
+
spacerElement.style.height = `${nextHeight}px`;
|
|
247
|
+
spacerElement.style.marginTop = nextHeight > 0 ? `${-spacerGapRef.current}px` : '';
|
|
248
|
+
}, []);
|
|
249
|
+
const clearSpacer = useCallback(() => {
|
|
250
|
+
setSpacerHeight(0);
|
|
251
|
+
}, [setSpacerHeight]);
|
|
252
|
+
const commitStatus = useCallback(() => {
|
|
253
|
+
const status = measureStatus();
|
|
254
|
+
const isAnchoredMode = modeRef.current === 'anchored-to-message' || modeRef.current === 'settling-jump';
|
|
255
|
+
if (autoFollowRef.current && bottomFollowIntentRef.current && !isAnchoredMode) {
|
|
256
|
+
modeRef.current = 'following-bottom';
|
|
257
|
+
}
|
|
258
|
+
if (autoFollowRef.current && status.isAtBottom && !isAnchoredMode) {
|
|
259
|
+
modeRef.current = 'following-bottom';
|
|
260
|
+
autoscrollingRef.current = false;
|
|
261
|
+
anchoredElementRef.current = null;
|
|
262
|
+
}
|
|
263
|
+
else if (modeRef.current === 'following-bottom' &&
|
|
264
|
+
!status.isAtBottom &&
|
|
265
|
+
!autoscrollingRef.current &&
|
|
266
|
+
!bottomFollowIntentRef.current) {
|
|
267
|
+
modeRef.current = 'free-scrolling';
|
|
268
|
+
}
|
|
269
|
+
applyStatusAttributes(status);
|
|
270
|
+
statusStore.setSnapshot(status);
|
|
271
|
+
return status;
|
|
272
|
+
}, [applyStatusAttributes, measureStatus, statusStore]);
|
|
273
|
+
const syncVisibility = useCallback(() => {
|
|
274
|
+
const viewportElement = viewportElementRef.current;
|
|
275
|
+
const contentElement = contentElementRef.current;
|
|
276
|
+
if (!viewportElement || !contentElement) {
|
|
277
|
+
visibilityStore.setSnapshot(DEFAULT_VISIBILITY);
|
|
278
|
+
return DEFAULT_VISIBILITY;
|
|
279
|
+
}
|
|
280
|
+
const viewportRectangle = viewportElement.getBoundingClientRect();
|
|
281
|
+
const anchorBoundary = viewportRectangle.top + scrollMarginRef.current + scrollPreviousItemPeekRef.current;
|
|
282
|
+
const visibleMessageIds = [];
|
|
283
|
+
let anchorMessageId = null;
|
|
284
|
+
for (const element of getMessageElements(contentElement, spacerElementRef.current)) {
|
|
285
|
+
const messageId = element.dataset.messageId;
|
|
286
|
+
if (!messageId) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const elementRectangle = element.getBoundingClientRect();
|
|
290
|
+
if (elementRectangle.bottom > viewportRectangle.top &&
|
|
291
|
+
elementRectangle.top < viewportRectangle.bottom) {
|
|
292
|
+
visibleMessageIds.push(messageId);
|
|
293
|
+
}
|
|
294
|
+
if (element.dataset.scrollAnchor === 'true' &&
|
|
295
|
+
elementRectangle.top <= anchorBoundary &&
|
|
296
|
+
elementRectangle.bottom > viewportRectangle.top) {
|
|
297
|
+
anchorMessageId = messageId;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const visibility = {
|
|
301
|
+
anchorMessageId,
|
|
302
|
+
visibleMessageIds: visibleMessageIds.length > 0 ? visibleMessageIds : EMPTY_VISIBLE_MESSAGE_IDS,
|
|
303
|
+
};
|
|
304
|
+
visibilityStore.setSnapshot(visibility);
|
|
305
|
+
return visibility;
|
|
306
|
+
}, [visibilityStore]);
|
|
307
|
+
const getFirstVisibleMessageElement = useCallback(() => {
|
|
308
|
+
const viewportElement = viewportElementRef.current;
|
|
309
|
+
const contentElement = contentElementRef.current;
|
|
310
|
+
if (!viewportElement || !contentElement) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
const viewportRectangle = viewportElement.getBoundingClientRect();
|
|
314
|
+
return (getMessageElements(contentElement, spacerElementRef.current).find(element => {
|
|
315
|
+
const elementRectangle = element.getBoundingClientRect();
|
|
316
|
+
return (Boolean(element.dataset.messageId) &&
|
|
317
|
+
elementRectangle.bottom > viewportRectangle.top &&
|
|
318
|
+
elementRectangle.top < viewportRectangle.bottom);
|
|
319
|
+
}) ?? null);
|
|
320
|
+
}, []);
|
|
321
|
+
const capturePrependAnchor = useCallback(() => {
|
|
322
|
+
const viewportElement = viewportElementRef.current;
|
|
323
|
+
const firstVisibleElement = getFirstVisibleMessageElement();
|
|
324
|
+
prependRestoreRef.current =
|
|
325
|
+
viewportElement && firstVisibleElement
|
|
326
|
+
? {
|
|
327
|
+
element: firstVisibleElement,
|
|
328
|
+
viewportTop: getElementViewportTop(firstVisibleElement, viewportElement),
|
|
329
|
+
}
|
|
330
|
+
: null;
|
|
331
|
+
}, [getFirstVisibleMessageElement]);
|
|
332
|
+
const syncAfterScroll = useCallback(() => {
|
|
333
|
+
commitStatus();
|
|
334
|
+
syncVisibility();
|
|
335
|
+
capturePrependAnchor();
|
|
336
|
+
}, [capturePrependAnchor, commitStatus, syncVisibility]);
|
|
337
|
+
const ignoreAnchoredBottomResumeForCurrentFrame = useCallback(() => {
|
|
338
|
+
ignoreAnchoredBottomResumeRef.current = true;
|
|
339
|
+
if (ignoreAnchoredBottomResumeFrameRef.current !== null) {
|
|
340
|
+
window.cancelAnimationFrame(ignoreAnchoredBottomResumeFrameRef.current);
|
|
341
|
+
}
|
|
342
|
+
ignoreAnchoredBottomResumeFrameRef.current = window.requestAnimationFrame(() => {
|
|
343
|
+
ignoreAnchoredBottomResumeFrameRef.current = null;
|
|
344
|
+
ignoreAnchoredBottomResumeRef.current = false;
|
|
345
|
+
});
|
|
346
|
+
}, []);
|
|
347
|
+
const scheduleSyncAfterScroll = useCallback(() => {
|
|
348
|
+
if (pendingSyncFrameRef.current !== null) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
pendingSyncFrameRef.current = window.requestAnimationFrame(() => {
|
|
352
|
+
pendingSyncFrameRef.current = null;
|
|
353
|
+
syncAfterScroll();
|
|
354
|
+
});
|
|
355
|
+
}, [syncAfterScroll]);
|
|
356
|
+
const setAutoscrolling = useCallback((autoscrolling) => {
|
|
357
|
+
autoscrollingRef.current = autoscrolling;
|
|
358
|
+
applyStatusAttributes(statusStore.getSnapshot());
|
|
359
|
+
}, [applyStatusAttributes, statusStore]);
|
|
360
|
+
const scrollToTopValue = useCallback((scrollTop, options = {}) => {
|
|
361
|
+
const viewportElement = viewportElementRef.current;
|
|
362
|
+
if (!viewportElement) {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
const nextScrollTop = Math.max(0, scrollTop);
|
|
366
|
+
if (options.autoscrolling) {
|
|
367
|
+
setAutoscrolling(true);
|
|
368
|
+
}
|
|
369
|
+
if (Math.abs(viewportElement.scrollTop - nextScrollTop) <= SCROLL_TOLERANCE) {
|
|
370
|
+
viewportElement.scrollTop = nextScrollTop;
|
|
371
|
+
syncAfterScroll();
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
if ((options.behavior ?? 'auto') === 'auto') {
|
|
375
|
+
viewportElement.scrollTop = nextScrollTop;
|
|
376
|
+
syncAfterScroll();
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
viewportElement.scrollTo({
|
|
380
|
+
behavior: options.behavior,
|
|
381
|
+
top: nextScrollTop,
|
|
382
|
+
});
|
|
383
|
+
scheduleSyncAfterScroll();
|
|
384
|
+
return true;
|
|
385
|
+
}, [scheduleSyncAfterScroll, setAutoscrolling, syncAfterScroll]);
|
|
386
|
+
const scrollToTop = useCallback(({ behavior = 'auto' } = {}) => {
|
|
387
|
+
clearSpacer();
|
|
388
|
+
setAutoscrolling(false);
|
|
389
|
+
anchoredElementRef.current = null;
|
|
390
|
+
bottomFollowIntentRef.current = false;
|
|
391
|
+
modeRef.current = 'free-scrolling';
|
|
392
|
+
return scrollToTopValue(0, { behavior });
|
|
393
|
+
}, [clearSpacer, scrollToTopValue, setAutoscrolling]);
|
|
394
|
+
const scrollToBottom = useCallback(({ behavior = 'auto' } = {}) => {
|
|
395
|
+
const viewportElement = viewportElementRef.current;
|
|
396
|
+
if (!viewportElement) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
clearSpacer();
|
|
400
|
+
anchoredElementRef.current = null;
|
|
401
|
+
modeRef.current = autoFollowRef.current ? 'following-bottom' : 'free-scrolling';
|
|
402
|
+
bottomFollowIntentRef.current =
|
|
403
|
+
autoFollowRef.current && (behavior === 'smooth' || bottomFollowIntentRef.current);
|
|
404
|
+
return scrollToTopValue(getMaximumScrollTop(viewportElement), {
|
|
405
|
+
autoscrolling: true,
|
|
406
|
+
behavior,
|
|
407
|
+
});
|
|
408
|
+
}, [clearSpacer, scrollToTopValue]);
|
|
409
|
+
const scrollToElement = useCallback((element, { align = 'start', behavior = 'auto', keepPreviousItemPeek = false, scrollMargin: scrollMarginOption, } = {}) => {
|
|
410
|
+
const viewportElement = viewportElementRef.current;
|
|
411
|
+
const contentElement = contentElementRef.current;
|
|
412
|
+
if (!viewportElement || !contentElement || !contentElement.contains(element)) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
clearSpacer();
|
|
416
|
+
setAutoscrolling(false);
|
|
417
|
+
bottomFollowIntentRef.current = false;
|
|
418
|
+
const effectiveScrollMargin = scrollMarginOption ??
|
|
419
|
+
scrollMarginRef.current + (keepPreviousItemPeek ? scrollPreviousItemPeekRef.current : 0);
|
|
420
|
+
const targetScrollTop = getTargetScrollTop({
|
|
421
|
+
align,
|
|
422
|
+
element,
|
|
423
|
+
scrollMargin: effectiveScrollMargin,
|
|
424
|
+
viewportElement,
|
|
425
|
+
});
|
|
426
|
+
const spacerHeight = targetScrollTop + viewportElement.clientHeight - viewportElement.scrollHeight;
|
|
427
|
+
setSpacerHeight(spacerHeight);
|
|
428
|
+
anchoredElementRef.current = keepPreviousItemPeek ? element : null;
|
|
429
|
+
modeRef.current = keepPreviousItemPeek ? 'anchored-to-message' : 'settling-jump';
|
|
430
|
+
ignoreAnchoredBottomResumeForCurrentFrame();
|
|
431
|
+
return scrollToTopValue(targetScrollTop, { behavior });
|
|
432
|
+
}, [
|
|
433
|
+
clearSpacer,
|
|
434
|
+
ignoreAnchoredBottomResumeForCurrentFrame,
|
|
435
|
+
scrollToTopValue,
|
|
436
|
+
setAutoscrolling,
|
|
437
|
+
setSpacerHeight,
|
|
438
|
+
]);
|
|
439
|
+
const flushPendingScrollToMessage = useCallback(() => {
|
|
440
|
+
const pendingScrollToMessage = pendingScrollToMessageRef.current;
|
|
441
|
+
if (!pendingScrollToMessage) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
const element = messageElementsRef.current.get(pendingScrollToMessage.messageId);
|
|
445
|
+
if (!element) {
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
pendingScrollToMessageRef.current = null;
|
|
449
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
450
|
+
return scrollToElement(element, pendingScrollToMessage.options);
|
|
451
|
+
}, [scrollToElement]);
|
|
452
|
+
const scrollToMessage = useCallback((messageId, options) => {
|
|
453
|
+
const element = messageElementsRef.current.get(messageId);
|
|
454
|
+
if (!element) {
|
|
455
|
+
if (messageElementsRef.current.size === 0) {
|
|
456
|
+
pendingScrollToMessageRef.current = { messageId, options };
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
pendingScrollToMessageRef.current = null;
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
pendingScrollToMessageRef.current = null;
|
|
463
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
464
|
+
return scrollToElement(element, options);
|
|
465
|
+
}, [scrollToElement]);
|
|
466
|
+
const scheduleDefaultBottomScroll = useCallback(() => {
|
|
467
|
+
if (pendingDefaultScrollFrameRef.current !== null) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
pendingDefaultScrollFrameRef.current = window.requestAnimationFrame(() => {
|
|
471
|
+
pendingDefaultScrollFrameRef.current = null;
|
|
472
|
+
if (defaultScrollPositionRef.current === 'bottom' &&
|
|
473
|
+
autoFollowRef.current &&
|
|
474
|
+
modeRef.current === 'following-bottom') {
|
|
475
|
+
scrollToBottom({ behavior: 'auto' });
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}, [scrollToBottom]);
|
|
479
|
+
const markExistingAnchorsHandled = useCallback(() => {
|
|
480
|
+
const contentElement = contentElementRef.current;
|
|
481
|
+
for (const element of getMessageElements(contentElement, spacerElementRef.current)) {
|
|
482
|
+
if (element.dataset.scrollAnchor === 'true') {
|
|
483
|
+
handledAnchorElementsRef.current.add(element);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}, []);
|
|
487
|
+
const markAnchorsBeforePrependRestoreHandled = useCallback((messageElements) => {
|
|
488
|
+
const restoreElement = prependRestoreRef.current?.element;
|
|
489
|
+
if (!restoreElement) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
for (const element of messageElements) {
|
|
493
|
+
if (element === restoreElement) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (element.dataset.scrollAnchor === 'true') {
|
|
497
|
+
handledAnchorElementsRef.current.add(element);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}, []);
|
|
501
|
+
const applyDefaultScrollPosition = useCallback(() => {
|
|
502
|
+
if (defaultScrollPositionAppliedRef.current) {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
const contentElement = contentElementRef.current;
|
|
506
|
+
const viewportElement = viewportElementRef.current;
|
|
507
|
+
if (!contentElement || !viewportElement) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
const messageElements = getMessageElements(contentElement, spacerElementRef.current);
|
|
511
|
+
if (messageElements.length === 0) {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
515
|
+
if (defaultScrollPositionRef.current === 'top') {
|
|
516
|
+
return scrollToTop({ behavior: 'auto' });
|
|
517
|
+
}
|
|
518
|
+
if (defaultScrollPositionRef.current === 'last-anchor') {
|
|
519
|
+
const lastAnchorElement = getLastAnchorElement(messageElements);
|
|
520
|
+
if (lastAnchorElement) {
|
|
521
|
+
return scrollToElement(lastAnchorElement, {
|
|
522
|
+
align: 'start',
|
|
523
|
+
behavior: 'auto',
|
|
524
|
+
keepPreviousItemPeek: true,
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const didScroll = scrollToBottom({ behavior: 'auto' });
|
|
529
|
+
scheduleDefaultBottomScroll();
|
|
530
|
+
return didScroll;
|
|
531
|
+
}, [scheduleDefaultBottomScroll, scrollToBottom, scrollToElement, scrollToTop]);
|
|
532
|
+
const restorePrependAnchor = useCallback(() => {
|
|
533
|
+
const restore = prependRestoreRef.current;
|
|
534
|
+
const viewportElement = viewportElementRef.current;
|
|
535
|
+
if (!preserveScrollOnPrependRef.current ||
|
|
536
|
+
!restore ||
|
|
537
|
+
!viewportElement ||
|
|
538
|
+
!restore.element.isConnected) {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
const offset = getElementViewportTop(restore.element, viewportElement) - restore.viewportTop;
|
|
542
|
+
if (Math.abs(offset) <= SCROLL_TOLERANCE) {
|
|
543
|
+
return false;
|
|
544
|
+
}
|
|
545
|
+
viewportElement.scrollTop += offset;
|
|
546
|
+
syncAfterScroll();
|
|
547
|
+
return true;
|
|
548
|
+
}, [syncAfterScroll]);
|
|
549
|
+
const reanchorToAnchoredMessage = useCallback(() => {
|
|
550
|
+
const anchoredElement = anchoredElementRef.current;
|
|
551
|
+
if (!anchoredElement ||
|
|
552
|
+
!anchoredElement.isConnected ||
|
|
553
|
+
modeRef.current !== 'anchored-to-message') {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
return scrollToElement(anchoredElement, {
|
|
557
|
+
align: 'start',
|
|
558
|
+
behavior: 'auto',
|
|
559
|
+
keepPreviousItemPeek: true,
|
|
560
|
+
});
|
|
561
|
+
}, [scrollToElement]);
|
|
562
|
+
const handleContentChange = useCallback(() => {
|
|
563
|
+
const contentElement = contentElementRef.current;
|
|
564
|
+
if (!contentElement) {
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (flushPendingScrollToMessage()) {
|
|
568
|
+
markExistingAnchorsHandled();
|
|
569
|
+
syncAfterScroll();
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (applyDefaultScrollPosition()) {
|
|
573
|
+
markExistingAnchorsHandled();
|
|
574
|
+
syncAfterScroll();
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const messageElements = getMessageElements(contentElement, spacerElementRef.current);
|
|
578
|
+
markAnchorsBeforePrependRestoreHandled(messageElements);
|
|
579
|
+
const nextAnchorElement = getFirstUnhandledAnchorElement(messageElements, handledAnchorElementsRef.current);
|
|
580
|
+
if (nextAnchorElement) {
|
|
581
|
+
handledAnchorElementsRef.current.add(nextAnchorElement);
|
|
582
|
+
scrollToElement(nextAnchorElement, {
|
|
583
|
+
align: 'start',
|
|
584
|
+
behavior: 'auto',
|
|
585
|
+
keepPreviousItemPeek: true,
|
|
586
|
+
});
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
if (autoFollowRef.current && modeRef.current === 'following-bottom') {
|
|
590
|
+
scrollToBottom({ behavior: 'auto' });
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
if (restorePrependAnchor()) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (reanchorToAnchoredMessage()) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
syncAfterScroll();
|
|
600
|
+
}, [
|
|
601
|
+
applyDefaultScrollPosition,
|
|
602
|
+
flushPendingScrollToMessage,
|
|
603
|
+
markAnchorsBeforePrependRestoreHandled,
|
|
604
|
+
markExistingAnchorsHandled,
|
|
605
|
+
reanchorToAnchoredMessage,
|
|
606
|
+
restorePrependAnchor,
|
|
607
|
+
scrollToBottom,
|
|
608
|
+
scrollToElement,
|
|
609
|
+
syncAfterScroll,
|
|
610
|
+
]);
|
|
611
|
+
const handleResize = useCallback(() => {
|
|
612
|
+
if (autoFollowRef.current && modeRef.current === 'following-bottom') {
|
|
613
|
+
scrollToBottom({ behavior: 'auto' });
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (reanchorToAnchoredMessage()) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
syncAfterScroll();
|
|
620
|
+
}, [reanchorToAnchoredMessage, scrollToBottom, syncAfterScroll]);
|
|
621
|
+
const handleViewportScroll = useCallback(() => {
|
|
622
|
+
syncAfterScroll();
|
|
623
|
+
if (ignoreAnchoredBottomResumeRef.current) {
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const status = measureStatus();
|
|
627
|
+
const isAnchoredMode = modeRef.current === 'anchored-to-message' || modeRef.current === 'settling-jump';
|
|
628
|
+
if (!autoFollowRef.current || !status.isAtBottom || !isAnchoredMode) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
modeRef.current = 'following-bottom';
|
|
632
|
+
autoscrollingRef.current = false;
|
|
633
|
+
bottomFollowIntentRef.current = false;
|
|
634
|
+
anchoredElementRef.current = null;
|
|
635
|
+
clearSpacer();
|
|
636
|
+
syncAfterScroll();
|
|
637
|
+
}, [clearSpacer, measureStatus, syncAfterScroll]);
|
|
638
|
+
const markUserScrollIntent = useCallback(() => {
|
|
639
|
+
if (modeRef.current === 'following-bottom' ||
|
|
640
|
+
modeRef.current === 'anchored-to-message' ||
|
|
641
|
+
modeRef.current === 'settling-jump') {
|
|
642
|
+
autoscrollingRef.current = false;
|
|
643
|
+
bottomFollowIntentRef.current = false;
|
|
644
|
+
anchoredElementRef.current = null;
|
|
645
|
+
modeRef.current = 'free-scrolling';
|
|
646
|
+
}
|
|
647
|
+
}, []);
|
|
648
|
+
const registerMessage = useCallback((messageId, element, previousElement) => {
|
|
649
|
+
if (!messageId) {
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
if (element) {
|
|
653
|
+
messageElementsRef.current.set(messageId, element);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if (previousElement && messageElementsRef.current.get(messageId) === previousElement) {
|
|
657
|
+
messageElementsRef.current.delete(messageId);
|
|
658
|
+
}
|
|
659
|
+
}, []);
|
|
660
|
+
const setRootElement = useCallback((element) => {
|
|
661
|
+
rootElementRef.current = element;
|
|
662
|
+
applyStatusAttributes(statusStore.getSnapshot());
|
|
663
|
+
}, [applyStatusAttributes, statusStore]);
|
|
664
|
+
const setViewportElement = useCallback((element) => {
|
|
665
|
+
viewportElementRef.current = element;
|
|
666
|
+
applyStatusAttributes(statusStore.getSnapshot());
|
|
667
|
+
syncAfterScroll();
|
|
668
|
+
}, [applyStatusAttributes, statusStore, syncAfterScroll]);
|
|
669
|
+
const setContentElement = useCallback((element) => {
|
|
670
|
+
contentElementRef.current = element;
|
|
671
|
+
if (element) {
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
clearSpacer();
|
|
675
|
+
messageElementsRef.current.clear();
|
|
676
|
+
handledAnchorElementsRef.current = new WeakSet();
|
|
677
|
+
anchoredElementRef.current = null;
|
|
678
|
+
bottomFollowIntentRef.current = false;
|
|
679
|
+
defaultScrollPositionAppliedRef.current = false;
|
|
680
|
+
prependRestoreRef.current = null;
|
|
681
|
+
ignoreAnchoredBottomResumeRef.current = false;
|
|
682
|
+
visibilityStore.setSnapshot(DEFAULT_VISIBILITY);
|
|
683
|
+
scheduleSyncAfterScroll();
|
|
684
|
+
}, [clearSpacer, scheduleSyncAfterScroll, visibilityStore]);
|
|
685
|
+
const setSpacerElement = useCallback((element) => {
|
|
686
|
+
spacerElementRef.current = element;
|
|
687
|
+
spacerGapRef.current = getGap(element?.parentElement ?? null);
|
|
688
|
+
setSpacerHeight(spacerHeightRef.current);
|
|
689
|
+
}, [setSpacerHeight]);
|
|
690
|
+
useEffect(() => () => {
|
|
691
|
+
if (pendingSyncFrameRef.current !== null) {
|
|
692
|
+
window.cancelAnimationFrame(pendingSyncFrameRef.current);
|
|
693
|
+
}
|
|
694
|
+
if (pendingDefaultScrollFrameRef.current !== null) {
|
|
695
|
+
window.cancelAnimationFrame(pendingDefaultScrollFrameRef.current);
|
|
696
|
+
}
|
|
697
|
+
if (ignoreAnchoredBottomResumeFrameRef.current !== null) {
|
|
698
|
+
window.cancelAnimationFrame(ignoreAnchoredBottomResumeFrameRef.current);
|
|
699
|
+
}
|
|
700
|
+
}, []);
|
|
701
|
+
return useMemo(() => ({
|
|
702
|
+
handleContentChange,
|
|
703
|
+
handleResize,
|
|
704
|
+
handleViewportScroll,
|
|
705
|
+
markUserScrollIntent,
|
|
706
|
+
preserveScrollOnPrependRef,
|
|
707
|
+
registerMessage,
|
|
708
|
+
scrollToBottom,
|
|
709
|
+
scrollToMessage,
|
|
710
|
+
scrollToTop,
|
|
711
|
+
setContentElement,
|
|
712
|
+
setRootElement,
|
|
713
|
+
setSpacerElement,
|
|
714
|
+
setViewportElement,
|
|
715
|
+
statusStore,
|
|
716
|
+
viewportElementRef,
|
|
717
|
+
visibilityStore,
|
|
718
|
+
}), [
|
|
719
|
+
handleContentChange,
|
|
720
|
+
handleResize,
|
|
721
|
+
handleViewportScroll,
|
|
722
|
+
markUserScrollIntent,
|
|
723
|
+
registerMessage,
|
|
724
|
+
scrollToBottom,
|
|
725
|
+
scrollToMessage,
|
|
726
|
+
scrollToTop,
|
|
727
|
+
setContentElement,
|
|
728
|
+
setRootElement,
|
|
729
|
+
setSpacerElement,
|
|
730
|
+
setViewportElement,
|
|
731
|
+
statusStore,
|
|
732
|
+
visibilityStore,
|
|
733
|
+
]);
|
|
734
|
+
}
|
|
735
|
+
function MessageScrollerProvider({ autoFollow = true, children, defaultScrollPosition = 'bottom', scrollEdgeThreshold = DEFAULT_SCROLL_EDGE_THRESHOLD, scrollMargin = DEFAULT_SCROLL_MARGIN, scrollPreviousItemPeek = DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK, }) {
|
|
736
|
+
const context = useMessageScrollerEngine({
|
|
737
|
+
autoFollow,
|
|
738
|
+
defaultScrollPosition,
|
|
739
|
+
scrollEdgeThreshold,
|
|
740
|
+
scrollMargin,
|
|
741
|
+
scrollPreviousItemPeek,
|
|
742
|
+
});
|
|
743
|
+
return (_jsx(MessageScrollerContext.Provider, { value: context, children: children }));
|
|
744
|
+
}
|
|
745
|
+
function MessageScrollerRoot({ children, className, ref, ...props }) {
|
|
746
|
+
const { setRootElement } = useMessageScrollerContext('MessageScroller.Root');
|
|
747
|
+
const handleRef = useCallback((element) => {
|
|
748
|
+
setRootElement(element);
|
|
749
|
+
assignRef(ref, element);
|
|
750
|
+
}, [ref, setRootElement]);
|
|
751
|
+
return (_jsx("div", { ref: handleRef, className: cn(className), ...props, children: children }));
|
|
752
|
+
}
|
|
753
|
+
function MessageScrollerViewport({ 'aria-label': ariaLabel, children, className, onKeyDown, onScroll, onTouchCancel, onTouchEnd, onTouchMove, onTouchStart, onWheel, preserveScrollOnPrepend = true, ref, role, tabIndex, ...props }) {
|
|
754
|
+
const { handleContentChange, handleResize, handleViewportScroll, markUserScrollIntent, preserveScrollOnPrependRef, setViewportElement, viewportElementRef, } = useMessageScrollerContext('MessageScroller.Viewport');
|
|
755
|
+
const touchClientYRef = useRef(null);
|
|
756
|
+
preserveScrollOnPrependRef.current = preserveScrollOnPrepend;
|
|
757
|
+
const handleRef = useCallback((element) => {
|
|
758
|
+
setViewportElement(element);
|
|
759
|
+
assignRef(ref, element);
|
|
760
|
+
}, [ref, setViewportElement]);
|
|
761
|
+
useLayoutEffect(() => {
|
|
762
|
+
handleContentChange();
|
|
763
|
+
}, [handleContentChange]);
|
|
764
|
+
const handleScroll = event => {
|
|
765
|
+
handleViewportScroll();
|
|
766
|
+
onScroll?.(event);
|
|
767
|
+
};
|
|
768
|
+
const handleWheel = event => {
|
|
769
|
+
onWheel?.(event);
|
|
770
|
+
if (!event.defaultPrevented && canWheelScroll(event.currentTarget, event.deltaY)) {
|
|
771
|
+
markUserScrollIntent();
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
const handleTouchStart = event => {
|
|
775
|
+
touchClientYRef.current = event.touches[0]?.clientY ?? null;
|
|
776
|
+
onTouchStart?.(event);
|
|
777
|
+
};
|
|
778
|
+
const handleTouchMove = event => {
|
|
779
|
+
onTouchMove?.(event);
|
|
780
|
+
const nextTouchClientY = event.touches[0]?.clientY ?? null;
|
|
781
|
+
const previousTouchClientY = touchClientYRef.current;
|
|
782
|
+
touchClientYRef.current = nextTouchClientY;
|
|
783
|
+
if (!event.defaultPrevented &&
|
|
784
|
+
nextTouchClientY !== null &&
|
|
785
|
+
previousTouchClientY !== null &&
|
|
786
|
+
canWheelScroll(event.currentTarget, previousTouchClientY - nextTouchClientY)) {
|
|
787
|
+
markUserScrollIntent();
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
const handleTouchEnd = event => {
|
|
791
|
+
touchClientYRef.current = null;
|
|
792
|
+
onTouchEnd?.(event);
|
|
793
|
+
};
|
|
794
|
+
const handleTouchCancel = event => {
|
|
795
|
+
touchClientYRef.current = null;
|
|
796
|
+
onTouchCancel?.(event);
|
|
797
|
+
};
|
|
798
|
+
const handleKeyDown = event => {
|
|
799
|
+
onKeyDown?.(event);
|
|
800
|
+
if (!event.defaultPrevented &&
|
|
801
|
+
USER_SCROLL_INTENT_KEYS.has(event.key) &&
|
|
802
|
+
canKeyboardScroll(event.currentTarget, event)) {
|
|
803
|
+
markUserScrollIntent();
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
useEffect(() => {
|
|
807
|
+
const viewportElement = viewportElementRef.current;
|
|
808
|
+
if (!viewportElement || typeof ResizeObserver === 'undefined') {
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
const observer = new ResizeObserver(handleResize);
|
|
812
|
+
observer.observe(viewportElement);
|
|
813
|
+
return () => observer.disconnect();
|
|
814
|
+
}, [handleResize, viewportElementRef]);
|
|
815
|
+
return (_jsx("div", { "aria-label": ariaLabel ?? 'Messages', className: cn('overflow-y-auto', className), onKeyDown: handleKeyDown, onScroll: handleScroll, onTouchCancel: handleTouchCancel, onTouchEnd: handleTouchEnd, onTouchMove: handleTouchMove, onTouchStart: handleTouchStart, onWheel: handleWheel, ref: handleRef, role: role ?? 'region', tabIndex: tabIndex ?? 0, ...props, children: children }));
|
|
816
|
+
}
|
|
817
|
+
function MessageScrollerContent({ 'aria-relevant': ariaRelevant, children, className, ref, role, spacerClassName, ...props }) {
|
|
818
|
+
const { handleContentChange, handleResize, setContentElement, setSpacerElement } = useMessageScrollerContext('MessageScroller.Content');
|
|
819
|
+
const contentElementRef = useRef(null);
|
|
820
|
+
const handleContentRef = useCallback((element) => {
|
|
821
|
+
contentElementRef.current = element;
|
|
822
|
+
setContentElement(element);
|
|
823
|
+
assignRef(ref, element);
|
|
824
|
+
}, [ref, setContentElement]);
|
|
825
|
+
const handleSpacerRef = useCallback((element) => {
|
|
826
|
+
setSpacerElement(element);
|
|
827
|
+
}, [setSpacerElement]);
|
|
828
|
+
useLayoutEffect(() => {
|
|
829
|
+
handleContentChange();
|
|
830
|
+
});
|
|
831
|
+
useLayoutEffect(() => {
|
|
832
|
+
const contentElement = contentElementRef.current;
|
|
833
|
+
if (!contentElement) {
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
if (typeof MutationObserver === 'undefined') {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const observer = new MutationObserver(handleContentChange);
|
|
840
|
+
observer.observe(contentElement, { childList: true });
|
|
841
|
+
return () => observer.disconnect();
|
|
842
|
+
}, [handleContentChange]);
|
|
843
|
+
useEffect(() => {
|
|
844
|
+
const contentElement = contentElementRef.current;
|
|
845
|
+
if (!contentElement || typeof ResizeObserver === 'undefined') {
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
const observer = new ResizeObserver(handleResize);
|
|
849
|
+
observer.observe(contentElement);
|
|
850
|
+
return () => observer.disconnect();
|
|
851
|
+
}, [handleResize]);
|
|
852
|
+
return (_jsxs("div", { "aria-relevant": ariaRelevant ?? 'additions', className: cn(className), ref: handleContentRef, role: role ?? 'log', ...props, children: [children, _jsx("div", { "aria-hidden": "true", className: spacerClassName, "data-message-scroller-spacer": "", hidden: true, ref: handleSpacerRef })] }));
|
|
853
|
+
}
|
|
854
|
+
function MessageScrollerItem({ children, messageId, ref, scrollAnchor = false, ...props }) {
|
|
855
|
+
const { registerMessage } = useMessageScrollerContext('MessageScroller.Item');
|
|
856
|
+
const elementRef = useRef(null);
|
|
857
|
+
const handleRef = useCallback((element) => {
|
|
858
|
+
const previousElement = elementRef.current;
|
|
859
|
+
elementRef.current = element;
|
|
860
|
+
registerMessage(messageId, element, previousElement);
|
|
861
|
+
assignRef(ref, element);
|
|
862
|
+
}, [messageId, ref, registerMessage]);
|
|
863
|
+
return (_jsx("div", { "data-message-id": messageId, "data-scroll-anchor": scrollAnchor ? 'true' : 'false', ref: handleRef, ...props, children: children }));
|
|
864
|
+
}
|
|
865
|
+
/** Headless transcript scroller shared by carbonid1 apps; consumers own row rendering. */
|
|
866
|
+
function useMessageScroller() {
|
|
867
|
+
const { scrollToBottom, scrollToMessage, scrollToTop } = useMessageScrollerContext('useMessageScroller');
|
|
868
|
+
return useMemo(() => ({ scrollToBottom, scrollToMessage, scrollToTop }), [scrollToBottom, scrollToMessage, scrollToTop]);
|
|
869
|
+
}
|
|
870
|
+
function useMessageScrollerStatus() {
|
|
871
|
+
const { statusStore } = useMessageScrollerContext('useMessageScrollerStatus');
|
|
872
|
+
return useSyncExternalStore(statusStore.subscribe, statusStore.getSnapshot, statusStore.getSnapshot);
|
|
873
|
+
}
|
|
874
|
+
function useMessageScrollerVisibility() {
|
|
875
|
+
const { visibilityStore } = useMessageScrollerContext('useMessageScrollerVisibility');
|
|
876
|
+
return useSyncExternalStore(visibilityStore.subscribe, visibilityStore.getSnapshot, visibilityStore.getSnapshot);
|
|
877
|
+
}
|
|
878
|
+
const MessageScroller = {
|
|
879
|
+
Content: MessageScrollerContent,
|
|
880
|
+
Item: MessageScrollerItem,
|
|
881
|
+
Provider: MessageScrollerProvider,
|
|
882
|
+
Root: MessageScrollerRoot,
|
|
883
|
+
Viewport: MessageScrollerViewport,
|
|
884
|
+
};
|
|
885
|
+
export { MessageScroller, useMessageScroller, useMessageScrollerStatus, useMessageScrollerVisibility, };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef, ReactNode, Ref } from 'react';
|
|
2
|
+
export type MessageScrollerDefaultScrollPosition = 'top' | 'bottom' | 'last-anchor';
|
|
3
|
+
export type MessageScrollerScrollAlign = 'start' | 'center' | 'end' | 'nearest';
|
|
4
|
+
export type MessageScrollerScrollOptions = {
|
|
5
|
+
align?: MessageScrollerScrollAlign;
|
|
6
|
+
behavior?: ScrollBehavior;
|
|
7
|
+
scrollMargin?: number;
|
|
8
|
+
};
|
|
9
|
+
export type MessageScrollerController = {
|
|
10
|
+
scrollToBottom: (options?: MessageScrollerScrollOptions) => boolean;
|
|
11
|
+
scrollToMessage: (messageId: string, options?: MessageScrollerScrollOptions) => boolean;
|
|
12
|
+
scrollToTop: (options?: MessageScrollerScrollOptions) => boolean;
|
|
13
|
+
};
|
|
14
|
+
export type MessageScrollerStatus = {
|
|
15
|
+
canScrollToBottom: boolean;
|
|
16
|
+
canScrollToTop: boolean;
|
|
17
|
+
isAtBottom: boolean;
|
|
18
|
+
isAtTop: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type MessageScrollerVisibility = {
|
|
21
|
+
anchorMessageId: string | null;
|
|
22
|
+
visibleMessageIds: string[];
|
|
23
|
+
};
|
|
24
|
+
export type MessageScrollerProviderProps = {
|
|
25
|
+
autoFollow?: boolean;
|
|
26
|
+
children?: ReactNode;
|
|
27
|
+
defaultScrollPosition?: MessageScrollerDefaultScrollPosition;
|
|
28
|
+
scrollEdgeThreshold?: number;
|
|
29
|
+
scrollMargin?: number;
|
|
30
|
+
scrollPreviousItemPeek?: number;
|
|
31
|
+
};
|
|
32
|
+
export type MessageScrollerRootProps = ComponentPropsWithoutRef<'div'> & {
|
|
33
|
+
ref?: Ref<HTMLDivElement>;
|
|
34
|
+
};
|
|
35
|
+
export type MessageScrollerViewportProps = ComponentPropsWithoutRef<'div'> & {
|
|
36
|
+
preserveScrollOnPrepend?: boolean;
|
|
37
|
+
ref?: Ref<HTMLDivElement>;
|
|
38
|
+
};
|
|
39
|
+
export type MessageScrollerContentProps = ComponentPropsWithoutRef<'div'> & {
|
|
40
|
+
ref?: Ref<HTMLDivElement>;
|
|
41
|
+
spacerClassName?: string;
|
|
42
|
+
};
|
|
43
|
+
export type MessageScrollerItemProps = ComponentPropsWithoutRef<'div'> & {
|
|
44
|
+
messageId?: string;
|
|
45
|
+
ref?: Ref<HTMLDivElement>;
|
|
46
|
+
scrollAnchor?: boolean;
|
|
47
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export { Select } from './Select/Select';
|
|
|
14
14
|
export type { SelectOption, SelectGroup, SelectOptionState, SelectProps, } from './Select/Select.types';
|
|
15
15
|
export { Table } from './Table/Table';
|
|
16
16
|
export type { Column, TableProps } from './Table/Table.types';
|
|
17
|
+
export { MessageScroller, useMessageScroller, useMessageScrollerStatus, useMessageScrollerVisibility, } from './MessageScroller/MessageScroller';
|
|
18
|
+
export type { MessageScrollerController, MessageScrollerContentProps, MessageScrollerDefaultScrollPosition, MessageScrollerItemProps, MessageScrollerProviderProps, MessageScrollerRootProps, MessageScrollerScrollAlign, MessageScrollerScrollOptions, MessageScrollerStatus, MessageScrollerViewportProps, MessageScrollerVisibility, } from './MessageScroller/MessageScroller.types';
|
|
17
19
|
export { Tooltip } from './Tooltip/Tooltip';
|
|
18
20
|
export { ThemeProvider } from './ThemeProvider/ThemeProvider';
|
|
19
21
|
export { ThemeCycler } from './ThemeCycler/ThemeCycler';
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { Slider } from './Slider/Slider';
|
|
|
8
8
|
export { ContextMenu } from './ContextMenu/ContextMenu';
|
|
9
9
|
export { Select } from './Select/Select';
|
|
10
10
|
export { Table } from './Table/Table';
|
|
11
|
+
export { MessageScroller, useMessageScroller, useMessageScrollerStatus, useMessageScrollerVisibility, } from './MessageScroller/MessageScroller';
|
|
11
12
|
export { Tooltip } from './Tooltip/Tooltip';
|
|
12
13
|
export { ThemeProvider } from './ThemeProvider/ThemeProvider';
|
|
13
14
|
export { ThemeCycler } from './ThemeCycler/ThemeCycler';
|