Skip to content

refactor(Transition): improve timeout prop as a object and make optional #629

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

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 22 additions & 35 deletions src/Transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,6 @@ class Transition extends React.Component {
this.cancelNextCallback()
}

getTimeouts() {
const { timeout } = this.props
let exit, enter, appear

exit = enter = appear = timeout

if (timeout != null && typeof timeout !== 'number') {
exit = timeout.exit
enter = timeout.enter
// TODO: remove fallback for next major
appear = timeout.appear !== undefined ? timeout.appear : enter
}
return { exit, enter, appear }
}

updateStatus(mounting = false, nextStatus) {
if (nextStatus !== null) {
// nextStatus will always be ENTERING or EXITING.
Expand All @@ -222,14 +207,19 @@ class Transition extends React.Component {
}

performEnter(mounting) {
const { enter } = this.props
const { enter, timeout } = this.props
const appearing = this.context ? this.context.isMounting : mounting
const [maybeNode, maybeAppearing] = this.props.nodeRef
? [appearing]
: [ReactDOM.findDOMNode(this), appearing]

const timeouts = this.getTimeouts()
const enterTimeout = appearing ? timeouts.appear : timeouts.enter
const enterTimeout = typeof timeout !== 'number' && timeout
// TODO: Remove appear-to-enter fallback for next major.
? appearing && timeout.appear !== undefined
? timeout.appear
: timeout.enter
: timeout

// no enter animation skip right to ENTERED
// if we are mounting and running this it means appear _must_ be set
if ((!mounting && !enter) || config.disabled) {
Expand All @@ -253,8 +243,11 @@ class Transition extends React.Component {
}

performExit() {
const { exit } = this.props
const timeouts = this.getTimeouts()
const { exit, timeout } = this.props
const exitTimeout = typeof timeout !== 'number' && timeout
? timeout.exit
: timeout

const maybeNode = this.props.nodeRef
? undefined
: ReactDOM.findDOMNode(this)
Expand All @@ -272,7 +265,7 @@ class Transition extends React.Component {
this.safeSetState({ status: EXITING }, () => {
this.props.onExiting(maybeNode)

this.onTransitionEnd(timeouts.exit, () => {
this.onTransitionEnd(exitTimeout, () => {
this.safeSetState({ status: EXITED }, () => {
this.props.onExited(maybeNode)
})
Expand Down Expand Up @@ -316,26 +309,19 @@ class Transition extends React.Component {

onTransitionEnd(timeout, handler) {
this.setNextCallback(handler)

const node = this.props.nodeRef
? this.props.nodeRef.current
: ReactDOM.findDOMNode(this)

const doesNotHaveTimeoutOrListener =
timeout == null && !this.props.addEndListener
if (!node || doesNotHaveTimeoutOrListener) {
setTimeout(this.nextCallback, 0)
return
}

if (this.props.addEndListener) {
if (node && this.props.addEndListener && timeout == null) {
Copy link
Author

@unrevised6419 unrevised6419 May 21, 2020

Choose a reason for hiding this comment

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

@taion why node check is needed here in onTransitionEnd?
addEndListener check is not enough for this?
Or is here for some historic reasons?

Tests pass if node check is removed from if check.

Copy link
Member

Choose a reason for hiding this comment

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

i think this is a legacy thing from the conversion to use nodeRef; if there's no associated node, then previously it was impossible to call addEndListener, because the first arg was expected to be the node.

Copy link
Author

Choose a reason for hiding this comment

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

Is there any case when node may be missing?

Copy link
Member

Choose a reason for hiding this comment

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

might be possible if e.g. the transition is rendering a child that's actually not there, maybe?

const [maybeNode, maybeNextCallback] = this.props.nodeRef
? [this.nextCallback]
: [node, this.nextCallback]
this.props.addEndListener(maybeNode, maybeNextCallback)
}

if (timeout != null) {
setTimeout(this.nextCallback, timeout)
this.props.addEndListener(maybeNode, maybeNextCallback)
Copy link
Member

Choose a reason for hiding this comment

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

this is not equivalent to the original code. previously, if:

  • node was defined
  • addEndListener was defined
  • timeout was defined

then we would call addEndListener and we would call setTimeout with this.nextCallback.

in this revision, there's no way for both addEndListener and setTimeout to get called

Copy link
Author

@unrevised6419 unrevised6419 May 21, 2020

Choose a reason for hiding this comment

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

Is this the right behaviour for both of them to call nextCallback?
Does setNextCallback appeared in codebase because of this, to call callback only once?

Copy link
Member

Choose a reason for hiding this comment

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

i think the idea is that, if there's a timeout and an end listener, that the first one that triggers will "win"

} else {
setTimeout(this.nextCallback, timeout == null ? 0 : timeout)
}
}

Expand Down Expand Up @@ -484,8 +470,9 @@ Transition.propTypes = {
* @type {number | { enter?: number, exit?: number, appear?: number }}
*/
timeout: (props, ...args) => {
let pt = timeoutsShape
if (!props.addEndListener) pt = pt.isRequired
const pt = props.addEndListener
? timeoutsShape
: timeoutsShape.isRequired
return pt(props, ...args)
},

Expand Down
18 changes: 0 additions & 18 deletions src/utils/SimpleSet.js

This file was deleted.