@helpscout/hsds-react
Version:
React component library for Help Scout's Design System
57 lines (50 loc) • 1.72 kB
JavaScript
exports.__esModule = true;
exports.allDefined = allDefined;
exports.shouldAutoScroll = shouldAutoScroll;
exports.getScrollProps = getScrollProps;
function allDefined(objs) {
if (!objs) return false;
var props = Array.isArray(objs) ? objs : Object.values(objs); // TODO: change when the other PR is merged for isNil
// return props.filter(p => !isNil(p)).length === props.length
return props.filter(function (p) {
return p != null;
}).length === props.length;
}
/**
* Determines if the ChatScroller is within range of scrolling.
*
* @param {Object} props
* @returns {boolean}
*/
function shouldAutoScroll(props) {
if (!allDefined(props)) return false;
var distanceForAutoScroll = props.distanceForAutoScroll,
scrollHeight = props.scrollHeight,
scrollTop = props.scrollTop;
return scrollTop + distanceForAutoScroll >= scrollHeight;
}
/**
* Transforms, calculates, and defines props for scrolling.
*
* @param {Object} props
* @returns {Object}
*/
function getScrollProps(props) {
if (!allDefined(props)) return {};
var distanceForAutoScroll = props.distanceForAutoScroll,
messageNode = props.messageNode,
offsetThreshold = props.offsetThreshold,
scrollableNode = props.scrollableNode;
var scrollableHeight = scrollableNode.clientHeight;
var scrollHeight = scrollableNode.scrollHeight;
var scrollTop = scrollableNode.scrollTop + scrollableHeight + messageNode.clientHeight;
var topOffset = scrollableHeight * offsetThreshold * -1;
var position = messageNode.offsetTop + topOffset;
return {
distanceForAutoScroll: distanceForAutoScroll,
position: position,
scrollHeight: scrollHeight,
scrollTop: scrollTop
};
}
;