2
0

🚸 Improve chat auto scroll

Instead of scrolling all the way to the bottom on each bubble, now we scroll to the top of the last bubble

Closes #486
This commit is contained in:
Baptiste Arnaud
2023-05-12 16:37:04 -04:00
parent df8a406513
commit a3fb098dfa
10 changed files with 33 additions and 33 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.0.48", "version": "0.0.49",
"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

@ -15,7 +15,7 @@ type Props = Pick<ChatReply, 'messages' | 'input'> & {
hasError: boolean hasError: boolean
hideAvatar: boolean hideAvatar: boolean
onNewBubbleDisplayed: (blockId: string) => Promise<void> onNewBubbleDisplayed: (blockId: string) => Promise<void>
onScrollToBottom: () => void onScrollToBottom: (top?: number) => void
onSubmit: (input: string) => void onSubmit: (input: string) => void
onSkip: () => void onSkip: () => void
onAllBubblesDisplayed: () => void onAllBubblesDisplayed: () => void
@ -31,7 +31,7 @@ export const ChatChunk = (props: Props) => {
props.onScrollToBottom() props.onScrollToBottom()
}) })
const displayNextMessage = async () => { const displayNextMessage = async (bubbleOffsetTop?: number) => {
const lastBubbleBlockId = props.messages[displayedMessageIndex()].id const lastBubbleBlockId = props.messages[displayedMessageIndex()].id
await props.onNewBubbleDisplayed(lastBubbleBlockId) await props.onNewBubbleDisplayed(lastBubbleBlockId)
setDisplayedMessageIndex( setDisplayedMessageIndex(
@ -39,7 +39,7 @@ export const ChatChunk = (props: Props) => {
? displayedMessageIndex() ? displayedMessageIndex()
: displayedMessageIndex() + 1 : displayedMessageIndex() + 1
) )
props.onScrollToBottom() props.onScrollToBottom(bubbleOffsetTop)
if (displayedMessageIndex() === props.messages.length) { if (displayedMessageIndex() === props.messages.length) {
props.onAllBubblesDisplayed() props.onAllBubblesDisplayed()
} }

View File

@ -44,7 +44,6 @@ type Props = {
export const ConversationContainer = (props: Props) => { export const ConversationContainer = (props: Props) => {
let chatContainer: HTMLDivElement | undefined let chatContainer: HTMLDivElement | undefined
let bottomSpacer: HTMLDivElement | undefined
const [chatChunks, setChatChunks] = createSignal< const [chatChunks, setChatChunks] = createSignal<
Pick<ChatReply, 'messages' | 'input' | 'clientSideActions'>[] Pick<ChatReply, 'messages' | 'input' | 'clientSideActions'>[]
>([ >([
@ -153,10 +152,9 @@ export const ConversationContainer = (props: Props) => {
]) ])
} }
const autoScrollToBottom = () => { const autoScrollToBottom = (offsetTop?: number) => {
if (!bottomSpacer) return
setTimeout(() => { setTimeout(() => {
chatContainer?.scrollTo(0, chatContainer.scrollHeight) chatContainer?.scrollTo(0, offsetTop ?? chatContainer.scrollHeight)
}, 50) }, 50)
} }
@ -227,14 +225,11 @@ export const ConversationContainer = (props: Props) => {
</div> </div>
)} )}
</Show> </Show>
<BottomSpacer ref={bottomSpacer} /> <BottomSpacer />
</div> </div>
) )
} }
type BottomSpacerProps = { const BottomSpacer = () => {
ref: HTMLDivElement | undefined return <div class="w-full h-32 flex-shrink-0" />
}
const BottomSpacer = (props: BottomSpacerProps) => {
return <div ref={props.ref} class="w-full h-32 flex-shrink-0" />
} }

View File

