Skip to content

Commit

Permalink
Merge pull request #197 from pascalgagneur/smoot-scrolling
Browse files Browse the repository at this point in the history
Added smooth scrolling option
  • Loading branch information
LucasBassetti committed Apr 3, 2019
2 parents 0c151f1 + c622c91 commit 9ca3b22
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/ChatBot.jsx
Expand Up @@ -29,6 +29,8 @@ class ChatBot extends Component {
this.content = null;
this.input = null;

this.supportsScrollBehavior = false;

this.setContentRef = element => {
this.content = element;
};
Expand Down Expand Up @@ -110,6 +112,8 @@ class ChatBot extends Component {
);
}

this.supportsScrollBehavior = 'scrollBehavior' in document.documentElement.style;

if (this.content) {
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
window.addEventListener('resize', this.onResize);
Expand Down Expand Up @@ -163,7 +167,18 @@ class ChatBot extends Component {
}

onNodeInserted = event => {
event.currentTarget.scrollTop = event.currentTarget.scrollHeight;
const { currentTarget: target } = event;
const { enableSmoothScroll } = this.props;

if (enableSmoothScroll && this.supportsScrollBehavior) {
target.scroll({
top: target.scrollHeight,
left: 0,
behavior: 'smooth'
});
} else {
target.scrollTop = target.scrollHeight;
}
};

onResize = () => {
Expand Down Expand Up @@ -698,6 +713,7 @@ ChatBot.propTypes = {
customDelay: PropTypes.number,
customStyle: PropTypes.objectOf(PropTypes.any),
enableMobileAutoFocus: PropTypes.bool,
enableSmoothScroll: PropTypes.bool,
floating: PropTypes.bool,
floatingIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
floatingStyle: PropTypes.objectOf(PropTypes.any),
Expand Down Expand Up @@ -746,6 +762,7 @@ ChatBot.defaultProps = {
customStyle: {},
customDelay: 1000,
enableMobileAutoFocus: false,
enableSmoothScroll: false,
floating: false,
floatingIcon: <ChatIcon />,
floatingStyle: {},
Expand Down

0 comments on commit 9ca3b22

Please sign in to comment.