diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index b52548d92b17fc..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)). diff --git a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx index 0b05f7159cf39d..252065c2463b47 100644 --- a/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx +++ b/packages/dataviews/src/components/dataviews-bulk-actions/index.tsx @@ -1,18 +1,28 @@ +import clsx from 'clsx'; import { __ } from '@wordpress/i18n'; -import { useMemo, useState, useContext } from '@wordpress/element'; +import { + 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'; @@ -217,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 >[], @@ -230,24 +248,47 @@ function renderBulkActionsContent< Item >( onChangeSelection: SetSelection, totalItems: number, isMobile: boolean, + fitRefs: ClearSelectionFitRefs, + isClearSelectionIconOnly: boolean, selectionCheckboxRef?: React.Ref< HTMLSpanElement >, onClearSelection?: () => void ) { + // Named for what it does, so it can't be read as closing the whole view. + // 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 ); - } } - /> + + { + onClearSelection?.(); + onChangeSelection( EMPTY_ARRAY ); + } } + /> + } + > + + + { __( 'Clear selection' ) } + + + { __( 'Clear selection' ) } + ); const renderActions = ( isMenuItem = false ) => actionsToShow.map( ( action ) => ( @@ -262,8 +303,11 @@ function renderBulkActionsContent< Item >( ) ); return ( ( ) } { !! selection.length && ( - + { getFooterMessage( selection.length, data.length, @@ -298,6 +345,7 @@ function renderBulkActionsContent< Item >( ) } ( { onClearSelection, }: ToolbarContentProps< Item > ) { const { paginationInfo } = useContext( DataViewsContext ); + // 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< React.JSX.Element >(); + useState< + ( isClearSelectionIconOnly: boolean ) => React.JSX.Element + >(); const registry = useRegistry(); const [ activeModal, setActiveModal ] = useState< { action: ActionModalType< Item >; @@ -358,7 +411,7 @@ function BulkActionsContent< Item >( { setActiveModal( { action, items } ); return; } - setPendingContent( renderContent( action.id ) ); + setPendingContent( () => renderContent( action.id ) ); onActionInProgressChange?.( true ); try { await action.callback( items, { registry } ); @@ -402,26 +455,70 @@ function BulkActionsContent< Item >( { } ), [ actions, selectedItems ] ); - const renderContent = ( actionInProgress: string | null ) => - renderBulkActionsContent( - data, - actions, - getItemId, - isInfiniteScroll, - selection, - actionsToShow, - selectedItems, - actionInProgress, - onAction, - onChangeSelection, - paginationInfo.totalItems, - isMobile, - selectionCheckboxRef, - onClearSelection + // 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 = () => { + 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. + if ( ! toolbar?.querySelector( '.dataviews-bulk-actions__clear' ) ) { + 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 ); + }; + // 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 fitRefs: ClearSelectionFitRefs = { + toolbar: useMergeRefs( [ + toolbarRef, + useResizeObserver( updateClearSelectionFit ), + ] ), + count: useResizeObserver( updateClearSelectionFit ), + actions: useResizeObserver( updateClearSelectionFit ), + clear: useResizeObserver( updateClearSelectionFit ), + }; + const renderContent = + ( actionInProgress: string | null ) => ( isIconOnly: boolean ) => + renderBulkActionsContent( + data, + actions, + getItemId, + isInfiniteScroll, + selection, + actionsToShow, + selectedItems, + actionInProgress, + onAction, + onChangeSelection, + paginationInfo.totalItems, + isMobile, + fitRefs, + 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,6 +66,126 @@ function SelectableTable( { isLoading = false } = {} ) { ); } +// 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 }
); + 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(); + const container = await renderInContainer( ); + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const clear = page.getByRole( 'button', { + 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 + ); + + // 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(); + 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 () => { + 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( ); @@ -95,10 +221,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 +275,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 +298,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 +408,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 +422,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',