-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Popover: pass missing anchor ref to the getAnchorRect callback prop #42076
Conversation
@@ -272,10 +272,10 @@ const Popover = ( | |||
return anchorRect; | |||
}, | |||
}; | |||
} else if ( getAnchorRect ) { | |||
} else if ( getAnchorRect && anchorRefFallback.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.
This extra check is to mimic the logic from the early return statement that existed prior to the refactor from #40740).
Not 100% sure about introducing it though, as it may cause regressions (since the getAnchorRect
callback may be called fewer times, in case anchorRefFallback.current
is not defined).
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.
Oh, interesting question. In this case from reading through the series of if / else
statements it sounds like if this if
block is reached, then the output should include the <span ref={ anchorRefFallback }
so it'd be expected by this point that anchorRefFallback.current
would most likely be truthy?
Given that the README says that the callback will be called with a reference to the popover anchor element, I think adding this extra check sounds consistent with the expectations of the component.
We might just want to double-check that the BlockPopover
and ListViewDropIndicator
components are working as expected — which is slightly tricky because they're already components that behave slightly awkwardly sometimes 😅
But overall, the logic of re-introducing this check sounds good to me.
Size Change: +414 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
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.
Thanks for digging into this one @ciampo! In testing this doesn't appear to cause any differences of behaviour in the block popover or list view drop indicator components 👍
While logging out in the Storybook example, I noticed that getAnchorRect()
is also called on this line:
return getAnchorRect()?.ownerDocument ?? document; |
undefined
when it is first called, but by the second call (the line change in this PR), my callback receives the correct element:
I suppose this potentially raises a couple of questions:
- Should that call be changed to also attempt to pass in the fallback ref? Or,
- Is it okay that the callback will be called sometimes with an
undefined
value, in which case, perhaps we don't need the additional check for.current
?
For the former question, the call to getAnchorRect()
appears to assume that the response from that function will be successful even if it's called with undefined
, which could be unexpected for consumers of this component 🤔
I'll be AFK on Monday but happy to do more testing next week if this hasn't landed already 🙂
Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
Hey @andrewserong , thank you for your review! Your observations make a lot of sense, and I made changes so that both calls to
Let me know what you think! |
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've given this a test as well. The recent changes make sense and look good to me 👍
✅ Works in editors (particularly block popovers and list view drop indicators)
✅ Unit tests and e2es are passing
✅ Storybook works as expected
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.
Thanks for following up @ciampo! This is still testing well for me in the block editor and storybook, and the changes look good to me, too 🎉
What?
Fix a regression introduced in #40740, by passing the anchor ref as an argument of the
getAnchorRect
callback prop.Why?
Prior to #40740, the
getAnchorRect
callback prop used to receive the fallback anchor ref as an argument. This behavior was removed as part of the refactor in #40740.This PR adds back that behavior.
How?
In an effort to replicate as closely as possible the logic that existed before #40740, the changes that this PR introduces are:
anchorRefFallback.current
ref is passed to thegetAnchorRef
callback propgetAnchorRect
prop is called only ifanchorRefFallback.current
holds a value (this is to mimic the logic from the early return statement that existed prior to the refactor from Refactor the Popover component to use the floatingUI library #40740)I believe that the reason why this regression hasn't been encountered until now is because it looks like that the few usages of the
getAnchorRect
don't actually rely on its arguments (a quick search across the codebase should confirm this hypothesis).Testing Instructions
getAnchorRef
prop to the Storybook example, notice how:undefined