-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 5 commits
c51a1c9
fa92eb2
cbec90f
4d6c4de
398281c
d096946
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ), | ||
|
@@ -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: 32, | ||
} } | ||
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' && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: It's a bit more readable if we check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the problem here is the |
||
<RangeControl | ||
label={ __( 'Height in pixels' ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General question from me, when do we normally add inline translator comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
</> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,8 @@ | |
.components-resizable-box__handle::before { | ||
content: none; | ||
} | ||
|
||
&.resize-horizontal { | ||
margin-bottom: 0; | ||
} | ||
} |
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 --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome PR, this is going to allow so many new designs! 🎉
I'm wondering if there might be cases where 32px is too much for a fixed height. Although adjusting vertically to the parent height is trickier than doing so horizontally, ideally, it would behave like the vertical spacing, where width is omitted. Maybe something maybe worth exploring later, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid observation. 24px feels like a plenty tappable size that also goes well with the "minimum" size of the navigation block, as it's the size of the appender.