Repository navigation
DataViews: Rename the bulk actions Cancel button to Clear selection #87232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
9109197
b9954dd
5ab03e9
b0d924c
e7db3da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; | ||
|
|
@@ -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 && ( | ||
| <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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. The trigger's |
||
| // 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 ) => ( | ||
|
|
@@ -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" | ||
|
|
@@ -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, | ||
|
|
@@ -298,6 +345,7 @@ function renderBulkActionsContent< Item >( | |
| </span> | ||
| ) } | ||
| <Stack | ||
| ref={ fitRefs.actions } | ||
| direction="row" | ||
| className="dataviews-bulk-actions__actions" | ||
| gap="md" | ||
|
|
@@ -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 >; | ||
|
|
@@ -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 ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| const fitRefs: ClearSelectionFitRefs = { | ||
| toolbar: useMergeRefs( [ | ||
| toolbarRef, | ||
| useResizeObserver( updateClearSelectionFit ), | ||
| ] ), | ||
| count: useResizeObserver( updateClearSelectionFit ), | ||
| actions: useResizeObserver( updateClearSelectionFit ), | ||
| clear: useResizeObserver( updateClearSelectionFit ), | ||
| }; | ||
|
Comment on lines
+488
to
+496
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional, not blocking: each |
||
| 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 } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.