Skip to content

Commit

Permalink
fix: scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Jul 28, 2023
1 parent 15545de commit 7d67ba7
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type MessagesProps = {

const MessageElement: React.FC<Message> = ({ content, additionalContent, isUser, onTypingEnd }) => {
const [showAdditionalContent, setShowAdditionalContent] = useState(false)
const messageRef = useRef<HTMLDivElement>(null)

const onInit = useCallback((typewriter: TypewriterClass) => {
typewriter
Expand All @@ -53,9 +54,18 @@ const MessageElement: React.FC<Message> = ({ content, additionalContent, isUser,
.start()
}, [])

useEffect(() => {
if (messageRef?.current) {
messageRef.current.scrollIntoView({
behavior: 'smooth',
})
}
}, [messageRef?.current])

if (isUser) {
return (
<HStack
ref={messageRef}
paddingBlock={4}
paddingInline={{
base: 2,
Expand Down Expand Up @@ -100,6 +110,7 @@ const MessageElement: React.FC<Message> = ({ content, additionalContent, isUser,

return (
<HStack
ref={messageRef}
paddingBlock={4}
paddingInline={{
base: 2,
Expand Down Expand Up @@ -188,13 +199,8 @@ function App() {
const [searchParams,] = useSearchParams()
const isLoop = useMemo(() => searchParams.get('loop') === 'true', [searchParams])
const isQr = useMemo(() => searchParams.get('qr') === 'true', [searchParams])
const messagesEndRef = useRef<HTMLDivElement>(null)

const scrollChat = useCallback(() => {
if (messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({ behavior: 'smooth' })
}
}, [])
console.log(searchParams)

const onInit = useCallback((typewriter: TypewriterClass) => {
typewriter
Expand Down Expand Up @@ -448,12 +454,6 @@ function App() {
.start()
}, [isLoop])

useEffect(() => {
if (messages.length > 1) {
scrollChat()
}
}, [messages, scrollChat])

useEffect(() => {
if (isQr) {
// if the page is loaded from the link in the QR code, log it to analytics
Expand Down Expand Up @@ -530,7 +530,6 @@ function App() {
</Box>
)}
</VStack>
<div ref={messagesEndRef} />
<VStack>
<Heading as='h2' textAlign='center'>
Interested in using Chat IoT for your business?
Expand Down

0 comments on commit 7d67ba7

Please sign in to comment.