diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index 17f79c819e48ba..a2db9e8afb1fe2 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -8,6 +8,7 @@ ### Enhancements +- Render table bulk actions outside the table while preserving their placement over the column headers ([#85250](https://github.com/WordPress/gutenberg/pull/85250)). - Use the design system outline for keyboard focus on list items ([#85358](https://github.com/WordPress/gutenberg/pull/85358)). - Simplify filter selection indicators and remove their hover colors ([#85191](https://github.com/WordPress/gutenberg/pull/85191)). diff --git a/packages/dataviews/src/components/dataviews-layouts/table/index.tsx b/packages/dataviews/src/components/dataviews-layouts/table/index.tsx index e9c6e816bab42f..7c790ae69e060d 100644 --- a/packages/dataviews/src/components/dataviews-layouts/table/index.tsx +++ b/packages/dataviews/src/components/dataviews-layouts/table/index.tsx @@ -2,10 +2,12 @@ import clsx from 'clsx'; import type { ComponentProps, CSSProperties, ReactElement } from 'react'; import { __, sprintf, isRTL } from '@wordpress/i18n'; import { Spinner as WCSpinner, Popover } from '@wordpress/components'; +import { useMergeRefs, useResizeObserver } from '@wordpress/compose'; import { useContext, useEffect, useId, + useLayoutEffect, useRef, useState, } from '@wordpress/element'; @@ -316,10 +318,32 @@ function ViewTable< Item >( { const tableSelectionRef = useRef< HTMLSpanElement >( null ); const bulkSelectionRef = useRef< HTMLSpanElement >( null ); const bulkActionsRef = useRef< HTMLDivElement >( null ); + // Place the bulk actions toolbar over the header by writing to the DOM + // directly: it is correct on first paint and skips a re-render. The sticky + // offset is copied so the toolbar follows a header that consumers move. + const syncBulkActionsPosition = () => { + const header = tableHeaderRef.current; + const overlay = bulkActionsRef.current; + const win = header?.ownerDocument.defaultView; + if ( header && overlay && win ) { + overlay.style.setProperty( + '--wp-dataviews-table-header-height', + `${ header.getBoundingClientRect().height }px` + ); + overlay.style.insetBlockStart = + win.getComputedStyle( header ).insetBlockStart; + } + }; + const resizeHeaderRef = useResizeObserver< HTMLTableSectionElement >( + syncBulkActionsPosition + ); + const tableHeaderRefs = useMergeRefs( [ tableHeaderRef, resizeHeaderRef ] ); const [ isActionInProgress, setIsActionInProgress ] = useState( false ); const hasBulkActions = useSomeItemHasAPossibleBulkAction( actions, data ); const showBulkActions = ( hasBulkActions && selection.length > 0 ) || isActionInProgress; + // The resize observer reports after the first paint, so measure on show. + useLayoutEffect( syncBulkActionsPosition, [ showBulkActions ] ); const hadSelectionRef = useRef( false ); useEffect( () => { const tableHead = tableHeaderRef.current; @@ -453,6 +477,25 @@ function ViewTable< Item >( { return ( <> + { ( hasBulkActions || isActionInProgress ) && ( + + ) } ( { ) } ( { ) } - { ( hasBulkActions || isActionInProgress ) && ( - - - - ) } { /* Render grouped data if groupBy is specified */ } { hasData && groupField && dataByGroup ? ( diff --git a/packages/dataviews/src/components/dataviews-layouts/table/style.scss b/packages/dataviews/src/components/dataviews-layouts/table/style.scss index 1a93ef06b7288e..c0e74526c461fb 100644 --- a/packages/dataviews/src/components/dataviews-layouts/table/style.scss +++ b/packages/dataviews/src/components/dataviews-layouts/table/style.scss @@ -7,6 +7,9 @@ border-color: inherit; border-collapse: collapse; position: relative; + // Keep the sticky header's z-index inside the table, so the bulk actions + // toolbar (a sibling) covers it without rising above the page's own layers. + isolation: isolate; color: var(--wpds-color-foreground-content-neutral-weak); margin-bottom: auto; background-color: inherit; @@ -345,26 +348,35 @@ .dataviews-view-table.is-refreshing { @include dataviews-refreshing(); } -.dataviews-view-table .dataviews-view-table__bulk-actions-row { - height: 0; - border: 0; - - > td { - height: 0; - padding: 0; - } -} .dataviews-view-table__bulk-actions-overlay { - position: absolute; - inset: 0; - z-index: 2; + position: sticky; + inset-block-start: 0; + inset-inline-start: 0; + z-index: 1; + height: 0; + flex-shrink: 0; > .dataviews-bulk-actions { - position: sticky; - inset-inline-start: 0; - width: 100cqi; - height: 100%; + position: absolute; + inset-inline: 0; + inset-block-start: 0; + height: var(--wp-dataviews-table-header-height); min-height: 0; } + + &.is-refreshing { + // Keep covered headers behind an opaque surface while the toolbar fades. + &::before { + content: ""; + position: absolute; + inset: 0; + height: var(--wp-dataviews-table-header-height); + background: var(--wp-dataviews-color-background, var(--wpds-color-background-surface-neutral-strong)); + } + + > .dataviews-bulk-actions { + @include dataviews-refreshing(); + } + } } diff --git a/packages/dataviews/src/dataviews/stories/free-composition.tsx b/packages/dataviews/src/dataviews/stories/free-composition.tsx index 8d556678f640ad..f976f941d97fe7 100644 --- a/packages/dataviews/src/dataviews/stories/free-composition.tsx +++ b/packages/dataviews/src/dataviews/stories/free-composition.tsx @@ -95,7 +95,7 @@ function PlanetOverview( { planets }: { planets: SpaceObject[] } ) { - + ); } diff --git a/packages/dataviews/src/dataviews/stories/style.module.css b/packages/dataviews/src/dataviews/stories/style.module.css index 8787ea479818da..653d9185a30add 100644 --- a/packages/dataviews/src/dataviews/stories/style.module.css +++ b/packages/dataviews/src/dataviews/stories/style.module.css @@ -10,10 +10,6 @@ background-color: #fff; } -.layout:global(.dataviews-view-table) thead { - inset-block-start: 67px; -} - .empty { border: 1px solid #000; border-radius: 8px; 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 2f7025fabd623f..9b03ad99e3fd27 100644 --- a/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx +++ b/packages/dataviews/src/dataviews/test/bulk-selection.browser.test.tsx @@ -1,7 +1,7 @@ import { render } from 'vitest-browser-react'; import { page, userEvent } from 'vitest/browser'; import { afterEach, beforeEach, expect, it } from 'vitest'; -import { useState } from '@wordpress/element'; +import { useLayoutEffect, useState } from '@wordpress/element'; import '@wordpress/theme/design-tokens.css'; import DataViews from '../index'; import type { Action, Field, View } from '../../types'; @@ -32,7 +32,7 @@ const actions: Action< Post >[] = [ { id: 'edit', label: 'Edit', supportsBulk: true, callback: () => {} }, ]; -function SelectableTable() { +function SelectableTable( { isLoading = false } = {} ) { const [ view, setView ] = useState< View >( { type: 'table', titleField: 'title', @@ -53,6 +53,7 @@ function SelectableTable() { view={ view } onChangeView={ setView } actions={ actions } + isLoading={ isLoading } paginationInfo={ paginationInfo } defaultLayouts={ { table: true } } /> @@ -125,6 +126,272 @@ it( 'keeps column names and sorting accessible during selection, skips covered c .not.toHaveAttribute( 'aria-sort' ); } ); +it( 'blocks focus on bulk actions and covers column headings while loading', async () => { + const { rerender } = await render( ); + const user = userEvent.setup(); + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const cancel = page + .getByRole( 'button', { name: 'Cancel', exact: true } ) + .element(); + const table = page.getByRole( 'table' ).element(); + const toolbar = document.querySelector( '.dataviews-bulk-actions' )!; + const overlay = toolbar.parentElement!; + await rerender( ); + cancel.focus(); + expect( cancel ).not.toHaveFocus(); + await expect + .poll( () => { + const style = getComputedStyle( table ); + return ( + style.animationName !== 'none' || Number( style.opacity ) < 1 + ); + } ) + .toBe( true ); + expect( getComputedStyle( overlay ).opacity ).toBe( '1' ); + expect( getComputedStyle( overlay ).animationName ).toBe( 'none' ); + const toolbarStyle = getComputedStyle( toolbar ); + expect( + toolbarStyle.animationName !== 'none' || + Number( toolbarStyle.opacity ) < 1 + ).toBe( true ); + const backing = getComputedStyle( overlay, '::before' ); + expect( backing.content ).toBe( '""' ); + expect( backing.backgroundColor ).toBe( + getComputedStyle( toolbar ).backgroundColor + ); + expect( backing.backgroundColor ).not.toBe( 'rgba(0, 0, 0, 0)' ); + await rerender( ); + cancel.focus(); + expect( cancel ).toHaveFocus(); +} ); + +it.each( [ 'balanced', 'compact', 'comfortable' ] as const )( + 'keeps the %s table toolbar aligned during scrolling and resizing', + async ( density ) => { + const data = Array.from( { length: 20 }, ( _, index ) => ( { + ...posts[ 0 ], + id: String( index ), + title: `Post ${ index }`, + } ) ); + await render( +
+ {} } + actions={ actions } + paginationInfo={ { + totalItems: data.length, + totalPages: 1, + } } + defaultLayouts={ { table: true } } + /> +
+ ); + const table = page.getByRole( 'table' ).element(); + const header = table.querySelector( 'thead' )!; + const scroller = table.parentElement!; + const firstRowTop = table + .querySelector( 'tbody' )! + .getBoundingClientRect().top; + const user = userEvent.setup(); + await user.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const toolbar = document.querySelector< HTMLDivElement >( + '.dataviews-bulk-actions' + )!; + expect( table ).not.toContainElement( toolbar ); + expect( + table.querySelector( 'tbody' )!.getBoundingClientRect().top + ).toBe( firstRowTop ); + + for ( const width of [ 1280, 390 ] ) { + await page.viewport( width, 900 ); + scroller.scrollTop = scroller.scrollHeight; + scroller.scrollLeft = scroller.scrollWidth; + expect( scroller.scrollTop ).toBeGreaterThan( 0 ); + expect( scroller.scrollLeft ).toBeGreaterThan( 0 ); + await expect + .poll( () => toolbar.getBoundingClientRect().height ) + .toBe( header.getBoundingClientRect().height ); + await expect + .poll( () => toolbar.getBoundingClientRect().top ) + .toBe( scroller.getBoundingClientRect().top ); + // Scroll extents round to whole pixels, unlike fractional sticky bounds. + expect( + Math.abs( + toolbar.getBoundingClientRect().left - + scroller.getBoundingClientRect().left + ) + ).toBeLessThanOrEqual( 0.5 ); + expect( + Math.abs( + toolbar.getBoundingClientRect().width - scroller.clientWidth + ) + ).toBeLessThanOrEqual( 0.5 ); + await expect + .element( + page.getByRole( 'button', { name: 'Cancel', exact: true } ) + ) + .toBeInViewport(); + } + + // Custom headers can change height after mount, independently of density. + header.style.height = '96px'; + await expect + .poll( () => toolbar.getBoundingClientRect().height ) + .toBe( 96 ); + await user.click( + page.getByRole( 'button', { name: 'Cancel', exact: true } ) + ); + await expect + .element( + page.getByRole( 'checkbox', { + name: 'Select all', + exact: true, + } ) + ) + .toHaveFocus(); + } +); + +it.each( [ 'balanced', 'compact', 'comfortable' ] as const )( + 'sizes the %s table toolbar to the header before the first paint', + async ( density ) => { + const heights: Array< { toolbar: number; header: number } > = []; + // Layout effects of a later sibling run after the table commits and + // before the browser paints, so this records the first painted frame. + function MeasureFirstFrame() { + useLayoutEffect( () => { + heights.push( { + toolbar: document + .querySelector( '.dataviews-bulk-actions' )! + .getBoundingClientRect().height, + header: document + .querySelector( 'thead' )! + .getBoundingClientRect().height, + } ); + }, [] ); + return null; + } + await render( + <> + {} } + actions={ actions } + selection={ [ '1' ] } + onChangeSelection={ () => {} } + paginationInfo={ { totalItems: 2, totalPages: 1 } } + defaultLayouts={ { table: true } } + /> + + + ); + expect( heights ).toHaveLength( 1 ); + expect( heights[ 0 ].toolbar ).toBe( heights[ 0 ].header ); + } +); + +it( 'keeps the table toolbar over a header that consumers offset', async () => { + const data = Array.from( { length: 20 }, ( _, index ) => ( { + ...posts[ 0 ], + id: String( index ), + title: `Post ${ index }`, + } ) ); + await render( +
+ + {} } + actions={ actions } + paginationInfo={ { totalItems: data.length, totalPages: 1 } } + defaultLayouts={ { table: true } } + > + + +
+ ); + await userEvent.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const header = document.querySelector( 'thead' )!; + const toolbar = document.querySelector( '.dataviews-bulk-actions' )!; + const scroller = document.querySelector( '.dataviews-layout__container' )!; + for ( const scrollTop of [ 0, scroller.scrollHeight ] ) { + scroller.scrollTop = scrollTop; + await expect + .poll( () => toolbar.getBoundingClientRect().top ) + .toBe( header.getBoundingClientRect().top ); + } +} ); + +it( 'keeps the table toolbar below sticky page elements', async () => { + const data = Array.from( { length: 20 }, ( _, index ) => ( { + ...posts[ 0 ], + id: String( index ), + title: `Post ${ index }`, + } ) ); + await render( + <> +
+ {} } + actions={ actions } + paginationInfo={ { totalItems: data.length, totalPages: 1 } } + defaultLayouts={ { table: true } } + /> + + ); + await userEvent.click( + page.getByRole( 'checkbox', { name: 'Select all', exact: true } ) + ); + const toolbar = document.querySelector( '.dataviews-bulk-actions' )!; + // Scroll the page so the toolbar passes under the sticky heading. + window.scrollBy( 0, toolbar.getBoundingClientRect().top - 20 ); + const { left, width } = toolbar.getBoundingClientRect(); + await expect + .poll( () => document.elementFromPoint( left + width / 2, 40 ) ) + .toBe( page.getByTestId( 'page-heading' ).element() ); + window.scrollTo( 0, 0 ); +} ); + it( 'keeps a grid item checkbox below the sticky toolbar after arrow navigation and Tab', async () => { const data = Array.from( { length: 8 }, ( _, index ) => ( { ...posts[ 0 ], diff --git a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx index a0b7faf6c7649d..c18115ef5d5031 100644 --- a/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx +++ b/packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx @@ -163,7 +163,7 @@ vi.mock( import( '@wordpress/compose' ), async ( importOriginal ) => { setTimeout( () => { callback( [ { - borderBoxSize: [ { inlineSize: 500 } ], + borderBoxSize: [ { inlineSize: 500, blockSize: 48 } ], }, ] ); }, 0 ); @@ -302,7 +302,7 @@ describe( 'DataViews component', () => { expect( callback ).not.toHaveBeenCalled(); } ); - it( 'moves default table bulk actions into the header and preserves focus', async () => { + it( 'shows bulk actions outside the table without adding rows and preserves focus', async () => { render( ); expect( screen.getByText( '3 items' ) ).toBeInTheDocument(); expect( @@ -317,9 +317,9 @@ describe( 'DataViews component', () => { const tableHeader = within( screen.getByRole( 'table' ) ).getAllByRole( 'rowgroup' )[ 0 ]; - const toolbarCell = within( tableHeader ).getByRole( 'cell', { - name: /3 items selected/, - } ); + const toolbar = getBulkActionToolbar(); + expect( screen.getByRole( 'table' ) ).not.toContainElement( toolbar ); + expect( within( tableHeader ).getAllByRole( 'row' ) ).toHaveLength( 1 ); const titleHeader = within( tableHeader ).getByRole( 'columnheader', { name: 'Title', } ); @@ -327,7 +327,7 @@ describe( 'DataViews component', () => { within( titleHeader ).getByRole( 'button', { hidden: true } ) ).toHaveAttribute( 'inert' ); expect( - within( toolbarCell ).getByRole( 'checkbox', { + within( toolbar ).getByRole( 'checkbox', { name: 'Deselect all', } ) ).toHaveFocus(); @@ -336,7 +336,7 @@ describe( 'DataViews component', () => { ).toBeInTheDocument(); await user.click( - within( toolbarCell ).getByRole( 'button', { name: 'Cancel' } ) + within( toolbar ).getByRole( 'button', { name: 'Cancel' } ) ); 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 26caa080b6dc45..e539d2b26da2c6 100644 --- a/test/e2e/specs/site-editor/page-list.spec.js +++ b/test/e2e/specs/site-editor/page-list.spec.js @@ -592,14 +592,14 @@ test.describe( 'Page List', () => { await table.getByRole( 'checkbox', { name: 'Select all' } ).click(); // The extensible site editor keeps the selection in the URL, so // the checkbox only flips once the route has re-rendered. - const bulkActions = table.getByRole( 'cell', { - name: /items selected/, - } ); await expect( - bulkActions.getByRole( 'checkbox', { name: 'Deselect all' } ) + page.getByRole( 'checkbox', { + name: 'Deselect all', + exact: true, + } ) ).toBeChecked(); - await bulkActions - .getByRole( 'button', { name: 'Bulk edit' } ) + await page + .getByRole( 'button', { name: 'Bulk edit', exact: true } ) .click(); const modal = page.locator( '.dataviews-action-modal__quick-edit' );