@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,167 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
import type { RefObject } from "react";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
areScrollStatesEqual,
|
|
6
|
+
createMessageScrollerStore,
|
|
7
|
+
createMessageScrollerVisibilityStore,
|
|
8
|
+
} from "#/lib/message-scroller/stores";
|
|
9
|
+
import { EMPTY_MESSAGE_SCROLLER_SCROLLABLE } from "#/lib/message-scroller/types";
|
|
10
|
+
import type {
|
|
11
|
+
MessageScrollerMode,
|
|
12
|
+
MessageScrollerScrollable,
|
|
13
|
+
MessageScrollerScrollOptions,
|
|
14
|
+
MessageScrollerStore,
|
|
15
|
+
MessageScrollerVisibilityStore,
|
|
16
|
+
} from "#/lib/message-scroller/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
|
+
/**
|
|
22
|
+
* @since 0.5.0-canary.3
|
|
23
|
+
*/
|
|
24
|
+
type MessageScrollerRefs = {
|
|
25
|
+
autoScrollRef: RefObject<boolean>;
|
|
26
|
+
autoscrollingRef: RefObject<boolean>;
|
|
27
|
+
autoscrollingTimeoutRef: RefObject<number | null>;
|
|
28
|
+
streamingTurnRef: RefObject<HTMLElement | null>;
|
|
29
|
+
contentRef: RefObject<HTMLDivElement | null>;
|
|
30
|
+
defaultScrollPositionAppliedRef: RefObject<boolean>;
|
|
31
|
+
firstItemRef: RefObject<HTMLElement | null>;
|
|
32
|
+
itemCountRef: RefObject<number>;
|
|
33
|
+
messageElementsRef: RefObject<Map<string, HTMLElement>>;
|
|
34
|
+
modeRef: RefObject<MessageScrollerMode>;
|
|
35
|
+
pendingScrollFrameRef: RefObject<number | null>;
|
|
36
|
+
pendingScrollToMessageRef: RefObject<{
|
|
37
|
+
messageId: string;
|
|
38
|
+
options?: MessageScrollerScrollOptions;
|
|
39
|
+
} | null>;
|
|
40
|
+
prependRestoreRef: RefObject<{
|
|
41
|
+
element: HTMLElement;
|
|
42
|
+
viewportTop: number;
|
|
43
|
+
} | null>;
|
|
44
|
+
preserveScrollOnPrependRef: RefObject<boolean>;
|
|
45
|
+
rootRef: RefObject<HTMLDivElement | null>;
|
|
46
|
+
scrollEdgeThresholdRef: RefObject<number>;
|
|
47
|
+
scrollMarginRef: RefObject<number>;
|
|
48
|
+
scrollPreviousItemPeekRef: RefObject<number>;
|
|
49
|
+
spacerGapRef: RefObject<number>;
|
|
50
|
+
spacerHeightRef: RefObject<number>;
|
|
51
|
+
spacerRef: RefObject<HTMLDivElement | null>;
|
|
52
|
+
stateFrameRef: RefObject<number | null>;
|
|
53
|
+
stateStore: MessageScrollerStore<MessageScrollerScrollable>;
|
|
54
|
+
viewportRef: RefObject<HTMLDivElement | null>;
|
|
55
|
+
visibilityFrameRef: RefObject<number | null>;
|
|
56
|
+
visibilityObserverRef: RefObject<IntersectionObserver | null>;
|
|
57
|
+
visibilityStore: MessageScrollerVisibilityStore;
|
|
58
|
+
visibleMessageIdsRef: RefObject<Set<string>>;
|
|
59
|
+
handledScrollAnchorsRef: RefObject<WeakSet<HTMLElement>>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Builds the per-instance ref bag: the two external stores constructed once, and
|
|
63
|
+
// the latest prop values mirrored onto refs each render so callbacks stay stable.
|
|
64
|
+
/**
|
|
65
|
+
* @since 0.5.0-canary.3
|
|
66
|
+
*/
|
|
67
|
+
function useMessageScrollerRefs({
|
|
68
|
+
autoScroll,
|
|
69
|
+
scrollEdgeThreshold,
|
|
70
|
+
scrollMargin,
|
|
71
|
+
scrollPreviousItemPeek,
|
|
72
|
+
}: {
|
|
73
|
+
autoScroll: boolean;
|
|
74
|
+
scrollEdgeThreshold: number;
|
|
75
|
+
scrollMargin: number;
|
|
76
|
+
scrollPreviousItemPeek: number;
|
|
77
|
+
}): MessageScrollerRefs {
|
|
78
|
+
const autoScrollRef = useRef(autoScroll);
|
|
79
|
+
const autoscrollingRef = useRef(false);
|
|
80
|
+
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
81
|
+
const defaultScrollPositionAppliedRef = useRef(false);
|
|
82
|
+
const scrollEdgeThresholdRef = useRef(scrollEdgeThreshold);
|
|
83
|
+
const itemCountRef = useRef(0);
|
|
84
|
+
const firstItemRef = useRef<HTMLElement | null>(null);
|
|
85
|
+
const modeRef = useRef<MessageScrollerMode>(autoScroll ? "following-bottom" : "free-scrolling");
|
|
86
|
+
const messageElementsRef = useRef(new Map<string, HTMLElement>());
|
|
87
|
+
const pendingScrollToMessageRef = useRef<{
|
|
88
|
+
messageId: string;
|
|
89
|
+
options?: MessageScrollerScrollOptions;
|
|
90
|
+
} | null>(null);
|
|
91
|
+
// The row to hold steady on the next prepend: the first visible row, or a jump
|
|
92
|
+
// target seeded by scrollToElement. restorePrependedAnchor reads only this.
|
|
93
|
+
const prependRestoreRef = useRef<{
|
|
94
|
+
element: HTMLElement;
|
|
95
|
+
viewportTop: number;
|
|
96
|
+
} | null>(null);
|
|
97
|
+
// The turn held at the reading line so a reply streaming in below it can re-pin
|
|
98
|
+
// it instead of letting scrollTop clamp it loose.
|
|
99
|
+
const streamingTurnRef = useRef<HTMLElement | null>(null);
|
|
100
|
+
const scrollPreviousItemPeekRef = useRef(scrollPreviousItemPeek);
|
|
101
|
+
const preserveScrollOnPrependRef = useRef(true);
|
|
102
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
103
|
+
const scrollMarginRef = useRef(scrollMargin);
|
|
104
|
+
const pendingScrollFrameRef = useRef<number | null>(null);
|
|
105
|
+
const spacerGapRef = useRef(0);
|
|
106
|
+
const spacerHeightRef = useRef(0);
|
|
107
|
+
const spacerRef = useRef<HTMLDivElement | null>(null);
|
|
108
|
+
const stateFrameRef = useRef<number | null>(null);
|
|
109
|
+
const stateStoreRef = useRef<MessageScrollerStore<MessageScrollerScrollable> | null>(null);
|
|
110
|
+
const autoscrollingTimeoutRef = useRef<number | null>(null);
|
|
111
|
+
const viewportRef = useRef<HTMLDivElement | null>(null);
|
|
112
|
+
const visibilityFrameRef = useRef<number | null>(null);
|
|
113
|
+
const visibilityObserverRef = useRef<IntersectionObserver | null>(null);
|
|
114
|
+
const visibilityStoreRef = useRef<MessageScrollerVisibilityStore | null>(null);
|
|
115
|
+
const visibleMessageIdsRef = useRef(new Set<string>());
|
|
116
|
+
const handledScrollAnchorsRef = useRef(new WeakSet<HTMLElement>());
|
|
117
|
+
|
|
118
|
+
if (stateStoreRef.current === null) {
|
|
119
|
+
stateStoreRef.current = createMessageScrollerStore(EMPTY_MESSAGE_SCROLLER_SCROLLABLE, areScrollStatesEqual);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (visibilityStoreRef.current === null) {
|
|
123
|
+
visibilityStoreRef.current = createMessageScrollerVisibilityStore();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Track the latest prop values on every render so callbacks read fresh values
|
|
127
|
+
// without being recreated (the useLatest pattern).
|
|
128
|
+
autoScrollRef.current = autoScroll;
|
|
129
|
+
scrollEdgeThresholdRef.current = scrollEdgeThreshold;
|
|
130
|
+
scrollMarginRef.current = scrollMargin;
|
|
131
|
+
scrollPreviousItemPeekRef.current = scrollPreviousItemPeek;
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
autoScrollRef,
|
|
135
|
+
autoscrollingRef,
|
|
136
|
+
autoscrollingTimeoutRef,
|
|
137
|
+
streamingTurnRef,
|
|
138
|
+
contentRef,
|
|
139
|
+
defaultScrollPositionAppliedRef,
|
|
140
|
+
firstItemRef,
|
|
141
|
+
itemCountRef,
|
|
142
|
+
messageElementsRef,
|
|
143
|
+
modeRef,
|
|
144
|
+
pendingScrollFrameRef,
|
|
145
|
+
pendingScrollToMessageRef,
|
|
146
|
+
prependRestoreRef,
|
|
147
|
+
preserveScrollOnPrependRef,
|
|
148
|
+
rootRef,
|
|
149
|
+
scrollEdgeThresholdRef,
|
|
150
|
+
scrollMarginRef,
|
|
151
|
+
scrollPreviousItemPeekRef,
|
|
152
|
+
spacerGapRef,
|
|
153
|
+
spacerHeightRef,
|
|
154
|
+
spacerRef,
|
|
155
|
+
stateFrameRef,
|
|
156
|
+
stateStore: stateStoreRef.current,
|
|
157
|
+
viewportRef,
|
|
158
|
+
visibilityFrameRef,
|
|
159
|
+
visibilityObserverRef,
|
|
160
|
+
visibilityStore: visibilityStoreRef.current,
|
|
161
|
+
visibleMessageIdsRef,
|
|
162
|
+
handledScrollAnchorsRef,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { useMessageScrollerRefs };
|
|
167
|
+
export type { MessageScrollerRefs };
|
|
@@ -3,7 +3,7 @@ import { useMemo } from "react";
|
|
|
3
3
|
/**
|
|
4
4
|
* @since 0.3.16-canary.0
|
|
5
5
|
*/
|
|
6
|
-
export interface
|
|
6
|
+
export interface UsePaginationOptions {
|
|
7
7
|
/**
|
|
8
8
|
* Current active page number.
|
|
9
9
|
*/
|
|
@@ -50,7 +50,7 @@ const createRange = (start: number, end: number): Array<number> => {
|
|
|
50
50
|
* collapsed ranges. The shape adapts to the total pages and the requested
|
|
51
51
|
* sibling window around the current page.
|
|
52
52
|
*
|
|
53
|
-
* @param props - Pagination options. See {@link
|
|
53
|
+
* @param props - Pagination options. See {@link UsePaginationOptions}.
|
|
54
54
|
* @returns Array of page numbers and `ELLIPSIS` representing the pagination model.
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
@@ -70,7 +70,7 @@ export function usePagination({
|
|
|
70
70
|
resultsPerPage,
|
|
71
71
|
siblingPagesCount = 1,
|
|
72
72
|
totalResults,
|
|
73
|
-
}:
|
|
73
|
+
}: UsePaginationOptions): Array<number | string> {
|
|
74
74
|
return useMemo<Array<number | string>>(() => {
|
|
75
75
|
// Total pages derived from results and page size
|
|
76
76
|
const totalPages = Math.ceil(totalResults / Math.floor(resultsPerPage));
|
package/src/index.ts
CHANGED
|
@@ -35,6 +35,28 @@ export type {
|
|
|
35
35
|
} from "#/components/alert-dialog";
|
|
36
36
|
export { AspectRatio } from "#/components/aspect-ratio";
|
|
37
37
|
export type { AspectRatioProps } from "#/components/aspect-ratio";
|
|
38
|
+
export {
|
|
39
|
+
Attachment,
|
|
40
|
+
AttachmentAction,
|
|
41
|
+
AttachmentActions,
|
|
42
|
+
AttachmentContent,
|
|
43
|
+
AttachmentDescription,
|
|
44
|
+
AttachmentGroup,
|
|
45
|
+
AttachmentMedia,
|
|
46
|
+
AttachmentTitle,
|
|
47
|
+
AttachmentTrigger,
|
|
48
|
+
} from "#/components/attachment";
|
|
49
|
+
export type {
|
|
50
|
+
AttachmentActionProps,
|
|
51
|
+
AttachmentActionsProps,
|
|
52
|
+
AttachmentContentProps,
|
|
53
|
+
AttachmentDescriptionProps,
|
|
54
|
+
AttachmentGroupProps,
|
|
55
|
+
AttachmentMediaProps,
|
|
56
|
+
AttachmentProps,
|
|
57
|
+
AttachmentTitleProps,
|
|
58
|
+
AttachmentTriggerProps,
|
|
59
|
+
} from "#/components/attachment";
|
|
38
60
|
export { Avatar, AvatarBadge, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage } from "#/components/avatar";
|
|
39
61
|
export type {
|
|
40
62
|
AvatarBadgeProps,
|
|
@@ -68,6 +90,8 @@ export type {
|
|
|
68
90
|
} from "#/components/breadcrumb";
|
|
69
91
|
export { Button } from "#/components/button";
|
|
70
92
|
export type { ButtonProps } from "#/components/button";
|
|
93
|
+
export { Bubble, BubbleContent, BubbleGroup, BubbleReactions } from "#/components/bubble";
|
|
94
|
+
export type { BubbleContentProps, BubbleGroupProps, BubbleProps, BubbleReactionsProps } from "#/components/bubble";
|
|
71
95
|
export { Calendar, CalendarDayButton } from "#/components/calendar";
|
|
72
96
|
export { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "#/components/card";
|
|
73
97
|
export type {
|
|
@@ -383,6 +407,8 @@ export type {
|
|
|
383
407
|
} from "#/components/item";
|
|
384
408
|
export { Label } from "#/components/label";
|
|
385
409
|
export type { LabelProps } from "#/components/label";
|
|
410
|
+
export { Marker, MarkerContent, MarkerIcon } from "#/components/marker";
|
|
411
|
+
export type { MarkerContentProps, MarkerIconProps, MarkerProps } from "#/components/marker";
|
|
386
412
|
export {
|
|
387
413
|
Menubar,
|
|
388
414
|
MenubarArrow,
|
|
@@ -419,6 +445,46 @@ export type {
|
|
|
419
445
|
MenubarSubTriggerProps,
|
|
420
446
|
MenubarTriggerProps,
|
|
421
447
|
} from "#/components/menubar";
|
|
448
|
+
export {
|
|
449
|
+
Message,
|
|
450
|
+
MessageAvatar,
|
|
451
|
+
MessageContent,
|
|
452
|
+
MessageFooter,
|
|
453
|
+
MessageGroup,
|
|
454
|
+
MessageHeader,
|
|
455
|
+
} from "#/components/message";
|
|
456
|
+
export type {
|
|
457
|
+
MessageAvatarProps,
|
|
458
|
+
MessageContentProps,
|
|
459
|
+
MessageFooterProps,
|
|
460
|
+
MessageGroupProps,
|
|
461
|
+
MessageHeaderProps,
|
|
462
|
+
MessageProps,
|
|
463
|
+
} from "#/components/message";
|
|
464
|
+
export {
|
|
465
|
+
MessageScroller,
|
|
466
|
+
MessageScrollerButton,
|
|
467
|
+
MessageScrollerContent,
|
|
468
|
+
MessageScrollerItem,
|
|
469
|
+
MessageScrollerProvider,
|
|
470
|
+
MessageScrollerViewport,
|
|
471
|
+
useMessageScroller,
|
|
472
|
+
useMessageScrollerScrollable,
|
|
473
|
+
useMessageScrollerVisibility,
|
|
474
|
+
} from "#/components/message-scroller";
|
|
475
|
+
export type {
|
|
476
|
+
MessageScrollerButtonProps,
|
|
477
|
+
MessageScrollerContentProps,
|
|
478
|
+
MessageScrollerDefaultScrollPosition,
|
|
479
|
+
MessageScrollerItemProps,
|
|
480
|
+
MessageScrollerProps,
|
|
481
|
+
MessageScrollerProviderProps,
|
|
482
|
+
MessageScrollerScrollable,
|
|
483
|
+
MessageScrollerScrollAlign,
|
|
484
|
+
MessageScrollerScrollOptions,
|
|
485
|
+
MessageScrollerViewportProps,
|
|
486
|
+
MessageScrollerVisibilityState,
|
|
487
|
+
} from "#/components/message-scroller";
|
|
422
488
|
export {
|
|
423
489
|
NavigationMenu,
|
|
424
490
|
NavigationMenuContent,
|
|
@@ -644,11 +710,15 @@ export { useIsMobile } from "#/hooks/use-is-mobile";
|
|
|
644
710
|
export { useMediaQuery } from "#/hooks/use-media-query";
|
|
645
711
|
export { useMutationObserver } from "#/hooks/use-mutation-observer";
|
|
646
712
|
export { ELLIPSIS, usePagination } from "#/hooks/use-pagination";
|
|
647
|
-
export type {
|
|
713
|
+
export type { UsePaginationOptions } from "#/hooks/use-pagination";
|
|
648
714
|
export { alertVariants } from "#/variants/alert";
|
|
649
715
|
export type { AlertVariants } from "#/variants/alert";
|
|
716
|
+
export { attachmentMediaVariants, attachmentVariants } from "#/variants/attachment";
|
|
717
|
+
export type { AttachmentMediaVariants, AttachmentVariants } from "#/variants/attachment";
|
|
650
718
|
export { badgeVariants } from "#/variants/badge";
|
|
651
719
|
export type { BadgeVariants } from "#/variants/badge";
|
|
720
|
+
export { bubbleReactionsVariants, bubbleVariants } from "#/variants/bubble";
|
|
721
|
+
export type { BubbleReactionsVariants, BubbleVariants } from "#/variants/bubble";
|
|
652
722
|
export { buttonGroupVariants } from "#/variants/button-group";
|
|
653
723
|
export type { ButtonGroupVariants } from "#/variants/button-group";
|
|
654
724
|
export { buttonVariants } from "#/variants/button";
|
|
@@ -661,6 +731,8 @@ export { inputGroupAddonVariants, inputGroupButtonVariants, inputGroupVariants }
|
|
|
661
731
|
export type { InputGroupAddonVariants, InputGroupButtonVariants, InputGroupVariants } from "#/variants/input-group";
|
|
662
732
|
export { itemMediaVariants, itemVariants } from "#/variants/item";
|
|
663
733
|
export type { ItemMediaVariants, ItemVariants } from "#/variants/item";
|
|
734
|
+
export { markerVariants } from "#/variants/marker";
|
|
735
|
+
export type { MarkerVariants } from "#/variants/marker";
|
|
664
736
|
export { navigationMenuTriggerVariants } from "#/variants/navigation-menu";
|
|
665
737
|
export type { NavigationMenuTriggerVariants } from "#/variants/navigation-menu";
|
|
666
738
|
export { progressCircleVariants } from "#/variants/progress-circle";
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EMPTY_MESSAGE_SCROLLER_SCROLLABLE,
|
|
3
|
+
EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE,
|
|
4
|
+
SCROLL_POSITION_EPSILON,
|
|
5
|
+
} from "./types";
|
|
6
|
+
import type { MessageScrollerScrollable, MessageScrollerScrollAlign, MessageScrollerVisibilityState } from "./types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @since 0.5.0-canary.3
|
|
10
|
+
*/
|
|
11
|
+
function getMessageScrollerScrollable({
|
|
12
|
+
content,
|
|
13
|
+
scrollEdgeThreshold,
|
|
14
|
+
spacer,
|
|
15
|
+
viewport,
|
|
16
|
+
}: {
|
|
17
|
+
content: HTMLElement | null;
|
|
18
|
+
scrollEdgeThreshold: number;
|
|
19
|
+
spacer: HTMLElement | null;
|
|
20
|
+
viewport: HTMLElement | null;
|
|
21
|
+
}): MessageScrollerScrollable {
|
|
22
|
+
if (!viewport || !content) {
|
|
23
|
+
return EMPTY_MESSAGE_SCROLLER_SCROLLABLE;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const contentBottom = getContentBottom({ content, spacer, viewport });
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
start: viewport.scrollTop > scrollEdgeThreshold,
|
|
30
|
+
end: contentBottom - viewport.scrollTop - viewport.clientHeight > scrollEdgeThreshold,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @since 0.5.0-canary.3
|
|
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: Array<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
|
+
/**
|
|
109
|
+
* @since 0.5.0-canary.3
|
|
110
|
+
*/
|
|
111
|
+
function getMessageScrollerItems(content: HTMLElement, spacer: HTMLElement | null) {
|
|
112
|
+
return Array.from(content.children).filter(
|
|
113
|
+
(child): child is HTMLElement => child instanceof HTMLElement && child !== spacer,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @since 0.5.0-canary.3
|
|
119
|
+
*/
|
|
120
|
+
function getNewScrollAnchor(items: Array<HTMLElement>, previousItemCount: number) {
|
|
121
|
+
for (let index = previousItemCount; index < items.length; index++) {
|
|
122
|
+
const item = items[index];
|
|
123
|
+
|
|
124
|
+
if (item?.dataset.scrollAnchor === "true") {
|
|
125
|
+
return item;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @since 0.5.0-canary.3
|
|
134
|
+
*/
|
|
135
|
+
function getUnanchoredScrollAnchor(items: Array<HTMLElement>, handledAnchors: { has(element: HTMLElement): boolean }) {
|
|
136
|
+
for (const item of items) {
|
|
137
|
+
if (item.dataset.scrollAnchor === "true" && !handledAnchors.has(item)) {
|
|
138
|
+
return item;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @since 0.5.0-canary.3
|
|
147
|
+
*/
|
|
148
|
+
function hasMultipleNewScrollAnchors(items: Array<HTMLElement>, previousItemCount: number) {
|
|
149
|
+
let count = 0;
|
|
150
|
+
|
|
151
|
+
for (let index = previousItemCount; index < items.length; index++) {
|
|
152
|
+
const item = items[index];
|
|
153
|
+
|
|
154
|
+
if (item?.dataset.scrollAnchor !== "true") {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
count += 1;
|
|
159
|
+
|
|
160
|
+
if (count > 1) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @since 0.5.0-canary.3
|
|
170
|
+
*/
|
|
171
|
+
function getLastScrollAnchor(items: Array<HTMLElement>) {
|
|
172
|
+
for (let index = items.length - 1; index >= 0; index--) {
|
|
173
|
+
const item = items[index];
|
|
174
|
+
|
|
175
|
+
if (item?.dataset.scrollAnchor === "true") {
|
|
176
|
+
return item;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @since 0.5.0-canary.3
|
|
185
|
+
*/
|
|
186
|
+
function getFirstVisibleMessageItem({
|
|
187
|
+
content,
|
|
188
|
+
spacer,
|
|
189
|
+
viewport,
|
|
190
|
+
}: {
|
|
191
|
+
content: HTMLElement;
|
|
192
|
+
spacer: HTMLElement | null;
|
|
193
|
+
viewport: HTMLElement;
|
|
194
|
+
}) {
|
|
195
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
196
|
+
|
|
197
|
+
for (const item of getMessageScrollerItems(content, spacer)) {
|
|
198
|
+
if (!item.dataset.messageId) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const rect = item.getBoundingClientRect();
|
|
203
|
+
|
|
204
|
+
if (rect.bottom > viewportRect.top && rect.top < viewportRect.bottom) {
|
|
205
|
+
return item;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @since 0.5.0-canary.3
|
|
214
|
+
*/
|
|
215
|
+
function getElementScrollTop({
|
|
216
|
+
align,
|
|
217
|
+
element,
|
|
218
|
+
scrollMargin,
|
|
219
|
+
spacer,
|
|
220
|
+
viewport,
|
|
221
|
+
}: {
|
|
222
|
+
align: MessageScrollerScrollAlign;
|
|
223
|
+
element: HTMLElement;
|
|
224
|
+
scrollMargin: number;
|
|
225
|
+
spacer: HTMLElement | null;
|
|
226
|
+
viewport: HTMLElement;
|
|
227
|
+
}) {
|
|
228
|
+
const elementTop = getElementTop(element, viewport);
|
|
229
|
+
const elementHeight = element.getBoundingClientRect().height;
|
|
230
|
+
const contentPadding = getContentBlockPadding(spacer);
|
|
231
|
+
|
|
232
|
+
if (align === "center") {
|
|
233
|
+
const insetHeight = Math.max(0, viewport.clientHeight - contentPadding.start - contentPadding.end);
|
|
234
|
+
|
|
235
|
+
return elementTop - contentPadding.start - (insetHeight - elementHeight) / 2 - scrollMargin;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (align === "end") {
|
|
239
|
+
return elementTop - viewport.clientHeight + elementHeight + contentPadding.end + scrollMargin;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (align === "nearest") {
|
|
243
|
+
const elementBottom = elementTop + elementHeight;
|
|
244
|
+
const viewportTop = viewport.scrollTop + contentPadding.start;
|
|
245
|
+
const viewportBottom = viewport.scrollTop + viewport.clientHeight - contentPadding.end;
|
|
246
|
+
|
|
247
|
+
if (elementTop >= viewportTop && elementBottom <= viewportBottom) {
|
|
248
|
+
return viewport.scrollTop;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (elementTop < viewportTop) {
|
|
252
|
+
return elementTop - contentPadding.start - scrollMargin;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return elementBottom - viewport.clientHeight + contentPadding.end + scrollMargin;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return elementTop - contentPadding.start - scrollMargin;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @since 0.5.0-canary.3
|
|
263
|
+
*/
|
|
264
|
+
function getElementTop(element: HTMLElement, viewport: HTMLElement) {
|
|
265
|
+
const elementRect = element.getBoundingClientRect();
|
|
266
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
267
|
+
|
|
268
|
+
return elementRect.top - viewportRect.top + viewport.scrollTop;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @since 0.5.0-canary.3
|
|
273
|
+
*/
|
|
274
|
+
function getElementViewportTop(element: HTMLElement, viewport: HTMLElement) {
|
|
275
|
+
return element.getBoundingClientRect().top - viewport.getBoundingClientRect().top;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @since 0.5.0-canary.3
|
|
280
|
+
*/
|
|
281
|
+
function getTailSpacerHeight({
|
|
282
|
+
content,
|
|
283
|
+
scrollTop,
|
|
284
|
+
spacer,
|
|
285
|
+
viewport,
|
|
286
|
+
}: {
|
|
287
|
+
content: HTMLElement;
|
|
288
|
+
scrollTop: number;
|
|
289
|
+
spacer: HTMLElement | null;
|
|
290
|
+
viewport: HTMLElement;
|
|
291
|
+
}) {
|
|
292
|
+
const contentBottom = getContentBottom({ content, spacer, viewport });
|
|
293
|
+
|
|
294
|
+
return scrollTop + viewport.clientHeight - contentBottom;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* @since 0.5.0-canary.3
|
|
299
|
+
*/
|
|
300
|
+
function getContentBottom({
|
|
301
|
+
content,
|
|
302
|
+
spacer,
|
|
303
|
+
viewport,
|
|
304
|
+
}: {
|
|
305
|
+
content: HTMLElement;
|
|
306
|
+
spacer: HTMLElement | null;
|
|
307
|
+
viewport: HTMLElement;
|
|
308
|
+
}) {
|
|
309
|
+
const items = getMessageScrollerItems(content, spacer);
|
|
310
|
+
const padding = getBlockPadding(content);
|
|
311
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
312
|
+
const scrollTop = viewport.scrollTop;
|
|
313
|
+
let contentBottom = padding.start + padding.end;
|
|
314
|
+
|
|
315
|
+
for (const item of items) {
|
|
316
|
+
const rect = item.getBoundingClientRect();
|
|
317
|
+
|
|
318
|
+
contentBottom = Math.max(contentBottom, rect.bottom - viewportRect.top + scrollTop + padding.end);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return contentBottom;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @since 0.5.0-canary.3
|
|
326
|
+
*/
|
|
327
|
+
function getMaxScrollTop(viewport: HTMLElement) {
|
|
328
|
+
return Math.max(0, viewport.scrollHeight - viewport.clientHeight);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function getBlockPadding(element: HTMLElement) {
|
|
332
|
+
const style = window.getComputedStyle(element);
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
end: readCssPixel(style.paddingBlockEnd || style.paddingBottom),
|
|
336
|
+
start: readCssPixel(style.paddingBlockStart || style.paddingTop),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* @since 0.5.0-canary.3
|
|
342
|
+
*/
|
|
343
|
+
function getContentBlockPadding(spacer: HTMLElement | null) {
|
|
344
|
+
const content = spacer?.parentElement;
|
|
345
|
+
|
|
346
|
+
if (!content) {
|
|
347
|
+
return {
|
|
348
|
+
end: 0,
|
|
349
|
+
start: 0,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return getBlockPadding(content);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @since 0.5.0-canary.3
|
|
358
|
+
*/
|
|
359
|
+
function getFlexGap(element: HTMLElement | null) {
|
|
360
|
+
if (!element) {
|
|
361
|
+
return 0;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const style = window.getComputedStyle(element);
|
|
365
|
+
const gap = style.rowGap === "normal" ? style.gap : style.rowGap;
|
|
366
|
+
|
|
367
|
+
return readCssPixel(gap);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function readCssPixel(value: string | undefined) {
|
|
371
|
+
if (!value) {
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const number = Number.parseFloat(value);
|
|
376
|
+
|
|
377
|
+
return Number.isFinite(number) ? number : 0;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export {
|
|
381
|
+
getContentBlockPadding,
|
|
382
|
+
getContentBottom,
|
|
383
|
+
getElementScrollTop,
|
|
384
|
+
getElementTop,
|
|
385
|
+
getElementViewportTop,
|
|
386
|
+
getFirstVisibleMessageItem,
|
|
387
|
+
getFlexGap,
|
|
388
|
+
getLastScrollAnchor,
|
|
389
|
+
getMaxScrollTop,
|
|
390
|
+
getMessageScrollerItems,
|
|
391
|
+
getMessageScrollerScrollable,
|
|
392
|
+
getMessageScrollerVisibilityState,
|
|
393
|
+
getNewScrollAnchor,
|
|
394
|
+
getTailSpacerHeight,
|
|
395
|
+
getUnanchoredScrollAnchor,
|
|
396
|
+
hasMultipleNewScrollAnchors,
|
|
397
|
+
};
|