Skip to content
Closed
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/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- List View: Add an appender button to the row of a block with inner blocks, so items can be added to a submenu or a Buttons block from the List View. The button is visible at rest and takes the appender style while the row, or a block inside it, is hovered, focused, or selected ([#85154](https://github.com/WordPress/gutenberg/issues/85154)).

### Bug Fixes

- Position, Text shadow: Show the "Default" option as selected instead of the `Select` placeholder ([#85172](https://github.com/WordPress/gutenberg/pull/85172)).
Expand Down
18 changes: 16 additions & 2 deletions packages/block-editor/src/components/list-view/appender.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import AriaReferencedText from './aria-referenced-text';
import { unlock } from '../../lock-unlock';

export const Appender = forwardRef(
( { nestingLevel, blockCount, clientId, ...props }, ref ) => {
(
{
nestingLevel,
blockCount,
clientId,
toggleProps,
onSelectOrClose,
...props
},
ref
) => {
const { setInsertedBlockClientId } = useListViewContext();
const insertedBlockClientId = useInsertedBlockClientId();

Expand Down Expand Up @@ -88,13 +98,17 @@ export const Appender = forwardRef(
shouldDirectInsert={ directInsert }
__experimentalIsQuick
{ ...props }
toggleProps={ { 'aria-describedby': descriptionId } }
toggleProps={ {
...toggleProps,
'aria-describedby': descriptionId,
} }
onSelectOrClose={ ( maybeInsertedBlock ) => {
if ( maybeInsertedBlock?.clientId ) {
setInsertedBlockClientId(
maybeInsertedBlock.clientId
);
}
onSelectOrClose?.( maybeInsertedBlock );
} }
/>
<AriaReferencedText id={ descriptionId }>
Expand Down
97 changes: 97 additions & 0 deletions packages/block-editor/src/components/list-view/block.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BlockMoverDownButton,
} from '../block-mover/button';
import ListViewBlockContents from './block-contents';
import { Appender } from './appender';
import { useListViewContext } from './context';
import {
getBlockPositionDescription,
Expand All @@ -41,6 +42,75 @@ import { unlock } from '../../lock-unlock';
import usePasteStyles from '../use-paste-styles';
import { getBlockVisibilityLabel } from '../block-visibility';

/**
* The cell holding a row's appender button, which adds a block at the end of
* the row's inner blocks. The button shows while the row is hovered, focused,
* or selected, and stays while the user is on one of the blocks inside the
* row, hovering, focusing, or selecting it. Rows highlight the block they are
* on, so the highlight stands in for hover and focus.
*
* @param {Object} props
* @param {string} props.clientId The row's block.
* @param {number} props.level The row's nesting level.
* @param {number} props.innerBlockCount The number of blocks inside the row's block.
* @param {boolean} props.isSelected Whether the row's block is selected.
* @param {boolean} props.isVisible Whether the row itself shows its controls.
* @param {( update: Object ) => void} props.updateExpansion Expands or collapses rows.
*/
function ListViewRowAppenderCell( {
clientId,
level,
innerBlockCount,
isSelected,
isVisible,
updateExpansion,
} ) {
const hasActiveDescendant = useSelect(
( select ) => {
const {
getClientIdsOfDescendants,
getSelectedBlockClientIds,
isBlockHighlighted,
} = select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
return getClientIdsOfDescendants( clientId ).some(
( descendantClientId ) =>
isBlockHighlighted( descendantClientId ) ||
selectedBlockClientIds.includes( descendantClientId )
);
},
[ clientId ]
);

return (
<TreeGridCell
className={ clsx( 'block-editor-list-view-block__appender-cell', {
'is-visible': isVisible || hasActiveDescendant,
} ) }
aria-selected={ !! isSelected }
>
{ ( { ref, tabIndex, onFocus } ) => (
<Appender
ref={ ref }
clientId={ clientId }
nestingLevel={ level + 1 }
blockCount={ innerBlockCount }
toggleProps={ { tabIndex, onFocus } }
onSelectOrClose={ ( maybeInsertedBlock ) => {
// Show the new item, if the row is collapsed.
if ( maybeInsertedBlock?.clientId ) {
updateExpansion( {
type: 'expand',
clientIds: [ clientId ],
} );
}
} }
/>
) }
</TreeGridCell>
);
}

function ListViewBlock( {
clientId,
displacement,
Expand All @@ -59,6 +129,8 @@ function ListViewBlock( {
isExpanded,
selectedClientIds,
isSyncedBranch,
showAppender,
innerBlockCount = 0,
} ) {
const cellRef = useRef( null );
const rowRef = useRef( null );
Expand Down Expand Up @@ -570,6 +642,12 @@ function ListViewBlock( {
{ 'is-visible': isHovered || isFirstSelectedBlock }
);

// A List View with appenders gives every row an appender cell, so the
// columns line up. Only a row with items inside it holds a button, which
// adds an item at the end of them.
const hasRowAppender = showAppender && innerBlockCount > 0 && ! isDisabled;
const isAppenderCellVisible = isHovered || isFirstSelectedBlock;

let colSpan;
if ( hasRenderedMovers ) {
colSpan = 2;
Expand Down Expand Up @@ -706,6 +784,25 @@ function ListViewBlock( {
</>
) }

{ showAppender && ! hasRowAppender && (
<TreeGridCell
className={ clsx(
'block-editor-list-view-block__appender-cell',
{ 'is-visible': isAppenderCellVisible }
) }
withoutGridItem
/>
) }
{ hasRowAppender && (
<ListViewRowAppenderCell
clientId={ clientId }
level={ level }
innerBlockCount={ innerBlockCount }
isSelected={ isSelected }
isVisible={ isAppenderCellVisible }
updateExpansion={ updateExpansion }
/>
) }
{ canShowBlockActions && BlockSettingsMenu && (
<TreeGridCell
className={ listViewBlockSettingsClassName }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/list-view/branch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ function ListViewBranch( props ) {
displacement={ displacement }
isAfterDraggedBlocks={ isAfterDraggedBlocks }
isNesting={ isNesting }
showAppender={ showAppenderProp }
innerBlockCount={ innerBlocks?.length ?? 0 }
/>
) }
{ showNestedBlocks && (
Expand Down
32 changes: 32 additions & 0 deletions packages/block-editor/src/components/list-view/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,42 @@

.block-editor-list-view-block__menu-cell,
.block-editor-list-view-block__mover-cell,
.block-editor-list-view-block__appender-cell,
.block-editor-list-view-block__contents-cell {
padding: 0;
}

// The row appender stays visible at rest, so it can be found without
// hovering: a plain "+" that takes the filled appender style while the
// row, or a block inside it, is hovered, focused, or selected.
.block-editor-list-view-block__appender-cell {
line-height: 0;
width: $button-size-small + $grid-unit-05;
vertical-align: middle;

// The button sits in its own cell, so it drops the appender row's
// indent and top margin.
.list-view-appender .block-editor-inserter__toggle {
margin: 0;
background: transparent;
color: $gray-700;
}

&:hover,
&:focus-within,
&.is-visible {
.list-view-appender .block-editor-inserter__toggle {
background: $gray-900;
color: $white;

&:hover,
&:focus {
background: var(--wp-admin-theme-color);
}
}
}
}

.block-editor-list-view-block__menu-cell,
.block-editor-list-view-block__mover-cell {
line-height: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@
color: var(--wpds-color-foreground-interactive-brand-strong-active);
}
}

// A row's appender button is a plain "+" at rest, like the row's other
// controls, and takes the appender colors while the row is active.
.block-editor-list-view-leaf .block-editor-list-view-block__appender-cell {
.list-view-appender .block-editor-inserter__toggle {
background: transparent;
color: var(--wpds-color-foreground-interactive-neutral-weak);
}

&:hover,
&:focus-within,
&.is-visible {
.list-view-appender .block-editor-inserter__toggle {
background: var(--wpds-color-background-interactive-brand-strong);
color: var(--wpds-color-foreground-interactive-brand-strong);

&:hover,
&:focus {
background: var(--wpds-color-background-interactive-brand-strong-active);
color: var(--wpds-color-foreground-interactive-brand-strong-active);
}
}
}
}
}

.edit-site-sidebar-navigation-screen-navigation-menus__empty-message {
Expand Down
22 changes: 17 additions & 5 deletions test/e2e/specs/editor/blocks/navigation-list-view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ test.describe( 'Navigation block - List view editing', () => {
description: 'Structure for navigation menu: Test Menu',
} );

const appender = listView.getByRole( 'button', {
name: 'Add page',
} );
// Submenu rows hold an appender too; the top-level appender row comes last.
const appender = listView
.getByRole( 'button', {
name: 'Add page',
} )
.last();

await expect( appender ).toBeVisible();

Expand Down Expand Up @@ -307,7 +310,11 @@ test.describe( 'Navigation block - List view editing', () => {
description: 'Structure for navigation menu: Test Menu',
} );

await listView.getByRole( 'button', { name: 'Add page' } ).click();
// The top-level appender row comes after the submenu rows' appenders.
await listView
.getByRole( 'button', { name: 'Add page' } )
.last()
.click();

await expect( linkControl.getSearchInput() ).toBeFocused();
await page.keyboard.type( 'Home', { delay: 50 } );
Expand Down Expand Up @@ -577,10 +584,12 @@ test.describe( 'Navigation block - List view editing', () => {
description: 'Structure for navigation menu: Test Menu',
} );

// The top-level appender row comes after the submenu rows' appenders.
await listView
.getByRole( 'button', {
name: 'Add page',
} )
.last()
.click();

// Expect the Link UI to be focused.
Expand Down Expand Up @@ -866,7 +875,10 @@ test.describe( 'Navigation block - List view editing', () => {
).toBeVisible();
// Items can be added here, as the canvas appender shows, so the List
// View offers its appender too.
const appender = listView.getByRole( 'button', { name: 'Add page' } );
// The top-level appender row comes after the submenu rows' appenders.
const appender = listView
.getByRole( 'button', { name: 'Add page' } )
.last();
await expect( appender ).toBeVisible();
await appender.click();

Expand Down
Loading