Popover: make sure that ownerDocument is always defined#42886
Conversation
ntsekouras
left a comment
There was a problem hiding this comment.
I mostly tested this patch in the arrow fix PR and everything seemed to work well. It would be good for others to verify though 😄
|
Size Change: +18 B (0%) Total Size: 1.27 MB
ℹ️ View Unchanged
|
mirka
left a comment
There was a problem hiding this comment.
I added some thoughts just in the wider context of the general Popover work, but as for this PR I'm totally fine with merging as is 👍 Thanks for taking the time to investigate tangential issues and leave explanations.
The
ownerDocumentcould sometimes beundefinedwhen the consumer of thePopoveruses a "modern style" ref (i.e. by using theuseRefhook) and passes that value through theanchorRefprop.
Would it be easy to add a quick unit test for this specific regression?
| const ownerDocument = useMemo( () => { | ||
| let documentToReturn; | ||
|
|
||
| if ( anchorRef?.top ) { |
There was a problem hiding this comment.
I'm surprised anchorRef is not even documented in the readme, when it's the prop that takes the highest priority. Looks like we have a lot of work to do (docs, stories, tests) as we work out the kinks in this component 💪
There was a problem hiding this comment.
Yup! This component definitely needs a lot of work
4e09d9f to
6d4e076
Compare
Done in 6d4e076. I checked that the test currently fails on |
What?
In the
Popovercomponent, the computation ofownerDocumentfrom the anchor could sometimes produce anundefinedvalue — which then would cause runtime errors in other parts of the component.This PR adds a fix which ensures that
ownerDocumentis always defined.Why?
The
ownerDocumentcould sometimes beundefinedwhen the consumer of thePopoveruses a "modern style" ref (i.e. by using theuseRefhook) and passes that value through theanchorRefprop.For more details on how the bug was initially discovered, see #42874 (comment)
How?
The logic has been slightly changed to make sure that the current
documentis used as a fallback value in caseownerDocumentwasundefined.Testing Instructions
Here's a patch to add a Storybook example that uses a "modern style" ref as the Popover's
anchorRefClick to expand
Apply the patch on
trunkand notice how the Storybook example generates an error. Then, apply the same patch on this PR's branch and notice how the Storybook example works as expected.Finally, do a general smoke tests across the codebase to make sure that no regressions are introduced.