Visitar URL original
fix: fix outer-page-scrolling-bug · hubtype/botonic@d16f43f · GitHub
Skip to content

Commit d16f43f

Browse files
committed
fix: fix outer-page-scrolling-bug
1 parent 0f4e6f7 commit d16f43f

6 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎packages/botonic-react/src/utils.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ export const _getThemeProperty = theme => (
6464

6565
export const ConditionalWrapper = ({ condition, wrapper, children }) =>
6666
condition ? wrapper(children) : children
67+
68+
export const scrollToBottom = (timeout = 100) => {
69+
const botonicScrollableArea = document.getElementById(
70+
'botonic-scrollable-content'
71+
)
72+
const frame =
73+
botonicScrollableArea &&
74+
botonicScrollableArea.querySelectorAll('.simplebar-content-wrapper')[0]
75+
if (frame) {
76+
frame.scrollTop = frame.scrollHeight
77+
setTimeout(() => (frame.scrollTop = frame.scrollHeight), timeout)
78+
}
79+
}

‎packages/botonic-react/src/webchat/components/styled-scrollbar.jsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { COLORS } from '../../constants'
22
import styled, { css } from 'styled-components'
33
import SimpleBar from 'simplebar-react'
44
import 'simplebar/dist/simplebar.css'
5+
import './styled-scrollbar.scss'
56

67
export const StyledScrollbar = styled(SimpleBar)`
78
${props =>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#botonic-scrollable-content
2+
.simplebar-mask
3+
.simplebar-offset
4+
.simplebar-content-wrapper {
5+
scroll-behavior: smooth;
6+
}

‎packages/botonic-react/src/webchat/hooks.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useReducer, useState, useRef } from 'react'
22
import { WEBCHAT, COLORS } from '../constants'
3+
import { scrollToBottom } from '../utils'
34

45
export const webchatInitialState = {
56
width: WEBCHAT.DEFAULTS.WIDTH,
@@ -191,11 +192,7 @@ export function useWebchat() {
191192
export function useTyping({ webchatState, updateTyping, updateMessage }) {
192193
useEffect(() => {
193194
let delayTimeout, typingTimeout
194-
const end = document.getElementById('messages-end')
195-
if (end) {
196-
end.scrollIntoView({ behavior: 'smooth' })
197-
setTimeout(() => end.scrollIntoView({ behavior: 'smooth' }), 100)
198-
}
195+
scrollToBottom()
199196
try {
200197
const nextMsg = webchatState.messagesJSON.filter(m => !m.display)[0]
201198
if (nextMsg.delay && nextMsg.typing) {

‎packages/botonic-react/src/webchat/message-list.jsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const WebchatMessageList = props => {
4242

4343
return (
4444
<StyledScrollbar
45+
id='botonic-scrollable-content'
4546
scrollbar={scrollbarOptions}
4647
autoHide={scrollbarOptions.autoHide}
4748
ismessagescontainer='true'

‎packages/botonic-react/src/webchat/webchat.jsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
staticAsset,
3333
_getThemeProperty,
3434
ConditionalWrapper,
35+
scrollToBottom,
3536
} from '../utils'
3637
import { WEBCHAT, MIME_WHITELIST, COLORS } from '../constants'
3738
import { motion } from 'framer-motion'
@@ -191,12 +192,7 @@ export const Webchat = forwardRef((props, ref) => {
191192

192193
useEffect(() => {
193194
if (!webchatState.isWebchatOpen) return
194-
setTimeout(() => {
195-
const end = document.getElementById('messages-end')
196-
if (end) {
197-
end.scrollIntoView()
198-
}
199-
})
195+
scrollToBottom()
200196
}, [webchatState.isWebchatOpen])
201197

202198
useEffect(() => {
@@ -484,7 +480,6 @@ export const Webchat = forwardRef((props, ref) => {
484480
messages={webchatState.messagesComponents}
485481
>
486482
{webchatState.typing && <TypingIndicator />}
487-
<div id='messages-end' />
488483
</WebchatMessageList>
489484
)
490485
const webchatReplies = () => <WebchatReplies replies={webchatState.replies} />

0 commit comments

Comments
 (0)