@zohodesk/hooks
Version:
Unified Component Library - Hooks
45 lines (40 loc) • 1.44 kB
JavaScript
import { useEffect } from 'react';
/**
* @param {Boolean} isScrollHandleNeed
* @param {Element} immediateScrollParent
* @param {Array} otherScrollParents
* @param {Function} immediateParentScrollHandler
* @param {Function} otherParentsScrollHandler
*/
export default function useFixedPopupScrollHandling() {
let {
isScrollHandleNeed,
immediateScrollParent,
otherScrollParents = [],
immediateParentScrollHandler,
otherParentsScrollHandler
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
useEffect(() => {
if (!isScrollHandleNeed) {
return;
}
if (typeof immediateParentScrollHandler === 'function') {
immediateScrollParent.addEventListener('scroll', immediateParentScrollHandler);
}
if (otherScrollParents.length > 0 && typeof otherParentsScrollHandler === 'function') {
otherScrollParents.map(elem => {
elem.addEventListener('scroll', otherParentsScrollHandler);
});
}
return () => {
if (typeof immediateParentScrollHandler === 'function') {
immediateScrollParent.removeEventListener('scroll', immediateParentScrollHandler);
}
if (otherScrollParents.length > 0 && typeof otherParentsScrollHandler === 'function') {
otherScrollParents.map(elem => {
elem.removeEventListener('scroll', otherParentsScrollHandler);
});
}
};
}, [isScrollHandleNeed]);
}