From 910919763a9dc0fbe4ebad06ce42cbdb16b310f1 Mon Sep 17 00:00:00 2001 From: Hrithik Dalal Date: Mon, 23 Nov 2026 10:52:49 +0530 Subject: [PATCH 1/5] DataViews: Rename the bulk actions Cancel button to Clear selection --- packages/dataviews/CHANGELOG.md | 1 + .../dataviews-bulk-actions/index.tsx | 50 +++++++++++++------ .../dataviews-bulk-actions/style.scss | 5 +- .../test/bulk-selection.browser.test.tsx | 36 ++++++++++--- .../dataviews/test/dataviews.jsdom.test.tsx | 6 ++- test/e2e/specs/site-editor/page-list.spec.js | 4 +- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index b52548d92b17fc..5869a4a55af8ee 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -20,6 +20,7 @@ - Add `aria-haspopup="dialog"` to the triggers of actions that open a modal (`RenderModal`) ([#87231](https://github.com/WordPress/gutenberg/pull/87231)). - Keep selected and hovered rows and grid cards, table header buttons, title links, and DataForm panel fields in neutral colors instead of switching to the brand color, and mark the selected item of the list layout with a gray line and selected grid cards with a dark ring. The ring around a selected grid card is drawn outside the preview, so it stays visible on any image. Filter options now match the `Select` and `Menu` items in `@wordpress/ui`, with a check mark for the selected options ([#87452](https://github.com/WordPress/gutenberg/pull/87452)). - Draw the focus rings of item titles and table thumbnails like the other DataViews focus rings, in place of the admin theme color shadow and the browser default ring ([#87691](https://github.com/WordPress/gutenberg/pull/87691)). +- Rename the "Cancel" button of the bulk actions toolbar to "Clear selection" and move it from the far end of the toolbar to right after the actions. From the `large` breakpoint up it shows as text; below that it stays an icon. Tests that find it by "Cancel" need updating ([#87232](https://github.com/WordPress/gutenberg/pull/87232)). ### Bug Fixes diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index 0b05f7159cf39d..f59b7b7644731a 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -230,25 +230,41 @@ function renderBulkActionsContent< Item >( onChangeSelection: SetSelection, totalItems: number, isMobile: boolean, + showClearSelectionText: boolean, selectionCheckboxRef?: React.Ref< HTMLSpanElement >, onClearSelection?: () => void ) { - const clearSelection = selectedItems.length > 0 && ( - { - onClearSelection?.(); - onChangeSelection( EMPTY_ARRAY ); - } } - /> - ); + // Named for what it does, so it can't be read as closing the whole view. + // A text button next to the actions on large screens, which also keeps it + // in sight for screen magnifier users. Below that, a compact icon: with a + // sidebar next to the view, the text no longer fits in the toolbar next + // to longer actions like "Permanently delete". + const clearSelectionProps = { + className: 'dataviews-bulk-actions__clear', + variant: 'minimal', + tone: 'neutral', + size: 'compact', + disabled: !! actionInProgress, + focusableWhenDisabled: false, + onClick: () => { + onClearSelection?.(); + onChangeSelection( EMPTY_ARRAY ); + }, + } as const; + let clearSelection = null; + if ( selectedItems.length > 0 ) { + clearSelection = showClearSelectionText ? ( + + ) : ( + + ); + } const renderActions = ( isMenuItem = false ) => actionsToShow.map( ( action ) => ( ( { } }; const isMobile = useViewportMatch( 'medium', '<' ); + const showClearSelectionText = useViewportMatch( 'large' ); const bulkActions = useMemo( () => actions.filter( ( action ) => action.supportsBulk ), @@ -416,6 +433,7 @@ function BulkActionsContent< Item >( { onChangeSelection, paginationInfo.totalItems, isMobile, + showClearSelectionText, selectionCheckboxRef, onClearSelection ); diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss index 5c29a3eed7f822..335fb9f7b9db1e 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss +++ b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss @@ -14,6 +14,9 @@ flex-shrink: 0; } +// Like the actions, keep it on one line rather than letting it wrap and grow +// taller than the toolbar. .dataviews-bulk-actions__clear { - margin-inline-start: auto; + flex-shrink: 0; + white-space: nowrap; } 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 07e12aaf5ce8f4..246a34c0415a9d 100644 --- a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx +++ b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx @@ -60,6 +60,23 @@ function SelectableTable( { isLoading = false } = {} ) { ); } +it( 'shows "Clear selection" as text on large screens and as an icon below', async () => { + const user = userEvent.setup(); + await render( ); + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const clear = page.getByRole( 'button', { + name: 'Clear selection', + exact: true, + } ); + await expect.element( clear ).toHaveTextContent( 'Clear selection' ); + + await page.viewport( 800, 900 ); + await expect.element( clear ).toHaveTextContent( '' ); + await expect.element( clear ).toBeVisible(); +} ); + it( 'keeps column names and sorting accessible during selection, skips covered controls, and restores sorting after deselection', async () => { const user = userEvent.setup(); await render( ); @@ -95,10 +112,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 @@ -147,7 +166,9 @@ it( 'does not move focus when the selection is cleared without an interaction', const { rerender } = await render( renderTable( [ '1', '2' ] ) ); const { ownerDocument } = page.getByRole( 'table' ).element(); await expect - .element( page.getByRole( 'button', { name: 'Cancel', exact: true } ) ) + .element( + page.getByRole( 'button', { name: 'Clear selection', exact: true } ) + ) .toBeVisible(); ( ownerDocument.activeElement as HTMLElement | null )?.blur(); @@ -168,7 +189,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' )!; @@ -278,7 +299,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(); } @@ -289,7 +313,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( diff --git a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx index 5e210d2ac432cd..a118726a8ba384 100644 --- a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx +++ b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx @@ -304,7 +304,9 @@ describe( 'DataViews component', () => { 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(); } ); @@ -429,7 +431,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( diff --git a/test/e2e/specs/site-editor/page-list.spec.js b/test/e2e/specs/site-editor/page-list.spec.js index 7ee2e596d65849..cfb9f1fba69d8d 100644 --- a/test/e2e/specs/site-editor/page-list.spec.js +++ b/test/e2e/specs/site-editor/page-list.spec.js @@ -640,7 +640,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', From b9954dd408dff49d86ea9fd896e8a9e9b181aa04 Mon Sep 17 00:00:00 2001 From: Hrithik Dalal Date: Fri, 27 Nov 2026 13:47:04 +0530 Subject: [PATCH 2/5] DataViews: Keep Clear selection as one button, shown as an icon only when the text doesn't fit --- packages/dataviews/CHANGELOG.md | 5 +- .../dataviews-bulk-actions/index.tsx | 124 ++++++++++++------ .../dataviews-bulk-actions/style.scss | 28 ++++ .../test/bulk-selection.browser.test.tsx | 17 ++- 4 files changed, 130 insertions(+), 44 deletions(-) diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index 5869a4a55af8ee..1a8673b565847a 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +- Replace the "Cancel" icon button of the bulk actions toolbar with a "Clear selection" text button, right after the actions instead of at the far end of the toolbar. When the text doesn't fit, it's an icon with a tooltip instead. Tests that find it by "Cancel" need updating ([#87232](https://github.com/WordPress/gutenberg/pull/87232)). + ### Bug Fixes - DataForm: Translate the "(Required)" and "(Optional)" suffixes of date and date time field labels together with the label, so translators can change their order ([#87487](https://github.com/WordPress/gutenberg/pull/87487)). @@ -20,7 +24,6 @@ - Add `aria-haspopup="dialog"` to the triggers of actions that open a modal (`RenderModal`) ([#87231](https://github.com/WordPress/gutenberg/pull/87231)). - Keep selected and hovered rows and grid cards, table header buttons, title links, and DataForm panel fields in neutral colors instead of switching to the brand color, and mark the selected item of the list layout with a gray line and selected grid cards with a dark ring. The ring around a selected grid card is drawn outside the preview, so it stays visible on any image. Filter options now match the `Select` and `Menu` items in `@wordpress/ui`, with a check mark for the selected options ([#87452](https://github.com/WordPress/gutenberg/pull/87452)). - Draw the focus rings of item titles and table thumbnails like the other DataViews focus rings, in place of the admin theme color shadow and the browser default ring ([#87691](https://github.com/WordPress/gutenberg/pull/87691)). -- Rename the "Cancel" button of the bulk actions toolbar to "Clear selection" and move it from the far end of the toolbar to right after the actions. From the `large` breakpoint up it shows as text; below that it stays an icon. Tests that find it by "Cancel" need updating ([#87232](https://github.com/WordPress/gutenberg/pull/87232)). ### Bug Fixes diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index f59b7b7644731a..847a73571e10cd 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -1,18 +1,29 @@ +import clsx from 'clsx'; import { __ } from '@wordpress/i18n'; -import { useMemo, useState, useContext } from '@wordpress/element'; +import { + useCallback, + useContext, + useLayoutEffect, + useMemo, + useRef, + useState, +} from '@wordpress/element'; import { useRegistry } from '@wordpress/data'; import { closeSmall, chevronDown } from '@wordpress/icons'; -import { useViewportMatch } from '@wordpress/compose'; +import { + useMergeRefs, + useResizeObserver, + useViewportMatch, +} from '@wordpress/compose'; import { // eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of Button, pending WordPress/gutenberg#76135. Button, Checkbox, Field, - // eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of IconButton, pending WordPress/gutenberg#76135. - IconButton, // eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of Menu, pending WordPress/gutenberg#76135. Menu, Stack, + Tooltip, } from '@wordpress/ui'; import DataViewsContext from '../dataviews-context'; import { ActionModal } from '../dataviews-item-actions'; @@ -230,41 +241,44 @@ function renderBulkActionsContent< Item >( onChangeSelection: SetSelection, totalItems: number, isMobile: boolean, - showClearSelectionText: boolean, + toolbarRef: React.Ref< HTMLDivElement >, + isClearSelectionIconOnly: boolean, selectionCheckboxRef?: React.Ref< HTMLSpanElement >, onClearSelection?: () => void ) { // Named for what it does, so it can't be read as closing the whole view. - // A text button next to the actions on large screens, which also keeps it - // in sight for screen magnifier users. Below that, a compact icon: with a - // sidebar next to the view, the text no longer fits in the toolbar next - // to longer actions like "Permanently delete". - const clearSelectionProps = { - className: 'dataviews-bulk-actions__clear', - variant: 'minimal', - tone: 'neutral', - size: 'compact', - disabled: !! actionInProgress, - focusableWhenDisabled: false, - onClick: () => { - onClearSelection?.(); - onChangeSelection( EMPTY_ARRAY ); - }, - } as const; - let clearSelection = null; - if ( selectedItems.length > 0 ) { - clearSelection = showClearSelectionText ? ( - - ) : ( - - ); - } + // Text next to the actions also keeps it in sight for screen magnifier + // users. When the text doesn't fit, it's an icon with a tooltip instead, + // on the same button, so it keeps focus when the view resizes. + const clearSelection = selectedItems.length > 0 && ( + + { + onClearSelection?.(); + onChangeSelection( EMPTY_ARRAY ); + } } + /> + } + > + + + { __( 'Clear selection' ) } + + + { __( 'Clear selection' ) } + + ); const renderActions = ( isMenuItem = false ) => actionsToShow.map( ( action ) => ( ( ) ); return ( ( { } }; const isMobile = useViewportMatch( 'medium', '<' ); - const showClearSelectionText = useViewportMatch( 'large' ); const bulkActions = useMemo( () => actions.filter( ( action ) => action.supportsBulk ), @@ -419,6 +435,37 @@ function BulkActionsContent< Item >( { } ), [ actions, selectedItems ] ); + // Whether "Clear selection" only fits as an icon. Measured with the text, + // as it doesn't depend on the viewport but on the room the view and the + // actions leave, which changes with their labels and translations. + const toolbarRef = useRef< HTMLDivElement >( null ); + const [ isClearSelectionIconOnly, setIsClearSelectionIconOnly ] = + useState( false ); + const updateClearSelectionFit = useCallback( () => { + const toolbar = toolbarRef.current; + if ( ! toolbar ) { + return; + } + const wasIconOnly = toolbar.classList.contains( + 'is-clear-selection-icon-only' + ); + toolbar.classList.remove( 'is-clear-selection-icon-only' ); + // Only a scroll container counts its end padding in `scrollWidth`. + const { overflow } = toolbar.style; + toolbar.style.overflow = 'hidden'; + const isOverflowing = toolbar.scrollWidth > toolbar.clientWidth; + toolbar.style.overflow = overflow; + // Leave the class to the next render. + toolbar.classList.toggle( 'is-clear-selection-icon-only', wasIconOnly ); + setIsClearSelectionIconOnly( isOverflowing ); + }, [] ); + // Whenever the content may have changed, e.g. the actions or the count, + // and when the toolbar resizes. + useLayoutEffect( updateClearSelectionFit ); + const resizeToolbarRef = useResizeObserver< HTMLDivElement >( + updateClearSelectionFit + ); + const toolbarRefs = useMergeRefs( [ toolbarRef, resizeToolbarRef ] ); const renderContent = ( actionInProgress: string | null ) => renderBulkActionsContent( data, @@ -433,7 +480,8 @@ function BulkActionsContent< Item >( { onChangeSelection, paginationInfo.totalItems, isMobile, - showClearSelectionText, + toolbarRefs, + isClearSelectionIconOnly, selectionCheckboxRef, onClearSelection ); diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss index 335fb9f7b9db1e..318faf13359d0b 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss +++ b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss @@ -20,3 +20,31 @@ flex-shrink: 0; white-space: nowrap; } + +.dataviews-bulk-actions__clear-icon { + display: none; +} + +// When the text doesn't fit, show the icon instead, and keep the text as the +// name of the button. +.dataviews-bulk-actions.is-clear-selection-icon-only { + // Square, like an icon button. + .dataviews-bulk-actions__clear { + --wp-ui-button-aspect-ratio: 1; + --wp-ui-button-padding-inline: 0px; + --wp-ui-button-min-width: unset; + } + + .dataviews-bulk-actions__clear-icon { + display: revert; + } + + .dataviews-bulk-actions__clear-text { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; + } +} 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 246a34c0415a9d..9e4c828d06df21 100644 --- a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx +++ b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx @@ -60,7 +60,7 @@ function SelectableTable( { isLoading = false } = {} ) { ); } -it( 'shows "Clear selection" as text on large screens and as an icon below', async () => { +it( 'shows "Clear selection" as text, or as an icon when the text does not fit, keeping focus', async () => { const user = userEvent.setup(); await render( ); await user.click( @@ -70,11 +70,18 @@ it( 'shows "Clear selection" as text on large screens and as an icon below', asy name: 'Clear selection', exact: true, } ); - await expect.element( clear ).toHaveTextContent( 'Clear selection' ); + clear.element().focus(); + await expect.element( clear ).toHaveFocus(); + expect( clear.element().getBoundingClientRect().width ).toBeGreaterThan( + 32 + ); - await page.viewport( 800, 900 ); - await expect.element( clear ).toHaveTextContent( '' ); - await expect.element( clear ).toBeVisible(); + await page.viewport( 320, 900 ); + await expect + .poll( () => clear.element().getBoundingClientRect().width ) + .toBe( 32 ); + await expect.element( clear ).toHaveAccessibleName( 'Clear selection' ); + await expect.element( clear ).toHaveFocus(); } ); it( 'keeps column names and sorting accessible during selection, skips covered controls, and restores sorting after deselection', async () => { From 5ab03e91939911d494b6b1abc410328326dce6db Mon Sep 17 00:00:00 2001 From: Hrithik Dalal Date: Thu, 3 Dec 2026 21:46:11 +0530 Subject: [PATCH 3/5] Keep Clear selection's fit live while an action runs, keep the count on one line, and test a narrow view --- .../dataviews-bulk-actions/index.tsx | 55 +++++++++------- .../dataviews-bulk-actions/style.scss | 2 + .../test/bulk-selection.browser.test.tsx | 65 +++++++++++++++++-- 3 files changed, 95 insertions(+), 27 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index 847a73571e10cd..255f558b4e1a4b 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -379,8 +379,12 @@ function BulkActionsContent< Item >( { onClearSelection, }: ToolbarContentProps< Item > ) { const { paginationInfo } = useContext( DataViewsContext ); + // While an action runs, the toolbar it started from, which takes whether + // "Clear selection" fits, as that can still change. const [ pendingContent, setPendingContent ] = - useState< React.JSX.Element >(); + useState< + ( isClearSelectionIconOnly: boolean ) => React.JSX.Element + >(); const registry = useRegistry(); const [ activeModal, setActiveModal ] = useState< { action: ActionModalType< Item >; @@ -391,7 +395,7 @@ function BulkActionsContent< Item >( { setActiveModal( { action, items } ); return; } - setPendingContent( renderContent( action.id ) ); + setPendingContent( () => renderContent( action.id ) ); onActionInProgressChange?.( true ); try { await action.callback( items, { registry } ); @@ -443,7 +447,9 @@ function BulkActionsContent< Item >( { useState( false ); const updateClearSelectionFit = useCallback( () => { const toolbar = toolbarRef.current; - if ( ! toolbar ) { + // Nothing to fit when nothing is selected. Checked on the toolbar, as + // the one shown while an action runs keeps its selection. + if ( ! toolbar?.querySelector( '.dataviews-bulk-actions__clear' ) ) { return; } const wasIconOnly = toolbar.classList.contains( @@ -466,28 +472,31 @@ function BulkActionsContent< Item >( { updateClearSelectionFit ); const toolbarRefs = useMergeRefs( [ toolbarRef, resizeToolbarRef ] ); - const renderContent = ( actionInProgress: string | null ) => - renderBulkActionsContent( - data, - actions, - getItemId, - isInfiniteScroll, - selection, - actionsToShow, - selectedItems, - actionInProgress, - onAction, - onChangeSelection, - paginationInfo.totalItems, - isMobile, - toolbarRefs, - isClearSelectionIconOnly, - selectionCheckboxRef, - onClearSelection - ); + const renderContent = + ( actionInProgress: string | null ) => ( isIconOnly: boolean ) => + renderBulkActionsContent( + data, + actions, + getItemId, + isInfiniteScroll, + selection, + actionsToShow, + selectedItems, + actionInProgress, + onAction, + onChangeSelection, + paginationInfo.totalItems, + isMobile, + toolbarRefs, + isIconOnly, + selectionCheckboxRef, + onClearSelection + ); return ( <> - { pendingContent ?? renderContent( null ) } + { ( pendingContent ?? renderContent( null ) )( + isClearSelectionIconOnly + ) } { activeModal && ( [] = [ { id: 'edit', label: 'Edit', supportsBulk: true, callback: () => {} }, ]; -function SelectableTable( { isLoading = false } = {} ) { +function SelectableTable( { + isLoading = false, + actions: tableActions = actions, +}: { + isLoading?: boolean; + actions?: Action< Post >[]; +} = {} ) { const [ view, setView ] = useState< View >( { type: 'table', titleField: 'title', @@ -52,7 +58,7 @@ function SelectableTable( { isLoading = false } = {} ) { fields={ fields } view={ view } onChangeView={ setView } - actions={ actions } + actions={ tableActions } isLoading={ isLoading } paginationInfo={ paginationInfo } defaultLayouts={ { table: true } } @@ -60,9 +66,15 @@ function SelectableTable( { isLoading = false } = {} ) { ); } +// Narrows the view, not the viewport, as in a sidebar. +async function renderInContainer( table: React.ReactElement ) { + await render(
{ table }
); + return page.getByTestId( 'container' ).element() as HTMLElement; +} + it( 'shows "Clear selection" as text, or as an icon when the text does not fit, keeping focus', async () => { const user = userEvent.setup(); - await render( ); + const container = await renderInContainer( ); await user.click( page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) ); @@ -70,20 +82,65 @@ it( 'shows "Clear selection" as text, or as an icon when the text does not fit, name: 'Clear selection', exact: true, } ); + const count = page.getByText( '2 items selected' ).element(); + const countHeight = count.getBoundingClientRect().height; clear.element().focus(); await expect.element( clear ).toHaveFocus(); expect( clear.element().getBoundingClientRect().width ).toBeGreaterThan( 32 ); - await page.viewport( 320, 900 ); + // Wide enough for the text only if the count wraps. + container.style.width = '340px'; await expect .poll( () => clear.element().getBoundingClientRect().width ) .toBe( 32 ); + expect( count.getBoundingClientRect().height ).toBe( countHeight ); await expect.element( clear ).toHaveAccessibleName( 'Clear selection' ); await expect.element( clear ).toHaveFocus(); } ); +it( 'shows "Clear selection" as an icon when the view narrows while an action runs', async () => { + const user = userEvent.setup(); + let finishAction = () => {}; + const container = await renderInContainer( + + new Promise< void >( ( resolve ) => { + finishAction = resolve; + } ), + }, + ] } + /> + ); + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + await user.click( page.getByRole( 'button', { name: 'Edit' } ) ); + const clear = page.getByRole( 'button', { + name: 'Clear selection', + exact: true, + } ); + await expect.element( clear ).toBeDisabled(); + expect( clear.element().getBoundingClientRect().width ).toBeGreaterThan( + 32 + ); + + container.style.width = '340px'; + await expect + .poll( () => clear.element().getBoundingClientRect().width ) + .toBe( 32 ); + + finishAction(); + await expect.element( clear ).toBeEnabled(); + expect( clear.element().getBoundingClientRect().width ).toBe( 32 ); +} ); + it( 'keeps column names and sorting accessible during selection, skips covered controls, and restores sorting after deselection', async () => { const user = userEvent.setup(); await render( ); From b0d924c20a5fb67edbb511ba523124f3d40e2c42 Mon Sep 17 00:00:00 2001 From: Hrithik Dalal Date: Thu, 3 Dec 2026 21:55:59 +0530 Subject: [PATCH 4/5] Let the count wrap once Clear selection is an icon, so the toolbar stays within narrow views --- .../src/components/dataviews-bulk-actions/index.tsx | 5 +++-- .../src/components/dataviews-bulk-actions/style.scss | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index 255f558b4e1a4b..35e5b650c1f30d 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -379,8 +379,9 @@ function BulkActionsContent< Item >( { onClearSelection, }: ToolbarContentProps< Item > ) { const { paginationInfo } = useContext( DataViewsContext ); - // While an action runs, the toolbar it started from, which takes whether - // "Clear selection" fits, as that can still change. + // While an action runs, renders the toolbar as it was when the action + // started. It takes whether "Clear selection" fits, as the view can still + // resize. const [ pendingContent, setPendingContent ] = useState< ( isClearSelectionIconOnly: boolean ) => React.JSX.Element diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss index 7f0196e0cca64e..844f32dd3377fb 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/style.scss +++ b/packages/dataviews/src/components/dataviews-bulk-actions/style.scss @@ -1,6 +1,10 @@ .dataviews-bulk-actions-footer__item-count { color: var(--wpds-color-foreground-content-neutral); - // Rather than wrap, leave the room to "Clear selection" becoming an icon. +} + +// Rather than wrap, leave the room to "Clear selection" becoming an icon. Once +// it is one, wrap as needed to keep the toolbar within the view. +.dataviews-bulk-actions:not(.is-clear-selection-icon-only) .dataviews-bulk-actions-footer__item-count { white-space: nowrap; } From e7db3da76346a4929aef3987a74b63973f2e76a8 Mon Sep 17 00:00:00 2001 From: Hrithik Dalal Date: Fri, 4 Dec 2026 11:05:46 +0530 Subject: [PATCH 5/5] Re-measure Clear selection when the content resizes, keep its tooltip closed while disabled, and test switching back --- .../dataviews-bulk-actions/index.tsx | 47 ++++++++++++++----- .../test/bulk-selection.browser.test.tsx | 45 ++++++++++++++++++ 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index 35e5b650c1f30d..252065c2463b47 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -1,7 +1,6 @@ import clsx from 'clsx'; import { __ } from '@wordpress/i18n'; import { - useCallback, useContext, useLayoutEffect, useMemo, @@ -228,6 +227,14 @@ function ActionButton< Item >( { ); } +// The elements whose size decides whether "Clear selection" fits. +type ClearSelectionFitRefs = { + toolbar: React.Ref< HTMLDivElement >; + count: React.Ref< HTMLSpanElement >; + actions: React.Ref< HTMLDivElement >; + clear: React.Ref< HTMLButtonElement >; +}; + function renderBulkActionsContent< Item >( data: Item[], actions: Action< Item >[], @@ -241,7 +248,7 @@ function renderBulkActionsContent< Item >( onChangeSelection: SetSelection, totalItems: number, isMobile: boolean, - toolbarRef: React.Ref< HTMLDivElement >, + fitRefs: ClearSelectionFitRefs, isClearSelectionIconOnly: boolean, selectionCheckboxRef?: React.Ref< HTMLSpanElement >, onClearSelection?: () => void @@ -253,8 +260,12 @@ function renderBulkActionsContent< Item >( const clearSelection = selectedItems.length > 0 && ( ( ) ); return ( ( ) } { !! selection.length && ( - + { getFooterMessage( selection.length, data.length, @@ -331,6 +345,7 @@ function renderBulkActionsContent< Item >( ) } ( { const toolbarRef = useRef< HTMLDivElement >( null ); const [ isClearSelectionIconOnly, setIsClearSelectionIconOnly ] = useState( false ); - const updateClearSelectionFit = useCallback( () => { + const updateClearSelectionFit = () => { const toolbar = toolbarRef.current; // Nothing to fit when nothing is selected. Checked on the toolbar, as // the one shown while an action runs keeps its selection. @@ -465,14 +480,20 @@ function BulkActionsContent< Item >( { // Leave the class to the next render. toolbar.classList.toggle( 'is-clear-selection-icon-only', wasIconOnly ); setIsClearSelectionIconOnly( isOverflowing ); - }, [] ); - // Whenever the content may have changed, e.g. the actions or the count, - // and when the toolbar resizes. + }; + // Measured whenever the content may have changed, e.g. the actions or the + // count, and when the toolbar or its content resizes without a render, e.g. + // when text spacing changes or a font loads. useLayoutEffect( updateClearSelectionFit ); - const resizeToolbarRef = useResizeObserver< HTMLDivElement >( - updateClearSelectionFit - ); - const toolbarRefs = useMergeRefs( [ toolbarRef, resizeToolbarRef ] ); + const fitRefs: ClearSelectionFitRefs = { + toolbar: useMergeRefs( [ + toolbarRef, + useResizeObserver( updateClearSelectionFit ), + ] ), + count: useResizeObserver( updateClearSelectionFit ), + actions: useResizeObserver( updateClearSelectionFit ), + clear: useResizeObserver( updateClearSelectionFit ), + }; const renderContent = ( actionInProgress: string | null ) => ( isIconOnly: boolean ) => renderBulkActionsContent( @@ -488,7 +509,7 @@ function BulkActionsContent< Item >( { onChangeSelection, paginationInfo.totalItems, isMobile, - toolbarRefs, + fitRefs, isIconOnly, selectionCheckboxRef, onClearSelection 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 40eb0d58b52bd4..d78eb4de66407a 100644 --- a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx +++ b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx @@ -66,6 +66,14 @@ function SelectableTable( { ); } +// The tooltip, rather than the visually hidden text of the button. +function getClearSelectionTooltip() { + return page + .getByText( 'Clear selection', { exact: true } ) + .elements() + .find( ( element ) => ! element.closest( 'button' ) ); +} + // Narrows the view, not the viewport, as in a sidebar. async function renderInContainer( table: React.ReactElement ) { await render(
{ table }
); @@ -98,6 +106,43 @@ it( 'shows "Clear selection" as text, or as an icon when the text does not fit, expect( count.getBoundingClientRect().height ).toBe( countHeight ); await expect.element( clear ).toHaveAccessibleName( 'Clear selection' ); await expect.element( clear ).toHaveFocus(); + const icon = clear.element().querySelector( 'svg' ); + expect( icon ).toBeVisible(); + await user.hover( clear.element() ); + await expect + .poll( getClearSelectionTooltip, { timeout: 3000 } ) + .toBeVisible(); + + container.style.width = ''; + await expect + .poll( () => clear.element().getBoundingClientRect().width ) + .toBeGreaterThan( 32 ); + expect( icon ).not.toBeVisible(); + await expect.poll( getClearSelectionTooltip ).toBeUndefined(); + await expect.element( clear ).toHaveFocus(); +} ); + +it( 'shows "Clear selection" as an icon when the text grows without a resize', async () => { + const user = userEvent.setup(); + const container = await renderInContainer( ); + container.style.width = '390px'; + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const clear = page.getByRole( 'button', { + name: 'Clear selection', + exact: true, + } ); + expect( clear.element().getBoundingClientRect().width ).toBeGreaterThan( + 32 + ); + + // The text spacing of WCAG 1.4.12, as a bookmarklet would set it. + container.style.letterSpacing = '0.12em'; + container.style.wordSpacing = '0.16em'; + await expect + .poll( () => clear.element().getBoundingClientRect().width ) + .toBe( 32 ); } ); it( 'shows "Clear selection" as an icon when the view narrows while an action runs', async () => {