Skip to content

Commit

Permalink
scroll chat down when non user scroll action (#2315)
Browse files Browse the repository at this point in the history
* scroll chat down when non user scroll action
resolves #2281

* fix test

---------

Co-authored-by: Fred Lefévère-Laoide <[email protected]>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide authored Dec 12, 2024
1 parent bf6c3a4 commit 2e8c6ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

0 comments on commit 2e8c6ba

Please sign in to comment.