Skip to content

Commit

Permalink
🚸 Fix auto scroll behavior
Browse files Browse the repository at this point in the history
Based on bottom of last element was a bad idea. Now it is based on
scroll position + some tolerance computed based on screen height
  • Loading branch information
baptisteArno committed Apr 25, 2024
1 parent a7fc413 commit 0a7d598
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.2.80",
"version": "0.2.81",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import { saveClientLogsQuery } from '@/queries/saveClientLogsQuery'
import { HTTPError } from 'ky'
import { persist } from '@/utils/persist'

const autoScrollBottomToleranceScreenPercent = 0.6
const bottomSpacerHeight = 128

const parseDynamicTheme = (
initialTheme: Theme,
dynamicTheme: ContinueChatResponse['dynamicTheme']
Expand Down Expand Up @@ -226,21 +229,23 @@ export const ConversationContainer = (props: Props) => {

const autoScrollToBottom = (lastElement?: HTMLDivElement, offset = 0) => {
if (!chatContainer) return
if (!lastElement) {
setTimeout(() => {
chatContainer?.scrollTo(0, chatContainer.scrollHeight)
}, 50)
return
}
const lastElementRect = lastElement.getBoundingClientRect()
const containerRect = chatContainer.getBoundingClientRect()

const bottomTolerance =
chatContainer.clientHeight * autoScrollBottomToleranceScreenPercent -
bottomSpacerHeight

const isBottomOfLastElementInView =
lastElementRect.top + lastElementRect.height < containerRect.height
chatContainer.scrollTop + chatContainer.clientHeight >=
chatContainer.scrollHeight - bottomTolerance

if (isBottomOfLastElementInView) {
setTimeout(() => {
chatContainer?.scrollTo(0, lastElement.offsetTop - offset)
chatContainer?.scrollTo(
0,
lastElement
? lastElement.offsetTop - offset
: chatContainer.scrollHeight
)
}, 50)
}
}
Expand Down Expand Up @@ -350,5 +355,10 @@ export const ConversationContainer = (props: Props) => {
}

const BottomSpacer = () => {
return <div class="w-full h-32 flex-shrink-0" />
return (
<div
class="w-full flex-shrink-0"
style={{ height: bottomSpacerHeight + 'px' }}
/>
)
}
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.2.80",
"version": "0.2.81",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.2.80",
"version": "0.2.81",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 0a7d598

Please sign in to comment.