Skip to content

Commit

Permalink
馃毟 Improve auto scroll behavior
Browse files Browse the repository at this point in the history
Now only based on bottom of last element if it is in view
  • Loading branch information
baptisteArno committed Apr 24, 2024
1 parent 873ba0b commit a7fc413
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 18 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.79",
"version": "0.2.80",
"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 @@ -234,17 +234,15 @@ export const ConversationContainer = (props: Props) => {
}
const lastElementRect = lastElement.getBoundingClientRect()
const containerRect = chatContainer.getBoundingClientRect()
const lastElementTopRelative =
lastElementRect.top - containerRect.top + chatContainer.scrollTop

const isLastElementInVisibleArea =
lastElementTopRelative < chatContainer.scrollTop + containerRect.height &&
lastElementTopRelative + lastElementRect.height > chatContainer.scrollTop
const isBottomOfLastElementInView =
lastElementRect.top + lastElementRect.height < containerRect.height

if (isLastElementInVisibleArea)
if (isBottomOfLastElementInView) {
setTimeout(() => {
chatContainer?.scrollTo(0, lastElement.offsetTop - offset)
}, 50)
}
}

const handleAllBubblesDisplayed = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const Buttons = (props: Props) => {
const [filteredItems, setFilteredItems] = createSignal(props.defaultItems)

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
})

const handleClick = (itemIndex: number) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const MultipleChoicesForm = (props: Props) => {
const [selectedItemIds, setSelectedItemIds] = createSignal<string[]>([])

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
})

const handleClick = (itemId: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const EmailInput = (props: Props) => {
}

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
window.addEventListener('message', processIncomingEvent)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const NumberInput = (props: NumberInputProps) => {
}

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
window.addEventListener('message', processIncomingEvent)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const PhoneInput = (props: PhoneInputProps) => {
}

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
window.addEventListener('message', processIncomingEvent)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const MultiplePictureChoice = (props: Props) => {
const [totalLoadedImages, setTotalLoadedImages] = createSignal(0)

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
})

const handleClick = (itemId: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const SinglePictureChoice = (props: Props) => {
const [totalLoadedImages, setTotalLoadedImages] = createSignal(0)

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true })
})

const handleClick = (itemIndex: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const TextInput = (props: Props) => {
}

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef)
inputRef.focus({
preventScroll: true,
})
window.addEventListener('message', processIncomingEvent)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const UrlInput = (props: Props) => {
}

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
if (!isMobile() && inputRef)
inputRef.focus({
preventScroll: true,
})
window.addEventListener('message', processIncomingEvent)
})

Expand Down
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.79",
"version": "0.2.80",
"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.79",
"version": "0.2.80",
"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 a7fc413

Please sign in to comment.