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

Migrating InserterListbox to use updated Composite implementation #56246

Merged
merged 2 commits into from
Nov 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { getBlockMenuDefaultClassName } from '@wordpress/blocks';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -25,11 +26,10 @@ function BlockTypesList( {
label,
isDraggable = true,
} ) {
const className = 'block-editor-block-types-list';
const listId = useInstanceId( BlockTypesList, className );
return (
<InserterListboxGroup
className="block-editor-block-types-list"
aria-label={ label }
>
<InserterListboxGroup className={ className } aria-label={ label }>
{ chunk( items, 3 ).map( ( row, i ) => (
<InserterListboxRow key={ i }>
{ row.map( ( item, j ) => (
Expand All @@ -43,6 +43,7 @@ function BlockTypesList( {
onHover={ onHover }
isDraggable={ isDraggable && ! item.isDisabled }
isFirst={ i === 0 && j === 0 }
rowId={ `${ listId }-${ i }` }
/>
) ) }
</InserterListboxRow>
Expand Down
18 changes: 11 additions & 7 deletions packages/block-editor/src/components/inserter-listbox/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
/**
* WordPress dependencies
*/
import { __unstableUseCompositeState as useCompositeState } from '@wordpress/components';
import { privateApis as componentsPrivateApis } from '@wordpress/components';

/**
* Internal dependencies
*/
import InserterListboxContext from './context';
import { unlock } from '../../lock-unlock';

export { default as InserterListboxGroup } from './group';
export { default as InserterListboxRow } from './row';
export { default as InserterListboxItem } from './item';

const { CompositeV2: Composite, useCompositeStoreV2: useCompositeStore } =
unlock( componentsPrivateApis );

function InserterListbox( { children } ) {
const compositeState = useCompositeState( {
shift: true,
wrap: 'horizontal',
const store = useCompositeStore( {
focusShift: true,
focusWrap: 'horizontal',
} );

return (
<InserterListboxContext.Provider value={ compositeState }>
<Composite store={ store } render={ <></> }>
andrewhayward marked this conversation as resolved.
Show resolved Hide resolved
{ children }
</InserterListboxContext.Provider>
</Composite>
);
}

Expand Down
23 changes: 11 additions & 12 deletions packages/block-editor/src/components/inserter-listbox/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@
*/
import {
Button,
__unstableCompositeItem as CompositeItem,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { forwardRef, useContext } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import InserterListboxContext from './context';
import { unlock } from '../../lock-unlock';

const { CompositeItemV2: CompositeItem } = unlock( componentsPrivateApis );

function InserterListboxItem(
{ isFirst, as: Component, children, ...props },
ref
) {
const state = useContext( InserterListboxContext );
return (
<CompositeItem
ref={ ref }
state={ state }
role="option"
// Use the CompositeItem `focusable` prop over Button's
// isFocusable. The latter was shown to cause an issue
// with tab order in the inserter list.
focusable
// Use the CompositeItem `accessibleWhenDisabled` prop
// over Button's `isFocusable`. The latter was shown to
// cause an issue with tab order in the inserter list.
accessibleWhenDisabled
{ ...props }
>
{ ( htmlProps ) => {
render={ ( htmlProps ) => {
const propsWithTabIndex = {
...htmlProps,
tabIndex: isFirst ? 0 : htmlProps.tabIndex,
Expand All @@ -45,7 +44,7 @@ function InserterListboxItem(
}
return <Button { ...propsWithTabIndex }>{ children }</Button>;
} }
</CompositeItem>
/>
);
}

Expand Down
18 changes: 6 additions & 12 deletions packages/block-editor/src/components/inserter-listbox/row.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
/**
* WordPress dependencies
*/
import { forwardRef, useContext } from '@wordpress/element';
import { __unstableCompositeGroup as CompositeGroup } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { privateApis as componentsPrivateApis } from '@wordpress/components';

/**
* Internal dependencies
*/
import InserterListboxContext from './context';
import { unlock } from '../../lock-unlock';

const { CompositeGroupV2: CompositeGroup } = unlock( componentsPrivateApis );

function InserterListboxRow( props, ref ) {
const state = useContext( InserterListboxContext );
return (
<CompositeGroup
state={ state }
role="presentation"
ref={ ref }
{ ...props }
/>
);
return <CompositeGroup role="presentation" ref={ ref } { ...props } />;
}

export default forwardRef( InserterListboxRow );
Loading