Skip to content

Commit

Permalink
Components: Use useStoreState() instead of store.useState() (#64648)
Browse files Browse the repository at this point in the history
* Components: Use useStoreState() instead of store.useState()

* CHANGELOG

* Cleanup TabList

Co-authored-by: ciampo <[email protected]>

---------

Co-authored-by: ciampo <[email protected]>

Co-authored-by: tyxla <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ciampo <[email protected]>
  • Loading branch information
4 people authored Aug 21, 2024
1 parent c47491a commit 9749a4a
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- `DropdownMenu` v2: use themed color variables ([#64647](https://github.com/WordPress/gutenberg/pull/64647)).
- `CustomSelectControl`: Improve type inferring ([#64412](https://github.com/WordPress/gutenberg/pull/64412)).
- Update `ariakit` to version `0.4.10` ([#64637](https://github.com/WordPress/gutenberg/pull/64637)).
- Ariakit: Use `useStoreState()` instead of `store.useState()` ([#64648](https://github.com/WordPress/gutenberg/pull/64648)).

## 28.5.0 (2024-08-07)

Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/alignment-matrix-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import clsx from 'clsx';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -68,7 +69,7 @@ export function AlignmentMatrixControl( {
rtl: isRTL(),
} );

const activeId = compositeStore.useState( 'activeId' );
const activeId = useStoreState( compositeStore, 'activeId' );

const classes = clsx( 'component-alignment-matrix-control', className );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import clsx from 'clsx';
import { useStoreState } from '@ariakit/react';
import type { ForwardedRef } from 'react';

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ function UnforwardedOptionAsOption(
forwardedRef: ForwardedRef< any >
) {
const { id, isSelected, compositeStore, ...additionalProps } = props;
const activeId = compositeStore.useState( 'activeId' );
const activeId = useStoreState( compositeStore, 'activeId' );

if ( isSelected && ! activeId ) {
compositeStore.setActiveId( id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -62,7 +63,7 @@ const CustomSelectButton = ( {
CustomSelectStore,
'onChange'
> ) => {
const { value: currentValue } = store.useState();
const { value: currentValue } = useStoreState( store );

const computedRenderSelectedValue = useMemo(
() => renderSelectedValue ?? defaultRenderSelectedValue,
Expand Down
8 changes: 5 additions & 3 deletions packages/components/src/dropdown-menu-v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -248,9 +249,10 @@ const UnconnectedDropdownMenu = (
);

// Extract the side from the applied placement — useful for animations.
const appliedPlacementSide = dropdownMenuStore
.useState( 'placement' )
.split( '-' )[ 0 ];
const appliedPlacementSide = useStoreState(
dropdownMenuStore,
'placement'
).split( '-' )[ 0 ];

if (
dropdownMenuStore.parent &&
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/radio-group/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { forwardRef, useContext } from '@wordpress/element';
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* Internal dependencies
Expand All @@ -26,7 +27,7 @@ function UnforwardedRadio(
) {
const { store, disabled } = useContext( RadioGroupContext );

const selectedValue = store?.useState( 'value' );
const selectedValue = useStoreState( store, 'value' );
const isChecked = selectedValue !== undefined && selectedValue === value;

return (
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/tab-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';
import clsx from 'clsx';
import type { ForwardedRef } from 'react';

Expand Down Expand Up @@ -121,7 +122,9 @@ const UnforwardedTabPanel = (
defaultSelectedId: prependInstanceId( initialTabName ),
} );

const selectedTabName = extractTabName( tabStore.useState( 'selectedId' ) );
const selectedTabName = extractTabName(
useStoreState( tabStore, 'selectedId' )
);

const setTabStoreSelectedId = useCallback(
( tabName: string ) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -48,7 +49,7 @@ function Tabs( {

const isControlled = selectedTabId !== undefined;

const { items, selectedId, activeId } = store.useState();
const { items, selectedId, activeId } = useStoreState( store );
const { setSelectedId, setActiveId } = store;

// Keep track of whether tabs have been populated. This is used to prevent
Expand Down
10 changes: 6 additions & 4 deletions packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand All @@ -26,7 +27,8 @@ export const TabList = forwardRef<
>( function TabList( { children, ...otherProps }, ref ) {
const context = useTabsContext();

const selectedId = context?.store.useState( 'selectedId' );
const tabStoreState = useStoreState( context?.store );
const selectedId = tabStoreState?.selectedId;
const indicatorPosition = useTrackElementOffsetRect(
context?.store.item( selectedId )?.element
);
Expand All @@ -37,13 +39,13 @@ export const TabList = forwardRef<
( { previousValue } ) => previousValue && setAnimationEnabled( true )
);

if ( ! context ) {
if ( ! context || ! tabStoreState ) {
warning( '`Tabs.TabList` must be wrapped in a `Tabs` component.' );
return null;
}
const { store } = context;

const { activeId, selectOnMove } = store.useState();
const { store } = context;
const { activeId, selectOnMove } = tabStoreState;
const { setActiveId } = store;

const onBlur = () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/components/src/tabs/tabpanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* WordPress dependencies
* External dependencies
*/
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';

/**
Expand All @@ -22,13 +26,13 @@ export const TabPanel = forwardRef<
ref
) {
const context = useTabsContext();
const selectedId = useStoreState( context?.store, 'selectedId' );
if ( ! context ) {
warning( '`Tabs.TabPanel` must be wrapped in a `Tabs` component.' );
return null;
}
const { store, instanceId } = context;
const instancedTabId = `${ instanceId }-${ tabId }`;
const selectedId = store.useState( ( state ) => state.selectedId );

return (
<StyledTabPanel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import type { ForwardedRef } from 'react';
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -66,7 +67,7 @@ function UnforwardedToggleGroupControlAsRadioGroup(
setValue: wrappedOnChangeProp,
} );

const selectedValue = radio.useState( 'value' );
const selectedValue = useStoreState( radio, 'value' );
const setValue = radio.setValue;

const groupContextValue = useMemo(
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { useStoreState } from '@ariakit/react';
import clsx from 'clsx';

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ function UnforwardedTooltip(
placement: computedPlacement,
showTimeout: delay,
} );
const mounted = tooltipStore.useState( 'mounted' );
const mounted = useStoreState( tooltipStore, 'mounted' );

if ( isNestedInTooltip ) {
return isOnlyChild ? (
Expand Down
7 changes: 5 additions & 2 deletions packages/dataviews/src/dataviews-layouts/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import clsx from 'clsx';
// TODO: use the @wordpress/components one once public
// eslint-disable-next-line no-restricted-imports
import { useStoreState } from '@ariakit/react';
// Import CompositeStore type, which is not exported from @wordpress/components.
// eslint-disable-next-line no-restricted-imports
import type { CompositeStore } from '@ariakit/react';
Expand Down Expand Up @@ -359,10 +361,11 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {

const store = useCompositeStore( {
defaultActiveId: getItemDomId( selectedItem ),
} );
} ) as CompositeStore; // TODO, remove once composite APIs are public

// Manage focused item, when the active one is removed from the list.
const isActiveIdInList = store.useState(
const isActiveIdInList = useStoreState(
store,
( state: { items: any[]; activeId: any } ) =>
state.items.some(
( item: { id: any } ) => item.id === state.activeId
Expand Down

1 comment on commit 9749a4a

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 9749a4a.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10490910187
📝 Reported issues:

Please sign in to comment.