@codefast/ui 0.5.0-canary.1 → 0.5.0-canary.3
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 +39 -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/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/chart.mjs +2 -1
- package/dist/components/empty.mjs +1 -1
- package/dist/components/form.mjs +2 -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/sidebar.mjs +6 -3
- package/dist/hooks/use-animated-value.mjs +3 -1
- 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.mjs +18 -12
- package/dist/index.d.mts +11 -1
- 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/checkbox-group.mjs +20 -10
- 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 +57 -11
- package/src/components/attachment.tsx +284 -0
- package/src/components/avatar.tsx +1 -1
- 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/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-message-scroller-commands.ts +312 -0
- package/src/hooks/use-message-scroller-controller.ts +651 -0
- package/src/hooks/use-message-scroller-refs.ts +168 -0
- package/src/index.ts +72 -0
- package/src/lib/message-scroller/geometry.ts +398 -0
- package/src/lib/message-scroller/stores.ts +89 -0
- package/src/lib/message-scroller/types.ts +310 -0
- package/src/lib/message-scroller/utils.ts +15 -0
- package/src/lib/use-render.ts +202 -0
- package/src/primitives/message-scroller.tsx +399 -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,312 @@
|
|
|
1
|
+
// Vendored from shadcn-ui/ui packages/react/src/message-scroller/use-message-scroller-commands.ts
|
|
2
|
+
// Callbacks read from stable refs; omitting them from deps is intentional and keeps identities stable.
|
|
3
|
+
/* oxlint-disable react-hooks/exhaustive-deps */
|
|
4
|
+
import { useCallback } from "react";
|
|
5
|
+
|
|
6
|
+
import type { MessageScrollerRefs } from "#/hooks/use-message-scroller-refs";
|
|
7
|
+
import {
|
|
8
|
+
getElementScrollTop,
|
|
9
|
+
getElementViewportTop,
|
|
10
|
+
getMaxScrollTop,
|
|
11
|
+
getTailSpacerHeight,
|
|
12
|
+
} from "#/lib/message-scroller/geometry";
|
|
13
|
+
import { AUTOSCROLLING_CLEAR_DELAY, SCROLL_POSITION_EPSILON } from "#/lib/message-scroller/types";
|
|
14
|
+
import type { MessageScrollerScrollOptions } from "#/lib/message-scroller/types";
|
|
15
|
+
|
|
16
|
+
// Imperative scroll primitives, split from the controller so the move mechanics
|
|
17
|
+
// live apart from the policy that decides when to run them. Each command resolves
|
|
18
|
+
// a target scrollTop and returns false when the viewport is not mounted yet.
|
|
19
|
+
/**
|
|
20
|
+
* @since 0.5.0-canary.3
|
|
21
|
+
*/
|
|
22
|
+
function useMessageScrollerCommands({
|
|
23
|
+
refs,
|
|
24
|
+
commitScrollState,
|
|
25
|
+
scheduleStateCommit,
|
|
26
|
+
scheduleVisibilitySync,
|
|
27
|
+
}: {
|
|
28
|
+
refs: MessageScrollerRefs;
|
|
29
|
+
commitScrollState: () => void;
|
|
30
|
+
scheduleStateCommit: () => void;
|
|
31
|
+
scheduleVisibilitySync: () => void;
|
|
32
|
+
}) {
|
|
33
|
+
const {
|
|
34
|
+
streamingTurnRef,
|
|
35
|
+
autoScrollRef,
|
|
36
|
+
autoscrollingRef,
|
|
37
|
+
autoscrollingTimeoutRef,
|
|
38
|
+
contentRef,
|
|
39
|
+
defaultScrollPositionAppliedRef,
|
|
40
|
+
itemCountRef,
|
|
41
|
+
messageElementsRef,
|
|
42
|
+
modeRef,
|
|
43
|
+
pendingScrollToMessageRef,
|
|
44
|
+
prependRestoreRef,
|
|
45
|
+
scrollMarginRef,
|
|
46
|
+
scrollPreviousItemPeekRef,
|
|
47
|
+
spacerGapRef,
|
|
48
|
+
spacerHeightRef,
|
|
49
|
+
spacerRef,
|
|
50
|
+
viewportRef,
|
|
51
|
+
} = refs;
|
|
52
|
+
|
|
53
|
+
const setAutoScrolling = useCallback(
|
|
54
|
+
(autoscrolling: boolean) => {
|
|
55
|
+
if (autoscrollingTimeoutRef.current !== null) {
|
|
56
|
+
window.clearTimeout(autoscrollingTimeoutRef.current);
|
|
57
|
+
autoscrollingTimeoutRef.current = null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (autoscrollingRef.current !== autoscrolling) {
|
|
61
|
+
autoscrollingRef.current = autoscrolling;
|
|
62
|
+
commitScrollState();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (autoscrolling) {
|
|
66
|
+
autoscrollingTimeoutRef.current = window.setTimeout(() => {
|
|
67
|
+
autoscrollingTimeoutRef.current = null;
|
|
68
|
+
autoscrollingRef.current = false;
|
|
69
|
+
commitScrollState();
|
|
70
|
+
}, AUTOSCROLLING_CLEAR_DELAY);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
[commitScrollState],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const setTailSpacerHeight = useCallback((height: number) => {
|
|
77
|
+
const spacer = spacerRef.current;
|
|
78
|
+
|
|
79
|
+
if (!spacer) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const nextHeight = Math.max(0, Math.ceil(height));
|
|
84
|
+
|
|
85
|
+
if (spacerHeightRef.current === nextHeight) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
spacerHeightRef.current = nextHeight;
|
|
90
|
+
spacer.hidden = nextHeight === 0;
|
|
91
|
+
spacer.style.height = `${nextHeight}px`;
|
|
92
|
+
spacer.style.marginTop = nextHeight > 0 ? `${-spacerGapRef.current}px` : "";
|
|
93
|
+
}, []);
|
|
94
|
+
|
|
95
|
+
const scrollToPosition = useCallback(
|
|
96
|
+
(
|
|
97
|
+
scrollTop: number,
|
|
98
|
+
{
|
|
99
|
+
behavior = "auto",
|
|
100
|
+
autoscrolling = false,
|
|
101
|
+
}: {
|
|
102
|
+
behavior?: ScrollBehavior;
|
|
103
|
+
autoscrolling?: boolean;
|
|
104
|
+
} = {},
|
|
105
|
+
) => {
|
|
106
|
+
const viewport = viewportRef.current;
|
|
107
|
+
|
|
108
|
+
if (!viewport) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const nextScrollTop = Math.max(0, scrollTop);
|
|
113
|
+
|
|
114
|
+
if (Math.abs(viewport.scrollTop - nextScrollTop) <= SCROLL_POSITION_EPSILON) {
|
|
115
|
+
viewport.scrollTop = nextScrollTop;
|
|
116
|
+
commitScrollState();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (autoscrolling) {
|
|
121
|
+
setAutoScrolling(true);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
viewport.scrollTo({
|
|
125
|
+
top: nextScrollTop,
|
|
126
|
+
behavior,
|
|
127
|
+
});
|
|
128
|
+
scheduleStateCommit();
|
|
129
|
+
},
|
|
130
|
+
[commitScrollState, scheduleStateCommit, setAutoScrolling],
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const scrollToStart = useCallback(
|
|
134
|
+
({ behavior = "auto" }: MessageScrollerScrollOptions = {}) => {
|
|
135
|
+
if (!viewportRef.current) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
setTailSpacerHeight(0);
|
|
140
|
+
streamingTurnRef.current = null;
|
|
141
|
+
modeRef.current = "free-scrolling";
|
|
142
|
+
scrollToPosition(0, { behavior });
|
|
143
|
+
scheduleVisibilitySync();
|
|
144
|
+
|
|
145
|
+
return true;
|
|
146
|
+
},
|
|
147
|
+
[scheduleVisibilitySync, scrollToPosition, setTailSpacerHeight],
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
const scrollToEnd = useCallback(
|
|
151
|
+
({ behavior = "auto" }: MessageScrollerScrollOptions = {}) => {
|
|
152
|
+
const viewport = viewportRef.current;
|
|
153
|
+
|
|
154
|
+
if (!viewport) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setTailSpacerHeight(0);
|
|
159
|
+
streamingTurnRef.current = null;
|
|
160
|
+
modeRef.current = autoScrollRef.current ? "following-bottom" : "free-scrolling";
|
|
161
|
+
scrollToPosition(getMaxScrollTop(viewport), {
|
|
162
|
+
autoscrolling: true,
|
|
163
|
+
behavior,
|
|
164
|
+
});
|
|
165
|
+
scheduleVisibilitySync();
|
|
166
|
+
|
|
167
|
+
return true;
|
|
168
|
+
},
|
|
169
|
+
[scheduleVisibilitySync, scrollToPosition, setTailSpacerHeight],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const scrollToElement = useCallback(
|
|
173
|
+
(
|
|
174
|
+
element: HTMLElement,
|
|
175
|
+
{ align = "start", behavior = "auto", scrollMargin = scrollMarginRef.current }: MessageScrollerScrollOptions = {},
|
|
176
|
+
{
|
|
177
|
+
keepPreviousPeek = false,
|
|
178
|
+
}: {
|
|
179
|
+
keepPreviousPeek?: boolean;
|
|
180
|
+
} = {},
|
|
181
|
+
) => {
|
|
182
|
+
const content = contentRef.current;
|
|
183
|
+
const viewport = viewportRef.current;
|
|
184
|
+
|
|
185
|
+
if (!content || !viewport || !content.contains(element)) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const scrollTop = getElementScrollTop({
|
|
190
|
+
align,
|
|
191
|
+
element,
|
|
192
|
+
scrollMargin: keepPreviousPeek ? scrollMargin + scrollPreviousItemPeekRef.current : scrollMargin,
|
|
193
|
+
spacer: spacerRef.current,
|
|
194
|
+
viewport,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const nextSpacerHeight = getTailSpacerHeight({
|
|
198
|
+
content,
|
|
199
|
+
scrollTop,
|
|
200
|
+
spacer: spacerRef.current,
|
|
201
|
+
viewport,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
setTailSpacerHeight(nextSpacerHeight);
|
|
205
|
+
// Seed the prepend anchor with the jump target so a prepend that lands
|
|
206
|
+
// before this scroll settles still preserves the jumped-to row; once it
|
|
207
|
+
// settles, syncAfterScroll's capturePrependAnchor re-captures it from the
|
|
208
|
+
// first visible row.
|
|
209
|
+
prependRestoreRef.current = {
|
|
210
|
+
element,
|
|
211
|
+
viewportTop: getElementViewportTop(element, viewport),
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
modeRef.current = keepPreviousPeek ? "anchored-to-message" : "settling-jump";
|
|
215
|
+
streamingTurnRef.current = keepPreviousPeek ? element : null;
|
|
216
|
+
|
|
217
|
+
scrollToPosition(scrollTop, { behavior });
|
|
218
|
+
scheduleVisibilitySync();
|
|
219
|
+
|
|
220
|
+
return true;
|
|
221
|
+
},
|
|
222
|
+
[scheduleVisibilitySync, scrollToPosition, setTailSpacerHeight],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const reanchorToAnchoredMessage = useCallback(() => {
|
|
226
|
+
const element = streamingTurnRef.current;
|
|
227
|
+
|
|
228
|
+
if (!element || !element.isConnected || modeRef.current !== "anchored-to-message") {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
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]);
|
|
236
|
+
|
|
237
|
+
// The target row may not be mounted yet (e.g. an async-loaded transcript).
|
|
238
|
+
// When it is missing the request is queued in pendingScrollToMessageRef and
|
|
239
|
+
// flushed later — on registerMessage for that id, or on the next content
|
|
240
|
+
// change. An explicit jump also marks the mount default as applied, so
|
|
241
|
+
// defaultScrollPosition does not override it.
|
|
242
|
+
const scrollToMessage = useCallback(
|
|
243
|
+
(messageId: string, options?: MessageScrollerScrollOptions) => {
|
|
244
|
+
const element = messageElementsRef.current.get(messageId);
|
|
245
|
+
|
|
246
|
+
if (!element) {
|
|
247
|
+
if (itemCountRef.current === 0) {
|
|
248
|
+
pendingScrollToMessageRef.current = {
|
|
249
|
+
messageId,
|
|
250
|
+
options,
|
|
251
|
+
};
|
|
252
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
253
|
+
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
261
|
+
|
|
262
|
+
if (scrollToElement(element, options)) {
|
|
263
|
+
pendingScrollToMessageRef.current = null;
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
pendingScrollToMessageRef.current = {
|
|
268
|
+
messageId,
|
|
269
|
+
options,
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
return true;
|
|
273
|
+
},
|
|
274
|
+
[scrollToElement],
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
const flushPendingScrollToMessage = useCallback(() => {
|
|
278
|
+
const pending = pendingScrollToMessageRef.current;
|
|
279
|
+
|
|
280
|
+
if (!pending) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const element = messageElementsRef.current.get(pending.messageId);
|
|
285
|
+
|
|
286
|
+
if (!element) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const handled = scrollToElement(element, pending.options);
|
|
291
|
+
|
|
292
|
+
if (!handled) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
pendingScrollToMessageRef.current = null;
|
|
297
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
298
|
+
|
|
299
|
+
return true;
|
|
300
|
+
}, [scrollToElement]);
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
flushPendingScrollToMessage,
|
|
304
|
+
reanchorToAnchoredMessage,
|
|
305
|
+
scrollToElement,
|
|
306
|
+
scrollToEnd,
|
|
307
|
+
scrollToMessage,
|
|
308
|
+
scrollToStart,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { useMessageScrollerCommands };
|