-
Notifications
You must be signed in to change notification settings - Fork 191
refactor: Portal render sync #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,8 +1,8 @@ | ||||
/* eslint-disable no-underscore-dangle,react/require-default-props */ | ||||
import React from 'react'; | ||||
import ReactDOM from 'react-dom'; | ||||
import * as React from 'react'; | ||||
import * as ReactDOM from 'react-dom'; | ||||
import ContainerRender from './ContainerRender'; | ||||
import Portal from './Portal'; | ||||
import Portal, { PortalRef } from './Portal'; | ||||
import switchScrollingEffect from './switchScrollingEffect'; | ||||
import setStyle from './setStyle'; | ||||
|
||||
|
@@ -19,7 +19,7 @@ const IS_REACT_16 = 'createPortal' in ReactDOM; | |||
// https://github.com/ant-design/ant-design/issues/19332 | ||||
let cacheOverflow = {}; | ||||
|
||||
const getParent = getContainer => { | ||||
const getParent = (getContainer: GetContainer) => { | ||||
if (windowIsUndefined) { | ||||
return null; | ||||
} | ||||
|
@@ -40,8 +40,42 @@ const getParent = getContainer => { | |||
return document.body; | ||||
}; | ||||
|
||||
class PortalWrapper extends React.Component { | ||||
constructor(props) { | ||||
export type GetContainer = string | HTMLElement | (() => HTMLElement); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这边的支持的更多一些: Line 22 in 69046a6
|
||||
|
||||
export interface PortalWrapperProps { | ||||
visible?: boolean; | ||||
getContainer?: GetContainer; | ||||
wrapperClassName?: string; | ||||
forceRender?: boolean; | ||||
children: (info: { | ||||
getOpenCount: () => number; | ||||
getContainer: () => HTMLElement; | ||||
switchScrollingEffect: () => void; | ||||
ref?: (c: any) => void; | ||||
}) => React.ReactNode; | ||||
} | ||||
|
||||
export interface PortalWrapperState { | ||||
_self: PortalWrapper; | ||||
} | ||||
|
||||
class PortalWrapper extends React.Component< | ||||
PortalWrapperProps, | ||||
PortalWrapperState | ||||
> { | ||||
container?: HTMLElement; | ||||
|
||||
_component?: PortalRef; | ||||
|
||||
renderComponent?: (info: { | ||||
afterClose: Function; | ||||
onClose: Function; | ||||
visible: boolean; | ||||
}) => void; | ||||
|
||||
removeContainer?: Function; | ||||
|
||||
constructor(props: PortalWrapperProps) { | ||||
super(props); | ||||
const { visible, getContainer } = props; | ||||
if (!windowIsUndefined && getParent(getContainer) === document.body) { | ||||
|
@@ -121,7 +155,7 @@ class PortalWrapper extends React.Component { | |||
} | ||||
}; | ||||
|
||||
savePortal = c => { | ||||
savePortal = (c: PortalRef) => { | ||||
// Warning: don't rename _component | ||||
// https://github.com/react-component/util/pull/65#discussion_r352407916 | ||||
this._component = c; | ||||
|
@@ -175,7 +209,7 @@ class PortalWrapper extends React.Component { | |||
getContainer: this.getContainer, | ||||
switchScrollingEffect: this.switchScrollingEffect, | ||||
}; | ||||
// suppport react15 | ||||
// support react15 | ||||
shaodahong marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
if (!IS_REACT_16) { | ||||
return ( | ||||
<ContainerRender | ||||
|
This file contains hidden or 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(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.