@majapisoftwares/ui 6.8.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/THIRD_PARTY_NOTICES.md +2 -0
- package/docs/STANDARDS.md +1 -1
- package/package.json +1 -2
- package/src/components/ui/message-scroller.tsx +1 -1
- 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
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
areScrollStatesEqual,
|
|
5
|
+
createMessageScrollerStore,
|
|
6
|
+
createMessageScrollerVisibilityStore,
|
|
7
|
+
} from "./stores"
|
|
8
|
+
import { EMPTY_MESSAGE_SCROLLER_SCROLLABLE } from "./types"
|
|
9
|
+
import type {
|
|
10
|
+
MessageScrollerDefaultScrollPosition,
|
|
11
|
+
MessageScrollerMode,
|
|
12
|
+
MessageScrollerScrollable,
|
|
13
|
+
MessageScrollerScrollOptions,
|
|
14
|
+
MessageScrollerStore,
|
|
15
|
+
MessageScrollerVisibilityStore,
|
|
16
|
+
} from "./types"
|
|
17
|
+
|
|
18
|
+
// Shared mutable ref bag for one MessageScroller, closed over by both the
|
|
19
|
+
// controller and the commands so writes are visible across them without prop
|
|
20
|
+
// threading. stateStore and visibilityStore fan out via useSyncExternalStore.
|
|
21
|
+
type MessageScrollerRefs = {
|
|
22
|
+
autoScrollRef: React.RefObject<boolean>
|
|
23
|
+
autoscrollingRef: React.RefObject<boolean>
|
|
24
|
+
autoscrollingTimeoutRef: React.RefObject<number | null>
|
|
25
|
+
streamingTurnRef: React.RefObject<HTMLElement | null>
|
|
26
|
+
contentRef: React.RefObject<HTMLDivElement | null>
|
|
27
|
+
defaultScrollPositionAppliedRef: React.RefObject<boolean>
|
|
28
|
+
firstItemRef: React.RefObject<HTMLElement | null>
|
|
29
|
+
itemCountRef: React.RefObject<number>
|
|
30
|
+
lastScrollTopRef: React.RefObject<number>
|
|
31
|
+
messageElementsRef: React.RefObject<Map<string, HTMLElement>>
|
|
32
|
+
modeRef: React.RefObject<MessageScrollerMode>
|
|
33
|
+
pendingScrollFrameRef: React.RefObject<number | null>
|
|
34
|
+
pendingScrollToMessageRef: React.RefObject<{
|
|
35
|
+
messageId: string
|
|
36
|
+
options?: MessageScrollerScrollOptions
|
|
37
|
+
} | null>
|
|
38
|
+
prependRestoreRef: React.RefObject<{
|
|
39
|
+
element: HTMLElement
|
|
40
|
+
viewportTop: number
|
|
41
|
+
} | null>
|
|
42
|
+
preserveScrollOnPrependRef: React.RefObject<boolean>
|
|
43
|
+
pendingDefaultScrollStore: MessageScrollerStore<boolean>
|
|
44
|
+
rootRef: React.RefObject<HTMLDivElement | null>
|
|
45
|
+
scrollEdgeThresholdRef: React.RefObject<number>
|
|
46
|
+
scrollMarginRef: React.RefObject<number>
|
|
47
|
+
scrollPreviousItemPeekRef: React.RefObject<number>
|
|
48
|
+
spacerGapRef: React.RefObject<number>
|
|
49
|
+
spacerHeightRef: React.RefObject<number>
|
|
50
|
+
spacerRef: React.RefObject<HTMLDivElement | null>
|
|
51
|
+
stateFrameRef: React.RefObject<number | null>
|
|
52
|
+
stateStore: MessageScrollerStore<MessageScrollerScrollable>
|
|
53
|
+
viewportRef: React.RefObject<HTMLDivElement | null>
|
|
54
|
+
visibilityFrameRef: React.RefObject<number | null>
|
|
55
|
+
visibilityObserverRef: React.RefObject<IntersectionObserver | null>
|
|
56
|
+
visibilityStore: MessageScrollerVisibilityStore
|
|
57
|
+
visibleMessageIdsRef: React.RefObject<Set<string>>
|
|
58
|
+
handledScrollAnchorsRef: React.RefObject<WeakSet<HTMLElement>>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Builds the per-instance ref bag: the two external stores constructed once, and
|
|
62
|
+
// the latest prop values mirrored onto refs each render so callbacks stay stable.
|
|
63
|
+
function useMessageScrollerRefs({
|
|
64
|
+
autoScroll,
|
|
65
|
+
defaultScrollPosition,
|
|
66
|
+
scrollEdgeThreshold,
|
|
67
|
+
scrollMargin,
|
|
68
|
+
scrollPreviousItemPeek,
|
|
69
|
+
}: {
|
|
70
|
+
autoScroll: boolean
|
|
71
|
+
defaultScrollPosition: MessageScrollerDefaultScrollPosition
|
|
72
|
+
scrollEdgeThreshold: number
|
|
73
|
+
scrollMargin: number
|
|
74
|
+
scrollPreviousItemPeek: number
|
|
75
|
+
}): MessageScrollerRefs {
|
|
76
|
+
const autoScrollRef = React.useRef(autoScroll)
|
|
77
|
+
const autoscrollingRef = React.useRef(false)
|
|
78
|
+
const contentRef = React.useRef<HTMLDivElement | null>(null)
|
|
79
|
+
const defaultScrollPositionAppliedRef = React.useRef(false)
|
|
80
|
+
const scrollEdgeThresholdRef = React.useRef(scrollEdgeThreshold)
|
|
81
|
+
const itemCountRef = React.useRef(0)
|
|
82
|
+
const firstItemRef = React.useRef<HTMLElement | null>(null)
|
|
83
|
+
// The scrollTop seen by the previous state commit, so follow-release can tell
|
|
84
|
+
// a reader scrolling up from content growing past the live edge.
|
|
85
|
+
const lastScrollTopRef = React.useRef(0)
|
|
86
|
+
const modeRef = React.useRef<MessageScrollerMode>(
|
|
87
|
+
autoScroll ? "following-bottom" : "free-scrolling"
|
|
88
|
+
)
|
|
89
|
+
const messageElementsRef = React.useRef(new Map<string, HTMLElement>())
|
|
90
|
+
const pendingScrollToMessageRef = React.useRef<{
|
|
91
|
+
messageId: string
|
|
92
|
+
options?: MessageScrollerScrollOptions
|
|
93
|
+
} | null>(null)
|
|
94
|
+
// The row to hold steady on the next prepend: the first visible row, or a jump
|
|
95
|
+
// target seeded by scrollToElement. restorePrependedAnchor reads only this.
|
|
96
|
+
const prependRestoreRef = React.useRef<{
|
|
97
|
+
element: HTMLElement
|
|
98
|
+
viewportTop: number
|
|
99
|
+
} | null>(null)
|
|
100
|
+
// The turn held at the reading line so a reply streaming in below it can re-pin
|
|
101
|
+
// it instead of letting scrollTop clamp it loose.
|
|
102
|
+
const streamingTurnRef = React.useRef<HTMLElement | null>(null)
|
|
103
|
+
const scrollPreviousItemPeekRef = React.useRef(scrollPreviousItemPeek)
|
|
104
|
+
const preserveScrollOnPrependRef = React.useRef(true)
|
|
105
|
+
const rootRef = React.useRef<HTMLDivElement | null>(null)
|
|
106
|
+
const scrollMarginRef = React.useRef(scrollMargin)
|
|
107
|
+
const pendingScrollFrameRef = React.useRef<number | null>(null)
|
|
108
|
+
const spacerGapRef = React.useRef(0)
|
|
109
|
+
const spacerHeightRef = React.useRef(0)
|
|
110
|
+
const spacerRef = React.useRef<HTMLDivElement | null>(null)
|
|
111
|
+
const stateFrameRef = React.useRef<number | null>(null)
|
|
112
|
+
const pendingDefaultScrollStoreRef =
|
|
113
|
+
React.useRef<MessageScrollerStore<boolean> | null>(null)
|
|
114
|
+
const stateStoreRef =
|
|
115
|
+
React.useRef<MessageScrollerStore<MessageScrollerScrollable> | null>(null)
|
|
116
|
+
const autoscrollingTimeoutRef = React.useRef<number | null>(null)
|
|
117
|
+
const viewportRef = React.useRef<HTMLDivElement | null>(null)
|
|
118
|
+
const visibilityFrameRef = React.useRef<number | null>(null)
|
|
119
|
+
const visibilityObserverRef = React.useRef<IntersectionObserver | null>(null)
|
|
120
|
+
const visibilityStoreRef =
|
|
121
|
+
React.useRef<MessageScrollerVisibilityStore | null>(null)
|
|
122
|
+
const visibleMessageIdsRef = React.useRef(new Set<string>())
|
|
123
|
+
const handledScrollAnchorsRef = React.useRef(new WeakSet<HTMLElement>())
|
|
124
|
+
|
|
125
|
+
if (pendingDefaultScrollStoreRef.current === null) {
|
|
126
|
+
pendingDefaultScrollStoreRef.current = createMessageScrollerStore(
|
|
127
|
+
defaultScrollPosition === "end" ||
|
|
128
|
+
defaultScrollPosition === "last-anchor",
|
|
129
|
+
(current, next) => current === next
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (stateStoreRef.current === null) {
|
|
134
|
+
stateStoreRef.current = createMessageScrollerStore(
|
|
135
|
+
EMPTY_MESSAGE_SCROLLER_SCROLLABLE,
|
|
136
|
+
areScrollStatesEqual
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (visibilityStoreRef.current === null) {
|
|
141
|
+
visibilityStoreRef.current = createMessageScrollerVisibilityStore()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Track the latest prop values on every render so callbacks read fresh values
|
|
145
|
+
// without being recreated (the useLatest pattern).
|
|
146
|
+
autoScrollRef.current = autoScroll
|
|
147
|
+
scrollEdgeThresholdRef.current = scrollEdgeThreshold
|
|
148
|
+
scrollMarginRef.current = scrollMargin
|
|
149
|
+
scrollPreviousItemPeekRef.current = scrollPreviousItemPeek
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
autoScrollRef,
|
|
153
|
+
autoscrollingRef,
|
|
154
|
+
autoscrollingTimeoutRef,
|
|
155
|
+
streamingTurnRef,
|
|
156
|
+
contentRef,
|
|
157
|
+
defaultScrollPositionAppliedRef,
|
|
158
|
+
firstItemRef,
|
|
159
|
+
itemCountRef,
|
|
160
|
+
lastScrollTopRef,
|
|
161
|
+
messageElementsRef,
|
|
162
|
+
modeRef,
|
|
163
|
+
pendingScrollFrameRef,
|
|
164
|
+
pendingScrollToMessageRef,
|
|
165
|
+
prependRestoreRef,
|
|
166
|
+
preserveScrollOnPrependRef,
|
|
167
|
+
pendingDefaultScrollStore: pendingDefaultScrollStoreRef.current,
|
|
168
|
+
rootRef,
|
|
169
|
+
scrollEdgeThresholdRef,
|
|
170
|
+
scrollMarginRef,
|
|
171
|
+
scrollPreviousItemPeekRef,
|
|
172
|
+
spacerGapRef,
|
|
173
|
+
spacerHeightRef,
|
|
174
|
+
spacerRef,
|
|
175
|
+
stateFrameRef,
|
|
176
|
+
stateStore: stateStoreRef.current,
|
|
177
|
+
viewportRef,
|
|
178
|
+
visibilityFrameRef,
|
|
179
|
+
visibilityObserverRef,
|
|
180
|
+
visibilityStore: visibilityStoreRef.current,
|
|
181
|
+
visibleMessageIdsRef,
|
|
182
|
+
handledScrollAnchorsRef,
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function clearPendingDefaultScroll(refs: MessageScrollerRefs) {
|
|
187
|
+
refs.pendingDefaultScrollStore.setSnapshot(false)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function markDefaultScrollPositionApplied(refs: MessageScrollerRefs) {
|
|
191
|
+
refs.defaultScrollPositionAppliedRef.current = true
|
|
192
|
+
clearPendingDefaultScroll(refs)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export {
|
|
196
|
+
clearPendingDefaultScroll,
|
|
197
|
+
markDefaultScrollPositionApplied,
|
|
198
|
+
useMessageScrollerRefs,
|
|
199
|
+
}
|
|
200
|
+
export type { MessageScrollerRefs }
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// A poor man's version of base-ui useRender.
|
|
2
|
+
// see https://base-ui.com/react/utils/use-render.
|
|
3
|
+
// TODO: Replace if base-ui useRender is published as a package.
|
|
4
|
+
import * as React from "react"
|
|
5
|
+
|
|
6
|
+
type RenderState = Record<string, unknown>
|
|
7
|
+
|
|
8
|
+
type RenderFunction<TState extends RenderState> = (
|
|
9
|
+
props: Record<string, unknown>,
|
|
10
|
+
state: TState
|
|
11
|
+
) => React.ReactElement | null
|
|
12
|
+
|
|
13
|
+
type RenderProp<TState extends RenderState> =
|
|
14
|
+
| React.ReactElement
|
|
15
|
+
| RenderFunction<TState>
|
|
16
|
+
|
|
17
|
+
type StateAttributesMapping<TState extends RenderState> = Partial<{
|
|
18
|
+
[K in keyof TState]: (
|
|
19
|
+
value: TState[K]
|
|
20
|
+
) => Record<string, string | undefined> | null | undefined
|
|
21
|
+
}>
|
|
22
|
+
|
|
23
|
+
type UseRenderComponentProps<
|
|
24
|
+
TElement extends React.ElementType,
|
|
25
|
+
TState extends RenderState = RenderState,
|
|
26
|
+
> = React.ComponentPropsWithRef<TElement> & {
|
|
27
|
+
render?: RenderProp<TState>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type UseRenderOptions<
|
|
31
|
+
TElement extends React.ElementType,
|
|
32
|
+
TState extends RenderState,
|
|
33
|
+
> = {
|
|
34
|
+
defaultTagName: TElement
|
|
35
|
+
props?: React.ComponentPropsWithRef<TElement>
|
|
36
|
+
render?: RenderProp<TState>
|
|
37
|
+
state?: TState
|
|
38
|
+
stateAttributesMapping?: StateAttributesMapping<TState>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function useRender<
|
|
42
|
+
TElement extends React.ElementType,
|
|
43
|
+
TState extends RenderState = RenderState,
|
|
44
|
+
>({
|
|
45
|
+
defaultTagName,
|
|
46
|
+
props,
|
|
47
|
+
render,
|
|
48
|
+
state = {} as TState,
|
|
49
|
+
stateAttributesMapping,
|
|
50
|
+
}: UseRenderOptions<TElement, TState>) {
|
|
51
|
+
const elementProps = mergeProps<TElement>(
|
|
52
|
+
getStateAttributes(state, stateAttributesMapping),
|
|
53
|
+
props
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
if (!render) {
|
|
57
|
+
return React.createElement(defaultTagName, elementProps)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof render === "function") {
|
|
61
|
+
return render(elementProps, state)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!React.isValidElement(render)) {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const renderProps = render.props as Record<string, unknown>
|
|
69
|
+
const propsWithRenderProps = mergeProps<TElement>(elementProps, renderProps)
|
|
70
|
+
const propsWithRef = {
|
|
71
|
+
...propsWithRenderProps,
|
|
72
|
+
ref: composeRefs(
|
|
73
|
+
elementProps.ref as React.Ref<unknown> | undefined,
|
|
74
|
+
renderProps.ref as React.Ref<unknown> | undefined
|
|
75
|
+
),
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return React.cloneElement(
|
|
79
|
+
render,
|
|
80
|
+
propsWithRef as React.Attributes & Record<string, unknown>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function mergeProps<TElement extends React.ElementType>(
|
|
85
|
+
...sources: Array<
|
|
86
|
+
React.ComponentPropsWithRef<TElement> | Record<string, unknown> | undefined
|
|
87
|
+
>
|
|
88
|
+
) {
|
|
89
|
+
const result: Record<string, unknown> = {}
|
|
90
|
+
|
|
91
|
+
for (const source of sources) {
|
|
92
|
+
if (!source) {
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const sourceProps = source as Record<string, unknown>
|
|
97
|
+
|
|
98
|
+
for (const key of Object.keys(sourceProps)) {
|
|
99
|
+
const value = sourceProps[key]
|
|
100
|
+
|
|
101
|
+
if (value === undefined) {
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const current = result[key]
|
|
106
|
+
|
|
107
|
+
if (key === "className") {
|
|
108
|
+
result[key] = [current, value].filter(Boolean).join(" ")
|
|
109
|
+
} else if (key === "style") {
|
|
110
|
+
result[key] = {
|
|
111
|
+
...(current as React.CSSProperties | undefined),
|
|
112
|
+
...(value as React.CSSProperties | undefined),
|
|
113
|
+
}
|
|
114
|
+
} else if (key === "ref") {
|
|
115
|
+
result[key] = composeRefs(
|
|
116
|
+
current as React.Ref<unknown> | undefined,
|
|
117
|
+
value as React.Ref<unknown> | undefined
|
|
118
|
+
)
|
|
119
|
+
} else if (
|
|
120
|
+
isEventHandler(key) &&
|
|
121
|
+
typeof current === "function" &&
|
|
122
|
+
typeof value === "function"
|
|
123
|
+
) {
|
|
124
|
+
result[key] = composeEventHandlers(
|
|
125
|
+
value as EventHandler,
|
|
126
|
+
current as EventHandler
|
|
127
|
+
)
|
|
128
|
+
} else {
|
|
129
|
+
result[key] = value
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return result as React.ComponentPropsWithRef<TElement>
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getStateAttributes<TState extends RenderState>(
|
|
138
|
+
state: TState,
|
|
139
|
+
mapping?: StateAttributesMapping<TState>
|
|
140
|
+
) {
|
|
141
|
+
const props: Record<string, unknown> = {}
|
|
142
|
+
|
|
143
|
+
for (const key of Object.keys(state) as Array<keyof TState>) {
|
|
144
|
+
const value = state[key]
|
|
145
|
+
const attributes = mapping?.[key]?.(value)
|
|
146
|
+
|
|
147
|
+
if (attributes) {
|
|
148
|
+
Object.assign(props, attributes)
|
|
149
|
+
continue
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (key === "slot") {
|
|
153
|
+
props["data-slot"] = value
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const attribute = `data-${String(key).replace(
|
|
158
|
+
/[A-Z]/g,
|
|
159
|
+
(letter) => `-${letter.toLowerCase()}`
|
|
160
|
+
)}`
|
|
161
|
+
|
|
162
|
+
if (typeof value === "boolean") {
|
|
163
|
+
props[attribute] = value ? "" : undefined
|
|
164
|
+
} else if (value !== null && value !== undefined) {
|
|
165
|
+
props[attribute] = String(value)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return props
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type EventHandler = (event: React.SyntheticEvent) => void
|
|
173
|
+
|
|
174
|
+
function composeEventHandlers(theirs: EventHandler, ours: EventHandler) {
|
|
175
|
+
return function handleEvent(event: React.SyntheticEvent) {
|
|
176
|
+
theirs(event)
|
|
177
|
+
|
|
178
|
+
if (!event.defaultPrevented) {
|
|
179
|
+
ours(event)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isEventHandler(key: string) {
|
|
185
|
+
return /^on[A-Z]/.test(key)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function composeRefs<T>(
|
|
189
|
+
...refs: Array<React.Ref<T> | undefined>
|
|
190
|
+
): React.RefCallback<T> | undefined {
|
|
191
|
+
const validRefs = refs.filter(Boolean)
|
|
192
|
+
|
|
193
|
+
if (validRefs.length === 0) {
|
|
194
|
+
return undefined
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return (value) => {
|
|
198
|
+
for (const ref of validRefs) {
|
|
199
|
+
if (typeof ref === "function") {
|
|
200
|
+
ref(value)
|
|
201
|
+
} else if (ref) {
|
|
202
|
+
ref.current = value
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { composeRefs, mergeProps, useRender, type UseRenderComponentProps }
|