Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 7049848

Browse files
authored
fix: 非根元素滚动时没有更新位置
现在,Dropdown有一个bug,如果触发滚动的元素在一个可以滚动的div内,div滚动时dropdown的位置不会更新。 虽然现在有一个getPopupContainer的属性,但是这个属性局限性很大,比如有下面两个问题: 一:zIndex的问题,如果dropdown的popup在div内,有时候会被遮挡。 二:多层滚动嵌套时。 解决方法:监听所有的滚动事件, 只要是trgger的父元素滚动,就重新定位。 这里使用事件捕获而不是事件冒泡,因为滚动事件不会冒泡到根元素。
1 parent 868712e commit 7049848

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Align.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,30 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
141141
cancelForceAlign();
142142
}
143143
}, [disabled]);
144+
145+
// listen scroll event
146+
const scrollObserveRef = React.useRef<{ remove: () => void }>();
147+
React.useEffect(() => {
148+
const element = getElement(target);
149+
if (!element) return;
150+
const listener = e => {
151+
if (typeof e.target.contains === 'function') {
152+
if (e.target.contains(element)) {
153+
forceAlign();
154+
}
155+
}
156+
};
157+
window.addEventListener('scroll', listener, true);
158+
scrollObserveRef.current = {
159+
remove: () => {
160+
window.removeEventListener('scroll', listener);
161+
},
162+
};
163+
return () => {
164+
scrollObserveRef.current && scrollObserveRef.current.remove();
165+
scrollObserveRef.current = null;
166+
};
167+
}, [target]);
144168

145169
// Listen for window resize
146170
const winResizeRef = React.useRef<{ remove: Function }>(null);

0 commit comments

Comments
 (0)