@majapisoftwares/ui 6.7.0 → 6.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +2 -1
- package/THIRD_PARTY_NOTICES.md +2 -0
- package/docs/STANDARDS.md +15 -4
- package/package.json +6 -3
- package/src/components/ui/code-block.tsx +246 -0
- package/src/components/ui/marker.tsx +72 -0
- package/src/components/ui/message-scroller.tsx +2 -2
- package/src/hooks/use-highlighted-code.ts +77 -0
- package/src/lib/highlighter.tsx +124 -0
- package/src/lib/i18n.tsx +81 -0
- package/src/lib/message-scroller/components.tsx +446 -0
- package/src/lib/message-scroller/geometry.ts +387 -0
- package/src/lib/message-scroller/index.ts +31 -0
- package/src/lib/message-scroller/stores.ts +97 -0
- package/src/lib/message-scroller/types.ts +240 -0
- package/src/lib/message-scroller/use-message-scroller-commands.ts +337 -0
- package/src/lib/message-scroller/use-message-scroller-controller.ts +874 -0
- package/src/lib/message-scroller/use-message-scroller-refs.ts +200 -0
- package/src/lib/message-scroller/use-render.ts +208 -0
- package/src/lib/message-scroller/utils.ts +11 -0
- package/src/lib/utils.ts +16 -0
- package/src/patterns/chat-checkpoint.tsx +49 -0
- package/src/patterns/chat-citation.tsx +57 -0
- package/src/patterns/chat-context.tsx +82 -0
- package/src/patterns/chat-trace.tsx +739 -9
- package/src/patterns/chat.tsx +119 -4
- package/src/patterns/file-tree.tsx +122 -0
- package/src/patterns/model-selector.tsx +153 -0
- package/src/patterns/prompt-input.tsx +18 -0
package/src/lib/i18n.tsx
CHANGED
|
@@ -85,6 +85,8 @@ export const uiMessages = defineMessages({
|
|
|
85
85
|
responding: "Responding",
|
|
86
86
|
copy: "Copy",
|
|
87
87
|
copied: "Copied",
|
|
88
|
+
reply: "Reply",
|
|
89
|
+
cancelReply: "Cancel reply",
|
|
88
90
|
goodResponse: "Good response",
|
|
89
91
|
badResponse: "Bad response",
|
|
90
92
|
thinking: "Thinking",
|
|
@@ -93,6 +95,44 @@ export const uiMessages = defineMessages({
|
|
|
93
95
|
approve: "Approve",
|
|
94
96
|
deny: "Deny",
|
|
95
97
|
suggestions: "Suggestions",
|
|
98
|
+
researching: "Researching",
|
|
99
|
+
researched: (steps: number) =>
|
|
100
|
+
`Researched in ${steps} ${steps === 1 ? "step" : "steps"}`,
|
|
101
|
+
planProgress: (done: number, total: number) => `${done} of ${total} done`,
|
|
102
|
+
toDo: (count: number) => `${count} to do`,
|
|
103
|
+
completedCount: (count: number) => `${count} completed`,
|
|
104
|
+
usedSources: (count: number) =>
|
|
105
|
+
`Used ${count} ${count === 1 ? "source" : "sources"}`,
|
|
106
|
+
opensInNewTab: "(opens in a new tab)",
|
|
107
|
+
runningCommand: (command: string) => `Running ${command}`,
|
|
108
|
+
ranCommand: (command: string) => `Ran ${command}`,
|
|
109
|
+
exitedWith: (command: string, code: number) =>
|
|
110
|
+
`${command} exited with code ${code}`,
|
|
111
|
+
testCounts: (total: number, failed: number, skipped: number) =>
|
|
112
|
+
[
|
|
113
|
+
`${total} ${total === 1 ? "test" : "tests"}`,
|
|
114
|
+
failed > 0 && `${failed} failed`,
|
|
115
|
+
skipped > 0 && `${skipped} skipped`,
|
|
116
|
+
]
|
|
117
|
+
.filter(Boolean)
|
|
118
|
+
.join(", "),
|
|
119
|
+
ranTests: (counts: string) => `Ran ${counts}`,
|
|
120
|
+
skippedTest: (name: string) => `Skipped ${name}`,
|
|
121
|
+
failedWith: (error: string) => `Failed: ${error}`,
|
|
122
|
+
frameworkFrames: (count: number) =>
|
|
123
|
+
`${count} framework ${count === 1 ? "frame" : "frames"}`,
|
|
124
|
+
restore: "Restore",
|
|
125
|
+
restoreTo: (label: string) => `Restore to ${label}`,
|
|
126
|
+
contextPercent: (percent: number) => `${percent}% context`,
|
|
127
|
+
context: "Context",
|
|
128
|
+
contextUsed: "Context used",
|
|
129
|
+
inputTokens: "Input",
|
|
130
|
+
outputTokens: "Output",
|
|
131
|
+
reasoningTokens: "Reasoning",
|
|
132
|
+
model: "Model",
|
|
133
|
+
effort: "Effort",
|
|
134
|
+
modelIs: (name: string) => `Model: ${name}`,
|
|
135
|
+
effortIs: (name: string) => `Effort: ${name}`,
|
|
96
136
|
},
|
|
97
137
|
"pt-BR": {
|
|
98
138
|
sidebar: "Barra lateral",
|
|
@@ -135,6 +175,8 @@ export const uiMessages = defineMessages({
|
|
|
135
175
|
responding: "Respondendo",
|
|
136
176
|
copy: "Copiar",
|
|
137
177
|
copied: "Copiado",
|
|
178
|
+
reply: "Responder",
|
|
179
|
+
cancelReply: "Cancelar resposta",
|
|
138
180
|
goodResponse: "Boa resposta",
|
|
139
181
|
badResponse: "Resposta ruim",
|
|
140
182
|
thinking: "Pensando",
|
|
@@ -143,6 +185,45 @@ export const uiMessages = defineMessages({
|
|
|
143
185
|
approve: "Aprovar",
|
|
144
186
|
deny: "Negar",
|
|
145
187
|
suggestions: "Sugestões",
|
|
188
|
+
researching: "Pesquisando",
|
|
189
|
+
researched: (steps: number) =>
|
|
190
|
+
`Pesquisou em ${steps} ${steps === 1 ? "passo" : "passos"}`,
|
|
191
|
+
planProgress: (done: number, total: number) => `${done} de ${total} feitos`,
|
|
192
|
+
toDo: (count: number) => `${count} a fazer`,
|
|
193
|
+
completedCount: (count: number) =>
|
|
194
|
+
`${count} ${count === 1 ? "concluído" : "concluídos"}`,
|
|
195
|
+
usedSources: (count: number) =>
|
|
196
|
+
`Usou ${count} ${count === 1 ? "fonte" : "fontes"}`,
|
|
197
|
+
opensInNewTab: "(abre em uma nova aba)",
|
|
198
|
+
runningCommand: (command: string) => `Rodando ${command}`,
|
|
199
|
+
ranCommand: (command: string) => `Rodou ${command}`,
|
|
200
|
+
exitedWith: (command: string, code: number) =>
|
|
201
|
+
`${command} saiu com código ${code}`,
|
|
202
|
+
testCounts: (total: number, failed: number, skipped: number) =>
|
|
203
|
+
[
|
|
204
|
+
`${total} ${total === 1 ? "teste" : "testes"}`,
|
|
205
|
+
failed > 0 && `${failed} ${failed === 1 ? "falhou" : "falharam"}`,
|
|
206
|
+
skipped > 0 && `${skipped} ${skipped === 1 ? "pulado" : "pulados"}`,
|
|
207
|
+
]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join(", "),
|
|
210
|
+
ranTests: (counts: string) => `Rodou ${counts}`,
|
|
211
|
+
skippedTest: (name: string) => `Pulou ${name}`,
|
|
212
|
+
failedWith: (error: string) => `Falhou: ${error}`,
|
|
213
|
+
frameworkFrames: (count: number) =>
|
|
214
|
+
`${count} ${count === 1 ? "quadro" : "quadros"} do framework`,
|
|
215
|
+
restore: "Restaurar",
|
|
216
|
+
restoreTo: (label: string) => `Restaurar até ${label}`,
|
|
217
|
+
contextPercent: (percent: number) => `${percent}% do contexto`,
|
|
218
|
+
context: "Contexto",
|
|
219
|
+
contextUsed: "Contexto usado",
|
|
220
|
+
inputTokens: "Entrada",
|
|
221
|
+
outputTokens: "Saída",
|
|
222
|
+
reasoningTokens: "Raciocínio",
|
|
223
|
+
model: "Modelo",
|
|
224
|
+
effort: "Esforço",
|
|
225
|
+
modelIs: (name: string) => `Modelo: ${name}`,
|
|
226
|
+
effortIs: (name: string) => `Esforço: ${name}`,
|
|
146
227
|
},
|
|
147
228
|
});
|
|
148
229
|
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import { composeRefs, mergeProps, useRender } from "./use-render"
|
|
4
|
+
import { USER_SCROLL_KEYS } from "./types"
|
|
5
|
+
import type {
|
|
6
|
+
MessageScrollerButtonProps,
|
|
7
|
+
MessageScrollerContentProps,
|
|
8
|
+
MessageScrollerContextValue,
|
|
9
|
+
MessageScrollerItemProps,
|
|
10
|
+
MessageScrollerProps,
|
|
11
|
+
MessageScrollerProviderProps,
|
|
12
|
+
MessageScrollerRegisterMessage,
|
|
13
|
+
MessageScrollerViewportProps,
|
|
14
|
+
} from "./types"
|
|
15
|
+
import { useMessageScrollerController } from "./use-message-scroller-controller"
|
|
16
|
+
import { useLatest } from "./utils"
|
|
17
|
+
|
|
18
|
+
const MessageScrollerContext =
|
|
19
|
+
React.createContext<MessageScrollerContextValue | null>(null)
|
|
20
|
+
const MessageScrollerItemContext =
|
|
21
|
+
React.createContext<MessageScrollerRegisterMessage | null>(null)
|
|
22
|
+
|
|
23
|
+
function useMessageScrollerContext() {
|
|
24
|
+
const context = React.useContext(MessageScrollerContext)
|
|
25
|
+
|
|
26
|
+
if (!context) {
|
|
27
|
+
throw new Error("useMessageScroller must be used within a MessageScroller.")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return context
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function useMessageScrollerItemContext() {
|
|
34
|
+
const context = React.useContext(MessageScrollerItemContext)
|
|
35
|
+
|
|
36
|
+
if (!context) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"MessageScrollerItem must be used within a MessageScroller."
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return context
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function useMessageScroller() {
|
|
46
|
+
const { scrollToEnd, scrollToMessage, scrollToStart } =
|
|
47
|
+
useMessageScrollerContext()
|
|
48
|
+
|
|
49
|
+
return React.useMemo(
|
|
50
|
+
() => ({
|
|
51
|
+
scrollToEnd,
|
|
52
|
+
scrollToMessage,
|
|
53
|
+
scrollToStart,
|
|
54
|
+
}),
|
|
55
|
+
[scrollToEnd, scrollToMessage, scrollToStart]
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function useMessageScrollerScrollable() {
|
|
60
|
+
const { stateStore } = useMessageScrollerContext()
|
|
61
|
+
|
|
62
|
+
return React.useSyncExternalStore(
|
|
63
|
+
stateStore.subscribe,
|
|
64
|
+
stateStore.getSnapshot,
|
|
65
|
+
stateStore.getSnapshot
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function useMessageScrollerVisibility() {
|
|
70
|
+
const { observeVisibility, unobserveVisibility, visibilityStore } =
|
|
71
|
+
useMessageScrollerContext()
|
|
72
|
+
const subscribe = React.useCallback(
|
|
73
|
+
(listener: () => void) =>
|
|
74
|
+
visibilityStore.subscribe(
|
|
75
|
+
listener,
|
|
76
|
+
observeVisibility,
|
|
77
|
+
unobserveVisibility
|
|
78
|
+
),
|
|
79
|
+
[observeVisibility, unobserveVisibility, visibilityStore]
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return React.useSyncExternalStore(
|
|
83
|
+
subscribe,
|
|
84
|
+
visibilityStore.getSnapshot,
|
|
85
|
+
visibilityStore.getSnapshot
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function MessageScrollerProvider({
|
|
90
|
+
autoScroll = false,
|
|
91
|
+
children,
|
|
92
|
+
defaultScrollPosition = "end",
|
|
93
|
+
scrollEdgeThreshold,
|
|
94
|
+
scrollPreviousItemPeek,
|
|
95
|
+
scrollMargin,
|
|
96
|
+
}: MessageScrollerProviderProps) {
|
|
97
|
+
const { context, registerMessage } = useMessageScrollerController({
|
|
98
|
+
autoScroll,
|
|
99
|
+
defaultScrollPosition,
|
|
100
|
+
scrollEdgeThreshold,
|
|
101
|
+
scrollPreviousItemPeek,
|
|
102
|
+
scrollMargin,
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<MessageScrollerContext.Provider value={context}>
|
|
107
|
+
<MessageScrollerItemContext.Provider value={registerMessage}>
|
|
108
|
+
{children}
|
|
109
|
+
</MessageScrollerItemContext.Provider>
|
|
110
|
+
</MessageScrollerContext.Provider>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function usePendingDefaultScroll() {
|
|
115
|
+
const { pendingDefaultScrollStore } = useMessageScrollerContext()
|
|
116
|
+
|
|
117
|
+
return React.useSyncExternalStore(
|
|
118
|
+
pendingDefaultScrollStore.subscribe,
|
|
119
|
+
pendingDefaultScrollStore.getSnapshot,
|
|
120
|
+
pendingDefaultScrollStore.getSnapshot
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function MessageScroller({ children, ...props }: MessageScrollerProps) {
|
|
125
|
+
const { setRootElement } = useMessageScrollerContext()
|
|
126
|
+
const pendingDefaultScroll = usePendingDefaultScroll()
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<div
|
|
130
|
+
ref={setRootElement}
|
|
131
|
+
{...props}
|
|
132
|
+
{...(pendingDefaultScroll ? { "data-pending-scroll": "" } : null)}
|
|
133
|
+
>
|
|
134
|
+
{children}
|
|
135
|
+
</div>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function MessageScrollerViewport({
|
|
140
|
+
"aria-label": ariaLabel,
|
|
141
|
+
children,
|
|
142
|
+
onKeyDown,
|
|
143
|
+
onScroll,
|
|
144
|
+
onTouchMove,
|
|
145
|
+
onWheel,
|
|
146
|
+
preserveScrollOnPrepend = true,
|
|
147
|
+
ref,
|
|
148
|
+
role,
|
|
149
|
+
tabIndex,
|
|
150
|
+
...props
|
|
151
|
+
}: MessageScrollerViewportProps) {
|
|
152
|
+
const {
|
|
153
|
+
handleResize,
|
|
154
|
+
preserveScrollOnPrependRef,
|
|
155
|
+
setViewportElement,
|
|
156
|
+
syncAfterScroll,
|
|
157
|
+
userScrollIntent,
|
|
158
|
+
viewportRef,
|
|
159
|
+
} = useMessageScrollerContext()
|
|
160
|
+
const pendingDefaultScroll = usePendingDefaultScroll()
|
|
161
|
+
|
|
162
|
+
preserveScrollOnPrependRef.current = preserveScrollOnPrepend
|
|
163
|
+
|
|
164
|
+
const setViewportRef = React.useCallback(
|
|
165
|
+
(element: HTMLDivElement | null) => {
|
|
166
|
+
setViewportElement(element)
|
|
167
|
+
composeRefs(ref)?.(element)
|
|
168
|
+
},
|
|
169
|
+
[ref, setViewportElement]
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
function handleScroll(event: React.UIEvent<HTMLDivElement>) {
|
|
173
|
+
syncAfterScroll()
|
|
174
|
+
onScroll?.(event)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function handleWheel(event: React.WheelEvent<HTMLDivElement>) {
|
|
178
|
+
userScrollIntent()
|
|
179
|
+
onWheel?.(event)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function handleTouchMove(event: React.TouchEvent<HTMLDivElement>) {
|
|
183
|
+
userScrollIntent()
|
|
184
|
+
onTouchMove?.(event)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
|
|
188
|
+
if (USER_SCROLL_KEYS.has(event.key)) {
|
|
189
|
+
userScrollIntent()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
onKeyDown?.(event)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
React.useEffect(() => {
|
|
196
|
+
const viewport = viewportRef.current
|
|
197
|
+
|
|
198
|
+
if (!viewport || typeof ResizeObserver === "undefined") {
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Coalesce into rAF: handleResize mutates the spacer inside the observed
|
|
203
|
+
// content, and resizing an observed element during delivery fires
|
|
204
|
+
// "ResizeObserver loop completed with undelivered notifications".
|
|
205
|
+
let frame = 0
|
|
206
|
+
|
|
207
|
+
const observer = new ResizeObserver(() => {
|
|
208
|
+
window.cancelAnimationFrame(frame)
|
|
209
|
+
frame = window.requestAnimationFrame(handleResize)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
observer.observe(viewport)
|
|
213
|
+
|
|
214
|
+
return () => {
|
|
215
|
+
window.cancelAnimationFrame(frame)
|
|
216
|
+
observer.disconnect()
|
|
217
|
+
}
|
|
218
|
+
}, [handleResize, viewportRef])
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<div
|
|
222
|
+
ref={setViewportRef}
|
|
223
|
+
role={role ?? "region"}
|
|
224
|
+
aria-label={ariaLabel ?? "Messages"}
|
|
225
|
+
tabIndex={tabIndex ?? 0}
|
|
226
|
+
onKeyDown={handleKeyDown}
|
|
227
|
+
onScroll={handleScroll}
|
|
228
|
+
onTouchMove={handleTouchMove}
|
|
229
|
+
onWheel={handleWheel}
|
|
230
|
+
{...props}
|
|
231
|
+
{...(pendingDefaultScroll ? { "data-pending-scroll": "" } : null)}
|
|
232
|
+
>
|
|
233
|
+
{children}
|
|
234
|
+
</div>
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function MessageScrollerContent({
|
|
239
|
+
"aria-relevant": ariaRelevant,
|
|
240
|
+
children,
|
|
241
|
+
ref,
|
|
242
|
+
role,
|
|
243
|
+
spacerClassName,
|
|
244
|
+
...props
|
|
245
|
+
}: MessageScrollerContentProps) {
|
|
246
|
+
const {
|
|
247
|
+
handleContentChange,
|
|
248
|
+
handleResize,
|
|
249
|
+
setContentElement,
|
|
250
|
+
setSpacerElement,
|
|
251
|
+
} = useMessageScrollerContext()
|
|
252
|
+
const contentRef = React.useRef<HTMLDivElement | null>(null)
|
|
253
|
+
|
|
254
|
+
const setContentRef = React.useCallback(
|
|
255
|
+
(element: HTMLDivElement | null) => {
|
|
256
|
+
contentRef.current = element
|
|
257
|
+
setContentElement(element)
|
|
258
|
+
composeRefs(ref)?.(element)
|
|
259
|
+
},
|
|
260
|
+
[ref, setContentElement]
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
React.useLayoutEffect(() => {
|
|
264
|
+
const content = contentRef.current
|
|
265
|
+
|
|
266
|
+
if (!content) {
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
handleContentChange()
|
|
271
|
+
|
|
272
|
+
if (typeof MutationObserver === "undefined") {
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const observer = new MutationObserver(() => {
|
|
277
|
+
handleContentChange()
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
observer.observe(content, { childList: true })
|
|
281
|
+
|
|
282
|
+
return () => observer.disconnect()
|
|
283
|
+
}, [handleContentChange])
|
|
284
|
+
|
|
285
|
+
React.useEffect(() => {
|
|
286
|
+
const content = contentRef.current
|
|
287
|
+
|
|
288
|
+
if (!content || typeof ResizeObserver === "undefined") {
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Coalesce into rAF: handleResize mutates the spacer inside this observed
|
|
293
|
+
// element, and resizing an observed element during delivery fires
|
|
294
|
+
// "ResizeObserver loop completed with undelivered notifications".
|
|
295
|
+
let frame = 0
|
|
296
|
+
|
|
297
|
+
const observer = new ResizeObserver(() => {
|
|
298
|
+
window.cancelAnimationFrame(frame)
|
|
299
|
+
frame = window.requestAnimationFrame(handleResize)
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
observer.observe(content)
|
|
303
|
+
|
|
304
|
+
return () => {
|
|
305
|
+
window.cancelAnimationFrame(frame)
|
|
306
|
+
observer.disconnect()
|
|
307
|
+
}
|
|
308
|
+
}, [handleResize])
|
|
309
|
+
|
|
310
|
+
return (
|
|
311
|
+
<div
|
|
312
|
+
ref={setContentRef}
|
|
313
|
+
role={role ?? "log"}
|
|
314
|
+
aria-relevant={ariaRelevant ?? "additions"}
|
|
315
|
+
{...props}
|
|
316
|
+
>
|
|
317
|
+
{children}
|
|
318
|
+
<div
|
|
319
|
+
ref={setSpacerElement}
|
|
320
|
+
aria-hidden="true"
|
|
321
|
+
data-message-scroller-spacer=""
|
|
322
|
+
hidden
|
|
323
|
+
className={spacerClassName}
|
|
324
|
+
/>
|
|
325
|
+
</div>
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function MessageScrollerItem({
|
|
330
|
+
messageId,
|
|
331
|
+
ref,
|
|
332
|
+
scrollAnchor = false,
|
|
333
|
+
...props
|
|
334
|
+
}: MessageScrollerItemProps) {
|
|
335
|
+
const registerMessage = useMessageScrollerItemContext()
|
|
336
|
+
const elementRef = React.useRef<HTMLDivElement | null>(null)
|
|
337
|
+
|
|
338
|
+
const setItemRef = React.useCallback(
|
|
339
|
+
(element: HTMLDivElement | null) => {
|
|
340
|
+
const previousElement = elementRef.current
|
|
341
|
+
|
|
342
|
+
elementRef.current = element
|
|
343
|
+
|
|
344
|
+
if (messageId) {
|
|
345
|
+
registerMessage(messageId, element, previousElement)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
composeRefs(ref)?.(element)
|
|
349
|
+
},
|
|
350
|
+
[messageId, ref, registerMessage]
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
return (
|
|
354
|
+
<div
|
|
355
|
+
ref={setItemRef}
|
|
356
|
+
data-message-id={messageId}
|
|
357
|
+
data-scroll-anchor={scrollAnchor ? "true" : "false"}
|
|
358
|
+
{...props}
|
|
359
|
+
/>
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function MessageScrollerButton({
|
|
364
|
+
behavior = "smooth",
|
|
365
|
+
children,
|
|
366
|
+
direction = "end",
|
|
367
|
+
onClick,
|
|
368
|
+
render,
|
|
369
|
+
tabIndex,
|
|
370
|
+
type = "button",
|
|
371
|
+
...props
|
|
372
|
+
}: MessageScrollerButtonProps) {
|
|
373
|
+
const { scrollToEnd, scrollToStart, stateStore } = useMessageScrollerContext()
|
|
374
|
+
const onClickRef = useLatest(onClick)
|
|
375
|
+
const subscribe = React.useCallback(
|
|
376
|
+
(listener: () => void) => stateStore.subscribe(listener),
|
|
377
|
+
[stateStore]
|
|
378
|
+
)
|
|
379
|
+
const getSnapshot = React.useCallback(() => {
|
|
380
|
+
const state = stateStore.getSnapshot()
|
|
381
|
+
|
|
382
|
+
return direction === "start" ? state.start : state.end
|
|
383
|
+
}, [direction, stateStore])
|
|
384
|
+
const isActive = React.useSyncExternalStore(
|
|
385
|
+
subscribe,
|
|
386
|
+
getSnapshot,
|
|
387
|
+
getSnapshot
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
const handleClick = React.useCallback(
|
|
391
|
+
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
392
|
+
if (!isActive) {
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
onClickRef.current?.(event)
|
|
397
|
+
|
|
398
|
+
if (!event.defaultPrevented) {
|
|
399
|
+
event.currentTarget.blur()
|
|
400
|
+
|
|
401
|
+
if (direction === "start") {
|
|
402
|
+
scrollToStart({ behavior })
|
|
403
|
+
} else {
|
|
404
|
+
scrollToEnd({ behavior })
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
[behavior, direction, isActive, onClickRef, scrollToEnd, scrollToStart]
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
return useRender({
|
|
412
|
+
defaultTagName: "button",
|
|
413
|
+
props: mergeProps<"button">(
|
|
414
|
+
{
|
|
415
|
+
type,
|
|
416
|
+
inert: !isActive,
|
|
417
|
+
tabIndex: isActive ? tabIndex : -1,
|
|
418
|
+
children: children ?? <span>Scroll to {direction}</span>,
|
|
419
|
+
onClick: handleClick,
|
|
420
|
+
},
|
|
421
|
+
props
|
|
422
|
+
),
|
|
423
|
+
render,
|
|
424
|
+
state: {
|
|
425
|
+
active: isActive,
|
|
426
|
+
direction,
|
|
427
|
+
},
|
|
428
|
+
stateAttributesMapping: {
|
|
429
|
+
active: (value) => ({
|
|
430
|
+
"data-active": value ? "true" : "false",
|
|
431
|
+
}),
|
|
432
|
+
},
|
|
433
|
+
})
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export {
|
|
437
|
+
MessageScroller,
|
|
438
|
+
MessageScrollerButton,
|
|
439
|
+
MessageScrollerContent,
|
|
440
|
+
MessageScrollerItem,
|
|
441
|
+
MessageScrollerProvider,
|
|
442
|
+
MessageScrollerViewport,
|
|
443
|
+
useMessageScroller,
|
|
444
|
+
useMessageScrollerScrollable,
|
|
445
|
+
useMessageScrollerVisibility,
|
|
446
|
+
}
|