@codefast/ui 0.5.0-canary.2 → 0.5.0-canary.4
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 +51 -0
- package/dist/components/attachment.d.mts +136 -0
- package/dist/components/attachment.mjs +112 -0
- package/dist/components/avatar.mjs +1 -1
- package/dist/components/breadcrumb.d.mts +2 -4
- package/dist/components/bubble.d.mts +67 -0
- package/dist/components/bubble.mjs +58 -0
- package/dist/components/card.mjs +2 -2
- package/dist/components/carousel.mjs +12 -3
- package/dist/components/empty.mjs +1 -1
- package/dist/components/marker.d.mts +49 -0
- package/dist/components/marker.mjs +42 -0
- package/dist/components/message-scroller.d.mts +89 -0
- package/dist/components/message-scroller.mjs +77 -0
- package/dist/components/message.d.mts +86 -0
- package/dist/components/message.mjs +66 -0
- package/dist/components/progress-circle.d.mts +4 -4
- package/dist/components/progress-circle.mjs +7 -7
- package/dist/components/sidebar.mjs +6 -3
- package/dist/hooks/use-animated-value.d.mts +2 -2
- package/dist/hooks/use-animated-value.mjs +4 -4
- package/dist/hooks/use-message-scroller-commands.d.mts +39 -0
- package/dist/hooks/use-message-scroller-commands.mjs +165 -0
- package/dist/hooks/use-message-scroller-controller.d.mts +19 -0
- package/dist/hooks/use-message-scroller-controller.mjs +382 -0
- package/dist/hooks/use-message-scroller-refs.d.mts +60 -0
- package/dist/hooks/use-message-scroller-refs.mjs +77 -0
- package/dist/hooks/use-pagination.d.mts +4 -4
- package/dist/hooks/use-pagination.mjs +1 -1
- package/dist/index.d.mts +12 -2
- package/dist/index.mjs +11 -2
- package/dist/lib/message-scroller/geometry.d.mts +136 -0
- package/dist/lib/message-scroller/geometry.mjs +199 -0
- package/dist/lib/message-scroller/stores.d.mts +21 -0
- package/dist/lib/message-scroller/stores.mjs +52 -0
- package/dist/lib/message-scroller/types.d.mts +171 -0
- package/dist/lib/message-scroller/types.mjs +53 -0
- package/dist/lib/message-scroller/utils.d.mts +7 -0
- package/dist/lib/message-scroller/utils.mjs +12 -0
- package/dist/lib/use-render.d.mts +43 -0
- package/dist/lib/use-render.mjs +84 -0
- package/dist/primitives/message-scroller.d.mts +88 -0
- package/dist/primitives/message-scroller.mjs +240 -0
- package/dist/variants/attachment.d.mts +42 -0
- package/dist/variants/attachment.mjs +42 -0
- package/dist/variants/bubble.d.mts +45 -0
- package/dist/variants/bubble.mjs +45 -0
- package/dist/variants/marker.d.mts +21 -0
- package/dist/variants/marker.mjs +18 -0
- package/package.json +93 -7
- package/src/components/attachment.tsx +284 -0
- package/src/components/avatar.tsx +1 -1
- package/src/components/breadcrumb.tsx +2 -4
- package/src/components/bubble.tsx +116 -0
- package/src/components/card.tsx +2 -2
- package/src/components/carousel.tsx +12 -3
- package/src/components/empty.tsx +1 -1
- package/src/components/marker.tsx +94 -0
- package/src/components/message-scroller.tsx +213 -0
- package/src/components/message.tsx +173 -0
- package/src/components/progress-circle.tsx +11 -11
- package/src/components/sidebar.tsx +11 -4
- package/src/css/foundation/tokens.css +6 -18
- package/src/css/foundation/utilities.css +451 -0
- package/src/css/preset.css +2 -1
- package/src/hooks/use-animated-value.ts +4 -4
- package/src/hooks/use-message-scroller-commands.ts +311 -0
- package/src/hooks/use-message-scroller-controller.ts +650 -0
- package/src/hooks/use-message-scroller-refs.ts +167 -0
- package/src/hooks/use-pagination.ts +3 -3
- package/src/index.ts +73 -1
- package/src/lib/message-scroller/geometry.ts +397 -0
- package/src/lib/message-scroller/stores.ts +88 -0
- package/src/lib/message-scroller/types.ts +309 -0
- package/src/lib/message-scroller/utils.ts +14 -0
- package/src/lib/use-render.ts +201 -0
- package/src/primitives/message-scroller.tsx +403 -0
- package/src/variants/attachment.ts +73 -0
- package/src/variants/bubble.ts +81 -0
- package/src/variants/marker.ts +38 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MessageScrollerContextValue, MessageScrollerProviderProps, MessageScrollerRegisterMessage } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { MessageScrollerRefs } from "./use-message-scroller-refs.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/use-message-scroller-controller.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
declare function useMessageScrollerController({
|
|
9
|
+
autoScroll,
|
|
10
|
+
defaultScrollPosition,
|
|
11
|
+
scrollEdgeThreshold,
|
|
12
|
+
scrollPreviousItemPeek,
|
|
13
|
+
scrollMargin
|
|
14
|
+
}: MessageScrollerProviderProps): {
|
|
15
|
+
context: MessageScrollerContextValue;
|
|
16
|
+
registerMessage: MessageScrollerRegisterMessage;
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { type MessageScrollerRefs, useMessageScrollerController };
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { getContentBottom, getElementTop, getElementViewportTop, getFirstVisibleMessageItem, getFlexGap, getLastScrollAnchor, getMessageScrollerItems, getMessageScrollerScrollable, getMessageScrollerVisibilityState, getNewScrollAnchor, getUnanchoredScrollAnchor, hasMultipleNewScrollAnchors } from "../lib/message-scroller/geometry.mjs";
|
|
3
|
+
import { useMessageScrollerCommands } from "./use-message-scroller-commands.mjs";
|
|
4
|
+
import { useMessageScrollerRefs } from "./use-message-scroller-refs.mjs";
|
|
5
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
|
6
|
+
//#region src/hooks/use-message-scroller-controller.ts
|
|
7
|
+
function useElementRef(elementRef, onMount) {
|
|
8
|
+
return useCallback((element) => {
|
|
9
|
+
elementRef.current = element;
|
|
10
|
+
if (element) onMount();
|
|
11
|
+
}, [elementRef, onMount]);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @since 0.5.0-canary.3
|
|
15
|
+
*/
|
|
16
|
+
function useMessageScrollerController({ autoScroll = false, defaultScrollPosition = "end", scrollEdgeThreshold = 8, scrollPreviousItemPeek = 64, scrollMargin = 0 }) {
|
|
17
|
+
const refs = useMessageScrollerRefs({
|
|
18
|
+
autoScroll,
|
|
19
|
+
scrollEdgeThreshold,
|
|
20
|
+
scrollMargin,
|
|
21
|
+
scrollPreviousItemPeek
|
|
22
|
+
});
|
|
23
|
+
const { streamingTurnRef, autoScrollRef, autoscrollingRef, autoscrollingTimeoutRef, contentRef, defaultScrollPositionAppliedRef, firstItemRef, itemCountRef, messageElementsRef, modeRef, pendingScrollFrameRef, pendingScrollToMessageRef, prependRestoreRef, preserveScrollOnPrependRef, rootRef, scrollEdgeThresholdRef, scrollMarginRef, scrollPreviousItemPeekRef, spacerGapRef, spacerRef, stateFrameRef, stateStore, viewportRef, visibilityFrameRef, visibilityObserverRef, visibilityStore, visibleMessageIdsRef, handledScrollAnchorsRef } = refs;
|
|
24
|
+
const previousDefaultScrollPositionRef = useRef(defaultScrollPosition);
|
|
25
|
+
if (previousDefaultScrollPositionRef.current !== defaultScrollPosition) {
|
|
26
|
+
previousDefaultScrollPositionRef.current = defaultScrollPosition;
|
|
27
|
+
defaultScrollPositionAppliedRef.current = false;
|
|
28
|
+
}
|
|
29
|
+
const writeStateAttributes = useCallback((state) => {
|
|
30
|
+
const root = rootRef.current;
|
|
31
|
+
const viewport = viewportRef.current;
|
|
32
|
+
const scrollable = [state.start && "start", state.end && "end"].filter(Boolean).join(" ");
|
|
33
|
+
const autoScrolling = autoscrollingRef.current;
|
|
34
|
+
for (const element of [root, viewport]) {
|
|
35
|
+
if (!element) continue;
|
|
36
|
+
if (scrollable) element.setAttribute("data-scrollable", scrollable);
|
|
37
|
+
else element.removeAttribute("data-scrollable");
|
|
38
|
+
element.toggleAttribute("data-autoscrolling", autoScrolling);
|
|
39
|
+
}
|
|
40
|
+
}, []);
|
|
41
|
+
const reconcileFollowMode = useCallback((scrollable) => {
|
|
42
|
+
if (autoScrollRef.current && !scrollable.end && modeRef.current !== "settling-jump") modeRef.current = "following-bottom";
|
|
43
|
+
else if (modeRef.current === "following-bottom" && scrollable.end && !autoscrollingRef.current) modeRef.current = "free-scrolling";
|
|
44
|
+
}, []);
|
|
45
|
+
const commitScrollState = useCallback(() => {
|
|
46
|
+
const nextState = getMessageScrollerScrollable({
|
|
47
|
+
content: contentRef.current,
|
|
48
|
+
scrollEdgeThreshold: scrollEdgeThresholdRef.current,
|
|
49
|
+
spacer: spacerRef.current,
|
|
50
|
+
viewport: viewportRef.current
|
|
51
|
+
});
|
|
52
|
+
reconcileFollowMode(nextState);
|
|
53
|
+
writeStateAttributes(nextState);
|
|
54
|
+
stateStore.setSnapshot(nextState);
|
|
55
|
+
}, [
|
|
56
|
+
reconcileFollowMode,
|
|
57
|
+
stateStore,
|
|
58
|
+
writeStateAttributes
|
|
59
|
+
]);
|
|
60
|
+
const scheduleStateCommit = useCallback(() => {
|
|
61
|
+
if (stateFrameRef.current !== null) return;
|
|
62
|
+
stateFrameRef.current = window.requestAnimationFrame(() => {
|
|
63
|
+
stateFrameRef.current = null;
|
|
64
|
+
commitScrollState();
|
|
65
|
+
});
|
|
66
|
+
}, [commitScrollState]);
|
|
67
|
+
const scheduleVisibilitySync = useCallback(() => {
|
|
68
|
+
if (!visibilityStore.hasListeners()) return;
|
|
69
|
+
if (visibilityFrameRef.current !== null) return;
|
|
70
|
+
visibilityFrameRef.current = window.requestAnimationFrame(() => {
|
|
71
|
+
visibilityFrameRef.current = null;
|
|
72
|
+
if (!visibilityStore.hasListeners()) return;
|
|
73
|
+
visibilityStore.setSnapshot(getMessageScrollerVisibilityState({
|
|
74
|
+
content: contentRef.current,
|
|
75
|
+
scrollMargin: scrollMarginRef.current,
|
|
76
|
+
scrollPreviousItemPeek: scrollPreviousItemPeekRef.current,
|
|
77
|
+
spacer: spacerRef.current,
|
|
78
|
+
viewport: viewportRef.current,
|
|
79
|
+
visibleMessageIds: visibleMessageIdsRef.current
|
|
80
|
+
}));
|
|
81
|
+
});
|
|
82
|
+
}, [visibilityStore]);
|
|
83
|
+
const { flushPendingScrollToMessage, reanchorToAnchoredMessage, scrollToElement, scrollToEnd, scrollToMessage, scrollToStart } = useMessageScrollerCommands({
|
|
84
|
+
refs,
|
|
85
|
+
commitScrollState,
|
|
86
|
+
scheduleStateCommit,
|
|
87
|
+
scheduleVisibilitySync
|
|
88
|
+
});
|
|
89
|
+
const restorePrependedAnchor = useCallback(() => {
|
|
90
|
+
const anchor = prependRestoreRef.current;
|
|
91
|
+
const viewport = viewportRef.current;
|
|
92
|
+
if (!anchor || !viewport || !anchor.element.isConnected) return false;
|
|
93
|
+
const delta = getElementViewportTop(anchor.element, viewport) - anchor.viewportTop;
|
|
94
|
+
if (Math.abs(delta) <= .5) return false;
|
|
95
|
+
viewport.scrollTop += delta;
|
|
96
|
+
anchor.viewportTop = getElementViewportTop(anchor.element, viewport);
|
|
97
|
+
scheduleStateCommit();
|
|
98
|
+
scheduleVisibilitySync();
|
|
99
|
+
return true;
|
|
100
|
+
}, [scheduleStateCommit, scheduleVisibilitySync]);
|
|
101
|
+
const capturePrependAnchor = useCallback(() => {
|
|
102
|
+
const content = contentRef.current;
|
|
103
|
+
const viewport = viewportRef.current;
|
|
104
|
+
if (!content || !viewport) {
|
|
105
|
+
prependRestoreRef.current = null;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const anchor = getFirstVisibleMessageItem({
|
|
109
|
+
content,
|
|
110
|
+
spacer: spacerRef.current,
|
|
111
|
+
viewport
|
|
112
|
+
});
|
|
113
|
+
prependRestoreRef.current = anchor ? {
|
|
114
|
+
element: anchor,
|
|
115
|
+
viewportTop: getElementViewportTop(anchor, viewport)
|
|
116
|
+
} : null;
|
|
117
|
+
}, []);
|
|
118
|
+
const schedulePendingScrollToMessageFlush = useCallback(() => {
|
|
119
|
+
if (pendingScrollFrameRef.current !== null) return;
|
|
120
|
+
pendingScrollFrameRef.current = window.requestAnimationFrame(() => {
|
|
121
|
+
pendingScrollFrameRef.current = null;
|
|
122
|
+
if (flushPendingScrollToMessage()) capturePrependAnchor();
|
|
123
|
+
});
|
|
124
|
+
}, [capturePrependAnchor, flushPendingScrollToMessage]);
|
|
125
|
+
const applyDefaultScrollPosition = useCallback(() => {
|
|
126
|
+
if (!defaultScrollPosition || defaultScrollPositionAppliedRef.current || itemCountRef.current === 0) return false;
|
|
127
|
+
let handled = false;
|
|
128
|
+
if (defaultScrollPosition === "last-anchor") {
|
|
129
|
+
const content = contentRef.current;
|
|
130
|
+
const viewport = viewportRef.current;
|
|
131
|
+
const anchor = content && viewport ? getLastScrollAnchor(getMessageScrollerItems(content, spacerRef.current)) : null;
|
|
132
|
+
if (!content || !viewport || !anchor) handled = scrollToEnd({ behavior: "auto" });
|
|
133
|
+
else {
|
|
134
|
+
const anchorTop = getElementTop(anchor, viewport);
|
|
135
|
+
handled = getContentBottom({
|
|
136
|
+
content,
|
|
137
|
+
spacer: spacerRef.current,
|
|
138
|
+
viewport
|
|
139
|
+
}) - anchorTop <= viewport.clientHeight ? scrollToEnd({ behavior: "auto" }) : scrollToElement(anchor, { align: "start" }, { keepPreviousPeek: true });
|
|
140
|
+
}
|
|
141
|
+
} else handled = defaultScrollPosition === "end" ? scrollToEnd({ behavior: "auto" }) : scrollToStart({ behavior: "auto" });
|
|
142
|
+
if (!handled) return false;
|
|
143
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
144
|
+
return true;
|
|
145
|
+
}, [
|
|
146
|
+
defaultScrollPosition,
|
|
147
|
+
scrollToElement,
|
|
148
|
+
scrollToEnd,
|
|
149
|
+
scrollToStart
|
|
150
|
+
]);
|
|
151
|
+
const handleContentChange = useCallback(() => {
|
|
152
|
+
const content = contentRef.current;
|
|
153
|
+
if (!content) return;
|
|
154
|
+
const items = getMessageScrollerItems(content, spacerRef.current);
|
|
155
|
+
const previousItemCount = itemCountRef.current;
|
|
156
|
+
const previousFirstItem = firstItemRef.current;
|
|
157
|
+
itemCountRef.current = items.length;
|
|
158
|
+
firstItemRef.current = items[0] ?? null;
|
|
159
|
+
const reconcileScrollPosition = () => {
|
|
160
|
+
if (flushPendingScrollToMessage()) return;
|
|
161
|
+
if (previousItemCount === 0) {
|
|
162
|
+
if (applyDefaultScrollPosition()) return;
|
|
163
|
+
if (items.length > 0 && autoScrollRef.current && scrollToEnd({ behavior: "auto" })) return;
|
|
164
|
+
commitScrollState();
|
|
165
|
+
scheduleVisibilitySync();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const previousFirstItemIndex = previousFirstItem ? items.indexOf(previousFirstItem) : -1;
|
|
169
|
+
if (preserveScrollOnPrependRef.current && previousFirstItemIndex > 0) {
|
|
170
|
+
restorePrependedAnchor();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (items.length > previousItemCount) {
|
|
174
|
+
const anchor = getNewScrollAnchor(items, previousItemCount);
|
|
175
|
+
if (anchor) {
|
|
176
|
+
if (autoScrollRef.current && modeRef.current === "following-bottom" && hasMultipleNewScrollAnchors(items, previousItemCount)) {
|
|
177
|
+
scrollToEnd({ behavior: "auto" });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
scrollToElement(anchor, { align: "start" }, { keepPreviousPeek: true });
|
|
181
|
+
handledScrollAnchorsRef.current.add(anchor);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (items.length === previousItemCount) {
|
|
186
|
+
const anchor = getUnanchoredScrollAnchor(items, handledScrollAnchorsRef.current);
|
|
187
|
+
if (anchor) {
|
|
188
|
+
scrollToElement(anchor, { align: "start" }, { keepPreviousPeek: true });
|
|
189
|
+
handledScrollAnchorsRef.current.add(anchor);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (modeRef.current === "following-bottom" && autoScrollRef.current) scrollToEnd({ behavior: "auto" });
|
|
194
|
+
else {
|
|
195
|
+
commitScrollState();
|
|
196
|
+
scheduleVisibilitySync();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
reconcileScrollPosition();
|
|
200
|
+
capturePrependAnchor();
|
|
201
|
+
}, [
|
|
202
|
+
applyDefaultScrollPosition,
|
|
203
|
+
capturePrependAnchor,
|
|
204
|
+
commitScrollState,
|
|
205
|
+
flushPendingScrollToMessage,
|
|
206
|
+
restorePrependedAnchor,
|
|
207
|
+
scheduleVisibilitySync,
|
|
208
|
+
scrollToElement,
|
|
209
|
+
scrollToEnd
|
|
210
|
+
]);
|
|
211
|
+
const handleResize = useCallback(() => {
|
|
212
|
+
if (modeRef.current === "following-bottom" && autoScrollRef.current) {
|
|
213
|
+
scrollToEnd({ behavior: "auto" });
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (reanchorToAnchoredMessage()) return;
|
|
217
|
+
scheduleStateCommit();
|
|
218
|
+
scheduleVisibilitySync();
|
|
219
|
+
}, [
|
|
220
|
+
reanchorToAnchoredMessage,
|
|
221
|
+
scheduleStateCommit,
|
|
222
|
+
scheduleVisibilitySync,
|
|
223
|
+
scrollToEnd
|
|
224
|
+
]);
|
|
225
|
+
const observeVisibility = useCallback(() => {
|
|
226
|
+
const viewport = viewportRef.current;
|
|
227
|
+
if (!viewport || !visibilityStore.hasListeners()) return;
|
|
228
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
229
|
+
scheduleVisibilitySync();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (!visibilityObserverRef.current) visibilityObserverRef.current = new IntersectionObserver((entries) => {
|
|
233
|
+
for (const entry of entries) {
|
|
234
|
+
const messageId = entry.target.dataset.messageId;
|
|
235
|
+
if (!messageId) continue;
|
|
236
|
+
if (entry.isIntersecting) visibleMessageIdsRef.current.add(messageId);
|
|
237
|
+
else visibleMessageIdsRef.current.delete(messageId);
|
|
238
|
+
}
|
|
239
|
+
scheduleVisibilitySync();
|
|
240
|
+
}, {
|
|
241
|
+
root: viewport,
|
|
242
|
+
rootMargin: `${-(scrollMarginRef.current + scrollPreviousItemPeekRef.current)}px 0px 0px 0px`,
|
|
243
|
+
threshold: [
|
|
244
|
+
0,
|
|
245
|
+
.01,
|
|
246
|
+
.5,
|
|
247
|
+
1
|
|
248
|
+
]
|
|
249
|
+
});
|
|
250
|
+
messageElementsRef.current.forEach((element) => {
|
|
251
|
+
visibilityObserverRef.current?.observe(element);
|
|
252
|
+
});
|
|
253
|
+
scheduleVisibilitySync();
|
|
254
|
+
}, [scheduleVisibilitySync, visibilityStore]);
|
|
255
|
+
const unobserveVisibility = useCallback(() => {
|
|
256
|
+
if (visibilityFrameRef.current !== null) {
|
|
257
|
+
window.cancelAnimationFrame(visibilityFrameRef.current);
|
|
258
|
+
visibilityFrameRef.current = null;
|
|
259
|
+
}
|
|
260
|
+
visibilityObserverRef.current?.disconnect();
|
|
261
|
+
visibilityObserverRef.current = null;
|
|
262
|
+
visibleMessageIdsRef.current.clear();
|
|
263
|
+
visibilityStore.setSnapshot(EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE);
|
|
264
|
+
}, [visibilityStore]);
|
|
265
|
+
const registerMessage = useCallback((messageId, element, removedElement) => {
|
|
266
|
+
if (element) {
|
|
267
|
+
messageElementsRef.current.set(messageId, element);
|
|
268
|
+
visibilityObserverRef.current?.observe(element);
|
|
269
|
+
scheduleVisibilitySync();
|
|
270
|
+
if (pendingScrollToMessageRef.current?.messageId === messageId) schedulePendingScrollToMessageFlush();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (removedElement && messageElementsRef.current.get(messageId) === removedElement) {
|
|
274
|
+
messageElementsRef.current.delete(messageId);
|
|
275
|
+
visibleMessageIdsRef.current.delete(messageId);
|
|
276
|
+
visibilityObserverRef.current?.unobserve(removedElement);
|
|
277
|
+
scheduleVisibilitySync();
|
|
278
|
+
}
|
|
279
|
+
}, [schedulePendingScrollToMessageFlush, scheduleVisibilitySync]);
|
|
280
|
+
const userScrollIntent = useCallback(() => {
|
|
281
|
+
if (modeRef.current === "following-bottom" || modeRef.current === "anchored-to-message" || modeRef.current === "settling-jump") {
|
|
282
|
+
streamingTurnRef.current = null;
|
|
283
|
+
modeRef.current = "free-scrolling";
|
|
284
|
+
}
|
|
285
|
+
}, []);
|
|
286
|
+
const mirrorStateAttributes = useCallback(() => writeStateAttributes(stateStore.getSnapshot()), [stateStore, writeStateAttributes]);
|
|
287
|
+
const setRootElement = useElementRef(rootRef, mirrorStateAttributes);
|
|
288
|
+
const setViewportElement = useElementRef(viewportRef, mirrorStateAttributes);
|
|
289
|
+
const setContentElement = useCallback((element) => {
|
|
290
|
+
contentRef.current = element;
|
|
291
|
+
}, []);
|
|
292
|
+
const setSpacerElement = useCallback((element) => {
|
|
293
|
+
spacerRef.current = element;
|
|
294
|
+
spacerGapRef.current = getFlexGap(element?.parentElement ?? null);
|
|
295
|
+
}, []);
|
|
296
|
+
const syncAfterScroll = useCallback(() => {
|
|
297
|
+
commitScrollState();
|
|
298
|
+
scheduleVisibilitySync();
|
|
299
|
+
capturePrependAnchor();
|
|
300
|
+
}, [
|
|
301
|
+
capturePrependAnchor,
|
|
302
|
+
commitScrollState,
|
|
303
|
+
scheduleVisibilitySync
|
|
304
|
+
]);
|
|
305
|
+
const context = useMemo(() => ({
|
|
306
|
+
handleContentChange,
|
|
307
|
+
handleResize,
|
|
308
|
+
observeVisibility,
|
|
309
|
+
preserveScrollOnPrependRef,
|
|
310
|
+
scrollToEnd,
|
|
311
|
+
scrollToMessage,
|
|
312
|
+
scrollToStart,
|
|
313
|
+
setContentElement,
|
|
314
|
+
setRootElement,
|
|
315
|
+
setSpacerElement,
|
|
316
|
+
setViewportElement,
|
|
317
|
+
stateStore,
|
|
318
|
+
syncAfterScroll,
|
|
319
|
+
unobserveVisibility,
|
|
320
|
+
userScrollIntent,
|
|
321
|
+
viewportRef,
|
|
322
|
+
visibilityStore
|
|
323
|
+
}), [
|
|
324
|
+
handleContentChange,
|
|
325
|
+
handleResize,
|
|
326
|
+
observeVisibility,
|
|
327
|
+
scrollToEnd,
|
|
328
|
+
scrollToMessage,
|
|
329
|
+
scrollToStart,
|
|
330
|
+
setContentElement,
|
|
331
|
+
setRootElement,
|
|
332
|
+
setSpacerElement,
|
|
333
|
+
setViewportElement,
|
|
334
|
+
stateStore,
|
|
335
|
+
syncAfterScroll,
|
|
336
|
+
unobserveVisibility,
|
|
337
|
+
userScrollIntent,
|
|
338
|
+
visibilityStore
|
|
339
|
+
]);
|
|
340
|
+
useLayoutEffect(() => {
|
|
341
|
+
applyDefaultScrollPosition();
|
|
342
|
+
}, [applyDefaultScrollPosition]);
|
|
343
|
+
useEffect(() => {
|
|
344
|
+
return () => {
|
|
345
|
+
if (stateFrameRef.current !== null) {
|
|
346
|
+
window.cancelAnimationFrame(stateFrameRef.current);
|
|
347
|
+
stateFrameRef.current = null;
|
|
348
|
+
}
|
|
349
|
+
if (visibilityFrameRef.current !== null) {
|
|
350
|
+
window.cancelAnimationFrame(visibilityFrameRef.current);
|
|
351
|
+
visibilityFrameRef.current = null;
|
|
352
|
+
}
|
|
353
|
+
if (autoscrollingTimeoutRef.current !== null) {
|
|
354
|
+
window.clearTimeout(autoscrollingTimeoutRef.current);
|
|
355
|
+
autoscrollingTimeoutRef.current = null;
|
|
356
|
+
}
|
|
357
|
+
if (pendingScrollFrameRef.current !== null) {
|
|
358
|
+
window.cancelAnimationFrame(pendingScrollFrameRef.current);
|
|
359
|
+
pendingScrollFrameRef.current = null;
|
|
360
|
+
}
|
|
361
|
+
visibilityObserverRef.current?.disconnect();
|
|
362
|
+
visibilityObserverRef.current = null;
|
|
363
|
+
};
|
|
364
|
+
}, []);
|
|
365
|
+
useLayoutEffect(() => {
|
|
366
|
+
if (autoScroll && modeRef.current === "following-bottom" && itemCountRef.current > 0) {
|
|
367
|
+
scrollToEnd({ behavior: "auto" });
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
commitScrollState();
|
|
371
|
+
}, [
|
|
372
|
+
autoScroll,
|
|
373
|
+
commitScrollState,
|
|
374
|
+
scrollToEnd
|
|
375
|
+
]);
|
|
376
|
+
return {
|
|
377
|
+
context,
|
|
378
|
+
registerMessage
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
//#endregion
|
|
382
|
+
export { useMessageScrollerController };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { MessageScrollerMode, MessageScrollerScrollOptions, MessageScrollerScrollable, MessageScrollerStore, MessageScrollerVisibilityStore } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { RefObject } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/use-message-scroller-refs.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
type MessageScrollerRefs = {
|
|
9
|
+
autoScrollRef: RefObject<boolean>;
|
|
10
|
+
autoscrollingRef: RefObject<boolean>;
|
|
11
|
+
autoscrollingTimeoutRef: RefObject<number | null>;
|
|
12
|
+
streamingTurnRef: RefObject<HTMLElement | null>;
|
|
13
|
+
contentRef: RefObject<HTMLDivElement | null>;
|
|
14
|
+
defaultScrollPositionAppliedRef: RefObject<boolean>;
|
|
15
|
+
firstItemRef: RefObject<HTMLElement | null>;
|
|
16
|
+
itemCountRef: RefObject<number>;
|
|
17
|
+
messageElementsRef: RefObject<Map<string, HTMLElement>>;
|
|
18
|
+
modeRef: RefObject<MessageScrollerMode>;
|
|
19
|
+
pendingScrollFrameRef: RefObject<number | null>;
|
|
20
|
+
pendingScrollToMessageRef: RefObject<{
|
|
21
|
+
messageId: string;
|
|
22
|
+
options?: MessageScrollerScrollOptions;
|
|
23
|
+
} | null>;
|
|
24
|
+
prependRestoreRef: RefObject<{
|
|
25
|
+
element: HTMLElement;
|
|
26
|
+
viewportTop: number;
|
|
27
|
+
} | null>;
|
|
28
|
+
preserveScrollOnPrependRef: RefObject<boolean>;
|
|
29
|
+
rootRef: RefObject<HTMLDivElement | null>;
|
|
30
|
+
scrollEdgeThresholdRef: RefObject<number>;
|
|
31
|
+
scrollMarginRef: RefObject<number>;
|
|
32
|
+
scrollPreviousItemPeekRef: RefObject<number>;
|
|
33
|
+
spacerGapRef: RefObject<number>;
|
|
34
|
+
spacerHeightRef: RefObject<number>;
|
|
35
|
+
spacerRef: RefObject<HTMLDivElement | null>;
|
|
36
|
+
stateFrameRef: RefObject<number | null>;
|
|
37
|
+
stateStore: MessageScrollerStore<MessageScrollerScrollable>;
|
|
38
|
+
viewportRef: RefObject<HTMLDivElement | null>;
|
|
39
|
+
visibilityFrameRef: RefObject<number | null>;
|
|
40
|
+
visibilityObserverRef: RefObject<IntersectionObserver | null>;
|
|
41
|
+
visibilityStore: MessageScrollerVisibilityStore;
|
|
42
|
+
visibleMessageIdsRef: RefObject<Set<string>>;
|
|
43
|
+
handledScrollAnchorsRef: RefObject<WeakSet<HTMLElement>>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* @since 0.5.0-canary.3
|
|
47
|
+
*/
|
|
48
|
+
declare function useMessageScrollerRefs({
|
|
49
|
+
autoScroll,
|
|
50
|
+
scrollEdgeThreshold,
|
|
51
|
+
scrollMargin,
|
|
52
|
+
scrollPreviousItemPeek
|
|
53
|
+
}: {
|
|
54
|
+
autoScroll: boolean;
|
|
55
|
+
scrollEdgeThreshold: number;
|
|
56
|
+
scrollMargin: number;
|
|
57
|
+
scrollPreviousItemPeek: number;
|
|
58
|
+
}): MessageScrollerRefs;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { type MessageScrollerRefs, useMessageScrollerRefs };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { EMPTY_MESSAGE_SCROLLER_SCROLLABLE } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { areScrollStatesEqual, createMessageScrollerStore, createMessageScrollerVisibilityStore } from "../lib/message-scroller/stores.mjs";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
//#region src/hooks/use-message-scroller-refs.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
function useMessageScrollerRefs({ autoScroll, scrollEdgeThreshold, scrollMargin, scrollPreviousItemPeek }) {
|
|
9
|
+
const autoScrollRef = useRef(autoScroll);
|
|
10
|
+
const autoscrollingRef = useRef(false);
|
|
11
|
+
const contentRef = useRef(null);
|
|
12
|
+
const defaultScrollPositionAppliedRef = useRef(false);
|
|
13
|
+
const scrollEdgeThresholdRef = useRef(scrollEdgeThreshold);
|
|
14
|
+
const itemCountRef = useRef(0);
|
|
15
|
+
const firstItemRef = useRef(null);
|
|
16
|
+
const modeRef = useRef(autoScroll ? "following-bottom" : "free-scrolling");
|
|
17
|
+
const messageElementsRef = useRef(/* @__PURE__ */ new Map());
|
|
18
|
+
const pendingScrollToMessageRef = useRef(null);
|
|
19
|
+
const prependRestoreRef = useRef(null);
|
|
20
|
+
const streamingTurnRef = useRef(null);
|
|
21
|
+
const scrollPreviousItemPeekRef = useRef(scrollPreviousItemPeek);
|
|
22
|
+
const preserveScrollOnPrependRef = useRef(true);
|
|
23
|
+
const rootRef = useRef(null);
|
|
24
|
+
const scrollMarginRef = useRef(scrollMargin);
|
|
25
|
+
const pendingScrollFrameRef = useRef(null);
|
|
26
|
+
const spacerGapRef = useRef(0);
|
|
27
|
+
const spacerHeightRef = useRef(0);
|
|
28
|
+
const spacerRef = useRef(null);
|
|
29
|
+
const stateFrameRef = useRef(null);
|
|
30
|
+
const stateStoreRef = useRef(null);
|
|
31
|
+
const autoscrollingTimeoutRef = useRef(null);
|
|
32
|
+
const viewportRef = useRef(null);
|
|
33
|
+
const visibilityFrameRef = useRef(null);
|
|
34
|
+
const visibilityObserverRef = useRef(null);
|
|
35
|
+
const visibilityStoreRef = useRef(null);
|
|
36
|
+
const visibleMessageIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
37
|
+
const handledScrollAnchorsRef = useRef(/* @__PURE__ */ new WeakSet());
|
|
38
|
+
if (stateStoreRef.current === null) stateStoreRef.current = createMessageScrollerStore(EMPTY_MESSAGE_SCROLLER_SCROLLABLE, areScrollStatesEqual);
|
|
39
|
+
if (visibilityStoreRef.current === null) visibilityStoreRef.current = createMessageScrollerVisibilityStore();
|
|
40
|
+
autoScrollRef.current = autoScroll;
|
|
41
|
+
scrollEdgeThresholdRef.current = scrollEdgeThreshold;
|
|
42
|
+
scrollMarginRef.current = scrollMargin;
|
|
43
|
+
scrollPreviousItemPeekRef.current = scrollPreviousItemPeek;
|
|
44
|
+
return {
|
|
45
|
+
autoScrollRef,
|
|
46
|
+
autoscrollingRef,
|
|
47
|
+
autoscrollingTimeoutRef,
|
|
48
|
+
streamingTurnRef,
|
|
49
|
+
contentRef,
|
|
50
|
+
defaultScrollPositionAppliedRef,
|
|
51
|
+
firstItemRef,
|
|
52
|
+
itemCountRef,
|
|
53
|
+
messageElementsRef,
|
|
54
|
+
modeRef,
|
|
55
|
+
pendingScrollFrameRef,
|
|
56
|
+
pendingScrollToMessageRef,
|
|
57
|
+
prependRestoreRef,
|
|
58
|
+
preserveScrollOnPrependRef,
|
|
59
|
+
rootRef,
|
|
60
|
+
scrollEdgeThresholdRef,
|
|
61
|
+
scrollMarginRef,
|
|
62
|
+
scrollPreviousItemPeekRef,
|
|
63
|
+
spacerGapRef,
|
|
64
|
+
spacerHeightRef,
|
|
65
|
+
spacerRef,
|
|
66
|
+
stateFrameRef,
|
|
67
|
+
stateStore: stateStoreRef.current,
|
|
68
|
+
viewportRef,
|
|
69
|
+
visibilityFrameRef,
|
|
70
|
+
visibilityObserverRef,
|
|
71
|
+
visibilityStore: visibilityStoreRef.current,
|
|
72
|
+
visibleMessageIdsRef,
|
|
73
|
+
handledScrollAnchorsRef
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { useMessageScrollerRefs };
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* @since 0.3.16-canary.0
|
|
4
4
|
*/
|
|
5
|
-
interface
|
|
5
|
+
interface UsePaginationOptions {
|
|
6
6
|
/**
|
|
7
7
|
* Current active page number.
|
|
8
8
|
*/
|
|
@@ -34,7 +34,7 @@ declare const ELLIPSIS = "\u2022\u2022\u2022";
|
|
|
34
34
|
* collapsed ranges. The shape adapts to the total pages and the requested
|
|
35
35
|
* sibling window around the current page.
|
|
36
36
|
*
|
|
37
|
-
* @param props - Pagination options. See {@link
|
|
37
|
+
* @param props - Pagination options. See {@link UsePaginationOptions}.
|
|
38
38
|
* @returns Array of page numbers and `ELLIPSIS` representing the pagination model.
|
|
39
39
|
*
|
|
40
40
|
* @example
|
|
@@ -54,6 +54,6 @@ declare function usePagination({
|
|
|
54
54
|
resultsPerPage,
|
|
55
55
|
siblingPagesCount,
|
|
56
56
|
totalResults
|
|
57
|
-
}:
|
|
57
|
+
}: UsePaginationOptions): Array<number | string>;
|
|
58
58
|
//#endregion
|
|
59
|
-
export { ELLIPSIS,
|
|
59
|
+
export { ELLIPSIS, UsePaginationOptions, usePagination };
|
|
@@ -24,7 +24,7 @@ const createRange = (start, end) => {
|
|
|
24
24
|
* collapsed ranges. The shape adapts to the total pages and the requested
|
|
25
25
|
* sibling window around the current page.
|
|
26
26
|
*
|
|
27
|
-
* @param props - Pagination options. See {@link
|
|
27
|
+
* @param props - Pagination options. See {@link UsePaginationOptions}.
|
|
28
28
|
* @returns Array of page numbers and `ELLIPSIS` representing the pagination model.
|
|
29
29
|
*
|
|
30
30
|
* @example
|