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

[a11y][Dataviews] Fix: Dropdown menus with only one item. #56792

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
93 changes: 58 additions & 35 deletions packages/dataviews/src/item-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,54 @@ function ActionsDropdownMenuGroup( { actions, item } ) {
);
}

function PrimaryActions( { primaryActions, item } ) {
return primaryActions.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
item={ item }
ActionTrigger={ ButtonTrigger }
/>
);
}
return (
<ButtonTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( item ) }
/>
);
} );
}

function SecondaryActions( { secondaryActions, item } ) {
// If there is only one secondary action, it should be rendered as a primary action avoid an unnecessary dropdown.
if ( secondaryActions.length === 1 ) {
return (
<PrimaryActions primaryActions={ secondaryActions } item={ item } />
);
}
return (
<DropdownMenu
trigger={
<Button
size="compact"
icon={ moreVertical }
label={ __( 'Actions' ) }
/>
}
placement="bottom-end"
>
<ActionsDropdownMenuGroup
actions={ secondaryActions }
item={ item }
/>
</DropdownMenu>
);
}

export default function ItemActions( { item, actions, isCompact } ) {
const { primaryActions, secondaryActions } = useMemo( () => {
return actions.reduce(
Expand Down Expand Up @@ -141,42 +189,17 @@ export default function ItemActions( { item, actions, isCompact } ) {
width: 'auto',
} }
>
{ !! primaryActions.length &&
primaryActions.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
item={ item }
ActionTrigger={ ButtonTrigger }
/>
);
}
return (
<ButtonTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( item ) }
/>
);
} ) }
{ !! primaryActions.length && (
<PrimaryActions
primaryActions={ primaryActions }
item={ item }
/>
) }
{ !! secondaryActions.length && (
<DropdownMenu
trigger={
<Button
size="compact"
icon={ moreVertical }
label={ __( 'Actions' ) }
/>
}
placement="bottom-end"
>
<ActionsDropdownMenuGroup
actions={ secondaryActions }
item={ item }
/>
</DropdownMenu>
<SecondaryActions
item={ item }
secondaryActions={ secondaryActions }
/>
) }
</HStack>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-site/src/components/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { external, trash, backup } from '@wordpress/icons';
import { external, trash, backup, edit } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';
import { useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -225,6 +225,7 @@ export function useEditPostAction() {
isEligible( { status } ) {
return status !== 'trash';
},
icon: edit,
callback( post ) {
history.push( {
postId: post.id,
Expand Down
Loading