Skip to content

Commit ccb6bb7

Browse files
committed
fix: Retry when parent is not ready
1 parent 1cace93 commit ccb6bb7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/index.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { HTMLAttributes } from 'react';
22
import ReactDOM from 'react-dom';
3+
import raf from 'rc-util/lib/raf';
34
import contains from 'rc-util/lib/Dom/contains';
45
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
56
import { composeRef, supportRef } from 'rc-util/lib/ref';
@@ -538,19 +539,31 @@ export function generateTrigger(
538539
);
539540
};
540541

541-
getContainer = () => {
542+
attachParent = (popupContainer: HTMLDivElement) => {
542543
const { props } = this;
544+
const mountNode = props.getPopupContainer
545+
? props.getPopupContainer(this.getRootDomNode())
546+
: props.getDocument().body;
547+
548+
if (mountNode) {
549+
mountNode.appendChild(popupContainer);
550+
} else {
551+
// Retry after frame render in case parent not ready
552+
raf(() => {
553+
this.attachParent(popupContainer);
554+
});
555+
}
556+
};
557+
558+
getContainer = () => {
543559
const popupContainer = document.createElement('div');
544560
// Make sure default popup container will never cause scrollbar appearing
545561
// https://github.com/react-component/trigger/issues/41
546562
popupContainer.style.position = 'absolute';
547563
popupContainer.style.top = '0';
548564
popupContainer.style.left = '0';
549565
popupContainer.style.width = '100%';
550-
const mountNode = props.getPopupContainer
551-
? props.getPopupContainer(this.getRootDomNode())
552-
: props.getDocument().body;
553-
mountNode.appendChild(popupContainer);
566+
this.attachParent(popupContainer);
554567
return popupContainer;
555568
};
556569

0 commit comments

Comments
 (0)