Skip to content

Commit

Permalink
Grid: Better looking block movers (WordPress#63394)
Browse files Browse the repository at this point in the history
Co-authored-by: noisysocks <noisysocks@git.wordpress.org>
Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>
  • Loading branch information
3 people authored and carstingaxion committed Jul 18, 2024
1 parent 396eb5e commit 733da80
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 69 deletions.
208 changes: 139 additions & 69 deletions packages/block-editor/src/components/grid/grid-item-movers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { ToolbarButton } from '@wordpress/components';
import { arrowLeft, arrowUp, arrowDown, arrowRight } from '@wordpress/icons';
import {
VisuallyHidden,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import {
chevronLeft,
chevronUp,
chevronDown,
chevronRight,
} from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -39,74 +54,129 @@ export function GridItemMovers( {

return (
<BlockControls group="parent">
<ToolbarGroup className="block-editor-grid-item-mover__move-button-container">
<GridItemMover
className="is-left-button"
icon={ chevronLeft }
label={ __( 'Move left' ) }
description={ __( 'Move left' ) }
isDisabled={ columnStart <= 1 }
onClick={ () => {
onChange( {
columnStart: columnStart - 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell(
columnStart - 1,
rowStart
)
);
} }
/>
<div className="block-editor-grid-item-mover__move-vertical-button-container">
<GridItemMover
className="is-up-button"
icon={ chevronUp }
label={ __( 'Move up' ) }
description={ __( 'Move up' ) }
isDisabled={ rowStart <= 1 }
onClick={ () => {
onChange( {
rowStart: rowStart - 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell(
columnStart,
rowStart - 1
)
);
} }
/>
<GridItemMover
className="is-down-button"
icon={ chevronDown }
label={ __( 'Move down' ) }
description={ __( 'Move down' ) }
isDisabled={ rowCount && rowEnd >= rowCount }
onClick={ () => {
onChange( {
rowStart: rowStart + 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell(
columnStart,
rowStart + 1
)
);
} }
/>
</div>
<GridItemMover
className="is-right-button"
icon={ chevronRight }
label={ __( 'Move right' ) }
description={ __( 'Move right' ) }
isDisabled={ columnCount && columnEnd >= columnCount }
onClick={ () => {
onChange( {
columnStart: columnStart + 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell(
columnStart + 1,
rowStart
)
);
} }
/>
</ToolbarGroup>
</BlockControls>
);
}

function GridItemMover( {
className,
icon,
label,
isDisabled,
onClick,
description,
} ) {
const instanceId = useInstanceId( GridItemMover );
const descriptionId = `block-editor-grid-item-mover-button__description-${ instanceId }`;
return (
<>
<ToolbarButton
icon={ arrowUp }
label={ __( 'Move block up' ) }
disabled={ rowStart <= 1 }
onClick={ () => {
onChange( {
rowStart: rowStart - 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell( columnStart, rowStart - 1 )
);
} }
/>
<ToolbarButton
icon={ arrowDown }
label={ __( 'Move block down' ) }
disabled={ rowCount && rowEnd >= rowCount }
onClick={ () => {
onChange( {
rowStart: rowStart + 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell( columnStart, rowStart + 1 )
);
} }
/>
<ToolbarButton
icon={ arrowLeft }
label={ __( 'Move block left' ) }
disabled={ columnStart <= 1 }
onClick={ () => {
onChange( {
columnStart: columnStart - 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell( columnStart - 1, rowStart )
);
} }
/>
<ToolbarButton
icon={ arrowRight }
label={ __( 'Move block right' ) }
disabled={ columnCount && columnEnd >= columnCount }
onClick={ () => {
onChange( {
columnStart: columnStart + 1,
} );
__unstableMarkNextChangeAsNotPersistent();
moveBlocksToPosition(
[ blockClientId ],
gridClientId,
gridClientId,
getNumberOfBlocksBeforeCell( columnStart + 1, rowStart )
);
} }
className={ clsx(
'block-editor-grid-item-mover-button',
className
) }
icon={ icon }
label={ label }
aria-describedby={ descriptionId }
onClick={ isDisabled ? null : onClick }
disabled={ isDisabled }
accessibleWhenDisabled
/>
</BlockControls>
<VisuallyHidden id={ descriptionId }>
{ description }
</VisuallyHidden>
</>
);
}
114 changes: 114 additions & 0 deletions packages/block-editor/src/components/grid/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,117 @@
}
}

.block-editor-grid-item-mover__move-button-container {
display: flex;
padding: 0;
border: none;
justify-content: center;

.block-editor-grid-item-mover-button {
width: $block-toolbar-height * 0.5;
min-width: 0 !important; // overrides default button width.
overflow: hidden;
padding-left: 0;
padding-right: 0;

svg {
min-width: $grid-unit-30;
}

// Focus and toggle pseudo elements.
&::before {
content: "";
position: absolute;
display: block;
border-radius: $radius-block-ui;
height: $grid-unit-40;

// Position the focus rectangle.
left: $grid-unit-10;
right: $grid-unit-10;
z-index: -1;

// Animate in.
animation: components-button__appear-animation 0.1s ease;
animation-fill-mode: forwards;
@include reduce-motion("animation");
}

// Don't show the focus inherited by the Button component.
&:focus,
&:focus:enabled,
&:focus::before {
box-shadow: none;
outline: none;
}

&:focus-visible::before {
@include block-toolbar-button-style__focus();
}
}
}


.block-editor-grid-item-mover__move-vertical-button-container {
display: flex;
position: relative;
@include break-small() {
flex-direction: column;
justify-content: space-around;

> .block-editor-grid-item-mover-button.block-editor-grid-item-mover-button {
height: $block-toolbar-height * 0.5 - $grid-unit-05;
width: 100%;
min-width: 0 !important; // overrides default button width.

// Focus style.
&::before {
height: calc(100% - 4px);
}
}

.block-editor-grid-item-mover-button.is-up-button svg,
.block-editor-grid-item-mover-button.is-down-button svg {
flex-shrink: 0;
height: $block-toolbar-height * 0.5 - $grid-unit-05;
}
}
}

.show-icon-labels {

.block-editor-grid-item-mover-button.block-editor-grid-item-mover-button.is-left-button {
border-right: 1px solid $gray-700;
padding-right: 12px;
}

.block-editor-grid-item-mover-button.block-editor-grid-item-mover-button.is-right-button {
border-left: 1px solid $gray-700;
padding-left: 12px;
}


.block-editor-grid-item-mover__move-vertical-button-container {
&::before {
@include break-small() {
content: "";
height: $border-width;
width: 100%;
background: $gray-200;
position: absolute;
top: 50%;
left: 50%;
// With Top toolbar enabled, this separator has a smaller width. Translating the
// X axis allows to make the separator always centered regardless of its width.
transform: translate(-50%, 0);
margin-top: -$border-width * 0.5;
}

@include break-medium {
background: $gray-900;
}
}
}

}

0 comments on commit 733da80

Please sign in to comment.