@codefast/ui 0.5.0-canary.2 → 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 +32 -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/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/sidebar.mjs +6 -3
- 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/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/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 +52 -6
- 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,77 @@
|
|
|
1
|
+
import { cn } from "../lib/utils.mjs";
|
|
2
|
+
import { Button } from "./button.mjs";
|
|
3
|
+
import { MessageScroller as MessageScroller$1, MessageScrollerButton as MessageScrollerButton$1, MessageScrollerContent as MessageScrollerContent$1, MessageScrollerItem as MessageScrollerItem$1, MessageScrollerProvider as MessageScrollerProvider$1, MessageScrollerViewport as MessageScrollerViewport$1, useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility } from "../primitives/message-scroller.mjs";
|
|
4
|
+
import { ArrowDownIcon } from "lucide-react";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
//#region src/components/message-scroller.tsx
|
|
7
|
+
/**
|
|
8
|
+
* @since 0.5.0-canary.3
|
|
9
|
+
*/
|
|
10
|
+
function MessageScrollerProvider(props) {
|
|
11
|
+
return /* @__PURE__ */ jsx(MessageScrollerProvider$1, { ...props });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @since 0.5.0-canary.3
|
|
15
|
+
*/
|
|
16
|
+
function MessageScroller({ className, ...props }) {
|
|
17
|
+
return /* @__PURE__ */ jsx(MessageScroller$1, {
|
|
18
|
+
className: cn("group/message-scroller relative flex size-full min-h-0 flex-col overflow-hidden", className),
|
|
19
|
+
"data-slot": "message-scroller",
|
|
20
|
+
...props
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @since 0.5.0-canary.3
|
|
25
|
+
*/
|
|
26
|
+
function MessageScrollerViewport({ className, ...props }) {
|
|
27
|
+
return /* @__PURE__ */ jsx(MessageScrollerViewport$1, {
|
|
28
|
+
className: cn("size-full min-h-0 min-w-0 scroll-fade-b scrollbar-thin scrollbar-gutter-stable overflow-y-auto overscroll-contain contain-content data-autoscrolling:scrollbar-thumb-transparent data-autoscrolling:scrollbar-track-transparent", className),
|
|
29
|
+
"data-slot": "message-scroller-viewport",
|
|
30
|
+
...props
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @since 0.5.0-canary.3
|
|
35
|
+
*/
|
|
36
|
+
function MessageScrollerContent({ className, ...props }) {
|
|
37
|
+
return /* @__PURE__ */ jsx(MessageScrollerContent$1, {
|
|
38
|
+
className: cn("flex h-max min-h-full flex-col gap-6", className),
|
|
39
|
+
"data-slot": "message-scroller-content",
|
|
40
|
+
...props
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @since 0.5.0-canary.3
|
|
45
|
+
*/
|
|
46
|
+
function MessageScrollerItem({ className, scrollAnchor = false, ...props }) {
|
|
47
|
+
return /* @__PURE__ */ jsx(MessageScrollerItem$1, {
|
|
48
|
+
className: cn("min-w-0 shrink-0 [contain-intrinsic-size:auto_10rem] [content-visibility:auto]", className),
|
|
49
|
+
"data-slot": "message-scroller-item",
|
|
50
|
+
scrollAnchor,
|
|
51
|
+
...props
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @since 0.5.0-canary.3
|
|
56
|
+
*/
|
|
57
|
+
function MessageScrollerButton({ children, className, direction = "end", render, size = "icon-sm", variant = "secondary", ...props }) {
|
|
58
|
+
return /* @__PURE__ */ jsx(MessageScrollerButton$1, {
|
|
59
|
+
className: cn("absolute inset-s-1/2 -translate-x-1/2 border-border bg-background text-foreground transition-[translate,scale,opacity] duration-200 hover:bg-muted hover:text-foreground data-[active=false]:pointer-events-none data-[active=false]:scale-95 data-[active=false]:opacity-0 data-[active=false]:duration-400 data-[active=false]:ease-[cubic-bezier(0.7,0,0.84,0)] data-[active=true]:translate-y-0 data-[active=true]:scale-100 data-[active=true]:opacity-100 data-[active=true]:ease-[cubic-bezier(0.23,1,0.32,1)] data-[direction=end]:bottom-4 data-[direction=end]:data-[active=false]:translate-y-full data-[direction=start]:top-4 data-[direction=start]:data-[active=false]:-translate-y-full rtl:translate-x-1/2 data-[direction=start]:[&_svg]:rotate-180", className),
|
|
60
|
+
"data-direction": direction,
|
|
61
|
+
"data-size": size,
|
|
62
|
+
"data-slot": "message-scroller-button",
|
|
63
|
+
"data-variant": variant,
|
|
64
|
+
direction,
|
|
65
|
+
render: render ?? /* @__PURE__ */ jsx(Button, {
|
|
66
|
+
size,
|
|
67
|
+
variant
|
|
68
|
+
}),
|
|
69
|
+
...props,
|
|
70
|
+
children: children ?? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ArrowDownIcon, {}), /* @__PURE__ */ jsx("span", {
|
|
71
|
+
className: "sr-only",
|
|
72
|
+
children: direction === "end" ? "Scroll to end" : "Scroll to start"
|
|
73
|
+
})] })
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport, useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ComponentProps, JSX } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/components/message.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Vertical stack of related messages.
|
|
6
|
+
*
|
|
7
|
+
* @since 0.5.0-canary.3
|
|
8
|
+
*/
|
|
9
|
+
type MessageGroupProps = ComponentProps<"div">;
|
|
10
|
+
/**
|
|
11
|
+
* @since 0.5.0-canary.3
|
|
12
|
+
*/
|
|
13
|
+
declare function MessageGroup({
|
|
14
|
+
className,
|
|
15
|
+
...props
|
|
16
|
+
}: MessageGroupProps): JSX.Element;
|
|
17
|
+
/**
|
|
18
|
+
* A single message row. `align="end"` mirrors the row for the current author.
|
|
19
|
+
*
|
|
20
|
+
* @since 0.5.0-canary.3
|
|
21
|
+
*/
|
|
22
|
+
interface MessageProps extends ComponentProps<"div"> {
|
|
23
|
+
align?: "start" | "end";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @since 0.5.0-canary.3
|
|
27
|
+
*/
|
|
28
|
+
declare function Message({
|
|
29
|
+
align,
|
|
30
|
+
className,
|
|
31
|
+
...props
|
|
32
|
+
}: MessageProps): JSX.Element;
|
|
33
|
+
/**
|
|
34
|
+
* Author avatar; lifts above the footer when a message footer is present.
|
|
35
|
+
*
|
|
36
|
+
* @since 0.5.0-canary.3
|
|
37
|
+
*/
|
|
38
|
+
type MessageAvatarProps = ComponentProps<"div">;
|
|
39
|
+
/**
|
|
40
|
+
* @since 0.5.0-canary.3
|
|
41
|
+
*/
|
|
42
|
+
declare function MessageAvatar({
|
|
43
|
+
className,
|
|
44
|
+
...props
|
|
45
|
+
}: MessageAvatarProps): JSX.Element;
|
|
46
|
+
/**
|
|
47
|
+
* Column holding the message bubbles and metadata.
|
|
48
|
+
*
|
|
49
|
+
* @since 0.5.0-canary.3
|
|
50
|
+
*/
|
|
51
|
+
type MessageContentProps = ComponentProps<"div">;
|
|
52
|
+
/**
|
|
53
|
+
* @since 0.5.0-canary.3
|
|
54
|
+
*/
|
|
55
|
+
declare function MessageContent({
|
|
56
|
+
className,
|
|
57
|
+
...props
|
|
58
|
+
}: MessageContentProps): JSX.Element;
|
|
59
|
+
/**
|
|
60
|
+
* Metadata above the bubbles (author, timestamp).
|
|
61
|
+
*
|
|
62
|
+
* @since 0.5.0-canary.3
|
|
63
|
+
*/
|
|
64
|
+
type MessageHeaderProps = ComponentProps<"div">;
|
|
65
|
+
/**
|
|
66
|
+
* @since 0.5.0-canary.3
|
|
67
|
+
*/
|
|
68
|
+
declare function MessageHeader({
|
|
69
|
+
className,
|
|
70
|
+
...props
|
|
71
|
+
}: MessageHeaderProps): JSX.Element;
|
|
72
|
+
/**
|
|
73
|
+
* Metadata below the bubbles (status, reactions); aligns with the row.
|
|
74
|
+
*
|
|
75
|
+
* @since 0.5.0-canary.3
|
|
76
|
+
*/
|
|
77
|
+
type MessageFooterProps = ComponentProps<"div">;
|
|
78
|
+
/**
|
|
79
|
+
* @since 0.5.0-canary.3
|
|
80
|
+
*/
|
|
81
|
+
declare function MessageFooter({
|
|
82
|
+
className,
|
|
83
|
+
...props
|
|
84
|
+
}: MessageFooterProps): JSX.Element;
|
|
85
|
+
//#endregion
|
|
86
|
+
export { Message, MessageAvatar, type MessageAvatarProps, MessageContent, type MessageContentProps, MessageFooter, type MessageFooterProps, MessageGroup, type MessageGroupProps, MessageHeader, type MessageHeaderProps, type MessageProps };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { cn } from "../lib/utils.mjs";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
//#region src/components/message.tsx
|
|
4
|
+
/**
|
|
5
|
+
* @since 0.5.0-canary.3
|
|
6
|
+
*/
|
|
7
|
+
function MessageGroup({ className, ...props }) {
|
|
8
|
+
return /* @__PURE__ */ jsx("div", {
|
|
9
|
+
className: cn("flex min-w-0 flex-col gap-2", className),
|
|
10
|
+
"data-slot": "message-group",
|
|
11
|
+
...props
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @since 0.5.0-canary.3
|
|
16
|
+
*/
|
|
17
|
+
function Message({ align = "start", className, ...props }) {
|
|
18
|
+
return /* @__PURE__ */ jsx("div", {
|
|
19
|
+
className: cn("group/message relative flex w-full min-w-0 gap-2 text-sm data-[align=end]:flex-row-reverse", className),
|
|
20
|
+
"data-align": align,
|
|
21
|
+
"data-slot": "message",
|
|
22
|
+
...props
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @since 0.5.0-canary.3
|
|
27
|
+
*/
|
|
28
|
+
function MessageAvatar({ className, ...props }) {
|
|
29
|
+
return /* @__PURE__ */ jsx("div", {
|
|
30
|
+
className: cn("flex w-fit min-w-8 shrink-0 items-center justify-center self-end overflow-hidden rounded-full bg-muted group-has-data-[slot=message-footer]/message:-translate-y-8", className),
|
|
31
|
+
"data-slot": "message-avatar",
|
|
32
|
+
...props
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @since 0.5.0-canary.3
|
|
37
|
+
*/
|
|
38
|
+
function MessageContent({ className, ...props }) {
|
|
39
|
+
return /* @__PURE__ */ jsx("div", {
|
|
40
|
+
className: cn("flex w-full min-w-0 flex-col gap-2.5 wrap-break-word group-data-[align=end]/message:*:data-slot:self-end", className),
|
|
41
|
+
"data-slot": "message-content",
|
|
42
|
+
...props
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @since 0.5.0-canary.3
|
|
47
|
+
*/
|
|
48
|
+
function MessageHeader({ className, ...props }) {
|
|
49
|
+
return /* @__PURE__ */ jsx("div", {
|
|
50
|
+
className: cn("flex max-w-full min-w-0 items-center px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0", className),
|
|
51
|
+
"data-slot": "message-header",
|
|
52
|
+
...props
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @since 0.5.0-canary.3
|
|
57
|
+
*/
|
|
58
|
+
function MessageFooter({ className, ...props }) {
|
|
59
|
+
return /* @__PURE__ */ jsx("div", {
|
|
60
|
+
className: cn("flex max-w-full min-w-0 items-center px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0 group-data-[align=end]/message:justify-end", className),
|
|
61
|
+
"data-slot": "message-footer",
|
|
62
|
+
...props
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
export { Message, MessageAvatar, MessageContent, MessageFooter, MessageGroup, MessageHeader };
|
|
@@ -10,7 +10,7 @@ import { sidebarMenuButtonVariants } from "../variants/sidebar.mjs";
|
|
|
10
10
|
import { PanelLeftIcon } from "lucide-react";
|
|
11
11
|
import { Slot } from "radix-ui";
|
|
12
12
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
-
import { useCallback, useEffect, useState } from "react";
|
|
13
|
+
import { useCallback, useEffect, useId, useState } from "react";
|
|
14
14
|
import { Context } from "radix-ui/internal";
|
|
15
15
|
//#region src/components/sidebar.tsx
|
|
16
16
|
const SIDEBAR_COOKIE_NAME = "sidebar_state";
|
|
@@ -159,7 +159,7 @@ const SIDEBAR_RAIL_NAME = "SidebarRail";
|
|
|
159
159
|
function SidebarRail({ className, ...props }) {
|
|
160
160
|
const { toggleSidebar } = useSidebar(SIDEBAR_RAIL_NAME);
|
|
161
161
|
return /* @__PURE__ */ jsx("button", {
|
|
162
|
-
className: cn("absolute inset-y-0 z-20 hidden w-4 transition-all ease-gentle group-data-[collapsible=offcanvas]:translate-x-0 group-data-side-right:left-0 group-data-side-left:-right-4 after:absolute after:inset-y-0 after:inset-s-1/2 after:w-0.5 group-data-[collapsible=offcanvas]:after:
|
|
162
|
+
className: cn("absolute inset-y-0 z-20 hidden w-4 transition-all ease-gentle group-data-[collapsible=offcanvas]:translate-x-0 group-data-side-right:left-0 group-data-side-left:-right-4 after:absolute after:inset-y-0 after:inset-s-1/2 after:w-0.5 group-data-[collapsible=offcanvas]:after:start-full hover:group-data-[collapsible=offcanvas]:bg-sidebar hover:after:bg-sidebar-border in-data-side-right:cursor-e-resize in-data-side-left:cursor-w-resize sm:flex ltr:-translate-x-1/2 rtl:-translate-x-1/2 rtl:group-data-[collapsible=offcanvas]:translate-x-0 rtl:in-data-side-right:cursor-w-resize rtl:in-data-side-left:cursor-e-resize [[data-side=left][data-collapsible=offcanvas]_&]:-right-2 [[data-side=left][data-state=collapsed]_&]:cursor-e-resize rtl:[[data-side=left][data-state=collapsed]_&]:cursor-w-resize [[data-side=right][data-collapsible=offcanvas]_&]:-left-2 [[data-side=right][data-state=collapsed]_&]:cursor-w-resize rtl:[[data-side=right][data-state=collapsed]_&]:cursor-e-resize", className),
|
|
163
163
|
"data-sidebar": "rail",
|
|
164
164
|
"data-slot": "sidebar-rail",
|
|
165
165
|
title: "Toggle Sidebar",
|
|
@@ -356,7 +356,10 @@ function SidebarMenuBadge({ className, ...props }) {
|
|
|
356
356
|
* @since 0.3.16-canary.0
|
|
357
357
|
*/
|
|
358
358
|
function SidebarMenuSkeleton({ className, showIcon = false, ...props }) {
|
|
359
|
-
const
|
|
359
|
+
const id = useId();
|
|
360
|
+
let sum = 0;
|
|
361
|
+
for (const char of id) sum += char.codePointAt(0) ?? 0;
|
|
362
|
+
const width = `${String(sum % 41 + 50)}%`;
|
|
360
363
|
return /* @__PURE__ */ jsxs("div", {
|
|
361
364
|
className: cn("flex h-8 items-center gap-2 rounded-md px-2", className),
|
|
362
365
|
"data-sidebar": "menu-skeleton",
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MessageScrollerScrollOptions } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { MessageScrollerRefs } from "./use-message-scroller-refs.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/use-message-scroller-commands.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
declare function useMessageScrollerCommands({
|
|
9
|
+
refs,
|
|
10
|
+
commitScrollState,
|
|
11
|
+
scheduleStateCommit,
|
|
12
|
+
scheduleVisibilitySync
|
|
13
|
+
}: {
|
|
14
|
+
refs: MessageScrollerRefs;
|
|
15
|
+
commitScrollState: () => void;
|
|
16
|
+
scheduleStateCommit: () => void;
|
|
17
|
+
scheduleVisibilitySync: () => void;
|
|
18
|
+
}): {
|
|
19
|
+
flushPendingScrollToMessage: () => boolean;
|
|
20
|
+
reanchorToAnchoredMessage: () => boolean;
|
|
21
|
+
scrollToElement: (element: HTMLElement, {
|
|
22
|
+
align,
|
|
23
|
+
behavior,
|
|
24
|
+
scrollMargin
|
|
25
|
+
}?: MessageScrollerScrollOptions, {
|
|
26
|
+
keepPreviousPeek
|
|
27
|
+
}?: {
|
|
28
|
+
keepPreviousPeek?: boolean;
|
|
29
|
+
}) => boolean;
|
|
30
|
+
scrollToEnd: ({
|
|
31
|
+
behavior
|
|
32
|
+
}?: MessageScrollerScrollOptions) => boolean;
|
|
33
|
+
scrollToMessage: (messageId: string, options?: MessageScrollerScrollOptions) => boolean;
|
|
34
|
+
scrollToStart: ({
|
|
35
|
+
behavior
|
|
36
|
+
}?: MessageScrollerScrollOptions) => boolean;
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { useMessageScrollerCommands };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { getElementScrollTop, getElementViewportTop, getMaxScrollTop, getTailSpacerHeight } from "../lib/message-scroller/geometry.mjs";
|
|
3
|
+
import { useCallback } from "react";
|
|
4
|
+
//#region src/hooks/use-message-scroller-commands.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
function useMessageScrollerCommands({ refs, commitScrollState, scheduleStateCommit, scheduleVisibilitySync }) {
|
|
9
|
+
const { streamingTurnRef, autoScrollRef, autoscrollingRef, autoscrollingTimeoutRef, contentRef, defaultScrollPositionAppliedRef, itemCountRef, messageElementsRef, modeRef, pendingScrollToMessageRef, prependRestoreRef, scrollMarginRef, scrollPreviousItemPeekRef, spacerGapRef, spacerHeightRef, spacerRef, viewportRef } = refs;
|
|
10
|
+
const setAutoScrolling = useCallback((autoscrolling) => {
|
|
11
|
+
if (autoscrollingTimeoutRef.current !== null) {
|
|
12
|
+
window.clearTimeout(autoscrollingTimeoutRef.current);
|
|
13
|
+
autoscrollingTimeoutRef.current = null;
|
|
14
|
+
}
|
|
15
|
+
if (autoscrollingRef.current !== autoscrolling) {
|
|
16
|
+
autoscrollingRef.current = autoscrolling;
|
|
17
|
+
commitScrollState();
|
|
18
|
+
}
|
|
19
|
+
if (autoscrolling) autoscrollingTimeoutRef.current = window.setTimeout(() => {
|
|
20
|
+
autoscrollingTimeoutRef.current = null;
|
|
21
|
+
autoscrollingRef.current = false;
|
|
22
|
+
commitScrollState();
|
|
23
|
+
}, 180);
|
|
24
|
+
}, [commitScrollState]);
|
|
25
|
+
const setTailSpacerHeight = useCallback((height) => {
|
|
26
|
+
const spacer = spacerRef.current;
|
|
27
|
+
if (!spacer) return;
|
|
28
|
+
const nextHeight = Math.max(0, Math.ceil(height));
|
|
29
|
+
if (spacerHeightRef.current === nextHeight) return;
|
|
30
|
+
spacerHeightRef.current = nextHeight;
|
|
31
|
+
spacer.hidden = nextHeight === 0;
|
|
32
|
+
spacer.style.height = `${nextHeight}px`;
|
|
33
|
+
spacer.style.marginTop = nextHeight > 0 ? `${-spacerGapRef.current}px` : "";
|
|
34
|
+
}, []);
|
|
35
|
+
const scrollToPosition = useCallback((scrollTop, { behavior = "auto", autoscrolling = false } = {}) => {
|
|
36
|
+
const viewport = viewportRef.current;
|
|
37
|
+
if (!viewport) return;
|
|
38
|
+
const nextScrollTop = Math.max(0, scrollTop);
|
|
39
|
+
if (Math.abs(viewport.scrollTop - nextScrollTop) <= .5) {
|
|
40
|
+
viewport.scrollTop = nextScrollTop;
|
|
41
|
+
commitScrollState();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (autoscrolling) setAutoScrolling(true);
|
|
45
|
+
viewport.scrollTo({
|
|
46
|
+
top: nextScrollTop,
|
|
47
|
+
behavior
|
|
48
|
+
});
|
|
49
|
+
scheduleStateCommit();
|
|
50
|
+
}, [
|
|
51
|
+
commitScrollState,
|
|
52
|
+
scheduleStateCommit,
|
|
53
|
+
setAutoScrolling
|
|
54
|
+
]);
|
|
55
|
+
const scrollToStart = useCallback(({ behavior = "auto" } = {}) => {
|
|
56
|
+
if (!viewportRef.current) return false;
|
|
57
|
+
setTailSpacerHeight(0);
|
|
58
|
+
streamingTurnRef.current = null;
|
|
59
|
+
modeRef.current = "free-scrolling";
|
|
60
|
+
scrollToPosition(0, { behavior });
|
|
61
|
+
scheduleVisibilitySync();
|
|
62
|
+
return true;
|
|
63
|
+
}, [
|
|
64
|
+
scheduleVisibilitySync,
|
|
65
|
+
scrollToPosition,
|
|
66
|
+
setTailSpacerHeight
|
|
67
|
+
]);
|
|
68
|
+
const scrollToEnd = useCallback(({ behavior = "auto" } = {}) => {
|
|
69
|
+
const viewport = viewportRef.current;
|
|
70
|
+
if (!viewport) return false;
|
|
71
|
+
setTailSpacerHeight(0);
|
|
72
|
+
streamingTurnRef.current = null;
|
|
73
|
+
modeRef.current = autoScrollRef.current ? "following-bottom" : "free-scrolling";
|
|
74
|
+
scrollToPosition(getMaxScrollTop(viewport), {
|
|
75
|
+
autoscrolling: true,
|
|
76
|
+
behavior
|
|
77
|
+
});
|
|
78
|
+
scheduleVisibilitySync();
|
|
79
|
+
return true;
|
|
80
|
+
}, [
|
|
81
|
+
scheduleVisibilitySync,
|
|
82
|
+
scrollToPosition,
|
|
83
|
+
setTailSpacerHeight
|
|
84
|
+
]);
|
|
85
|
+
const scrollToElement = useCallback((element, { align = "start", behavior = "auto", scrollMargin = scrollMarginRef.current } = {}, { keepPreviousPeek = false } = {}) => {
|
|
86
|
+
const content = contentRef.current;
|
|
87
|
+
const viewport = viewportRef.current;
|
|
88
|
+
if (!content || !viewport || !content.contains(element)) return false;
|
|
89
|
+
const scrollTop = getElementScrollTop({
|
|
90
|
+
align,
|
|
91
|
+
element,
|
|
92
|
+
scrollMargin: keepPreviousPeek ? scrollMargin + scrollPreviousItemPeekRef.current : scrollMargin,
|
|
93
|
+
spacer: spacerRef.current,
|
|
94
|
+
viewport
|
|
95
|
+
});
|
|
96
|
+
const nextSpacerHeight = getTailSpacerHeight({
|
|
97
|
+
content,
|
|
98
|
+
scrollTop,
|
|
99
|
+
spacer: spacerRef.current,
|
|
100
|
+
viewport
|
|
101
|
+
});
|
|
102
|
+
setTailSpacerHeight(nextSpacerHeight);
|
|
103
|
+
prependRestoreRef.current = {
|
|
104
|
+
element,
|
|
105
|
+
viewportTop: getElementViewportTop(element, viewport)
|
|
106
|
+
};
|
|
107
|
+
modeRef.current = keepPreviousPeek ? "anchored-to-message" : "settling-jump";
|
|
108
|
+
streamingTurnRef.current = keepPreviousPeek ? element : null;
|
|
109
|
+
scrollToPosition(scrollTop, { behavior });
|
|
110
|
+
scheduleVisibilitySync();
|
|
111
|
+
return true;
|
|
112
|
+
}, [
|
|
113
|
+
scheduleVisibilitySync,
|
|
114
|
+
scrollToPosition,
|
|
115
|
+
setTailSpacerHeight
|
|
116
|
+
]);
|
|
117
|
+
const reanchorToAnchoredMessage = useCallback(() => {
|
|
118
|
+
const element = streamingTurnRef.current;
|
|
119
|
+
if (!element || !element.isConnected || modeRef.current !== "anchored-to-message") return false;
|
|
120
|
+
return scrollToElement(element, { align: "start" }, { keepPreviousPeek: true });
|
|
121
|
+
}, [scrollToElement]);
|
|
122
|
+
const scrollToMessage = useCallback((messageId, options) => {
|
|
123
|
+
const element = messageElementsRef.current.get(messageId);
|
|
124
|
+
if (!element) {
|
|
125
|
+
if (itemCountRef.current === 0) {
|
|
126
|
+
pendingScrollToMessageRef.current = {
|
|
127
|
+
messageId,
|
|
128
|
+
options
|
|
129
|
+
};
|
|
130
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
136
|
+
if (scrollToElement(element, options)) {
|
|
137
|
+
pendingScrollToMessageRef.current = null;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
pendingScrollToMessageRef.current = {
|
|
141
|
+
messageId,
|
|
142
|
+
options
|
|
143
|
+
};
|
|
144
|
+
return true;
|
|
145
|
+
}, [scrollToElement]);
|
|
146
|
+
return {
|
|
147
|
+
flushPendingScrollToMessage: useCallback(() => {
|
|
148
|
+
const pending = pendingScrollToMessageRef.current;
|
|
149
|
+
if (!pending) return false;
|
|
150
|
+
const element = messageElementsRef.current.get(pending.messageId);
|
|
151
|
+
if (!element) return false;
|
|
152
|
+
if (!scrollToElement(element, pending.options)) return false;
|
|
153
|
+
pendingScrollToMessageRef.current = null;
|
|
154
|
+
defaultScrollPositionAppliedRef.current = true;
|
|
155
|
+
return true;
|
|
156
|
+
}, [scrollToElement]),
|
|
157
|
+
reanchorToAnchoredMessage,
|
|
158
|
+
scrollToElement,
|
|
159
|
+
scrollToEnd,
|
|
160
|
+
scrollToMessage,
|
|
161
|
+
scrollToStart
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
export { useMessageScrollerCommands };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MessageScrollerContextValue, MessageScrollerProviderProps, MessageScrollerRegisterMessage } from "../lib/message-scroller/types.mjs";
|
|
2
|
+
import { MessageScrollerRefs } from "./use-message-scroller-refs.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/use-message-scroller-controller.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* @since 0.5.0-canary.3
|
|
7
|
+
*/
|
|
8
|
+
declare function useMessageScrollerController({
|
|
9
|
+
autoScroll,
|
|
10
|
+
defaultScrollPosition,
|
|
11
|
+
scrollEdgeThreshold,
|
|
12
|
+
scrollPreviousItemPeek,
|
|
13
|
+
scrollMargin
|
|
14
|
+
}: MessageScrollerProviderProps): {
|
|
15
|
+
context: MessageScrollerContextValue;
|
|
16
|
+
registerMessage: MessageScrollerRegisterMessage;
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { type MessageScrollerRefs, useMessageScrollerController };
|