Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Make portal sync * add example * add examples * add test case * should use container check
- Loading branch information
Showing
7 changed files
with
139 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.storybook | ||
*.iml | ||
*.log | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import PortalWrapper from '../src/PortalWrapper'; | ||
|
||
export default () => { | ||
const divRef = React.useRef(); | ||
|
||
React.useEffect(() => { | ||
console.log('>>>', divRef.current); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<PortalWrapper forceRender> | ||
{() => <div ref={divRef}>2333</div>} | ||
</PortalWrapper> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as React from 'react'; | ||
import { useRef, forwardRef, useImperativeHandle } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import canUseDom from './Dom/canUseDom'; | ||
|
||
export type PortalRef = {}; | ||
|
||
export interface PortalProps { | ||
/** @deprecated Not know who use this? */ | ||
didUpdate?: (prevProps: PortalProps) => void; | ||
getContainer: () => HTMLElement; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const Portal = forwardRef<PortalRef, PortalProps>((props, ref) => { | ||
const { didUpdate, getContainer, children } = props; | ||
|
||
const containerRef = useRef<HTMLElement>(); | ||
|
||
// Ref return nothing, only for wrapper check exist | ||
useImperativeHandle(ref, () => ({})); | ||
|
||
// Create container in client side with sync to avoid useEffect not get ref | ||
const initRef = useRef(false); | ||
if (!initRef.current && canUseDom()) { | ||
containerRef.current = getContainer(); | ||
initRef.current = true; | ||
} | ||
|
||
// Not know who use this. Just keep it here | ||
React.useEffect(() => { | ||
didUpdate?.(props); | ||
|
||
return () => { | ||
if (containerRef.current) { | ||
containerRef.current.parentNode.removeChild(containerRef.current); | ||
} | ||
}; | ||
}, []); | ||
|
||
return containerRef.current | ||
? ReactDOM.createPortal(children, containerRef.current) | ||
: null; | ||
}); | ||
|
||
export default Portal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import PortalWrapper from '../src/PortalWrapper'; | ||
|
||
describe('Portal', () => { | ||
let container: HTMLDivElement; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
it('forceRender', () => { | ||
const divRef = React.createRef<any>(); | ||
|
||
const wrapper = mount( | ||
<PortalWrapper forceRender> | ||
{() => <div ref={divRef}>2333</div>} | ||
</PortalWrapper>, | ||
); | ||
|
||
expect(divRef.current).toBeTruthy(); | ||
|
||
wrapper.unmount(); | ||
}); | ||
}); |