Skip to content

Commit c622c91

Browse files
committed
Added smooth scrolling option
1 parent 2af96cf commit c622c91

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/ChatBot.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ChatBot extends Component {
2929
this.content = null;
3030
this.input = null;
3131

32+
this.supportsScrollBehavior = false;
33+
3234
this.setContentRef = element => {
3335
this.content = element;
3436
};
@@ -110,6 +112,8 @@ class ChatBot extends Component {
110112
);
111113
}
112114

115+
this.supportsScrollBehavior = 'scrollBehavior' in document.documentElement.style;
116+
113117
if (this.content) {
114118
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
115119
window.addEventListener('resize', this.onResize);
@@ -163,7 +167,18 @@ class ChatBot extends Component {
163167
}
164168

165169
onNodeInserted = event => {
166-
event.currentTarget.scrollTop = event.currentTarget.scrollHeight;
170+
const { currentTarget: target } = event;
171+
const { enableSmoothScroll } = this.props;
172+
173+
if (enableSmoothScroll && this.supportsScrollBehavior) {
174+
target.scroll({
175+
top: target.scrollHeight,
176+
left: 0,
177+
behavior: 'smooth'
178+
});
179+
} else {
180+
target.scrollTop = target.scrollHeight;
181+
}
167182
};
168183

169184
onResize = () => {
@@ -698,6 +713,7 @@ ChatBot.propTypes = {
698713
customDelay: PropTypes.number,
699714
customStyle: PropTypes.objectOf(PropTypes.any),
700715
enableMobileAutoFocus: PropTypes.bool,
716+
enableSmoothScroll: PropTypes.bool,
701717
floating: PropTypes.bool,
702718
floatingIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
703719
floatingStyle: PropTypes.objectOf(PropTypes.any),
@@ -746,6 +762,7 @@ ChatBot.defaultProps = {
746762
customStyle: {},
747763
customDelay: 1000,
748764
enableMobileAutoFocus: false,
765+
enableSmoothScroll: false,
749766
floating: false,
750767
floatingIcon: <ChatIcon />,
751768
floatingStyle: {},

0 commit comments

Comments
 (0)