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 =

{ __( 'No results' ) }

, } = useContext( DataViewsContext ); + const layoutRef = useMergeRefs( [ + containerRef, + useFocusFallback( { + isEmpty: ! isLoading && paginationInfo.totalItems === 0, + isInfiniteScroll: !! view.infiniteScrollEnabled, + } ), + ] ); const isDelayedInitialLoading = useDelayedLoading( ! hasInitiallyLoaded, { delay: 200, } ); @@ -59,7 +69,7 @@ export default function DataViewsLayout( { className }: DataViewsLayoutProps ) { )?.component as ComponentType< ViewBaseProps< any > >; return ( -
+
{ 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(
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 && (
+ { /* + * The preview replaces the media when clicked, + * but only the remove button shows on hover. Say + * so on the preview too. The preview's own label + * already names the action for assistive + * technologies. + */ } + [] | null, +} = {} ) { const registry = createRegistry(); registry.register( createReduxStore( 'core', { reducer: ( state = {} ) => state, selectors: { canUser: () => canUpload, - getEntityRecords: () => null, + getEntityRecords: () => attachments, }, actions: { receiveEntityRecords: () => ( { @@ -119,6 +122,39 @@ describe( 'MediaEdit', () => { ); } ); + it( 'shows that clicking the preview replaces the media', () => { + render( + + {} } + isExpanded + /> + + ); + expect( + screen.getByRole( 'button', { name: 'Replace Photo' } ) + ).toBeInTheDocument(); + // A visual hint only: the preview's label already names the action. + expect( screen.getByText( 'Replace' ) ).toHaveAttribute( + 'aria-hidden', + 'true' + ); + } ); + it( 'shows a message instead of the picker without upload permission', () => { render( { // Use arrow up/down to move through the list. await page.keyboard.press( 'ArrowDown' ); - await expect( page.getByLabel( 'Page Two' ) ).toBeFocused(); + await expect( + page.getByRole( 'button', { name: 'Page Two', exact: true } ) + ).toBeFocused(); await page.keyboard.press( 'ArrowUp' ); - await expect( page.getByLabel( 'Page One' ) ).toBeFocused(); + await expect( + page.getByRole( 'button', { name: 'Page One', exact: true } ) + ).toBeFocused(); } ); test( 'Actions are reachable via RIGHT/LEFT arrow keys', async ( { @@ -166,7 +170,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page One ${ primaryActionLabel } Actions`, + name: 'Page One', + exact: true, } ) .getByRole( 'button', { name: primaryActionLabel } ) ).toBeFocused(); @@ -175,7 +180,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page One ${ primaryActionLabel } Actions`, + name: 'Page One', + exact: true, } ) .getByLabel( 'Actions' ) ).toBeFocused(); @@ -184,13 +190,16 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page One ${ primaryActionLabel } Actions`, + name: 'Page One', + exact: true, } ) .getByRole( 'button', { name: primaryActionLabel } ) ).toBeFocused(); await page.keyboard.press( 'ArrowLeft' ); - await expect( page.getByLabel( 'Page One' ) ).toBeFocused(); + await expect( + page.getByRole( 'button', { name: 'Page One', exact: true } ) + ).toBeFocused(); } ); test( 'Search input retains focus while typing', async ( { page } ) => { @@ -237,7 +246,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page Two ${ primaryActionLabel } Actions`, + name: 'Page Two', + exact: true, } ) .getByRole( 'button', { name: primaryActionLabel } ) ).toBeFocused(); @@ -246,7 +256,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page One ${ primaryActionLabel } Actions`, + name: 'Page One', + exact: true, } ) .getByRole( 'button', { name: primaryActionLabel } ) ).toBeFocused(); @@ -257,7 +268,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page Two ${ primaryActionLabel } Actions`, + name: 'Page Two', + exact: true, } ) .getByLabel( 'Actions' ) ).toBeFocused(); @@ -266,7 +278,8 @@ test.describe( 'Dataviews List Layout', () => { await expect( page .getByRole( 'row', { - name: `Page One ${ primaryActionLabel } Actions`, + name: 'Page One', + exact: true, } ) .getByLabel( 'Actions' ) ).toBeFocused(); diff --git a/test/e2e/specs/site-editor/page-list.spec.js b/test/e2e/specs/site-editor/page-list.spec.js index e539d2b26da2c6..218466b9cdc40d 100644 --- a/test/e2e/specs/site-editor/page-list.spec.js +++ b/test/e2e/specs/site-editor/page-list.spec.js @@ -649,7 +649,9 @@ test.describe( 'Page List', () => { toolbar.getByText( /items selected/ ) ).toBeInViewport( { ratio: 1 } ); await page.keyboard.press( 'Escape' ); - await toolbar.getByRole( 'button', { name: 'Cancel' } ).click(); + await toolbar + .getByRole( 'button', { name: 'Clear selection' } ) + .click(); await expect( page.getByRole( 'checkbox', { name: 'Select all', @@ -659,6 +661,43 @@ test.describe( 'Page List', () => { } ); } ); + test.describe( 'Trash', () => { + test.afterAll( async ( { requestUtils } ) => { + await requestUtils.deleteAllPages(); + await createPages( requestUtils ); + } ); + + test( 'moves focus to the select all checkbox after trashing the selected page', async ( { + page, + } ) => { + await page.getByRole( 'button', { name: 'Layout' } ).click(); + await page.getByRole( 'menuitemradio', { name: 'Table' } ).click(); + const table = page.getByRole( 'table' ); + await table + .getByRole( 'checkbox', { name: 'Select Sample Page' } ) + .click(); + // The toolbar comes before the rows, which have a Trash button too. + await page + .locator( '.dataviews-bulk-actions' ) + .getByRole( 'button', { name: 'Trash', exact: true } ) + .click(); + await page + .getByRole( 'dialog' ) + .getByRole( 'button', { name: 'Trash', exact: true } ) + .click(); + + await expect( + table.getByRole( 'row', { name: /Sample Page/ } ) + ).toBeHidden(); + await expect( + table.getByRole( 'checkbox', { + name: 'Select all', + exact: true, + } ) + ).toBeFocused(); + } ); + } ); + test.describe( 'Quick Edit Date Timezone Consistency', () => { const PAGE_DATE_GMT = '2026-02-15T17:30:00'; // UTC-5 offset means the displayed time should be 5 hours earlier. diff --git a/test/performance/specs/site-editor.spec.js b/test/performance/specs/site-editor.spec.js index f00f19940f83e3..237c9f5df82861 100644 --- a/test/performance/specs/site-editor.spec.js +++ b/test/performance/specs/site-editor.spec.js @@ -459,7 +459,11 @@ test.describe( 'Site Editor Performance', () => { await Promise.all( Array.from( { length: perPage }, async ( el, index ) => { return await page - .getByLabel( `Page (${ index })` ) + .getByRole( 'button', { + name: `Page (${ index })`, + exact: true, + } ) + .first() .waitFor( { state: 'attached' } ); } ) );