Skip to content

Commit 4259210

Browse files
authored
show popup at the same document as the trigger element (#227)
* show popup at the same document as the trigger element * use current document in all use cases
1 parent 960f146 commit 4259210

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ function returnEmptyString() {
3434
return '';
3535
}
3636

37-
function returnDocument() {
37+
function returnDocument(element?: HTMLElement) {
38+
if (element) {
39+
return element.ownerDocument;
40+
}
3841
return window.document;
3942
}
4043

@@ -70,7 +73,7 @@ export interface TriggerProps {
7073
focusDelay?: number;
7174
blurDelay?: number;
7275
getPopupContainer?: (node: HTMLElement) => HTMLElement;
73-
getDocument?: () => HTMLDocument;
76+
getDocument?: (element?: HTMLElement) => HTMLDocument;
7477
forceRender?: boolean;
7578
destroyPopupOnHide?: boolean;
7679
mask?: boolean;
@@ -215,7 +218,7 @@ export function generateTrigger(
215218
!this.clickOutsideHandler &&
216219
(this.isClickToHide() || this.isContextMenuToShow())
217220
) {
218-
currentDocument = props.getDocument();
221+
currentDocument = props.getDocument(this.getRootDomNode());
219222
this.clickOutsideHandler = addEventListener(
220223
currentDocument,
221224
'mousedown',
@@ -224,7 +227,7 @@ export function generateTrigger(
224227
}
225228
// always hide on mobile
226229
if (!this.touchOutsideHandler) {
227-
currentDocument = currentDocument || props.getDocument();
230+
currentDocument = currentDocument || props.getDocument(this.getRootDomNode());
228231
this.touchOutsideHandler = addEventListener(
229232
currentDocument,
230233
'touchstart',
@@ -233,7 +236,7 @@ export function generateTrigger(
233236
}
234237
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
235238
if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
236-
currentDocument = currentDocument || props.getDocument();
239+
currentDocument = currentDocument || props.getDocument(this.getRootDomNode());
237240
this.contextMenuOutsideHandler1 = addEventListener(
238241
currentDocument,
239242
'scroll',
@@ -559,7 +562,7 @@ export function generateTrigger(
559562

560563
let mountNode: HTMLElement;
561564
if (!getPopupContainer) {
562-
mountNode = getDocument().body;
565+
mountNode = getDocument(this.getRootDomNode()).body;
563566
} else if (domNode || getPopupContainer.length === 0) {
564567
// Compatible for legacy getPopupContainer with domNode argument.
565568
// If no need `domNode` argument, will call directly.
@@ -578,7 +581,8 @@ export function generateTrigger(
578581
};
579582

580583
getContainer = () => {
581-
const popupContainer = document.createElement('div');
584+
const { getDocument } = this.props;
585+
const popupContainer = getDocument(this.getRootDomNode()).createElement('div');
582586
// Make sure default popup container will never cause scrollbar appearing
583587
// https://github.com/react-component/trigger/issues/41
584588
popupContainer.style.position = 'absolute';

0 commit comments

Comments
 (0)