-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fix Overlay2 storing stale onClose callback #6874
Conversation
Generate changelog in
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may be easier to first review this diff: https://github.com/palantir/blueprint/pull/6874/files/f49bee2d29a5a01eecfe831de5cf5121f1e4cb1a and then see the last commit that just moves the hooks up top lower
@@ -325,14 +306,41 @@ export const Overlay2 = React.forwardRef<OverlayInstance, Overlay2Props>((props, | |||
} | |||
}, [isOpen, overlayWillOpen, overlayWillClose, prevIsOpen]); | |||
|
|||
// run once on unmount | |||
// Important: clean up old document-level event listeners if their memoized values change (this is rare, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this order matters - I moved these below the useEffect
that calls overlayWillOpen
because these event listeners were attached towards the end of that method and I believe useEffect
hooks are called in order. But now I'm seeing these were also attached before setRef(lastActiveElementBeforeOpened, getActiveElement(getRef(containerElement)));
and now will be after.
I don't expect that to be an issue, but also don't see any issues if these get attached before the other code in overlayWillOpen
runs.
return () => { | ||
// we need to run cleanup code to remove some event handlers from the overlay element | ||
overlayWillClose(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to find a test case that can assert this because we only call onClose
from user actions, but I believe this is a function that has a stale closure around old versions of the document event listeners that may have already been removed.
Now we rely on the useEffect
that adds the event listeners to clean up themselves - but continue calling this for the other overlay cleanup code. I didn't see any warnings about calling removeEventListener
twice/for an already removed handler, and also didn't see any issues with this from my testing, but please flag if this seems bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is a function that has a stale closure around old versions of the document event listeners that may have already been removed.
Agree, overlayWillClose was not a dependency of this effect but itself had dependencies.
I didn't see any warnings about calling
removeEventListener
twice
Quoting the page you linked:
Calling
removeEventListener()
with arguments that do not identify any currently registered event listener on theEventTarget
has no effect.
This should be fine.
this should keep order closer to what it was beforeBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
dispatchMouseEvent(document.documentElement, "mousedown"); | ||
assert.isTrue(onClose.calledOnce); | ||
assert.isTrue(onClose.notCalled); | ||
assert.isTrue(onClose2.calledOnce); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test case was previously failing - the other methods of closing handled updating onClose
properly
formatBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
// run cleanup code once on unmount, ensuring we call the most recent overlayWillClose callback | ||
// by storing in a ref and keeping up to date | ||
return () => { | ||
mostRecetOverlayWillClose.current(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar to #6753 (review) this diff brings this closer to the original behavior, as overlayWillClose
was previously a class method and so could never be a stale reference
// Important: clean up old document-level event listeners if their memoized values change (this is rare, but | ||
// may happen, for example, if a user forgets to use `React.useCallback` in the `props.onClose` value). | ||
// Otherwise, we will lose the reference to those values and create a memory leak since we won't be able | ||
// to successfully detach them inside overlayWillClose. | ||
React.useEffect(() => { | ||
document.removeEventListener("mousedown", handleDocumentMousedown); | ||
}, [handleDocumentMousedown]); | ||
React.useEffect(() => { | ||
document.removeEventListener("focus", handleDocumentFocus, /* useCapture */ true); | ||
}, [handleDocumentFocus]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
linking diff that added: https://github.com/palantir/blueprint/pull/6681/files#diff-50ceef90072c82bdf126a28d4f90f718d8a384f60d4ac90e7b1a9d2c4f4e57d4R227-R232
I think this had the right idea but I don't think it does what the comment suggests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it does what the comment suggests
Do you mean because it's doing this directly in the effect vs. in a cleanup function returned from the effect? Or did you find something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, since it's directly in the effect it's reference the version of the handler that was just defined and never had a chance to be attached (or worse was just intentionally attached, but I didn't find a case of this happening)
return () => { | ||
// we need to run cleanup code to remove some event handlers from the overlay element | ||
overlayWillClose(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is a function that has a stale closure around old versions of the document event listeners that may have already been removed.
Agree, overlayWillClose was not a dependency of this effect but itself had dependencies.
I didn't see any warnings about calling
removeEventListener
twice
Quoting the page you linked:
Calling
removeEventListener()
with arguments that do not identify any currently registered event listener on theEventTarget
has no effect.
This should be fine.
// Important: clean up old document-level event listeners if their memoized values change (this is rare, but | ||
// may happen, for example, if a user forgets to use `React.useCallback` in the `props.onClose` value). | ||
// Otherwise, we will lose the reference to those values and create a memory leak since we won't be able | ||
// to successfully detach them inside overlayWillClose. | ||
React.useEffect(() => { | ||
document.removeEventListener("mousedown", handleDocumentMousedown); | ||
}, [handleDocumentMousedown]); | ||
React.useEffect(() => { | ||
document.removeEventListener("focus", handleDocumentFocus, /* useCapture */ true); | ||
}, [handleDocumentFocus]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it does what the comment suggests
Do you mean because it's doing this directly in the effect vs. in a cleanup function returned from the effect? Or did you find something else?
@@ -307,6 +276,8 @@ export const Overlay2 = React.forwardRef<OverlayInstance, Overlay2Props>((props, | |||
} | |||
} | |||
}, [closeOverlay, getLastOpened, handleDocumentFocus, handleDocumentMousedown, id]); | |||
const mostRecetOverlayWillClose = React.useRef(overlayWillClose); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
const mostRecetOverlayWillClose = React.useRef(overlayWillClose); | |
const mostRecentOverlayWillClose = React.useRef(overlayWillClose); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah thanks! I decided "mostRecent" was weird anyways and went with overlayWillCloseRef
and also co-located it with the useEffect
so it's a bit more clear why we have this ref
e7e56d3
to
faf9128
Compare
better name and co-locateBuild artifact links for this commit: documentation | landing | table | demoThis is an automated comment from the deploy-preview CircleCI job. |
Fixes #6748
Checklist
Changes proposed in this pull request:
There are 3 changes in this PR, will split to separate PRs on request but figured they are closely related:
onClose
updates. We handleonClose
updating for esc key and backdrop click because the latest callback versions are always passed to the element those are attached to, but did not do a similar update to the document listeners. A test case has been added for this behavior.overlayWillClose
callback on component unmount:evanj/overlay-fix?expand=1
#diff-50ceef9007Reviewers should focus on:
overlayWillOpen
callback directly intouseEffect
hooksScreenshot
n/a