Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scroll chat down when non user scroll action #2315

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/taipy-gui/src/components/Taipy/Chat.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe("Chat Component", () => {
});
jest.restoreAllMocks();
});
it("Not upload image over a file size limit", async () => {
it("does not upload image over a file size limit", async () => {
const dispatch = jest.fn();
const state: TaipyState = INITIAL_STATE;
const { getByText, getByAltText } = render(
Expand Down
13 changes: 10 additions & 3 deletions frontend/taipy-gui/src/components/Taipy/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, {
ReactNode,
lazy,
ChangeEvent,
UIEvent,
} from "react";
import { SxProps, Theme, darken, lighten } from "@mui/material/styles";
import Avatar from "@mui/material/Avatar";
Expand Down Expand Up @@ -250,6 +251,7 @@ const Chat = (props: ChatProps) => {
const [imagePreview, setImagePreview] = useState<string | null>(null);
const [objectURLs, setObjectURLs] = useState<string[]>([]);
const fileInputRef = useRef<HTMLInputElement>(null);
const userScrolled = useRef(false);

const className = useClassNames(props.libClassName, props.dynamicClassName, props.className);
const active = useDynamicProperty(props.active, props.defaultActive, true);
Expand Down Expand Up @@ -424,7 +426,7 @@ const Chat = (props: ChatProps) => {
);

const showBottom = useCallback(() => {
anchorDivRef.current?.scrollIntoView();
anchorDivRef.current?.scrollIntoView && anchorDivRef.current?.scrollIntoView();
setShowMessage(false);
}, []);

Expand All @@ -450,8 +452,9 @@ const Chat = (props: ChatProps) => {
}
}
page.current.key = getChatKey(0, pageSize);
!userScrolled.current && showBottom();
}
}, [refresh, pageSize, props.messages]);
}, [refresh, pageSize, props.messages, showBottom]);

useEffect(() => {
if (showMessage && !isAnchorDivVisible) {
Expand Down Expand Up @@ -490,10 +493,14 @@ const Chat = (props: ChatProps) => {
[loadMoreItems]
);

const handleOnScroll = useCallback((evt: UIEvent) => {
userScrolled.current = (evt.target as HTMLDivElement).scrollHeight - (evt.target as HTMLDivElement).offsetHeight - (evt.target as HTMLDivElement).scrollTop > 1;
}, []);

return (
<Tooltip title={hover || ""}>
<Paper className={`${className} ${getComponentClassName(props.children)}`} sx={boxSx} id={id}>
<Grid container rowSpacing={2} sx={gridSx} ref={scrollDivRef}>
<Grid container rowSpacing={2} sx={gridSx} ref={scrollDivRef} onScroll={handleOnScroll}>
{rows.length && !rows[0] ? (
<Grid className={getSuffixedClassNames(className, "-load")} size={12} sx={noAnchorSx}>
<Box sx={loadMoreSx}>
Expand Down
Loading