Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import type * as React from 'react';
import { useRef, useEffect, forwardRef, useImperativeHandle } from 'react';
import ReactDOM from 'react-dom';
import canUseDom from './Dom/canUseDom';
Expand All @@ -14,6 +14,7 @@ export interface PortalProps {
const Portal = forwardRef<PortalRef, PortalProps>((props, ref) => {
const { didUpdate, getContainer, children } = props;

const parentRef = useRef<ParentNode>();
const containerRef = useRef<HTMLElement>();

// Ref return nothing, only for wrapper check exist
Expand All @@ -23,6 +24,7 @@ const Portal = forwardRef<PortalRef, PortalProps>((props, ref) => {
const initRef = useRef(false);
if (!initRef.current && canUseDom()) {
containerRef.current = getContainer();
parentRef.current = containerRef.current.parentNode;
initRef.current = true;
}

Expand All @@ -32,6 +34,13 @@ const Portal = forwardRef<PortalRef, PortalProps>((props, ref) => {
});

useEffect(() => {
// Restore container to original place
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch ref comment here in case other people want to know why:

https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors

if (
containerRef.current.parentNode === null &&
parentRef.current !== null
) {
parentRef.current.appendChild(containerRef.current);
}
return () => {
// [Legacy] This should not be handle by Portal but parent PortalWrapper instead.
// Since some component use `Portal` directly, we have to keep the logic here.
Expand Down
28 changes: 27 additions & 1 deletion tests/Portal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { StrictMode, useEffect } from 'react';
import { render } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import PortalWrapper, { getOpenCount } from '../src/PortalWrapper';
Expand Down Expand Up @@ -189,4 +189,30 @@ describe('Portal', () => {
);
expect(document.body.querySelector('.light')).toBeTruthy();
});

it('should restore to original place in StrictMode', () => {
const parentContainer = document.createElement('div');
const domContainer = document.createElement('div');
parentContainer.appendChild(domContainer);
let mountCount = 0;
let unmountCount = 0;

const Demo = () => {
useEffect(() => {
mountCount += 1;
return () => {
unmountCount += 1;
};
}, []);

return <Portal getContainer={() => domContainer}>Contents</Portal>;
};

render(<Demo />, { wrapper: StrictMode });

expect(mountCount).toBe(2);
expect(unmountCount).toBe(1);
// portal should be attached to parent node
expect(parentContainer.textContent).toBe('Contents');
});
});