Skip to content

Commit

Permalink
Fix portal children ordering (#4573)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Nov 26, 2024
1 parent bd14437 commit b66c0ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compat/src/portals.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ function Portal(props) {
parentNode: container,
childNodes: [],
contains: () => true,
// Technically this isn't needed
appendChild(child) {
this.childNodes.push(child);
_this._container.appendChild(child);
},
insertBefore(child, before) {
this.childNodes.push(child);
_this._container.appendChild(child);
_this._container.insertBefore(child, before);
},
removeChild(child) {
this.childNodes.splice(this.childNodes.indexOf(child) >>> 1, 1);
Expand Down
32 changes: 31 additions & 1 deletion compat/test/browser/portals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import React, {
createPortal,
useState,
Component,
useEffect
useEffect,
Fragment
} from 'preact/compat';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { setupRerender, act } from 'preact/test-utils';
Expand Down Expand Up @@ -64,6 +65,35 @@ describe('Portal', () => {
expect(scratch.innerHTML).to.equal('<div><p>Hello</p></div>');
});

it('should order portal children well', () => {
let bump;

function Modal() {
const [state, setState] = useState(0);
bump = () => setState(() => 1);

return (
<Fragment>
{state === 1 && <div>top</div>}
<div>middle</div>
<div>bottom</div>
</Fragment>
);
}

function Foo(props) {
return createPortal(<Modal />, scratch);
}
render(<Foo />, scratch);
expect(scratch.innerHTML).to.equal('<div>middle</div><div>bottom</div>');

bump();
rerender();
expect(scratch.innerHTML).to.equal(
'<div>top</div><div>middle</div><div>bottom</div>'
);
});

it('should toggle the portal', () => {
let toggle;

Expand Down

0 comments on commit b66c0ae

Please sign in to comment.