Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a rebase, the CHANGELOG conflicts with trunk.


### 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)).
Expand Down
175 changes: 136 additions & 39 deletions packages/dataviews/src/components/dataviews-bulk-actions/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 >[],
Expand All @@ -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 && (
<IconButton
className="dataviews-bulk-actions__clear"
variant="minimal"
tone="neutral"
size="compact"
icon={ closeSmall }
label={ __( 'Cancel' ) }
disabled={ !! actionInProgress }
focusableWhenDisabled={ false }
onClick={ () => {
onClearSelection?.();
onChangeSelection( EMPTY_ARRAY );
} }
/>
<Tooltip.Root disabled={ ! isClearSelectionIconOnly }>
<Tooltip.Trigger

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: IconButton also passes disabled to Tooltip.Trigger, so the tooltip stays closed while an action runs. Want to add disabled={ !! actionInProgress } here to match?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. The trigger's disabled overrides the root's, so it's ! isClearSelectionIconOnly || !! actionInProgress there. In Chrome the disabled button doesn't get hover anyway, so I couldn't make a test fail without it, but it matches IconButton now.

// Overrides the root's `disabled`, so it repeats it. Like
// `IconButton`, also keeps the tooltip closed while disabled.
disabled={ ! isClearSelectionIconOnly || !! actionInProgress }
render={
<Button
ref={ fitRefs.clear }
className="dataviews-bulk-actions__clear"
variant="minimal"
tone="neutral"
size="compact"
disabled={ !! actionInProgress }
focusableWhenDisabled={ false }
onClick={ () => {
onClearSelection?.();
onChangeSelection( EMPTY_ARRAY );
} }
/>
}
>
<Button.Icon
className="dataviews-bulk-actions__clear-icon"
icon={ closeSmall }
/>
<span className="dataviews-bulk-actions__clear-text">
{ __( 'Clear selection' ) }
</span>
</Tooltip.Trigger>
<Tooltip.Popup>{ __( 'Clear selection' ) }</Tooltip.Popup>
</Tooltip.Root>
);
const renderActions = ( isMenuItem = false ) =>
actionsToShow.map( ( action ) => (
Expand All @@ -262,8 +303,11 @@ function renderBulkActionsContent< Item >(
) );
return (
<Stack
ref={ fitRefs.toolbar }
direction="row"
className="dataviews-bulk-actions"
className={ clsx( 'dataviews-bulk-actions', {
'is-clear-selection-icon-only': isClearSelectionIconOnly,
} ) }
gap="md"
align="center"
justify="start"
Expand All @@ -288,7 +332,10 @@ function renderBulkActionsContent< Item >(
) }
</Field.Root>
{ !! selection.length && (
<span className="dataviews-bulk-actions-footer__item-count">
<span
ref={ fitRefs.count }
className="dataviews-bulk-actions-footer__item-count"
>
{ getFooterMessage(
selection.length,
data.length,
Expand All @@ -298,6 +345,7 @@ function renderBulkActionsContent< Item >(
</span>
) }
<Stack
ref={ fitRefs.actions }
direction="row"
className="dataviews-bulk-actions__actions"
gap="md"
Expand Down Expand Up @@ -346,8 +394,13 @@ function BulkActionsContent< 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 >;
Expand All @@ -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 } );
Expand Down Expand Up @@ -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 );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this measures on every render, including in the grid before anything is selected, when there's no clear button to fit. An early return when nothing's selected would skip that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an early return when there's no Clear selection button in the toolbar. I check the toolbar rather than selectedItems, since the toolbar shown while an action runs keeps its selection even when the items are already gone from data (e.g. after a trash).

const fitRefs: ClearSelectionFitRefs = {
toolbar: useMergeRefs( [
toolbarRef,
useResizeObserver( updateClearSelectionFit ),
] ),
count: useResizeObserver( updateClearSelectionFit ),
actions: useResizeObserver( updateClearSelectionFit ),
clear: useResizeObserver( updateClearSelectionFit ),
};
Comment on lines +488 to +496

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, not blocking: each useResizeObserver makes its own observer, so a font load or a narrow resize can run the measurement (and force a layout) up to four times in one frame. One ResizeObserver on the toolbar and its children, like the Tabs list in @wordpress/ui, would only measure once.

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 && (
<ActionModal
action={ activeModal.action }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
color: var(--wpds-color-foreground-content-neutral);
}

// 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;
}

.dataviews-bulk-actions {
box-sizing: border-box;
min-height: 52px;
Expand All @@ -14,6 +20,37 @@
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 {
Comment on lines +23 to 25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The item count can still wrap, though. It's the only thing in the row that shrinks, so when space runs out it drops to two lines before the toolbar overflows, and "Clear selection" keeps its text the whole time. white-space: nowrap on .dataviews-bulk-actions-footer__item-count should make the button switch to the icon first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The count now stays on one line while "Clear selection" shows its text, so between about 340 and 360px it switches to the icon instead of wrapping. Once it's an icon the count can wrap again, otherwise the toolbar overflowed below 320px.

margin-inline-start: auto;
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;
}
}
Loading
Loading