@ -18,12 +18,12 @@ import { Match, Switch } from 'solid-js'
type Props = { type Props = {
message: ChatMessage message: ChatMessage
typingEmulation: TypingEmulation typingEmulation: TypingEmulation
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
export const HostBubble = (props: Props) => { export const HostBubble = (props: Props) => {
const onTransitionEnd = () => { const onTransitionEnd = (offsetTop?: number) => {
props.onTransitionEnd() props.onTransitionEnd(offsetTop)
} }
return ( return (

View File

@ -4,7 +4,7 @@ import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = { type Props = {
url: AudioBubbleContent['url'] url: AudioBubbleContent['url']
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
const showAnimationDuration = 400 const showAnimationDuration = 400
@ -13,13 +13,14 @@ const typingDuration = 500
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
export const AudioBubble = (props: Props) => { export const AudioBubble = (props: Props) => {
let ref: HTMLDivElement | undefined
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
onMount(() => { onMount(() => {
typingTimeout = setTimeout(() => { typingTimeout = setTimeout(() => {
setIsTyping(false) setIsTyping(false)
setTimeout(() => { setTimeout(() => {
props.onTransitionEnd() props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration) }, showAnimationDuration)
}, typingDuration) }, typingDuration)
}) })
@ -29,7 +30,7 @@ export const AudioBubble = (props: Props) => {
}) })
return ( return (
<div class="flex flex-col animate-fade-in"> <div class="flex flex-col animate-fade-in" ref={ref}>
<div class="flex w-full items-center"> <div class="flex w-full items-center">
<div class={'flex relative z-10 items-start typebot-host-bubble'}> <div class={'flex relative z-10 items-start typebot-host-bubble'}>
<div <div

View File

@ -4,7 +4,7 @@ import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = { type Props = {
content: EmbedBubbleContent content: EmbedBubbleContent
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
@ -12,13 +12,14 @@ let typingTimeout: NodeJS.Timeout
export const showAnimationDuration = 400 export const showAnimationDuration = 400
export const EmbedBubble = (props: Props) => { export const EmbedBubble = (props: Props) => {
let ref: HTMLDivElement | undefined
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
onMount(() => { onMount(() => {
typingTimeout = setTimeout(() => { typingTimeout = setTimeout(() => {
setIsTyping(false) setIsTyping(false)
setTimeout(() => { setTimeout(() => {
props.onTransitionEnd() props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration) }, showAnimationDuration)
}, 2000) }, 2000)
}) })
@ -28,7 +29,7 @@ export const EmbedBubble = (props: Props) => {
}) })
return ( return (
<div class="flex flex-col w-full animate-fade-in"> <div class="flex flex-col w-full animate-fade-in" ref={ref}>
<div class="flex w-full items-center"> <div class="flex w-full items-center">
<div <div
class={'flex relative z-10 items-start typebot-host-bubble w-full'} class={'flex relative z-10 items-start typebot-host-bubble w-full'}

View File

@ -4,7 +4,7 @@ import { createSignal, onCleanup, onMount } from 'solid-js'
type Props = { type Props = {
content: ImageBubbleContent content: ImageBubbleContent
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
export const showAnimationDuration = 400 export const showAnimationDuration = 400
@ -14,6 +14,7 @@ export const mediaLoadingFallbackTimeout = 5000
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
export const ImageBubble = (props: Props) => { export const ImageBubble = (props: Props) => {
let ref: HTMLDivElement | undefined
let image: HTMLImageElement | undefined let image: HTMLImageElement | undefined
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
@ -21,7 +22,7 @@ export const ImageBubble = (props: Props) => {
if (!isTyping()) return if (!isTyping()) return
setIsTyping(false) setIsTyping(false)
setTimeout(() => { setTimeout(() => {
props.onTransitionEnd() props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration) }, showAnimationDuration)
} }
@ -56,7 +57,7 @@ export const ImageBubble = (props: Props) => {
) )
return ( return (
<div class="flex flex-col animate-fade-in"> <div class="flex flex-col animate-fade-in" ref={ref}>
<div class="flex w-full items-center"> <div class="flex w-full items-center">
<div class={'flex relative z-10 items-start typebot-host-bubble'}> <div class={'flex relative z-10 items-start typebot-host-bubble'}>
<div <div

View File

@ -8,7 +8,7 @@ import { computePlainText } from '../helpers/convertRichTextToPlainText'
type Props = { type Props = {
content: TextBubbleContent content: TextBubbleContent
typingEmulation: TypingEmulation typingEmulation: TypingEmulation
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
export const showAnimationDuration = 400 export const showAnimationDuration = 400
@ -22,13 +22,14 @@ const defaultTypingEmulation = {
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
export const TextBubble = (props: Props) => { export const TextBubble = (props: Props) => {
let ref: HTMLDivElement | undefined
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
const onTypingEnd = () => { const onTypingEnd = () => {
if (!isTyping()) return if (!isTyping()) return
setIsTyping(false) setIsTyping(false)
setTimeout(() => { setTimeout(() => {
props.onTransitionEnd() props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration) }, showAnimationDuration)
} }
@ -50,7 +51,7 @@ export const TextBubble = (props: Props) => {
}) })
return ( return (
<div class="flex flex-col animate-fade-in"> <div class="flex flex-col animate-fade-in" ref={ref}>
<div class="flex w-full items-center"> <div class="flex w-full items-center">
<div class="flex relative items-start typebot-host-bubble"> <div class="flex relative items-start typebot-host-bubble">
<div <div

View File

@ -5,7 +5,7 @@ import { createSignal, Match, onCleanup, onMount, Switch } from 'solid-js'
type Props = { type Props = {
content: VideoBubbleContent content: VideoBubbleContent
onTransitionEnd: () => void onTransitionEnd: (offsetTop?: number) => void
} }
export const showAnimationDuration = 400 export const showAnimationDuration = 400
@ -13,13 +13,14 @@ export const showAnimationDuration = 400
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
export const VideoBubble = (props: Props) => { export const VideoBubble = (props: Props) => {
let ref: HTMLDivElement | undefined
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
const onTypingEnd = () => { const onTypingEnd = () => {
if (!isTyping()) return if (!isTyping()) return
setIsTyping(false) setIsTyping(false)
setTimeout(() => { setTimeout(() => {
props.onTransitionEnd() props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration) }, showAnimationDuration)
} }
@ -32,7 +33,7 @@ export const VideoBubble = (props: Props) => {
}) })
return ( return (
<div class="flex flex-col animate-fade-in"> <div class="flex flex-col animate-fade-in" ref={ref}>
<div class="flex w-full items-center"> <div class="flex w-full items-center">
<div class={'flex relative z-10 items-start typebot-host-bubble'}> <div class={'flex relative z-10 items-start typebot-host-bubble'}>
<div <div

View File

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