2
0

🚸 Fix auto scroll behavior

Based on bottom of last element was a bad idea. Now it is based on
scroll position + some tolerance computed based on screen height
This commit is contained in:
Baptiste Arnaud
2024-04-25 08:40:12 +02:00
parent a7fc41316c
commit 0a7d598a35
4 changed files with 24 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.2.80", "version": "0.2.81",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -34,6 +34,9 @@ import { saveClientLogsQuery } from '@/queries/saveClientLogsQuery'
import { HTTPError } from 'ky' import { HTTPError } from 'ky'
import { persist } from '@/utils/persist' import { persist } from '@/utils/persist'
const autoScrollBottomToleranceScreenPercent = 0.6
const bottomSpacerHeight = 128
const parseDynamicTheme = ( const parseDynamicTheme = (
initialTheme: Theme, initialTheme: Theme,
dynamicTheme: ContinueChatResponse['dynamicTheme'] dynamicTheme: ContinueChatResponse['dynamicTheme']
@@ -226,21 +229,23 @@ export const ConversationContainer = (props: Props) => {
const autoScrollToBottom = (lastElement?: HTMLDivElement, offset = 0) => { const autoScrollToBottom = (lastElement?: HTMLDivElement, offset = 0) => {
if (!chatContainer) return if (!chatContainer) return
if (!lastElement) {
setTimeout(() => { const bottomTolerance =
chatContainer?.scrollTo(0, chatContainer.scrollHeight) chatContainer.clientHeight * autoScrollBottomToleranceScreenPercent -
}, 50) bottomSpacerHeight
return
}
const lastElementRect = lastElement.getBoundingClientRect()
const containerRect = chatContainer.getBoundingClientRect()
const isBottomOfLastElementInView = const isBottomOfLastElementInView =
lastElementRect.top + lastElementRect.height < containerRect.height chatContainer.scrollTop + chatContainer.clientHeight >=
chatContainer.scrollHeight - bottomTolerance
if (isBottomOfLastElementInView) { if (isBottomOfLastElementInView) {
setTimeout(() => { setTimeout(() => {
chatContainer?.scrollTo(0, lastElement.offsetTop - offset) chatContainer?.scrollTo(
0,
lastElement
? lastElement.offsetTop - offset
: chatContainer.scrollHeight
)
}, 50) }, 50)
} }
} }
@@ -350,5 +355,10 @@ export const ConversationContainer = (props: Props) => {
} }
const BottomSpacer = () => { const BottomSpacer = () => {
return <div class="w-full h-32 flex-shrink-0" /> return (
<div
class="w-full flex-shrink-0"
style={{ height: bottomSpacerHeight + 'px' }}
/>
)
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/nextjs", "name": "@typebot.io/nextjs",
"version": "0.2.80", "version": "0.2.81",
"description": "Convenient library to display typebots on your Next.js website", "description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.2.80", "version": "0.2.81",
"description": "Convenient library to display typebots on your React app", "description": "Convenient library to display typebots on your React app",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",