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

Make Spacer block width adjustable and add it to Navigation block #29133

Merged
merged 6 commits into from
Mar 2, 2021
Merged
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
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export default function NavigationLinkEdit( {
} ),
},
{
allowedBlocks: [ 'core/navigation-link' ],
allowedBlocks: [ 'core/navigation-link', 'core/spacer' ],
renderAppender:
( isSelected && hasDescendants ) ||
( isImmediateParentOfSelectedBlock &&
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"fontSize": "fontSize",
"customFontSize": "customFontSize",
"showSubmenuIcon": "showSubmenuIcon",
"style": "style"
"style": "style",
"orientation": "orientation"
},
"supports": {
"align": [
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function Navigation( {
'core/search',
'core/social-links',
'core/page-list',
'core/spacer',
],
orientation: attributes.orientation || 'horizontal',
renderAppender:
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/spacer/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
"height": {
"type": "number",
"default": 100
},
"width": {
"type": "number"
}
},
"usesContext": [
"orientation"
],
"supports": {
"anchor": true
},
Expand Down
122 changes: 102 additions & 20 deletions packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,43 @@ import {
import { PanelBody, ResizableBox, RangeControl } from '@wordpress/components';
import { compose, withInstanceId } from '@wordpress/compose';
import { withDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { View } from '@wordpress/primitives';

const MIN_SPACER_HEIGHT = 1;
const MAX_SPACER_HEIGHT = 500;

const MIN_SPACER_WIDTH = 1;
const MAX_SPACER_WIDTH = 500;

const SpacerEdit = ( {
attributes,
isSelected,
setAttributes,
onResizeStart,
onResizeStop,
context,
} ) => {
const { orientation } = context;
const [ isResizing, setIsResizing ] = useState( false );
const { height } = attributes;
const { height, width } = attributes;
const updateHeight = ( value ) => {
setAttributes( {
height: value,
} );
};
const updateWidth = ( value ) => {
setAttributes( {
width: value,
} );
};

const handleOnResizeStart = ( ...args ) => {
onResizeStart( ...args );
setIsResizing( true );
};

const handleOnResizeStop = ( event, direction, elt, delta ) => {
const handleOnVerticalResizeStop = ( event, direction, elt, delta ) => {
onResizeStop();
const spacerHeight = Math.min(
parseInt( height + delta.height, 10 ),
Expand All @@ -51,50 +61,122 @@ const SpacerEdit = ( {
setIsResizing( false );
};

return (
<>
<View { ...useBlockProps() }>
const handleOnHorizontalResizeStop = ( event, direction, elt, delta ) => {
onResizeStop();
const spacerWidth = Math.min(
parseInt( width + delta.width, 10 ),
MAX_SPACER_WIDTH
);
updateWidth( spacerWidth );
setIsResizing( false );
};

const resizableBoxWithOrientation = ( blockOrientation ) => {
if ( blockOrientation === 'horizontal' ) {
return (
<ResizableBox
className={ classnames(
'block-library-spacer__resize-container',
'resize-horizontal',
{
'is-selected': isSelected,
}
) }
size={ {
height,
width,
height: 24,
} }
minHeight={ MIN_SPACER_HEIGHT }
minWidth={ MIN_SPACER_WIDTH }
enable={ {
top: false,
right: false,
bottom: true,
right: true,
bottom: false,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false,
} }
onResizeStart={ handleOnResizeStart }
onResizeStop={ handleOnResizeStop }
onResizeStop={ handleOnHorizontalResizeStop }
showHandle={ isSelected }
__experimentalShowTooltip={ true }
__experimentalTooltipProps={ {
axis: 'y',
position: 'bottom',
axis: 'x',
position: 'corner',
isVisible: isResizing,
} }
/>
);
}

return (
<ResizableBox
className={ classnames(
'block-library-spacer__resize-container',
{
'is-selected': isSelected,
}
) }
size={ {
height,
} }
minHeight={ MIN_SPACER_HEIGHT }
enable={ {
top: false,
right: false,
bottom: true,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false,
} }
onResizeStart={ handleOnResizeStart }
onResizeStop={ handleOnVerticalResizeStop }
showHandle={ isSelected }
__experimentalShowTooltip={ true }
__experimentalTooltipProps={ {
axis: 'y',
position: 'bottom',
isVisible: isResizing,
} }
/>
);
};

useEffect( () => {
if ( orientation === 'horizontal' && ! width ) {
updateWidth( 72 );
updateHeight( 0 );
}
}, [] );
draganescu marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
<View { ...useBlockProps() }>
{ resizableBoxWithOrientation( orientation ) }
</View>
<InspectorControls>
<PanelBody title={ __( 'Spacer settings' ) }>
<RangeControl
label={ __( 'Height in pixels' ) }
min={ MIN_SPACER_HEIGHT }
max={ Math.max( MAX_SPACER_HEIGHT, height ) }
value={ height }
onChange={ updateHeight }
/>
{ orientation === 'horizontal' && (
<RangeControl
label={ __( 'Width in pixels' ) }
min={ MIN_SPACER_WIDTH }
max={ Math.max( MAX_SPACER_WIDTH, width ) }
value={ width }
onChange={ updateWidth }
/>
) }
{ orientation !== 'horizontal' && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: It's a bit more readable if we check for orientation === 'vertical'. Though I understand if we're trying to guard against malformed data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the problem here is the orientation prop doesn't always exist, and when it doesn't, the default should be vertical. So I thought this was the least messy way of expressing that.

<RangeControl
label={ __( 'Height in pixels' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General question from me, when do we normally add inline translator comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are used to add information about the string's placement or meaning in situations where the text content, taken out of its context, might not be clear enough. This is because translators only see a list of strings and don't necessarily know whereabouts in the UI they might live.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, so more of a case by case basis if it's pretty short/generic 👍

min={ MIN_SPACER_HEIGHT }
max={ Math.max( MAX_SPACER_HEIGHT, height ) }
value={ height }
onChange={ updateHeight }
/>
) }
</PanelBody>
</InspectorControls>
</>
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
Expand Up @@ -27,4 +27,8 @@
.components-resizable-box__handle::before {
content: none;
}

&.resize-horizontal {
margin-bottom: 0;
}
}
2 changes: 1 addition & 1 deletion packages/block-library/src/spacer/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function save( { attributes } ) {
return (
<div
{ ...useBlockProps.save( {
style: { height: attributes.height },
style: { height: attributes.height, width: attributes.width },
'aria-hidden': true,
} ) }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:spacer {"height":0,"width":72} -->
<div style="height:0px;width:72px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"clientId": "_clientId_0",
"name": "core/spacer",
"isValid": true,
"attributes": {
"height": 0,
"width": 72
},
"innerBlocks": [],
"originalContent": "<div style=\"height:0px;width:72px\" aria-hidden=\"true\" class=\"wp-block-spacer\"></div>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"blockName": "core/spacer",
"attrs": {
"height": 0,
"width": 72
},
"innerBlocks": [],
"innerHTML": "\n<div style=\"height:0px;width:72px\" aria-hidden=\"true\" class=\"wp-block-spacer\"></div>\n",
"innerContent": [
"\n<div style=\"height:0px;width:72px\" aria-hidden=\"true\" class=\"wp-block-spacer\"></div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:spacer {"height":0,"width":72} -->
<div style="height:0;width:72px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->