Skip to content

Commit

Permalink
fix: ChatGPTNextWeb#244 better scroll ux
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Mar 30, 2023
1 parent 7827b40 commit 7599ae3
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function ChatList() {
state.currentSessionIndex,
state.selectSession,
state.removeSession,
]
],
);

return (
Expand Down Expand Up @@ -196,7 +196,7 @@ export function Chat(props: {
setPromptHints(promptStore.search(text));
},
100,
{ leading: true, trailing: true }
{ leading: true, trailing: true },
);

const onPromptSelect = (prompt: Prompt) => {
Expand All @@ -210,7 +210,7 @@ export function Chat(props: {
if (!dom) return;
const paddingBottomNum: number = parseInt(
window.getComputedStyle(dom).paddingBottom,
10
10,
);
dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum;
};
Expand Down Expand Up @@ -300,7 +300,7 @@ export function Chat(props: {
preview: true,
},
]
: []
: [],
)
.concat(
userInput.length > 0
Expand All @@ -312,14 +312,24 @@ export function Chat(props: {
preview: true,
},
]
: []
: [],
);

// auto scroll
useLayoutEffect(() => {
setTimeout(() => {
const dom = latestMessageRef.current;
if (dom && !isIOS() && autoScroll) {
const inputDom = inputRef.current;

// only scroll when input overlaped message body
let shouldScroll = true;
if (dom && inputDom) {
const domRect = dom.getBoundingClientRect();
const inputRect = inputDom.getBoundingClientRect();
shouldScroll = domRect.top > inputRect.top;
}

if (dom && autoScroll && shouldScroll) {
dom.scrollIntoView({
block: "end",
});
Expand All @@ -340,7 +350,7 @@ export function Chat(props: {
const newTopic = prompt(Locale.Chat.Rename, session.topic);
if (newTopic && newTopic !== session.topic) {
chatStore.updateCurrentSession(
(session) => (session.topic = newTopic!)
(session) => (session.topic = newTopic!),
);
}
}}
Expand Down Expand Up @@ -586,7 +596,7 @@ export function Home() {
state.newSession,
state.currentSessionIndex,
state.removeSession,
]
],
);
const loading = !useHasHydrated();
const [showSideBar, setShowSideBar] = useState(true);
Expand Down

0 comments on commit 7599ae3

Please sign in to comment.