Skip to content
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

Improve parent block selector button UI #23800

Closed
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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ import { castArray, flow, noop } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _n } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import {
DropdownMenu,
MenuGroup,
MenuItem,
ClipboardButton,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { moreVertical } from '@wordpress/icons';

import { Children, cloneElement, useCallback } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';
import { getBlockType, serialize } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import BlockActions from '../block-actions';
import BlockIcon from '../block-icon';
import BlockModeToggle from './block-mode-toggle';
import BlockHTMLConvertButton from './block-html-convert-button';
import __experimentalBlockSettingsMenuFirstItem from './block-settings-menu-first-item';
Expand All @@ -44,23 +45,41 @@ export function BlockSettingsDropdown( {
const count = blockClientIds.length;
const firstBlockClientId = blockClientIds[ 0 ];

const shortcuts = useSelect( ( select ) => {
const { getShortcutRepresentation } = select(
'core/keyboard-shortcuts'
);
return {
duplicate: getShortcutRepresentation(
'core/block-editor/duplicate'
),
remove: getShortcutRepresentation( 'core/block-editor/remove' ),
insertAfter: getShortcutRepresentation(
'core/block-editor/insert-after'
),
insertBefore: getShortcutRepresentation(
'core/block-editor/insert-before'
),
};
}, [] );
const { selectBlock } = useDispatch( 'core/block-editor' );

const { parentBlockType, firstParentClientId, shortcuts } = useSelect(
( select ) => {
const { getBlockName, getBlockParents } = select(
'core/block-editor'
);
const { getShortcutRepresentation } = select(
'core/keyboard-shortcuts'
);
const parents = getBlockParents( firstBlockClientId );
const _firstParentClientId = parents[ parents.length - 1 ];
const parentBlockName = getBlockName( _firstParentClientId );

return {
parentBlockType: getBlockType( parentBlockName ),
firstParentClientId: _firstParentClientId,
shortcuts: {
duplicate: getShortcutRepresentation(
'core/block-editor/duplicate'
),
remove: getShortcutRepresentation(
'core/block-editor/remove'
),
insertAfter: getShortcutRepresentation(
'core/block-editor/insert-after'
),
insertBefore: getShortcutRepresentation(
'core/block-editor/insert-before'
),
},
};
},
[ firstBlockClientId ]
);

const updateSelection = useCallback(
__experimentalSelectBlock
Expand Down Expand Up @@ -105,6 +124,24 @@ export function BlockSettingsDropdown( {
<__experimentalBlockSettingsMenuFirstItem.Slot
fillProps={ { onClose } }
/>
{ firstParentClientId !== undefined && (
<MenuItem
icon={
<BlockIcon
icon={ parentBlockType.icon }
/>
}
onClick={ () =>
selectBlock( firstParentClientId )
}
>
{ sprintf(
/* translators: %s: Name of the block's parent. */
__( 'Select parent (%s)' ),
parentBlockType.title
) }
</MenuItem>
) }
{ count === 1 && (
<BlockHTMLConvertButton
clientId={ firstBlockClientId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@
}
}

// Override standard MenuItem icon spacing, since the block transform icons are
// shown on the left, rather than the right.
.block-editor-block-switcher__transforms__menugroup .block-editor-block-icon {
margin-left: -2px; // This optically balances the icon.
margin-right: $grid-unit-10;
}

.block-editor-block-switcher__popover__preview__parent {
.block-editor-block-switcher__popover__preview__container {
position: absolute;
Expand Down
52 changes: 13 additions & 39 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { ToolbarGroup } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { ToolbarGroup } from '@wordpress/components';

/**
* Internal dependencies
*/
import BlockMover from '../block-mover';
import BlockParentSelector from '../block-parent-selector';
import BlockSwitcher from '../block-switcher';
import BlockControls from '../block-controls';
import BlockFormatControls from '../block-format-controls';
import BlockSettingsMenu from '../block-settings-menu';
import { useShowMoversGestures } from './utils';
import { useElementHoverFocusGestures } from './utils';
import ExpandedBlockControlsContainer from './expanded-block-controls-container';

export default function BlockToolbar( {
Expand All @@ -32,7 +25,6 @@ export default function BlockToolbar( {
blockClientIds,
blockClientId,
blockType,
hasFixedToolbar,
hasReducedUI,
isValid,
isVisual,
Expand All @@ -56,7 +48,6 @@ export default function BlockToolbar( {
blockType:
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) ),
hasFixedToolbar: settings.hasFixedToolbar,
hasReducedUI: settings.hasReducedUI,
rootClientId: blockRootClientId,
isValid: selectedBlockClientIds.every( ( id ) =>
Expand All @@ -71,53 +62,36 @@ export default function BlockToolbar( {
const { toggleBlockHighlight } = useDispatch( 'core/block-editor' );
const nodeRef = useRef();

const { showMovers, gestures: showMoversGestures } = useShowMoversGestures(
{
ref: nodeRef,
onChange( isFocused ) {
if ( isFocused && hasReducedUI ) {
return;
}
toggleBlockHighlight( blockClientId, isFocused );
},
}
);

const displayHeaderToolbar =
useViewportMatch( 'medium', '<' ) || hasFixedToolbar;
const showBlockHighlightGestures = useElementHoverFocusGestures( {
ref: nodeRef,
onChange( isFocused ) {
if ( isFocused && hasReducedUI ) {
return;
}
toggleBlockHighlight( blockClientId, isFocused );
},
} );

if ( blockType ) {
if ( ! hasBlockSupport( blockType, '__experimentalToolbar', true ) ) {
return null;
}
}

const shouldShowMovers = displayHeaderToolbar || showMovers;

if ( blockClientIds.length === 0 ) {
return null;
}

const shouldShowVisualToolbar = isValid && isVisual;
const isMultiToolbar = blockClientIds.length > 1;

const classes = classnames(
'block-editor-block-toolbar',
shouldShowMovers && 'is-showing-movers'
);

const Wrapper = __experimentalExpandedControl
? ExpandedBlockControlsContainer
: 'div';

return (
<Wrapper className={ classes }>
<div ref={ nodeRef } { ...showMoversGestures }>
{ ! isMultiToolbar && (
<div className="block-editor-block-toolbar__block-parent-selector-wrapper">
<BlockParentSelector clientIds={ blockClientIds } />
</div>
) }
<Wrapper className="block-editor-block-toolbar">
<div ref={ nodeRef } { ...showBlockHighlightGestures }>
{ ( shouldShowVisualToolbar || isMultiToolbar ) && (
<ToolbarGroup className="block-editor-block-toolbar__block-controls">
<BlockSwitcher clientIds={ blockClientIds } />
Expand Down
15 changes: 3 additions & 12 deletions packages/block-editor/src/components/block-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,9 @@
}

.block-editor-block-toolbar__block-parent-selector-wrapper {
position: absolute;
top: -1px;
left: -1px;
opacity: 0;
transition: all 60ms linear;
z-index: -1; // This makes it slide out from underneath the toolbar.

@include reduce-motion("transition");

.is-showing-movers & {
opacity: 1;
transform: translateY(-($block-toolbar-height + $grid-unit-15));
// Hide the Parent button in Top Toolbar mode.
.edit-post-header-toolbar__block-toolbar & {
display: none;
}
}

Expand Down
Loading