diff --git a/package-lock.json b/package-lock.json
index 22861e8ffe81c7..ec9b918f6f793d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -44838,6 +44838,10 @@
"@wordpress/url": "file:../url",
"clsx": "^2.1.1"
},
+ "devDependencies": {
+ "@testing-library/react": "^16.3.3",
+ "vitest": "^5.0.3"
+ },
"engines": {
"node": ">=22.13.0"
},
diff --git a/packages/boot/CHANGELOG.md b/packages/boot/CHANGELOG.md
index 458a902be92ad6..eeefd5e263837c 100644
--- a/packages/boot/CHANGELOG.md
+++ b/packages/boot/CHANGELOG.md
@@ -8,6 +8,7 @@
### Bug Fixes
+- Only announce the route title when the path changes, so it no longer cuts off notices, such as the one confirming that an item was moved to the trash ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
- Generated CSS module class names now change whenever their CSS changes, so they differ from earlier versions. Two versions of the package loaded on the same page, such as one in WordPress Core and an older copy bundled in a plugin, no longer apply their styles to each other's elements ([#85900](https://github.com/WordPress/gutenberg/pull/85900)).
## 0.25.0 (2026-10-20)
diff --git a/packages/boot/package.json b/packages/boot/package.json
index 29859b87aeed9a..ac9dd1bde82e34 100644
--- a/packages/boot/package.json
+++ b/packages/boot/package.json
@@ -66,6 +66,10 @@
"@wordpress/url": "file:../url",
"clsx": "^2.1.1"
},
+ "devDependencies": {
+ "@testing-library/react": "^16.3.3",
+ "vitest": "^5.0.3"
+ },
"peerDependencies": {
"@types/react": "^18 || ^19",
"react": "^18 || ^19",
diff --git a/packages/boot/src/components/app/test/use-route-title.jsdom.test.ts b/packages/boot/src/components/app/test/use-route-title.jsdom.test.ts
new file mode 100644
index 00000000000000..56916223e5fb96
--- /dev/null
+++ b/packages/boot/src/components/app/test/use-route-title.jsdom.test.ts
@@ -0,0 +1,45 @@
+import { renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { speak } from '@wordpress/a11y';
+import useRouteTitle from '../use-route-title';
+
+let location: { pathname: string; search: Record< string, unknown > };
+
+vi.mock( import( '@wordpress/a11y' ), () => ( { speak: vi.fn() } ) as any );
+vi.mock( import( '@wordpress/data' ), async ( importOriginal ) => ( {
+ ...( await importOriginal() ),
+ useSelect: ( () => 'My Site' ) as any,
+} ) );
+vi.mock(
+ import( '../../../lock-unlock' ),
+ () =>
+ ( {
+ unlock: () => ( {
+ useLocation: () => location,
+ useMatches: () => [ { loaderData: { title: 'Pages' } } ],
+ } ),
+ } ) as any
+);
+
+describe( 'useRouteTitle', () => {
+ beforeEach( () => {
+ vi.mocked( speak ).mockClear();
+ location = { pathname: '/types/page/list/all', search: {} };
+ } );
+
+ it( 'announces the title when the path changes', () => {
+ const { rerender } = renderHook( () => useRouteTitle() );
+ vi.mocked( speak ).mockClear();
+ location = { ...location, pathname: '/types/page/list/draft' };
+ rerender();
+ expect( speak ).toHaveBeenCalledWith( 'Pages', 'assertive' );
+ } );
+
+ it( 'does not announce the title when only the search changes', () => {
+ const { rerender } = renderHook( () => useRouteTitle() );
+ vi.mocked( speak ).mockClear();
+ location = { ...location, search: { postIds: [ '1' ] } };
+ rerender();
+ expect( speak ).not.toHaveBeenCalled();
+ } );
+} );
diff --git a/packages/boot/src/components/app/use-route-title.ts b/packages/boot/src/components/app/use-route-title.ts
index 26c27a13f96ba2..40597f802d953f 100644
--- a/packages/boot/src/components/app/use-route-title.ts
+++ b/packages/boot/src/components/app/use-route-title.ts
@@ -16,7 +16,11 @@ const { useLocation, useMatches } = unlock( routePrivateApis );
* This hook should be called from the Root component to ensure it runs on every route.
*/
export default function useRouteTitle() {
- const location = useLocation();
+ // Only a new path is a new screen. Search parameters also change while the
+ // screen stays, e.g. when the selection changes or an item is moved to the
+ // trash, and announcing the title then would clear the live regions and cut
+ // off the notice of what happened.
+ const { pathname } = useLocation();
const matches = useMatches();
const currentMatch = matches[ matches.length - 1 ];
const routeTitle = ( currentMatch?.loaderData as any )?.title as
@@ -35,7 +39,7 @@ export default function useRouteTitle() {
useEffect( () => {
isInitialLocationRef.current = false;
- }, [ location ] );
+ }, [ pathname ] );
useEffect( () => {
// Don't update or announce the title for initial page load.
@@ -68,5 +72,5 @@ export default function useRouteTitle() {
speak( decodedRouteTitle, 'assertive' );
}
}
- }, [ routeTitle, siteTitle, location ] );
+ }, [ routeTitle, siteTitle, pathname ] );
}
diff --git a/packages/core-fields/src/attachment/author/view.tsx b/packages/core-fields/src/attachment/author/view.tsx
index 7801f33b0dbf29..063af07337719b 100644
--- a/packages/core-fields/src/attachment/author/view.tsx
+++ b/packages/core-fields/src/attachment/author/view.tsx
@@ -1,5 +1,4 @@
import clsx from 'clsx';
-import { __ } from '@wordpress/i18n';
import { useState, useCallback, useEffect } from '@wordpress/element';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';
import { Icon, Stack } from '@wordpress/ui';
@@ -52,7 +51,8 @@ export default function AuthorView( { item }: { item: MediaItem } ) {
diff --git a/packages/core-fields/src/post_type_supports/author/view.tsx b/packages/core-fields/src/post_type_supports/author/view.tsx
index 227ad6cb25376e..e1bda67c06575e 100644
--- a/packages/core-fields/src/post_type_supports/author/view.tsx
+++ b/packages/core-fields/src/post_type_supports/author/view.tsx
@@ -1,5 +1,4 @@
import clsx from 'clsx';
-import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';
import { Icon, Stack } from '@wordpress/ui';
@@ -62,7 +61,8 @@ export default function AuthorView( { item }: { item: PostWithAuthor } ) {
>
setIsImageLoaded( true ) }
- alt={ __( 'Author avatar' ) }
+ // Decorative: the name of the author is next to it.
+ alt=""
src={ imageUrl }
/>
diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md
index 11c746846fc3cb..f76fc84dcc7327 100644
--- a/packages/dataviews/CHANGELOG.md
+++ b/packages/dataviews/CHANGELOG.md
@@ -9,6 +9,8 @@
### Enhancements
+- Add `opensDialog` for actions whose `callback` opens a dialog. The triggers of these actions and of `RenderModal` actions now have `aria-haspopup="dialog"` ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Replace the close icon that cancels the selection with a "Clear selection" button next to the bulk actions ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
- DataForm: Tighten the line height of field values in the panel layout, so values that wrap onto several lines take less room ([#85828](https://github.com/WordPress/gutenberg/pull/85828)).
- Omit trailing ellipses from action button labels ([#85396](https://github.com/WordPress/gutenberg/pull/85396)).
- Render table bulk actions outside the table while preserving their placement over the column headers ([#85250](https://github.com/WordPress/gutenberg/pull/85250)).
@@ -19,6 +21,12 @@
### Bug Fixes
+- Name the action in the labels of selection checkboxes: "Select [title]" ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Announce when selecting items makes bulk actions available and when clearing the selection removes them ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Move focus to the "Select all" checkbox, or the first control of the layout, when the focused control is removed, such as after moving an item to the trash ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Keep the table media out of the tab order and the accessibility tree when the title next to it leads to the same place, and show a focus ring on it otherwise ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Name list items by their title only, not by all of their content ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+- Set the grid selection checkbox apart from previews of any color ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
- DataForm: Keep the first line of a panel field label that wraps level with its value ([#85828](https://github.com/WordPress/gutenberg/pull/85828)).
- Replace the pagination page select with a page number input above 100 pages, so a dataset with tens of thousands of pages no longer freezes the browser ([#85303](https://github.com/WordPress/gutenberg/pull/85303)).
diff --git a/packages/dataviews/README.md b/packages/dataviews/README.md
index 053d458657b15a..7c9001983b2b42 100644
--- a/packages/dataviews/README.md
+++ b/packages/dataviews/README.md
@@ -1177,6 +1177,14 @@ Function that performs the required action.
}
```
+### `opensDialog`
+
+Whether the `callback` opens a dialog, such as a modal. The trigger then has `aria-haspopup="dialog"`, as the triggers of `RenderModal` actions do.
+
+- Type: `boolean`
+- Optional
+- Default: `false`
+
### `RenderModal`
Component to render UI in a modal for the action.
diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx
index 541ab5f95af0fe..a446ea01d024c9 100644
--- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx
+++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx
@@ -2,7 +2,7 @@ import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo, useState, useContext } from '@wordpress/element';
import { useRegistry } from '@wordpress/data';
-import { closeSmall, chevronDown } from '@wordpress/icons';
+import { chevronDown } from '@wordpress/icons';
import { useViewportMatch } from '@wordpress/compose';
import {
Checkbox,
@@ -18,6 +18,7 @@ import type { SetSelection } from '../../types/private';
import type { ActionTriggerProps } from '../dataviews-item-actions';
import getFooterMessage from '../../utils/get-footer-message';
import getActionLabel from '../../utils/get-action-label';
+import getActionPopup from '../../utils/get-action-popup';
export function hasAPossibleBulkAction< Item >(
actions: Action< Item >[],
@@ -157,6 +158,7 @@ function ActionTrigger< Item >( {
icon={ isMobile ? action.icon : undefined }
size="compact"
onClick={ onClick }
+ aria-haspopup={ getActionPopup( action ) }
isBusy={ isBusy }
>
{ ! isMobile && label }
@@ -189,6 +191,7 @@ function ActionButton< Item >( {
{ getActionLabel( action, selectedEligibleItems, 'menu' ) }
@@ -221,18 +224,20 @@ function renderBulkActionsContent< Item >(
isMobile: boolean,
selectionCheckboxRef?: React.Ref< HTMLSpanElement >
) {
+ // A text button next to the actions, rather than a close icon at the far
+ // end, so it can't be read as closing the whole view and stays in sight
+ // for screen magnifier users.
const clearSelection = selectedItems.length > 0 && (
);
const renderActions = ( isMenuItem = false ) =>
actionsToShow.map( ( action ) => (
diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss
index aa6e5b25e07b7d..fc7370f43bdc71 100644
--- a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss
+++ b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss
@@ -10,10 +10,6 @@
border-bottom: var(--wpds-border-width-xs) solid var(--wpds-color-stroke-surface-neutral-weak);
}
-.dataviews-bulk-actions__clear {
- margin-inline-start: auto;
-}
-
.dataviews-bulk-actions__action {
--wp-components-color-accent: var(--wpds-color-foreground-interactive-neutral);
--wp-components-color-accent-darker-20: var(--wpds-color-foreground-interactive-neutral-active);
diff --git a/packages/dataviews/src/components/dataviews-item-actions/index.tsx b/packages/dataviews/src/components/dataviews-item-actions/index.tsx
index 96ff74001983f8..7ac0ac7e4bb264 100644
--- a/packages/dataviews/src/components/dataviews-item-actions/index.tsx
+++ b/packages/dataviews/src/components/dataviews-item-actions/index.tsx
@@ -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 getActionPopup from '../../utils/get-action-popup';
export interface ActionTriggerProps< Item > {
action: Action< Item >;
@@ -69,6 +70,7 @@ function ButtonTrigger< Item >( {
size="compact"
variant={ variant }
onClick={ onClick }
+ aria-haspopup={ getActionPopup( action ) }
>
{ label }
@@ -82,7 +84,11 @@ function MenuItemTrigger< Item >( {
}: ActionTriggerProps< Item > ) {
const label = getActionLabel( action, items, 'menu' );
return (
-
+ { label }
);
diff --git a/packages/dataviews/src/components/dataviews-layout/index.tsx b/packages/dataviews/src/components/dataviews-layout/index.tsx
index 1de3c484661f0a..f05e91f27f67dc 100644
--- a/packages/dataviews/src/components/dataviews-layout/index.tsx
+++ b/packages/dataviews/src/components/dataviews-layout/index.tsx
@@ -1,10 +1,12 @@
import type { ComponentType } from 'react';
import { useContext } from '@wordpress/element';
import { Spinner as WCSpinner } from '@wordpress/components';
+import { useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import DataViewsContext from '../dataviews-context';
import { VIEW_LAYOUTS } from '../dataviews-layouts';
import { useDelayedLoading } from '../../hooks/use-delayed-loading';
+import useFocusFallback from '../../hooks/use-focus-fallback';
import type { ViewBaseProps } from '../../types';
type DataViewsLayoutProps = {
@@ -20,6 +22,7 @@ export default function DataViewsLayout( { className }: DataViewsLayoutProps ) {
getItemLevel,
hasInitiallyLoaded,
isLoading,
+ paginationInfo,
view,
onChangeView,
selection,
@@ -33,6 +36,13 @@ export default function DataViewsLayout( { className }: DataViewsLayoutProps ) {
empty =
{
view: ViewListType;
@@ -91,6 +92,7 @@ function PrimaryActionGridCell< Item >( {
accessibleWhenDisabled
text={ label }
size="small"
+ aria-haspopup="dialog"
onClick={ () => setIsModalOpen( true ) }
/>
}
@@ -113,6 +115,7 @@ function PrimaryActionGridCell< Item >( {
disabled={ !! primaryAction.disabled }
accessibleWhenDisabled
size="small"
+ aria-haspopup={ getActionPopup( primaryAction ) }
onClick={ () => {
primaryAction.callback( [ item ], { registry } );
} }
@@ -283,6 +286,15 @@ function ListItem< Item >( {
/>
}
role={ infiniteScrollEnabled ? 'article' : 'row' }
+ // Otherwise the row is named after all of its content: the title,
+ // the actions and every field. The fields stay available as the
+ // description of the item. Without a title, the content is all
+ // there is to name it by.
+ aria-labelledby={
+ renderedTitleField && titleField?.getValue?.( { item } )
+ ? labelId
+ : undefined
+ }
className={ clsx( {
'is-selected': isSelected,
'is-hovered': isHovered,
diff --git a/packages/dataviews/src/components/dataviews-layouts/picker-grid/style.scss b/packages/dataviews/src/components/dataviews-layouts/picker-grid/style.scss
index 83a7b6d883da67..058ba855731d31 100644
--- a/packages/dataviews/src/components/dataviews-layouts/picker-grid/style.scss
+++ b/packages/dataviews/src/components/dataviews-layouts/picker-grid/style.scss
@@ -172,6 +172,8 @@
left: var(--wpds-dimension-padding-sm);
// Ensure selection checkbox stays above the preview field.
z-index: 1;
+ // Set the checkbox apart from previews of any color, as the grid does.
+ box-shadow: 0 0 0 calc(2 * var(--wpds-border-width-xs)) var(--wpds-color-background-surface-neutral-strong);
@media (hover: none) {
// Show checkboxes on devices that do not support hover.
diff --git a/packages/dataviews/src/components/dataviews-layouts/table/column-primary.tsx b/packages/dataviews/src/components/dataviews-layouts/table/column-primary.tsx
index 982c5eb87ca75b..09c2b71b3d7c62 100644
--- a/packages/dataviews/src/components/dataviews-layouts/table/column-primary.tsx
+++ b/packages/dataviews/src/components/dataviews-layouts/table/column-primary.tsx
@@ -43,6 +43,14 @@ function ColumnPrimary< Item >( {
Math.round( ( 32 * ratioWidth ) / ratioHeight )
) }px`;
}
+ // The title next to the media leads to the same place, so keep the media
+ // out of the tab order and the accessibility tree. Otherwise keyboard and
+ // screen reader users meet the same control twice, and the cell is read
+ // with the title repeated.
+ const isMediaRedundant =
+ isItemClickable( item ) &&
+ ( !! onClickItem || !! renderItemLink ) &&
+ !! titleField;
return (
{ mediaField && (
@@ -52,13 +60,10 @@ function ColumnPrimary< Item >( {
onClickItem={ onClickItem }
renderItemLink={ renderItemLink }
className="dataviews-view-table__cell-content-wrapper dataviews-column-primary__media"
- aria-label={
- isItemClickable( item ) &&
- ( !! onClickItem || !! renderItemLink ) &&
- !! titleField
- ? titleField.getValue?.( { item } )
- : undefined
- }
+ { ...( isMediaRedundant && {
+ tabIndex: -1,
+ 'aria-hidden': true,
+ } ) }
>
( {
) => ReactElement;
className?: string;
title?: string;
+ tabIndex?: number;
children: ReactNode;
} ) {
// Always render a wrapper element so layout and styling relying on the wrapper
diff --git a/packages/dataviews/src/components/dataviews-selection-checkbox/index.tsx b/packages/dataviews/src/components/dataviews-selection-checkbox/index.tsx
index 8cdf51099e5a57..b9add108619e98 100644
--- a/packages/dataviews/src/components/dataviews-selection-checkbox/index.tsx
+++ b/packages/dataviews/src/components/dataviews-selection-checkbox/index.tsx
@@ -1,5 +1,5 @@
import { Checkbox } from '@wordpress/ui';
-import { __ } from '@wordpress/i18n';
+import { __, sprintf } from '@wordpress/i18n';
import type { SetSelection } from '../../types/private';
import type { NormalizedField } from '../../types';
@@ -29,9 +29,12 @@ export default function DataViewsSelectionCheckbox< Item >( {
const isInSelectionArray = selection.includes( id );
const checked = ! disabled && isInSelectionArray;
- // Fallback label to ensure accessibility
- const selectionLabel =
- titleField?.getValue?.( { item } ) || __( '(no title)' );
+ // Name the action as well as the item, as the list tables do.
+ const selectionLabel = sprintf(
+ /* translators: %s: The title of the item. */
+ __( 'Select %s' ),
+ titleField?.getValue?.( { item } ) || __( '(no title)' )
+ );
return (
( {
const containerRef = useRef< HTMLDivElement >( null );
const [ containerWidth, resizeObserverRef ] = useContainerWidth();
const [ openedFilter, setOpenedFilter ] = useState< string | null >( null );
+ function hasBulkActionsFor( ids: string[] ) {
+ // Only the table and grid show the bulk actions.
+ if ( view.type !== LAYOUT_TABLE && view.type !== LAYOUT_GRID ) {
+ return false;
+ }
+ // The items the toolbar works on, which with infinite scroll keep the
+ // selected items of the pages loaded before.
+ return displayData.some(
+ ( item ) =>
+ ids.includes( getItemId( item ) ) &&
+ hasAPossibleBulkAction( actions, item )
+ );
+ }
function setSelectionWithChange( value: SelectionOrUpdater ) {
const newValue =
typeof value === 'function' ? value( selection ) : value;
+ // The bulk actions only show once an item is selected, so screen
+ // reader users can't find them beforehand. Say when selecting makes
+ // them available and when clearing the selection removes them. Only
+ // changes made here, by the user, are announced: when the consumer
+ // clears the selection, e.g. after moving the items to the trash,
+ // announcing it would cut off the notice of that action.
+ const hadBulkActions = hasBulkActionsFor( selection );
+ const hasBulkActions = hasBulkActionsFor( newValue );
+ if ( hadBulkActions !== hasBulkActions ) {
+ speak(
+ hasBulkActions
+ ? __( 'Bulk actions available.' )
+ : __( 'Bulk actions no longer available.' )
+ );
+ }
if ( isUncontrolled ) {
setSelectionState( newValue );
}
diff --git a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx
index 9b03ad99e3fd27..38cc0f8e31ddb1 100644
--- a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx
+++ b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx
@@ -1,13 +1,17 @@
import { render } from 'vitest-browser-react';
import { page, userEvent } from 'vitest/browser';
-import { afterEach, beforeEach, expect, it } from 'vitest';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { speak } from '@wordpress/a11y';
import { useLayoutEffect, useState } from '@wordpress/element';
import '@wordpress/theme/design-tokens.css';
import DataViews from '../index';
+import useFocusFallback from '../../hooks/use-focus-fallback';
import type { Action, Field, View } from '../../types';
import filterSortAndPaginate from '../../utils/filter-sort-and-paginate';
import '../../style.scss';
+vi.mock( import( '@wordpress/a11y' ), () => ( { speak: vi.fn() } ) as any );
+
const initialViewport = {
width: window.innerWidth,
height: window.innerHeight,
@@ -95,10 +99,12 @@ it( 'keeps column names and sorting accessible during selection, skips covered c
.toHaveFocus();
await user.keyboard( '{Tab}' );
await expect
- .element( page.getByRole( 'button', { name: 'Cancel', exact: true } ) )
+ .element(
+ page.getByRole( 'button', { name: 'Clear selection', exact: true } )
+ )
.toHaveFocus();
await user.click(
- page.getByRole( 'button', { name: 'Cancel', exact: true } )
+ page.getByRole( 'button', { name: 'Clear selection', exact: true } )
);
await expect
@@ -133,7 +139,7 @@ it( 'blocks focus on bulk actions and covers column headings while loading', asy
page.getByRole( 'checkbox', { name: 'Select all', exact: true } )
);
const cancel = page
- .getByRole( 'button', { name: 'Cancel', exact: true } )
+ .getByRole( 'button', { name: 'Clear selection', exact: true } )
.element();
const table = page.getByRole( 'table' ).element();
const toolbar = document.querySelector( '.dataviews-bulk-actions' )!;
@@ -243,7 +249,10 @@ it.each( [ 'balanced', 'compact', 'comfortable' ] as const )(
).toBeLessThanOrEqual( 0.5 );
await expect
.element(
- page.getByRole( 'button', { name: 'Cancel', exact: true } )
+ page.getByRole( 'button', {
+ name: 'Clear selection',
+ exact: true,
+ } )
)
.toBeInViewport();
}
@@ -254,7 +263,7 @@ it.each( [ 'balanced', 'compact', 'comfortable' ] as const )(
.poll( () => toolbar.getBoundingClientRect().height )
.toBe( 96 );
await user.click(
- page.getByRole( 'button', { name: 'Cancel', exact: true } )
+ page.getByRole( 'button', { name: 'Clear selection', exact: true } )
);
await expect
.element(
@@ -416,7 +425,7 @@ it( 'keeps a grid item checkbox below the sticky toolbar after arrow navigation
page.getByRole( 'gridcell' ).first().element().focus();
await user.keyboard( '{ArrowDown}{ArrowDown}{ArrowUp}{Tab}{Tab}' );
const checkbox = page.getByRole( 'checkbox', {
- name: 'Post 1',
+ name: 'Select Post 1',
exact: true,
} );
await expect.element( checkbox ).toHaveFocus();
@@ -432,3 +441,506 @@ it( 'keeps a grid item checkbox below the sticky toolbar after arrow navigation
} )
.toBe( true );
} );
+
+function TrashableTable() {
+ const [ items, setItems ] = useState( posts );
+ const [ view, setView ] = useState< View >( {
+ type: 'table',
+ titleField: 'title',
+ fields: [ 'author' ],
+ page: 1,
+ perPage: 10,
+ } );
+ const { data, paginationInfo } = filterSortAndPaginate(
+ items,
+ view,
+ fields
+ );
+ const trashAction: Action< Post > = {
+ id: 'trash',
+ label: 'Trash',
+ isPrimary: true,
+ supportsBulk: true,
+ RenderModal: ( { items: itemsToTrash, closeModal } ) => (
+ <>
+
+
+
+ >
+ ),
+ };
+ return (
+
+ );
+}
+
+it( 'moves focus to the select all checkbox when an action removes the item that held it', async () => {
+ const user = userEvent.setup();
+ await render( );
+ await user.click(
+ page
+ .getByRole( 'row', { name: /First post/ } )
+ .getByRole( 'button', { name: 'Trash', exact: true } )
+ );
+ await user.click(
+ page.getByRole( 'button', { name: 'Confirm', exact: true } )
+ );
+ await expect
+ .element( page.getByRole( 'row', { name: /First post/ } ) )
+ .not.toBeInTheDocument();
+ await expect
+ .element(
+ page.getByRole( 'checkbox', { name: 'Select all', exact: true } )
+ )
+ .toHaveFocus();
+} );
+
+// Focus is restored a frame after the change, so wait for it before
+// checking that it was left alone.
+function nextFrames() {
+ return new Promise( ( resolve ) =>
+ requestAnimationFrame( () => requestAnimationFrame( resolve ) )
+ );
+}
+
+function TableOf( {
+ items,
+ totalItems = items.length,
+ infiniteScroll,
+}: {
+ items: Post[];
+ totalItems?: number;
+ infiniteScroll?: boolean;
+} ) {
+ return (
+ {} }
+ actions={ actions }
+ paginationInfo={ { totalItems, totalPages: 1 } }
+ defaultLayouts={ { table: true } }
+ />
+ );
+}
+
+it( 'leaves focus alone when it was moved away before the item was removed', async () => {
+ const { rerender } = await render( );
+ const checkbox = page
+ .getByRole( 'checkbox', { name: 'Select First post', exact: true } )
+ .element() as HTMLElement;
+ checkbox.focus();
+ checkbox.blur();
+ await rerender( );
+ await expect
+ .element( page.getByRole( 'row', { name: /First post/ } ) )
+ .not.toBeInTheDocument();
+ await nextFrames();
+ expect( checkbox.ownerDocument.body ).toHaveFocus();
+} );
+
+it( 'moves focus once there is a control again after the focused item was removed', async () => {
+ const { rerender } = await render( );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ // Like the last item of a page, while the page before it loads.
+ await rerender( );
+ await rerender( );
+ await expect
+ .element(
+ page.getByRole( 'checkbox', { name: 'Select all', exact: true } )
+ )
+ .toHaveFocus();
+} );
+
+it( 'moves focus to the select all checkbox when the focused item is removed', async () => {
+ const { rerender } = await render( );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ await rerender( );
+ await expect
+ .element(
+ page.getByRole( 'checkbox', { name: 'Select all', exact: true } )
+ )
+ .toHaveFocus();
+} );
+
+it( 'moves focus to the select all checkbox when a dialog closes after its action removed the items', async () => {
+ const user = userEvent.setup();
+ await render( );
+ await user.click(
+ page.getByRole( 'checkbox', { name: 'Select First post', exact: true } )
+ );
+ // The toolbar comes before the rows, which have a Trash button too.
+ await user.click(
+ page.getByRole( 'button', { name: 'Trash', exact: true } ).first()
+ );
+ await user.click(
+ page.getByRole( 'button', { name: 'Remove', exact: true } )
+ );
+ await expect
+ .element( page.getByRole( 'row', { name: /First post/ } ) )
+ .not.toBeInTheDocument();
+ await user.click(
+ page.getByRole( 'button', { name: 'Done', exact: true } )
+ );
+ await expect
+ .element(
+ page.getByRole( 'checkbox', { name: 'Select all', exact: true } )
+ )
+ .toHaveFocus();
+} );
+
+it( 'does not announce bulk actions in layouts that do not show them', async () => {
+ vi.mocked( speak ).mockClear();
+ const user = userEvent.setup();
+ await render(
+ {} }
+ actions={ actions }
+ paginationInfo={ { totalItems: posts.length, totalPages: 1 } }
+ defaultLayouts={ { list: true } }
+ />
+ );
+ await user.click(
+ page.getByRole( 'button', { name: 'First post', exact: true } )
+ );
+ await expect
+ .element(
+ page.getByRole( 'button', { name: 'First post', exact: true } )
+ )
+ .toHaveAttribute( 'aria-pressed', 'true' );
+ expect( speak ).not.toHaveBeenCalled();
+} );
+
+it( 'moves focus to the search when no items are left', async () => {
+ const { rerender } = await render( );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ await rerender( );
+ await expect
+ .element( page.getByRole( 'searchbox', { name: 'Search' } ) )
+ .toHaveFocus();
+} );
+
+it( 'forgets the focused item once focus moves to another control', async () => {
+ const { rerender } = await render( );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ const search = page
+ .getByRole( 'searchbox', { name: 'Search' } )
+ .element() as HTMLElement;
+ search.focus();
+ search.blur();
+ await rerender( );
+ await expect
+ .element( page.getByRole( 'row', { name: /First post/ } ) )
+ .not.toBeInTheDocument();
+ await nextFrames();
+ expect( search.ownerDocument.body ).toHaveFocus();
+} );
+
+it( 'leaves focus alone after the search took it and the items come back', async () => {
+ const { rerender } = await render( );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ await rerender( );
+ const search = page.getByRole( 'searchbox', { name: 'Search' } );
+ await expect.element( search ).toHaveFocus();
+ const searchElement = search.element() as HTMLElement;
+ searchElement.blur();
+ await rerender( );
+ await expect
+ .element( page.getByRole( 'row', { name: /Second post/ } ) )
+ .toBeInTheDocument();
+ await nextFrames();
+ expect( searchElement.ownerDocument.body ).toHaveFocus();
+} );
+
+it( 'leaves focus to a dialog that closes without removing anything', async () => {
+ const user = userEvent.setup();
+ await render( );
+ const trigger = page
+ .getByRole( 'row', { name: /First post/ } )
+ .getByRole( 'button', { name: 'Trash', exact: true } );
+ await user.click( trigger );
+ await user.click(
+ page.getByRole( 'button', { name: 'Done', exact: true } )
+ );
+ await nextFrames();
+ await expect.element( trigger ).toHaveFocus();
+} );
+
+function LoadingTable( { isLoading }: { isLoading: boolean } ) {
+ const renameAction: Action< Post > = {
+ id: 'rename',
+ label: 'Rename',
+ isPrimary: true,
+ RenderModal: ( { closeModal } ) => (
+
+ ),
+ };
+ return (
+ {} }
+ actions={ [ renameAction ] }
+ isLoading={ isLoading }
+ paginationInfo={ { totalItems: posts.length, totalPages: 1 } }
+ defaultLayouts={ { table: true } }
+ />
+ );
+}
+
+it( 'brings focus back to the trigger of a dialog that closed while loading', async () => {
+ const user = userEvent.setup();
+ const { rerender } = await render( );
+ const trigger = page
+ .getByRole( 'row', { name: /First post/ } )
+ .getByRole( 'button', { name: 'Rename', exact: true } );
+ await user.click( trigger );
+ // The action saves, so the view loads, and the dialog closes meanwhile:
+ // it can't return focus to its trigger in the inert table.
+ await rerender( );
+ await user.click(
+ page.getByRole( 'button', { name: 'Done', exact: true } )
+ );
+ await rerender( );
+ await expect.element( trigger ).toHaveFocus();
+} );
+
+const LIST = Array.from( { length: 30 }, ( _, index ) => `Item ${ index }` );
+
+function FallbackList( {
+ items = LIST,
+ hidden = [],
+ ariaHidden = [],
+ disabled = [],
+ isInfiniteScroll = false,
+}: {
+ items?: string[];
+ hidden?: string[];
+ ariaHidden?: string[];
+ disabled?: string[];
+ isInfiniteScroll?: boolean;
+} ) {
+ const ref = useFocusFallback( { isEmpty: false, isInfiniteScroll } );
+ return (
+
+
+ { items.map( ( item ) => (
+
+
+
+ ) ) }
+
+
+ );
+}
+
+function focusItem( name: string ) {
+ const element = page
+ .getByRole( 'button', { name, exact: true, includeHidden: true } )
+ .element() as HTMLElement;
+ element.focus();
+ return element;
+}
+
+it.each( [ false, true ] )(
+ 'moves focus on when the focused item is removed, with infinite scroll %s',
+ async ( isInfiniteScroll ) => {
+ const { rerender } = await render(
+
+ );
+ focusItem( 'Item 0' );
+ await nextFrames();
+ // Like an action removing the item it was triggered on.
+ await rerender(
+
+ );
+ await expect
+ .element(
+ page.getByRole( 'button', { name: 'Item 1', exact: true } )
+ )
+ .toHaveFocus();
+ }
+);
+
+it( 'moves focus on when the focused control is hidden, with infinite scroll', async () => {
+ const { rerender } = await render( );
+ focusItem( 'Item 0' );
+ await nextFrames();
+ await rerender( );
+ await expect
+ .element( page.getByRole( 'button', { name: 'Item 1', exact: true } ) )
+ .toHaveFocus();
+} );
+
+it( 'leaves focus alone when infinite scroll unloads an item scrolled off screen', async () => {
+ const { rerender } = await render( );
+ const item = focusItem( 'Item 0' );
+ const scroller = page.getByTestId( 'scroller' ).element() as HTMLElement;
+ scroller.scrollTop = scroller.scrollHeight;
+ await nextFrames();
+ await nextFrames();
+ // Infinite scroll unloads the items scrolled past.
+ await rerender(
+
+ );
+ await nextFrames();
+ expect( item.ownerDocument.body ).toHaveFocus();
+} );
+
+it( 'moves focus on when an item slightly off screen is removed, with infinite scroll', async () => {
+ const { rerender } = await render( );
+ focusItem( 'Item 0' );
+ const scroller = page.getByTestId( 'scroller' ).element() as HTMLElement;
+ scroller.scrollTop = 150;
+ await nextFrames();
+ await nextFrames();
+ await rerender(
+
+ );
+ await expect
+ .element( page.getByRole( 'button', { name: 'Item 1', exact: true } ) )
+ .toHaveFocus();
+} );
+
+it( 'forgets a control hidden on its own once focus leaves it', async () => {
+ // Like the table media, hidden next to the title but focusable by mouse.
+ const { rerender } = await render(
+
+ );
+ const item = focusItem( 'Item 0' );
+ item.blur();
+ await rerender(
+
+ );
+ await nextFrames();
+ expect( item.ownerDocument.body ).toHaveFocus();
+} );
+
+it( 'skips disabled and hidden controls when moving focus on', async () => {
+ const { rerender } = await render( );
+ focusItem( 'Item 0' );
+ await rerender(
+
+ );
+ await expect
+ .element( page.getByRole( 'button', { name: 'Item 3', exact: true } ) )
+ .toHaveFocus();
+} );
+
+it( 'moves focus within a view inside a dialog', async () => {
+ const { rerender } = await render(
+
+
+
+ );
+ page.getByRole( 'checkbox', {
+ name: 'Select First post',
+ exact: true,
+ } )
+ .element()
+ .focus();
+ const search = page
+ .getByRole( 'searchbox', { name: 'Search' } )
+ .element() as HTMLElement;
+ // Moving on to another control of the dialog is moving on.
+ search.focus();
+ search.blur();
+ await rerender(
+
+
+
+ );
+ await expect
+ .element( page.getByRole( 'row', { name: /First post/ } ) )
+ .not.toBeInTheDocument();
+ await nextFrames();
+ expect( search.ownerDocument.body ).toHaveFocus();
+} );
diff --git a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx
index 120a11960fb0b9..6285c7bd400dcf 100644
--- a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx
+++ b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx
@@ -1,6 +1,7 @@
import { render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { speak } from '@wordpress/a11y';
import { useMemo, useState } from '@wordpress/element';
import DataViews from '../index';
import {
@@ -174,6 +175,8 @@ vi.mock( import( '@wordpress/compose' ), async ( importOriginal ) => {
} as unknown as typeof original;
} );
+vi.mock( import( '@wordpress/a11y' ), () => ( { speak: vi.fn() } ) as any );
+
describe( 'DataViews component', () => {
it.each( [ 'Delete', 'Delete……' ] )(
'uses button and item-menu labels for modal action "%s"',
@@ -202,6 +205,39 @@ describe( 'DataViews component', () => {
}
);
+ it( 'marks the triggers of actions that open a dialog', async () => {
+ render(
+
+ );
+ expect(
+ screen.getByRole( 'button', { name: 'Delete' } )
+ ).toHaveAttribute( 'aria-haspopup', 'dialog' );
+ expect(
+ screen.getByRole( 'button', { name: 'Quick edit' } )
+ ).toHaveAttribute( 'aria-haspopup', 'dialog' );
+ const user = userEvent.setup();
+ await user.click( screen.getByRole( 'button', { name: 'Actions' } ) );
+ expect(
+ await screen.findByRole( 'menuitem', { name: 'View' } )
+ ).not.toHaveAttribute( 'aria-haspopup' );
+ expect(
+ screen.getByRole( 'menuitem', { name: 'Delete…' } )
+ ).toHaveAttribute( 'aria-haspopup', 'dialog' );
+ } );
+
it( 'renders header bulk actions once in a custom table composition', () => {
render(
{
expect(
screen.getByRole( 'button', { name: 'Delete' } )
).toBeInTheDocument();
- await user.click( screen.getByRole( 'button', { name: 'Cancel' } ) );
+ await user.click(
+ screen.getByRole( 'button', { name: 'Clear selection' } )
+ );
expect( screen.getByText( 'Select all' ) ).toBeVisible();
} );
@@ -305,6 +343,46 @@ describe( 'DataViews component', () => {
expect( within( titleHeader ).getByRole( 'button' ) ).toBeEnabled();
} );
+ it( 'announces when selecting makes bulk actions available or unavailable', async () => {
+ vi.mocked( speak ).mockClear();
+ render(
+
+ );
+ const user = userEvent.setup();
+ await user.click(
+ screen.getByRole( 'checkbox', { name: 'Select Hello World' } )
+ );
+ expect( speak ).toHaveBeenLastCalledWith( 'Bulk actions available.' );
+ await user.click(
+ screen.getByRole( 'checkbox', { name: 'Select Homepage' } )
+ );
+ expect( speak ).toHaveBeenCalledTimes( 1 );
+ await user.click(
+ screen.getByRole( 'checkbox', { name: 'Select Hello World' } )
+ );
+ expect( speak ).toHaveBeenCalledTimes( 1 );
+ await user.click(
+ screen.getByRole( 'checkbox', { name: 'Select Homepage' } )
+ );
+ expect( speak ).toHaveBeenLastCalledWith(
+ 'Bulk actions no longer available.'
+ );
+ expect( speak ).toHaveBeenCalledTimes( 2 );
+ } );
+
+ it( 'does not announce bulk actions when the consumer clears the selection', () => {
+ vi.mocked( speak ).mockClear();
+ const props = { onChangeSelection: vi.fn(), actions };
+ const { rerender } = render(
+
+ );
+ rerender( );
+ expect( speak ).not.toHaveBeenCalled();
+ } );
+
it( 'disabled desktop bulk action cannot run', async () => {
const callback = vi.fn();
render(
@@ -363,7 +441,7 @@ describe( 'DataViews component', () => {
).toBeInTheDocument();
await user.click(
- within( toolbar ).getByRole( 'button', { name: 'Cancel' } )
+ within( toolbar ).getByRole( 'button', { name: 'Clear selection' } )
);
expect( within( titleHeader ).getByRole( 'button' ) ).toBeEnabled();
expect(
@@ -735,6 +813,33 @@ describe( 'DataViews component', () => {
}
} );
+ it( 'keeps clickable media next to a clickable title out of the tab order', () => {
+ render(
+ true }
+ onClickItem={ () => {} }
+ />
+ );
+ expect(
+ screen.getAllByRole( 'button', { name: data[ 0 ].title } )
+ ).toHaveLength( 1 );
+ const media = screen
+ .getAllByRole( 'button', { hidden: true } )
+ .find( ( button ) =>
+ button.classList.contains(
+ 'dataviews-column-primary__media'
+ )
+ );
+ expect( media ).toHaveAttribute( 'tabindex', '-1' );
+ expect( media ).toHaveAttribute( 'aria-hidden', 'true' );
+ } );
+
it( 'should render actions column if actions are supported and passed in', () => {
render( );
expect( screen.getByText( 'Actions' ) ).toBeInTheDocument();
@@ -790,16 +895,22 @@ describe( 'DataViews component', () => {
// First item should be selected.
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
await user.click( thirdItemElement );
// Both items should be selected.
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
expect(
- screen.getByRole( 'checkbox', { name: data[ 2 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 2 ].title }`,
+ } )
).toBeChecked();
// Don't keep the modifier pressed down, that's just mean.
@@ -824,7 +935,9 @@ describe( 'DataViews component', () => {
await user.click( screen.getByText( data[ 0 ].title ) );
await user.keyboard( '{/Control}' );
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
// Shift+Click selects everything between the anchor and the
@@ -833,7 +946,9 @@ describe( 'DataViews component', () => {
await user.click( screen.getByText( data[ 2 ].title ) );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
@@ -856,10 +971,14 @@ describe( 'DataViews component', () => {
// Checkbox clicks select the first and third items; the third
// becomes the anchor.
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
);
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 2 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 2 ].title }`,
+ } )
);
// Shift+Click applies the anchor's state to the range without
@@ -870,7 +989,9 @@ describe( 'DataViews component', () => {
await user.keyboard( '{/Shift}' );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
} );
@@ -893,7 +1014,9 @@ describe( 'DataViews component', () => {
);
// Unchecking the third item makes it the anchor.
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 2 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 2 ].title }`,
+ } )
);
// Shift-clicking the selected second item selects the range
@@ -903,7 +1026,9 @@ describe( 'DataViews component', () => {
await user.keyboard( '{/Shift}' );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
} );
@@ -924,10 +1049,14 @@ describe( 'DataViews component', () => {
// Selecting and deselecting the first item leaves it as the
// anchor with nothing selected.
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
);
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
);
// Shift-clicking the unselected third item selects the whole
@@ -937,7 +1066,9 @@ describe( 'DataViews component', () => {
await user.keyboard( '{/Shift}' );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
} );
@@ -958,7 +1089,9 @@ describe( 'DataViews component', () => {
// The checkbox click selects the third item and makes it the
// anchor.
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 2 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 2 ].title }`,
+ } )
);
// Shift-clicking the first item's checkbox selects the whole
@@ -967,12 +1100,16 @@ describe( 'DataViews component', () => {
// toggle after React re-renders, leaving it visually unchecked.
await user.keyboard( '{Shift>}' );
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
);
await user.keyboard( '{/Shift}' );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
} );
@@ -1007,14 +1144,18 @@ describe( 'DataViews component', () => {
await user.click( screen.getByText( data[ 1 ].title ) );
expect( onClickItem ).not.toHaveBeenCalled();
expect(
- screen.getByRole( 'checkbox', { name: data[ 1 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 1 ].title }`,
+ } )
).not.toBeChecked();
// Selectable items still respond and become the anchor.
await user.click( screen.getByText( data[ 0 ].title ) );
await user.keyboard( '{/Control}' );
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
// Shift+Click ranges skip the non-selectable item.
@@ -1022,13 +1163,19 @@ describe( 'DataViews component', () => {
await user.click( screen.getByText( data[ 2 ].title ) );
await user.keyboard( '{/Shift}' );
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
expect(
- screen.getByRole( 'checkbox', { name: data[ 1 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 1 ].title }`,
+ } )
).not.toBeChecked();
expect(
- screen.getByRole( 'checkbox', { name: data[ 2 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 2 ].title }`,
+ } )
).toBeChecked();
expect( onClickItem ).not.toHaveBeenCalled();
} );
@@ -1065,10 +1212,14 @@ describe( 'DataViews component', () => {
);
const user = userEvent.setup();
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
);
await user.click(
- screen.getByRole( 'checkbox', { name: data[ 1 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 1 ].title }`,
+ } )
);
await user.click(
@@ -1233,7 +1384,7 @@ describe( 'DataViews component', () => {
/>
);
const firstCheckbox = screen.getByRole( 'checkbox', {
- name: data[ 0 ].title,
+ name: `Select ${ data[ 0 ].title }`,
} );
const user = userEvent.setup();
await user.click( firstCheckbox );
@@ -1241,7 +1392,7 @@ describe( 'DataViews component', () => {
// First item should be selected.
expect( firstCheckbox ).toBeChecked();
const thirdCheckbox = screen.getByRole( 'checkbox', {
- name: data[ 2 ].title,
+ name: `Select ${ data[ 2 ].title }`,
} );
await user.click( thirdCheckbox );
@@ -1275,7 +1426,9 @@ describe( 'DataViews component', () => {
// First item should be selected.
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
await user.keyboard( '{/Control}' );
@@ -1304,7 +1457,9 @@ describe( 'DataViews component', () => {
);
await user.keyboard( '{/Control}' );
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toBeChecked();
// Shift+Click selects everything between the anchor and the
@@ -1316,7 +1471,9 @@ describe( 'DataViews component', () => {
await user.keyboard( '{/Shift}' );
for ( const item of data ) {
expect(
- screen.getByRole( 'checkbox', { name: item.title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ item.title }`,
+ } )
).toBeChecked();
}
} );
@@ -1358,7 +1515,9 @@ describe( 'DataViews component', () => {
await user.tab();
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toHaveFocus();
await user.tab();
@@ -1407,7 +1566,9 @@ describe( 'DataViews component', () => {
await user.tab();
expect(
- screen.getByRole( 'checkbox', { name: data[ 0 ].title } )
+ screen.getByRole( 'checkbox', {
+ name: `Select ${ data[ 0 ].title }`,
+ } )
).toHaveFocus();
await user.tab();
@@ -1547,6 +1708,57 @@ describe( 'DataViews component', () => {
}
} );
+ it( 'names each item by its title and describes it by its fields', async () => {
+ render(
+
+ );
+ expect(
+ await screen.findByRole( 'row', { name: 'Hello World' } )
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole( 'button', { name: 'Hello World' } )
+ ).toHaveAccessibleDescription( /Author\s*Jane/ );
+ } );
+
+ it( 'names an item by its content when its title is empty', async () => {
+ render(
+
+ );
+ expect(
+ await screen.findByRole( 'row', { name: /Jane/ } )
+ ).toBeInTheDocument();
+ } );
+
+ it( 'names an item by its content when the title is not shown', async () => {
+ render(
+
+ );
+ expect(
+ await screen.findByRole( 'row', { name: /Jane/ } )
+ ).toBeInTheDocument();
+ } );
+
it( 'should render actions dropdown if actions are supported and passed in for each list item', async () => {
render(
0;
+}
+
+function getScrollContainer( element: HTMLElement ) {
+ const { defaultView } = element.ownerDocument;
+ for (
+ let node: HTMLElement | null = element;
+ node;
+ node = node.parentElement
+ ) {
+ const { overflowY } = defaultView?.getComputedStyle( node ) ?? {};
+ if ( overflowY === 'auto' || overflowY === 'scroll' ) {
+ return node;
+ }
+ }
+ return null;
+}
+
+/**
+ * The control to focus when focus is lost: the first checkbox, which is the
+ * "Select all" one where the layout has it, or else the first control.
+ *
+ * @param container The layout container.
+ */
+function getFallback( container: HTMLElement ) {
+ const controls = Array.from(
+ container.querySelectorAll< HTMLElement >( FOCUSABLE_SELECTOR )
+ ).filter(
+ ( element ) =>
+ element.tabIndex >= 0 &&
+ isVisible( element ) &&
+ // Usable, rather than disabled but still focusable.
+ ! element.matches( ':disabled, [aria-disabled="true"]' ) &&
+ // Hidden on its own, like the table media, or inert while the
+ // layout loads, e.g. after an action.
+ ! element.closest( '[aria-hidden="true"], [inert]' )
+ );
+ return (
+ controls.find(
+ ( element ) =>
+ element.getAttribute( 'role' ) === 'checkbox' ||
+ ( element as HTMLInputElement ).type === 'checkbox'
+ ) ?? controls[ 0 ]
+ );
+}
+
+/**
+ * Keeps focus in the layout when the focused control goes away, e.g. when an
+ * action removes the item whose row held it, or the control a modal returns
+ * focus to is gone by the time the modal closes. Without this, focus falls
+ * back to the document body and keyboard users have to start over from the
+ * top of the page.
+ *
+ * @param options Options.
+ * @param options.isEmpty Whether there are no items left to show, in
+ * which case the layout has no controls and
+ * focus moves to the ones around it.
+ * @param options.isInfiniteScroll Whether items load as the view scrolls,
+ * which also unloads the ones scrolled past.
+ */
+export default function useFocusFallback( {
+ isEmpty,
+ isInfiniteScroll,
+}: {
+ isEmpty: boolean;
+ isInfiniteScroll: boolean;
+} ) {
+ const optionsRef = useRef( { isEmpty, isInfiniteScroll } );
+ useLayoutEffect( () => {
+ optionsRef.current = { isEmpty, isInfiniteScroll };
+ }, [ isEmpty, isInfiniteScroll ] );
+ return useRefEffect( ( container: HTMLElement ) => {
+ const { ownerDocument } = container;
+ // The control to bring focus back to if it goes away while focused. It
+ // is the last control focused in the layout, kept while focus is in a
+ // menu or dialog opened from it, which hand focus back when they close.
+ let lastFocused: HTMLElement | null = null;
+ // Whether it was on screen when last seen. Infinite scroll unloads
+ // items once they are well off screen, which isn't focus going away.
+ let wasInView = true;
+ const { defaultView } = ownerDocument;
+ const IntersectionObserverClass = defaultView?.IntersectionObserver;
+ const visibility = IntersectionObserverClass
+ ? new IntersectionObserverClass(
+ ( entries ) => {
+ for ( const entry of entries ) {
+ // Removing or hiding it also reports it off screen.
+ if (
+ entry.target === lastFocused &&
+ entry.target.isConnected &&
+ isVisible( entry.target )
+ ) {
+ wasInView = entry.isIntersecting;
+ }
+ }
+ },
+ {
+ // Against the scroll container, a little beyond its
+ // edges, as unloading only takes rows well past them.
+ // Sideways scrolling doesn't unload anything.
+ root: getScrollContainer( container ),
+ rootMargin: '400px 100000px',
+ }
+ )
+ : undefined;
+ const track = ( element: HTMLElement | null ) => {
+ if ( element === lastFocused ) {
+ return;
+ }
+ if ( lastFocused ) {
+ visibility?.unobserve( lastFocused );
+ }
+ lastFocused = element;
+ wasInView = true;
+ if ( element ) {
+ visibility?.observe( element );
+ }
+ };
+ const isHandOff = ( element: HTMLElement ) => {
+ const popup = element.closest(
+ '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"], .components-popover'
+ );
+ // Not the dialog the whole view is in, such as a media modal.
+ return !! popup && ! popup.contains( container );
+ };
+ // Hidden along with the rest of the page by a modal, or inert while
+ // the layout loads, rather than hidden on its own like the table media.
+ const isHiddenByModal = ( element: HTMLElement ) => {
+ const hidden = element.closest( '[aria-hidden="true"]' );
+ return (
+ !! element.closest( '[inert]' ) ||
+ ( !! hidden && hidden.contains( container ) )
+ );
+ };
+ const onFocusIn = ( event: FocusEvent ) => {
+ const target = event.target as HTMLElement;
+ if ( container.contains( target ) ) {
+ track( target );
+ } else if ( ! isHandOff( target ) ) {
+ // Focus moved on to another control, e.g. the search.
+ track( null );
+ }
+ };
+ // Focus that leaves for nowhere while the control is still usable was
+ // moved away on purpose, e.g. by clicking outside any control. Don't
+ // bring it back when that control goes away later. A modal hiding the
+ // rest of the page also blurs the control, but focus comes back on
+ // close. Removing or hiding the control blurs it too, so only decide
+ // once that is done.
+ const onFocusOut = ( event: FocusEvent ) => {
+ const target = event.target as HTMLElement;
+ if (
+ event.relatedTarget ||
+ ! ( container.contains( target ) || isHandOff( target ) ) ||
+ isHiddenByModal( target )
+ ) {
+ return;
+ }
+ queueMicrotask( () => {
+ const { activeElement } = ownerDocument;
+ if (
+ target.isConnected &&
+ isVisible( target ) &&
+ // Unless focus went on to another control meanwhile.
+ ( ! activeElement || activeElement === ownerDocument.body )
+ ) {
+ track( null );
+ }
+ } );
+ };
+ const restoreFocus = () => {
+ // Behind an open modal, focus belongs to the modal. Check again
+ // once it closes.
+ if (
+ ! lastFocused ||
+ container.closest( '[inert], [aria-hidden="true"]' )
+ ) {
+ return;
+ }
+ const { activeElement } = ownerDocument;
+ const wasRemoved =
+ ! lastFocused.isConnected || ! isVisible( lastFocused );
+ const isLost =
+ ! activeElement ||
+ activeElement === ownerDocument.body ||
+ ( activeElement === lastFocused && wasRemoved );
+ if ( ! isLost ) {
+ return;
+ }
+ // Still there, but focus was knocked off it, e.g. as the layout
+ // turned inert to load while a modal returned focus to it.
+ if ( ! wasRemoved ) {
+ if ( ! lastFocused.closest( '[inert]' ) ) {
+ lastFocused.focus( { preventScroll: true } );
+ }
+ return;
+ }
+ // Infinite scroll unloads the items scrolled past, which is not
+ // the focused item going away. An action removes one on screen.
+ if ( optionsRef.current.isInfiniteScroll && ! wasInView ) {
+ track( null );
+ return;
+ }
+ // With no items left, the layout has no controls: use the ones
+ // around it, such as the search.
+ const fallback =
+ getFallback( container ) ??
+ ( optionsRef.current.isEmpty
+ ? getFallback(
+ container.closest< HTMLElement >(
+ '.dataviews-wrapper, .dataviews-picker-wrapper'
+ ) ?? container
+ )
+ : undefined );
+ // Nothing to move focus to yet, e.g. while the previous page loads
+ // after the last item of a page was removed: try again on the next
+ // change.
+ if ( ! fallback ) {
+ return;
+ }
+ // Once it lands, `onFocusIn` tracks the fallback, or stops tracking
+ // for one outside the layout. Until then, the next change tries
+ // again.
+ fallback.focus();
+ };
+ // Wait a frame, so that a closing popover or modal returns focus to its
+ // trigger first. Focus is only lost if it is still nowhere after that.
+ let frame: number | undefined;
+ const scheduleRestore = () => {
+ if ( ! lastFocused ) {
+ return;
+ }
+ if ( frame !== undefined ) {
+ defaultView?.cancelAnimationFrame( frame );
+ }
+ frame = defaultView?.requestAnimationFrame( () => {
+ frame = undefined;
+ restoreFocus();
+ } );
+ };
+ const observer = new MutationObserver( scheduleRestore );
+ ownerDocument.addEventListener( 'focusin', onFocusIn );
+ ownerDocument.addEventListener( 'focusout', onFocusOut );
+ observer.observe( container, {
+ childList: true,
+ subtree: true,
+ attributes: true,
+ attributeFilter: [ 'hidden', 'inert' ],
+ } );
+ // Modals render outside the layout, and hide the rest of the page while
+ // open. One can close after its action removed the control it returns
+ // focus to, without changing the layout any more, so check again when
+ // it goes away and when the page is shown again.
+ observer.observe( ownerDocument.body, { childList: true } );
+ const pageObserver = new MutationObserver( scheduleRestore );
+ pageObserver.observe( ownerDocument.body, {
+ subtree: true,
+ attributes: true,
+ attributeFilter: [ 'aria-hidden', 'inert' ],
+ } );
+ return () => {
+ ownerDocument.removeEventListener( 'focusin', onFocusIn );
+ ownerDocument.removeEventListener( 'focusout', onFocusOut );
+ observer.disconnect();
+ pageObserver.disconnect();
+ visibility?.disconnect();
+ if ( frame !== undefined ) {
+ defaultView?.cancelAnimationFrame( frame );
+ }
+ };
+ }, [] );
+}
diff --git a/packages/dataviews/src/types/dataviews.ts b/packages/dataviews/src/types/dataviews.ts
index f0522585506645..7190005c7d3812 100644
--- a/packages/dataviews/src/types/dataviews.ts
+++ b/packages/dataviews/src/types/dataviews.ts
@@ -752,6 +752,12 @@ export interface ActionButton< Item > extends ActionBase< Item > {
onActionPerformed?: ( items: Item[] ) => void;
}
) => void;
+
+ /**
+ * Whether the callback opens a dialog, such as a modal, so the trigger
+ * can tell assistive technologies.
+ */
+ opensDialog?: boolean;
}
export type Action< Item > = ActionModal< Item > | ActionButton< Item >;
diff --git a/packages/dataviews/src/utils/get-action-popup.ts b/packages/dataviews/src/utils/get-action-popup.ts
new file mode 100644
index 00000000000000..01184ba9d68094
--- /dev/null
+++ b/packages/dataviews/src/utils/get-action-popup.ts
@@ -0,0 +1,13 @@
+import type { Action } from '../types';
+
+/**
+ * The `aria-haspopup` value of an action's trigger: `dialog` when the action
+ * opens one, either its own modal or one its callback opens.
+ *
+ * @param action The action.
+ */
+export default function getActionPopup< Item >( action: Action< Item > ) {
+ return 'RenderModal' in action || action.opensDialog
+ ? ( 'dialog' as const )
+ : undefined;
+}
diff --git a/packages/edit-site/src/components/dataviews-actions/index.js b/packages/edit-site/src/components/dataviews-actions/index.js
index 92448bb910360c..c2a089613fbb9b 100644
--- a/packages/edit-site/src/components/dataviews-actions/index.js
+++ b/packages/edit-site/src/components/dataviews-actions/index.js
@@ -42,6 +42,7 @@ export const useQuickEditPostAction = () => {
icon: drawerRight,
isPrimary: true,
supportsBulk: true,
+ opensDialog: true,
isEligible( post ) {
if ( post.status === 'trash' ) {
return false;
diff --git a/packages/editor/src/components/post-revisions-timeline/author-field.tsx b/packages/editor/src/components/post-revisions-timeline/author-field.tsx
index 636438c39dc8a3..f0aa4ae5c73f05 100644
--- a/packages/editor/src/components/post-revisions-timeline/author-field.tsx
+++ b/packages/editor/src/components/post-revisions-timeline/author-field.tsx
@@ -57,7 +57,8 @@ function AuthorView( { item }: { item: BasePostWithEmbeddedAuthor } ) {
>
setIsImageLoaded( true ) }
- alt={ __( 'Author avatar' ) }
+ // Decorative: the name of the author is next to it.
+ alt=""
src={ imageUrl }
/>
diff --git a/packages/media-fields/CHANGELOG.md b/packages/media-fields/CHANGELOG.md
index e08f919b1c8800..f9ad81f91a0be5 100644
--- a/packages/media-fields/CHANGELOG.md
+++ b/packages/media-fields/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+### Bug Fixes
+
+- Hide the decorative author avatar from assistive technologies, as the name of the author is next to it ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
+
## 0.23.0 (2026-10-20)
### Breaking Changes
diff --git a/packages/media-fields/src/author/view.tsx b/packages/media-fields/src/author/view.tsx
index ad8844b238f76d..733e8e6946c8d7 100644
--- a/packages/media-fields/src/author/view.tsx
+++ b/packages/media-fields/src/author/view.tsx
@@ -1,5 +1,4 @@
import clsx from 'clsx';
-import { __ } from '@wordpress/i18n';
import { useState, useCallback, useEffect } from '@wordpress/element';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';
import {
@@ -52,7 +51,8 @@ export default function AuthorView( {
diff --git a/packages/media-utils/CHANGELOG.md b/packages/media-utils/CHANGELOG.md
index 42e05b7941eae7..d6834bae5858ec 100644
--- a/packages/media-utils/CHANGELOG.md
+++ b/packages/media-utils/CHANGELOG.md
@@ -4,6 +4,7 @@
### Enhancements
+- `MediaEdit`: Show that clicking the preview of a media item replaces it ([#86150](https://github.com/WordPress/gutenberg/pull/86150)).
- `MediaEdit`: Use design system's outline focus ring instead of previous box-shadow based implementation & only animate on items reorder. ([#84940](https://github.com/WordPress/gutenberg/pull/84940)).
## 6.0.0 (2026-10-20)
diff --git a/packages/media-utils/src/components/media-edit/index.tsx b/packages/media-utils/src/components/media-edit/index.tsx
index 1d6d781042424a..19f74a3ffb5d21 100644
--- a/packages/media-utils/src/components/media-edit/index.tsx
+++ b/packages/media-utils/src/components/media-edit/index.tsx
@@ -399,6 +399,19 @@ function ExpandedMediaEditAttachments( {
{ ! isBlob && (