Skip to content
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

Merged
merged 6 commits into from
Oct 18, 2024
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `RadioGroup`: Fix arrow key navigation in RTL ([#66202](https://github.com/WordPress/gutenberg/pull/66202)).
- `Tabs` and `TabPanel`: Fix arrow key navigation in RTL ([#66201](https://github.com/WordPress/gutenberg/pull/66201)).
- `Tabs`: override tablist's tabindex only when necessary ([#66209](https://github.com/WordPress/gutenberg/pull/66209)).
- `Tabs`: update indicator more reactively ([#66207](https://github.com/WordPress/gutenberg/pull/66207)).

### Enhancements

Expand Down
32 changes: 21 additions & 11 deletions packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { useStoreState } from '@ariakit/react';
import * as Ariakit from '@ariakit/react';
import clsx from 'clsx';

/**
* WordPress dependencies
Expand All @@ -14,11 +15,10 @@ import { useMergeRefs } from '@wordpress/compose';
* Internal dependencies
*/
import type { TabListProps } from './types';
import { useTabsContext } from './context';
import { StyledTabList } from './styles';
import type { WordPressComponentProps } from '../context';
import clsx from 'clsx';
import type { ElementOffsetRect } from '../utils/element-rect';
import { useTabsContext } from './context';
import { StyledTabList } from './styles';
import { useTrackElementOffsetRect } from '../utils/element-rect';
import { useTrackOverflow } from './use-track-overflow';
import { useAnimatedOffsetRect } from '../utils/hooks/use-animated-offset-rect';
Expand Down Expand Up @@ -62,15 +62,25 @@ export const TabList = forwardRef<
>( function TabList( { children, ...otherProps }, ref ) {
const { store } = useTabsContext() ?? {};

const selectedId = useStoreState( store, 'selectedId' );
const activeId = useStoreState( store, 'activeId' );
const selectOnMove = useStoreState( store, 'selectOnMove' );
const items = useStoreState( store, 'items' );
const selectedId = Ariakit.useStoreState( store, 'selectedId' );
const activeId = Ariakit.useStoreState( store, 'activeId' );
const selectOnMove = Ariakit.useStoreState( store, 'selectOnMove' );
const items = Ariakit.useStoreState( store, 'items' );
const [ parent, setParent ] = useState< HTMLElement >();
const refs = useMergeRefs( [ ref, setParent ] );
const selectedRect = useTrackElementOffsetRect(
store?.item( selectedId )?.element
);

const selectedItem = store?.item( selectedId );
const renderedItems = Ariakit.useStoreState( store, 'renderedItems' );

const selectedItemIndex =
renderedItems && selectedItem
? renderedItems.indexOf( selectedItem )
: -1;
// Use selectedItemIndex as a dependency to force recalculation when the
// selected item index changes (elements are swapped / added / removed).
const selectedRect = useTrackElementOffsetRect( selectedItem?.element, [
selectedItemIndex,
] );

// Track overflow to show scroll hints.
const overflow = useTrackOverflow( parent, {
Expand Down
17 changes: 15 additions & 2 deletions packages/components/src/utils/element-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Comment on lines +145 to +147
Copy link
Contributor Author

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 the targetElement was holding a reference to an old node that in the meantime was removed from the DOM. The isConnected check makes sure that we can detect that edge case and act correctly.

An example of this:

  • in dev mode, react renders twice for the first render
  • this was causing the instanceId of the whole Tabs to update, causing also the individual Tabs.Tab buttons to update.
  • therefore, this measure function would loop indefinitely on first render

const elementOffsetRect = getElementOffsetRect( targetElement );
if ( elementOffsetRect ) {
setIndicatorPosition( elementOffsetRect );
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

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

Tackling this in #66324.

}, deps );

return indicatorPosition;
}

Expand Down
Loading