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

Spacer block: allow 1px height yet keep editor preview accuracy and improve selectability #25525

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
19 changes: 18 additions & 1 deletion packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { compose, withInstanceId } from '@wordpress/compose';
import { withDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';

const MIN_SPACER_HEIGHT = 20;
const MIN_SPACER_HEIGHT = 1;
const MAX_SPACER_HEIGHT = 500;
const MIN_SPACER_HEIGHT_EDITOR = 48;

const SpacerEdit = ( {
attributes,
Expand All @@ -31,6 +32,20 @@ const SpacerEdit = ( {
} );
};

const style = {};

const updateStyle = ( newHeight ) => {
const cheat = Math.max( 0, MIN_SPACER_HEIGHT_EDITOR - newHeight ) / 2;
style.top = `${ cheat }px`;
style.margin = `-${ cheat }px 0`;
};

updateStyle( height );

const handleOnResize = ( event, direction, elt, delta ) => {
updateStyle( parseInt( height + delta.height, 10 ) );
};

const handleOnResizeStart = ( ...args ) => {
onResizeStart( ...args );
setIsResizing( true );
Expand Down Expand Up @@ -58,6 +73,7 @@ const SpacerEdit = ( {
size={ {
height,
} }
style={ style }
minHeight={ MIN_SPACER_HEIGHT }
enable={ {
top: false,
Expand All @@ -69,6 +85,7 @@ const SpacerEdit = ( {
bottomLeft: false,
topLeft: false,
} }
onResize={ handleOnResize }
onResizeStart={ handleOnResizeStart }
onResizeStop={ handleOnResizeStop }
showHandle={ isSelected }
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/spacer/editor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.block-editor-block-list__block[data-type="core/spacer"] {
min-height: 48px;
}

.block-library-spacer__resize-container.has-show-handle {
background: $gray-100;

Expand Down