-
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
Tabs: update indicator more reactively #66207
Changes from all commits
7752f83
9cf5ae9
ed80444
0c27a3f
8ce4f85
6c0e288
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,14 +134,17 @@ const POLL_RATE = 100; | |
* milliseconds until it succeeds. | ||
*/ | ||
export function useTrackElementOffsetRect( | ||
targetElement: HTMLElement | undefined | null | ||
targetElement: HTMLElement | undefined | null, | ||
deps: unknown[] = [] | ||
) { | ||
const [ indicatorPosition, setIndicatorPosition ] = | ||
useState< ElementOffsetRect >( NULL_ELEMENT_OFFSET_RECT ); | ||
const intervalRef = useRef< ReturnType< typeof setInterval > >(); | ||
|
||
const measure = useEvent( () => { | ||
if ( targetElement ) { | ||
// Check that the targetElement is still attached to the DOM, in case | ||
// it was removed since the last `measure` call. | ||
if ( targetElement && targetElement.isConnected ) { | ||
const elementOffsetRect = getElementOffsetRect( targetElement ); | ||
if ( elementOffsetRect ) { | ||
setIndicatorPosition( elementOffsetRect ); | ||
|
@@ -171,6 +174,16 @@ export function useTrackElementOffsetRect( | |
} | ||
}, [ setElement, targetElement ] ); | ||
|
||
// Escape hatch to force a remeasurement when something else changes rather | ||
// than the target elements' ref or size (for example, the target element | ||
// can change its position within the tablist). | ||
useLayoutEffect( () => { | ||
measure(); | ||
// `measure` is a stable function, so it's safe to omit it from the deps array. | ||
// deps can't be statically analyzed by ESLint | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
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. ESLint disabling is a problem for React Compiler. I'd prefer if we leave the warning in place. There are plenty of those warnings across the codebase already. Yes, we'll have to deal with them, but that can be separately addressed. 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. As discussed, we will address this at a later point, so that we can work across the whole reporsitory 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. Tackling this in #66324. |
||
}, deps ); | ||
|
||
return indicatorPosition; | ||
} | ||
|
||
|
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 is a separate fix for a bug that I discovered while working on this PR. Basically, the
measure
function could go on an infinite loop in case thetargetElement
was holding a reference to an old node that in the meantime was removed from the DOM. TheisConnected
check makes sure that we can detect that edge case and act correctly.An example of this:
instanceId
of the wholeTabs
to update, causing also the individualTabs.Tab
buttons to update.measure
function would loop indefinitely on first render