feat: Improve scrolling for last message on agent side (#2421)

* Improve chat widget scrolling

* refactor the class names to snake-case

* refactor the scrollTop calculations to a helper

* Add tests for scrollTopCalculationHelper

Co-authored-by: Nithin David Thomas <webofnithin@gmail.com>
Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
This commit is contained in:
Anubhav Jain
2021-06-18 20:12:43 +05:30
committed by GitHub
parent 6c49e58ff8
commit e45abebe39
3 changed files with 56 additions and 1 deletions

View File

@@ -0,0 +1,18 @@
const totalMessageHeight = (total, element) => {
return total + element.scrollHeight;
};
export const calculateScrollTop = (
conversationPanelHeight,
parentHeight,
relevantMessages
) => {
// add up scrollHeight of a `relevantMessages`
let combinedMessageScrollHeight = [...relevantMessages].reduce(
totalMessageHeight,
0
);
return (
conversationPanelHeight - combinedMessageScrollHeight - parentHeight / 2
);
};

View File

@@ -0,0 +1,18 @@
import { calculateScrollTop } from '../scrollTopCalculationHelper';
describe('#calculateScrollTop', () => {
it('returns calculated value of the scrollTop property', () => {
class DOMElement {
constructor(scrollHeight) {
this.scrollHeight = scrollHeight;
}
}
let count = 3;
let relevantMessages = [];
while (count > 0) {
relevantMessages.push(new DOMElement(100));
count -= 1;
}
expect(calculateScrollTop(1000, 300, relevantMessages)).toEqual(550);
});
});