Skip to content

Commit

Permalink
fix(Overlay): fix maximum loop update error
Browse files Browse the repository at this point in the history
  • Loading branch information
YSMJ1994 committed Mar 26, 2024
1 parent 8c134aa commit f6a71fd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions components/overlay/position.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import ResizeObserver from 'resize-observer-polyfill';
import { func, dom, events } from '../util';
import position from './utils/position';
import findNode from './utils/find-node';
import { warning } from '../util/log';

const { noop, bindCtx } = func;
const { getStyle } = dom;
const place = position.place;
// Follow react NESTED_UPDATE_LIMIT = 50
const MAX_UPDATE_COUNT = 50;

export default class Position extends Component {
static VIEWPORT = position.VIEWPORT;
Expand Down Expand Up @@ -41,6 +44,8 @@ export default class Position extends Component {
rtl: false,
};

updateCount = 0;

constructor(props) {
super(props);

Expand Down Expand Up @@ -91,6 +96,36 @@ export default class Position extends Component {
this.resizeObserver.disconnect();
};

shouldIgnorePosition = () => {
const node = this.getContentNode();
if (!node) {
return true;
}
// 从文档中移除
if (!node.parentNode) {
return true;
}
// 元素隐藏
const { position, display, visibility } = getComputedStyle(node);
if (!node.offsetParent && position !== 'fixed') {
return true;
}
// Firefox offsetParent 会返回 body,这里兼容处理
if (display === 'none' || visibility === 'hidden') {
return true;
}
// 兜底处理,同步进程里连续更新多次,强制中断
this.updateCount++;
Promise.resolve().then(() => {
this.updateCount = 0;
});
if (this.updateCount > MAX_UPDATE_COUNT - 10) {
warning('Over maximum times to adjust position at one task, it is recommended to use v2.');
return true;
}
return false;
};

setPosition() {
const {
align,
Expand All @@ -104,6 +139,10 @@ export default class Position extends Component {
autoFit,
} = this.props;

if (this.shouldIgnorePosition()) {
return;
}

beforePosition();

const contentNode = this.getContentNode();
Expand Down

0 comments on commit f6a71fd

Please sign in to comment.