Skip to content

Commit

Permalink
fix: fix scroll to bottom via querySelector instead of ref (#2918)
Browse files Browse the repository at this point in the history
## Description
* use querySelector instead of ref for checking when to scroll to bottom
  • Loading branch information
vanbasten17 authored Oct 4, 2024
1 parent 1339085 commit fda6f6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/botonic-react/src/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { BotonicContainerId } from '../webchat/constants'
export const getWebchatElement = host =>
host && host.querySelector(`#${BotonicContainerId.Webchat}`)

export const getScrollableMessagesListElement = host =>
host && host.querySelector(`#${BotonicContainerId.ScrollableMessagesList}`)

// https://stackoverflow.com/questions/9457891/how-to-detect-if-domcontentloaded-was-fired
export const onDOMLoaded = callback => {
if (/complete|interactive|loaded/.test(document.readyState)) {
Expand Down
21 changes: 9 additions & 12 deletions packages/botonic-react/src/webchat/hooks/use-scroll-to-bottom.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { useContext } from 'react'

import { WebchatContext } from '../../contexts'
import { getWebchatElement } from '../../util/dom'
import {
getScrollableMessagesListElement,
getWebchatElement,
} from '../../util/dom'

export const useScrollToBottom = ({
host,
behavior = 'smooth',
timeout = 200,
}) => {
const {
webchatState: { isWebchatOpen },
scrollableMessagesListRef,
} = useContext(WebchatContext)

const scrollToBottom = () => {
const webchatElement = getWebchatElement(host)
if (!webchatElement) return
if (!isWebchatOpen) return
const scrollableMessagesListElement = getScrollableMessagesListElement(host)
if (!scrollableMessagesListElement) return

setTimeout(() => {
scrollableMessagesListRef.current?.scrollTo({
top: scrollableMessagesListRef.current?.scrollHeight,
scrollableMessagesListElement.scrollTo({
top: scrollableMessagesListElement.scrollHeight,
behavior: behavior as ScrollBehavior,
})
}, timeout)
Expand Down

0 comments on commit fda6f6f

Please sign in to comment.