Skip to content
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
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- DataForm: Translate the "(Required)" and "(Optional)" suffixes of date and date time field labels together with the label, so translators can change their order ([#87487](https://github.com/WordPress/gutenberg/pull/87487)).
- Keep focus in the view when an action, such as moving items to the trash, removes the control that had it. Focus moves to the "Select all" checkbox, or to the next item in the list layout, or to the search when no items are left, rather than to the document body ([#87248](https://github.com/WordPress/gutenberg/pull/87248)).

### Internal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ function ActionTrigger< Item >( {
size="compact"
disabled={ !! action.disabled || isBusy }
loading={ isBusy }
// Tells the focus handling after the action that it holds focus.
data-dataviews-busy={ isBusy || undefined }
onClick={ onClick }
aria-haspopup={ 'RenderModal' in action ? 'dialog' : undefined }
>
Expand Down Expand Up @@ -314,6 +316,9 @@ function renderBulkActionsContent< Item >(
tone="neutral"
size="compact"
loading={ !! actionInProgress }
data-dataviews-busy={
!! actionInProgress || undefined
}
>
{ __( 'Actions' ) }
<Button.Icon icon={ chevronDown } />
Expand Down Expand Up @@ -345,7 +350,8 @@ function BulkActionsContent< Item >( {
onActionInProgressChange,
onClearSelection,
}: ToolbarContentProps< Item > ) {
const { paginationInfo } = useContext( DataViewsContext );
const { paginationInfo, requestFocusAfterAction } =
useContext( DataViewsContext );
const [ pendingContent, setPendingContent ] =
useState< React.JSX.Element >();
const registry = useRegistry();
Expand All @@ -354,6 +360,7 @@ function BulkActionsContent< Item >( {
items: Item[];
} | null >( null );
const onAction = async ( action: Action< Item >, items: Item[] ) => {
requestFocusAfterAction?.();
if ( 'RenderModal' in action ) {
setActiveModal( { action, items } );
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ type DataViewsContextType< Item > = {
itemListLabel?: string;
onReset?: ( () => void ) | false;
intersectionObserver?: IntersectionObserver | null;
/**
* Asks to keep focus in the view once the action being run has removed
* the control that had it.
*/
requestFocusAfterAction?: () => void;
};

const DataViewsContext = createContext< DataViewsContextType< any > >( {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MouseEventHandler } from 'react';
import { Button, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { useContext, useMemo, useState } from '@wordpress/element';
import { moreVertical } from '@wordpress/icons';
import { useRegistry } from '@wordpress/data';
import { useViewportMatch } from '@wordpress/compose';
Expand All @@ -13,6 +13,7 @@ import {
import { kebabCase } from '@wordpress/kebab-case';
import type { Action, ActionModal as ActionModalType } from '../../types';
import getActionLabel from '../../utils/get-action-label';
import DataViewsContext from '../dataviews-context';

export interface ActionTriggerProps< Item > {
action: Action< Item >;
Expand Down Expand Up @@ -127,6 +128,7 @@ export function ActionsMenuGroup< Item >( {
registry,
setActiveModalAction,
}: ActionsMenuGroupProps< Item > ) {
const { requestFocusAfterAction } = useContext( DataViewsContext );
const { primaryActions, regularActions } = useMemo( () => {
return actions.reduce(
( acc, action ) => {
Expand All @@ -149,6 +151,7 @@ export function ActionsMenuGroup< Item >( {
key={ action.id }
action={ action }
onClick={ () => {
requestFocusAfterAction?.();
if ( 'RenderModal' in action ) {
setActiveModalAction( action );
return;
Expand Down Expand Up @@ -280,6 +283,7 @@ export function PrimaryActions< Item >( {
registry,
buttonVariant,
}: PrimaryActionsProps< Item > ) {
const { requestFocusAfterAction } = useContext( DataViewsContext );
const [ activeModalAction, setActiveModalAction ] = useState( null as any );
const isMobileViewport = useViewportMatch( 'medium', '<' );

Expand All @@ -297,6 +301,7 @@ export function PrimaryActions< Item >( {
key={ action.id }
action={ action }
onClick={ () => {
requestFocusAfterAction?.();
if ( 'RenderModal' in action ) {
setActiveModalAction( action );
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function PrimaryActionGridCell< Item >( {
item: Item;
} ) {
const registry = useRegistry();
const { requestFocusAfterAction } = useContext( DataViewsContext );
const [ isModalOpen, setIsModalOpen ] = useState( false );

const compositeItemId = generatePrimaryActionCompositeId(
Expand All @@ -92,7 +93,10 @@ function PrimaryActionGridCell< Item >( {
text={ label }
size="small"
aria-haspopup="dialog"
onClick={ () => setIsModalOpen( true ) }
onClick={ () => {
requestFocusAfterAction?.();
setIsModalOpen( true );
} }
/>
}
>
Expand All @@ -115,6 +119,7 @@ function PrimaryActionGridCell< Item >( {
accessibleWhenDisabled
size="small"
onClick={ () => {
requestFocusAfterAction?.();
primaryAction.callback( [ item ], { registry } );
} }
>
Expand Down
12 changes: 11 additions & 1 deletion packages/dataviews/src/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import DataViewsViewConfig, {
import normalizeFields from '../field-types';
import useContainerWidth from '../hooks/use-container-width';
import useData from '../hooks/use-data';
import useFocusAfterAction from '../hooks/use-focus-after-action';
import { useInfiniteScroll } from '../hooks/use-infinite-scroll';
import usePageClamp from '../hooks/use-page-clamp';
import type { SupportedLayouts, DataViewsProps, ItemWithId } from '../types';
Expand Down Expand Up @@ -213,6 +214,14 @@ function DataViews< Item >( {
[ defaultLayoutsProperty ]
);

const { ref: wrapperRef, request: requestFocusAfterAction } =
useFocusAfterAction( {
layoutRef: containerRef,
data: displayData,
getItemId,
isLoading,
} );

if ( ! defaultLayouts[ view.type ] ) {
return null;
}
Expand Down Expand Up @@ -248,9 +257,10 @@ function DataViews< Item >( {
hasInitiallyLoaded,
onReset,
intersectionObserver,
requestFocusAfterAction,
} }
>
<div className="dataviews-wrapper">
<div className="dataviews-wrapper" ref={ wrapperRef }>
{ children ?? (
<DefaultUI
header={ header }
Expand Down
Loading
Loading