@majapisoftwares/ui 6.8.0 → 6.8.2

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.
@@ -0,0 +1,387 @@
1
+ import {
2
+ EMPTY_MESSAGE_SCROLLER_SCROLLABLE,
3
+ EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE,
4
+ SCROLL_POSITION_EPSILON,
5
+ } from "./types"
6
+ import type {
7
+ MessageScrollerScrollable,
8
+ MessageScrollerScrollAlign,
9
+ MessageScrollerVisibilityState,
10
+ } from "./types"
11
+
12
+ function getMessageScrollerScrollable({
13
+ content,
14
+ scrollEdgeThreshold,
15
+ spacer,
16
+ viewport,
17
+ }: {
18
+ content: HTMLElement | null
19
+ scrollEdgeThreshold: number
20
+ spacer: HTMLElement | null
21
+ viewport: HTMLElement | null
22
+ }): MessageScrollerScrollable {
23
+ if (!viewport || !content) {
24
+ return EMPTY_MESSAGE_SCROLLER_SCROLLABLE
25
+ }
26
+
27
+ const contentBottom = getContentBottom({ content, spacer, viewport })
28
+
29
+ return {
30
+ start: viewport.scrollTop > scrollEdgeThreshold,
31
+ end:
32
+ contentBottom - viewport.scrollTop - viewport.clientHeight >
33
+ scrollEdgeThreshold,
34
+ }
35
+ }
36
+
37
+ function getMessageScrollerVisibilityState({
38
+ content,
39
+ scrollMargin,
40
+ scrollPreviousItemPeek,
41
+ spacer,
42
+ viewport,
43
+ visibleMessageIds,
44
+ }: {
45
+ content: HTMLElement | null
46
+ scrollMargin: number
47
+ scrollPreviousItemPeek: number
48
+ spacer: HTMLElement | null
49
+ viewport: HTMLElement | null
50
+ visibleMessageIds: Set<string>
51
+ }): MessageScrollerVisibilityState {
52
+ if (!content || !viewport) {
53
+ return EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE
54
+ }
55
+
56
+ const viewportRect = viewport.getBoundingClientRect()
57
+ // The reading line sits scrollPreviousItemPeek below scrollMargin: anchored
58
+ // turns land there with the previous turn peeking above. A row only peeking in
59
+ // that band has not been read down to yet, so it counts as neither visible nor
60
+ // current.
61
+ const lineTop = viewportRect.top + scrollMargin + scrollPreviousItemPeek
62
+ const trackByLayout = typeof IntersectionObserver === "undefined"
63
+
64
+ const visible: string[] = []
65
+ let currentAnchorId: string | null = null
66
+
67
+ // Walk rows in document order so visible ids come out top-to-bottom.
68
+ for (const item of getMessageScrollerItems(content, spacer)) {
69
+ const messageId = item.dataset.messageId
70
+
71
+ if (!messageId) {
72
+ continue
73
+ }
74
+
75
+ const isAnchor = item.dataset.scrollAnchor === "true"
76
+ // Anchors need a rect to place the current line; non-anchors lean on the
77
+ // observer set (or a rect in the no-observer fallback).
78
+ const rect = isAnchor || trackByLayout ? item.getBoundingClientRect() : null
79
+
80
+ const isVisible =
81
+ trackByLayout && rect
82
+ ? rect.bottom > lineTop && rect.top < viewportRect.bottom
83
+ : visibleMessageIds.has(messageId)
84
+
85
+ if (isVisible) {
86
+ visible.push(messageId)
87
+ }
88
+
89
+ // Current is the last anchor to have reached the reading line: the turn you
90
+ // scrolled to (placed at the line) wins over newer turns lower down, the
91
+ // previous turn peeking above the line has been passed, and it stays current
92
+ // even after its header scrolls above the viewport.
93
+ if (isAnchor && rect && rect.top <= lineTop + SCROLL_POSITION_EPSILON) {
94
+ currentAnchorId = messageId
95
+ }
96
+ }
97
+
98
+ if (visible.length === 0 && currentAnchorId === null) {
99
+ return EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE
100
+ }
101
+
102
+ return {
103
+ currentAnchorId,
104
+ visibleMessageIds: visible,
105
+ }
106
+ }
107
+
108
+ function getMessageScrollerItems(
109
+ content: HTMLElement,
110
+ spacer: HTMLElement | null
111
+ ) {
112
+ return Array.from(content.children).filter(
113
+ (child): child is HTMLElement =>
114
+ child instanceof HTMLElement && child !== spacer
115
+ )
116
+ }
117
+
118
+ function getNewScrollAnchor(items: HTMLElement[], previousItemCount: number) {
119
+ for (let index = previousItemCount; index < items.length; index++) {
120
+ const item = items[index]
121
+
122
+ if (item?.dataset.scrollAnchor === "true") {
123
+ return item
124
+ }
125
+ }
126
+
127
+ return null
128
+ }
129
+
130
+ function getUnanchoredScrollAnchor(
131
+ items: HTMLElement[],
132
+ handledAnchors: { has(element: HTMLElement): boolean }
133
+ ) {
134
+ for (const item of items) {
135
+ if (item.dataset.scrollAnchor === "true" && !handledAnchors.has(item)) {
136
+ return item
137
+ }
138
+ }
139
+
140
+ return null
141
+ }
142
+
143
+ function hasMultipleNewScrollAnchors(
144
+ items: HTMLElement[],
145
+ previousItemCount: number
146
+ ) {
147
+ let count = 0
148
+
149
+ for (let index = previousItemCount; index < items.length; index++) {
150
+ const item = items[index]
151
+
152
+ if (item?.dataset.scrollAnchor !== "true") {
153
+ continue
154
+ }
155
+
156
+ count += 1
157
+
158
+ if (count > 1) {
159
+ return true
160
+ }
161
+ }
162
+
163
+ return false
164
+ }
165
+
166
+ function getLastScrollAnchor(items: HTMLElement[]) {
167
+ for (let index = items.length - 1; index >= 0; index--) {
168
+ const item = items[index]
169
+
170
+ if (item?.dataset.scrollAnchor === "true") {
171
+ return item
172
+ }
173
+ }
174
+
175
+ return null
176
+ }
177
+
178
+ function getFirstVisibleMessageItem({
179
+ content,
180
+ spacer,
181
+ viewport,
182
+ }: {
183
+ content: HTMLElement
184
+ spacer: HTMLElement | null
185
+ viewport: HTMLElement
186
+ }) {
187
+ const viewportRect = viewport.getBoundingClientRect()
188
+
189
+ for (const item of getMessageScrollerItems(content, spacer)) {
190
+ if (!item.dataset.messageId) {
191
+ continue
192
+ }
193
+
194
+ const rect = item.getBoundingClientRect()
195
+
196
+ if (rect.bottom > viewportRect.top && rect.top < viewportRect.bottom) {
197
+ return item
198
+ }
199
+ }
200
+
201
+ return null
202
+ }
203
+
204
+ function getElementScrollTop({
205
+ align,
206
+ element,
207
+ scrollMargin,
208
+ spacer,
209
+ viewport,
210
+ }: {
211
+ align: MessageScrollerScrollAlign
212
+ element: HTMLElement
213
+ scrollMargin: number
214
+ spacer: HTMLElement | null
215
+ viewport: HTMLElement
216
+ }) {
217
+ const elementTop = getElementTop(element, viewport)
218
+ const elementHeight = element.getBoundingClientRect().height
219
+ const contentPadding = getContentBlockPadding(spacer)
220
+
221
+ if (align === "center") {
222
+ const insetHeight = Math.max(
223
+ 0,
224
+ viewport.clientHeight - contentPadding.start - contentPadding.end
225
+ )
226
+
227
+ return (
228
+ elementTop -
229
+ contentPadding.start -
230
+ (insetHeight - elementHeight) / 2 -
231
+ scrollMargin
232
+ )
233
+ }
234
+
235
+ if (align === "end") {
236
+ return (
237
+ elementTop -
238
+ viewport.clientHeight +
239
+ elementHeight +
240
+ contentPadding.end +
241
+ scrollMargin
242
+ )
243
+ }
244
+
245
+ if (align === "nearest") {
246
+ const elementBottom = elementTop + elementHeight
247
+ const viewportTop = viewport.scrollTop + contentPadding.start
248
+ const viewportBottom =
249
+ viewport.scrollTop + viewport.clientHeight - contentPadding.end
250
+
251
+ if (elementTop >= viewportTop && elementBottom <= viewportBottom) {
252
+ return viewport.scrollTop
253
+ }
254
+
255
+ if (elementTop < viewportTop) {
256
+ return elementTop - contentPadding.start - scrollMargin
257
+ }
258
+
259
+ return (
260
+ elementBottom - viewport.clientHeight + contentPadding.end + scrollMargin
261
+ )
262
+ }
263
+
264
+ return elementTop - contentPadding.start - scrollMargin
265
+ }
266
+
267
+ function getElementTop(element: HTMLElement, viewport: HTMLElement) {
268
+ const elementRect = element.getBoundingClientRect()
269
+ const viewportRect = viewport.getBoundingClientRect()
270
+
271
+ return elementRect.top - viewportRect.top + viewport.scrollTop
272
+ }
273
+
274
+ function getElementViewportTop(element: HTMLElement, viewport: HTMLElement) {
275
+ return (
276
+ element.getBoundingClientRect().top - viewport.getBoundingClientRect().top
277
+ )
278
+ }
279
+
280
+ function getTailSpacerHeight({
281
+ content,
282
+ scrollTop,
283
+ spacer,
284
+ viewport,
285
+ }: {
286
+ content: HTMLElement
287
+ scrollTop: number
288
+ spacer: HTMLElement | null
289
+ viewport: HTMLElement
290
+ }) {
291
+ const contentBottom = getContentBottom({ content, spacer, viewport })
292
+
293
+ return scrollTop + viewport.clientHeight - contentBottom
294
+ }
295
+
296
+ function getContentBottom({
297
+ content,
298
+ spacer,
299
+ viewport,
300
+ }: {
301
+ content: HTMLElement
302
+ spacer: HTMLElement | null
303
+ viewport: HTMLElement
304
+ }) {
305
+ const items = getMessageScrollerItems(content, spacer)
306
+ const padding = getBlockPadding(content)
307
+ const viewportRect = viewport.getBoundingClientRect()
308
+ const scrollTop = viewport.scrollTop
309
+ let contentBottom = padding.start + padding.end
310
+
311
+ for (const item of items) {
312
+ const rect = item.getBoundingClientRect()
313
+
314
+ contentBottom = Math.max(
315
+ contentBottom,
316
+ rect.bottom - viewportRect.top + scrollTop + padding.end
317
+ )
318
+ }
319
+
320
+ return contentBottom
321
+ }
322
+
323
+ function getMaxScrollTop(viewport: HTMLElement) {
324
+ return Math.max(0, viewport.scrollHeight - viewport.clientHeight)
325
+ }
326
+
327
+ function getBlockPadding(element: HTMLElement) {
328
+ const style = window.getComputedStyle(element)
329
+
330
+ return {
331
+ end: readCssPixel(style.paddingBlockEnd || style.paddingBottom),
332
+ start: readCssPixel(style.paddingBlockStart || style.paddingTop),
333
+ }
334
+ }
335
+
336
+ function getContentBlockPadding(spacer: HTMLElement | null) {
337
+ const content = spacer?.parentElement
338
+
339
+ if (!content) {
340
+ return {
341
+ end: 0,
342
+ start: 0,
343
+ }
344
+ }
345
+
346
+ return getBlockPadding(content)
347
+ }
348
+
349
+ function getFlexGap(element: HTMLElement | null) {
350
+ if (!element) {
351
+ return 0
352
+ }
353
+
354
+ const style = window.getComputedStyle(element)
355
+ const gap = style.rowGap === "normal" ? style.gap : style.rowGap
356
+
357
+ return readCssPixel(gap)
358
+ }
359
+
360
+ function readCssPixel(value: string | undefined) {
361
+ if (!value) {
362
+ return 0
363
+ }
364
+
365
+ const number = Number.parseFloat(value)
366
+
367
+ return Number.isFinite(number) ? number : 0
368
+ }
369
+
370
+ export {
371
+ getContentBlockPadding,
372
+ getContentBottom,
373
+ getElementScrollTop,
374
+ getElementTop,
375
+ getElementViewportTop,
376
+ getFirstVisibleMessageItem,
377
+ getFlexGap,
378
+ getLastScrollAnchor,
379
+ getMaxScrollTop,
380
+ getMessageScrollerItems,
381
+ getMessageScrollerScrollable,
382
+ getMessageScrollerVisibilityState,
383
+ getNewScrollAnchor,
384
+ getTailSpacerHeight,
385
+ getUnanchoredScrollAnchor,
386
+ hasMultipleNewScrollAnchors,
387
+ }
@@ -0,0 +1,31 @@
1
+ import {
2
+ MessageScrollerButton as Button,
3
+ MessageScrollerContent as Content,
4
+ MessageScrollerItem as Item,
5
+ MessageScrollerProvider as Provider,
6
+ MessageScroller as Root,
7
+ MessageScrollerViewport as Viewport,
8
+ } from "./components"
9
+
10
+ export const MessageScroller = {
11
+ Provider,
12
+ Root,
13
+ Viewport,
14
+ Content,
15
+ Item,
16
+ Button,
17
+ }
18
+
19
+ export {
20
+ useMessageScroller,
21
+ useMessageScrollerScrollable,
22
+ useMessageScrollerVisibility,
23
+ } from "./components"
24
+
25
+ export type {
26
+ MessageScrollerDefaultScrollPosition,
27
+ MessageScrollerScrollAlign,
28
+ MessageScrollerScrollOptions,
29
+ MessageScrollerScrollable,
30
+ MessageScrollerVisibilityState,
31
+ } from "./types"
@@ -0,0 +1,97 @@
1
+ import { EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE } from "./types"
2
+ import type {
3
+ MessageScrollerScrollable,
4
+ MessageScrollerStore,
5
+ MessageScrollerVisibilityState,
6
+ MessageScrollerVisibilityStore,
7
+ } from "./types"
8
+
9
+ // Generic useSyncExternalStore backing: a stable snapshot (referentially equal
10
+ // while isEqual holds, so subscribers only re-render on real transitions) plus
11
+ // optional reference-counting via the subscribe-time lifecycle callbacks.
12
+ function createExternalStore<T>(
13
+ initialSnapshot: T,
14
+ isEqual: (a: T, b: T) => boolean
15
+ ) {
16
+ let snapshot = initialSnapshot
17
+ const listeners = new Set<() => void>()
18
+
19
+ return {
20
+ getSnapshot: () => snapshot,
21
+ hasListeners: () => listeners.size > 0,
22
+ setSnapshot: (nextSnapshot: T) => {
23
+ if (isEqual(snapshot, nextSnapshot)) {
24
+ return
25
+ }
26
+
27
+ snapshot = nextSnapshot
28
+ listeners.forEach((listener) => listener())
29
+ },
30
+ subscribe: (
31
+ listener: () => void,
32
+ onFirstSubscribe?: () => void,
33
+ onLastUnsubscribe?: () => void
34
+ ) => {
35
+ const wasEmpty = listeners.size === 0
36
+
37
+ listeners.add(listener)
38
+
39
+ if (wasEmpty) {
40
+ onFirstSubscribe?.()
41
+ }
42
+
43
+ return () => {
44
+ listeners.delete(listener)
45
+
46
+ if (listeners.size === 0) {
47
+ onLastUnsubscribe?.()
48
+ }
49
+ }
50
+ },
51
+ }
52
+ }
53
+
54
+ function createMessageScrollerStore<T>(
55
+ initialSnapshot: T,
56
+ isEqual: (a: T, b: T) => boolean
57
+ ): MessageScrollerStore<T> {
58
+ return createExternalStore(initialSnapshot, isEqual)
59
+ }
60
+
61
+ function createMessageScrollerVisibilityStore(): MessageScrollerVisibilityStore {
62
+ return createExternalStore(
63
+ EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE,
64
+ areVisibilityStatesEqual
65
+ )
66
+ }
67
+
68
+ function areScrollStatesEqual(
69
+ current: MessageScrollerScrollable,
70
+ next: MessageScrollerScrollable
71
+ ) {
72
+ return current.start === next.start && current.end === next.end
73
+ }
74
+
75
+ function areVisibilityStatesEqual(
76
+ current: MessageScrollerVisibilityState,
77
+ next: MessageScrollerVisibilityState
78
+ ) {
79
+ if (current.currentAnchorId !== next.currentAnchorId) {
80
+ return false
81
+ }
82
+
83
+ if (current.visibleMessageIds.length !== next.visibleMessageIds.length) {
84
+ return false
85
+ }
86
+
87
+ return current.visibleMessageIds.every(
88
+ (messageId, index) => messageId === next.visibleMessageIds[index]
89
+ )
90
+ }
91
+
92
+ export {
93
+ areScrollStatesEqual,
94
+ areVisibilityStatesEqual,
95
+ createMessageScrollerStore,
96
+ createMessageScrollerVisibilityStore,
97
+ }