amis-rpc-design/node_modules/@rc-component/trigger/es/hooks/useWatch.js
2023-10-07 19:42:30 +08:00

36 lines
1.3 KiB
JavaScript

import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import useLayoutEffect from "rc-util/es/hooks/useLayoutEffect";
import { collectScroller, getWin } from "../util";
export default function useWatch(open, target, popup, onAlign, onScroll) {
useLayoutEffect(function () {
if (open && target && popup) {
var targetElement = target;
var popupElement = popup;
var targetScrollList = collectScroller(targetElement);
var popupScrollList = collectScroller(popupElement);
var win = getWin(popupElement);
var mergedList = new Set([win].concat(_toConsumableArray(targetScrollList), _toConsumableArray(popupScrollList)));
function notifyScroll() {
onAlign();
onScroll();
}
mergedList.forEach(function (scroller) {
scroller.addEventListener('scroll', notifyScroll, {
passive: true
});
});
win.addEventListener('resize', notifyScroll, {
passive: true
});
// First time always do align
onAlign();
return function () {
mergedList.forEach(function (scroller) {
scroller.removeEventListener('scroll', notifyScroll);
win.removeEventListener('resize', notifyScroll);
});
};
}
}, [open, target, popup]);
}