-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add top/bottom margins to separator block
This leverages the modified BoxControl component from `update/boxcontrol-with-configurable-sides` to allow only top and bottom margins to be set.
- Loading branch information
1 parent
d8bf09e
commit 2f96692
Showing
9 changed files
with
366 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
}, | ||
"customColor": { | ||
"type": "string" | ||
}, | ||
"style": { | ||
"type": "object" | ||
} | ||
}, | ||
"supports": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { HorizontalRule, useConvertUnitToMobile } from '@wordpress/components'; | ||
import { withColors, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SeparatorSettings from './separator-settings'; | ||
import { MARGIN_CONSTRAINTS, parseUnit } from './shared'; | ||
|
||
function SeparatorEdit( props ) { | ||
const { | ||
color, | ||
attributes: { style }, | ||
} = props; | ||
|
||
const { top, bottom } = style?.spacing?.margin || {}; | ||
const marginUnit = parseUnit( top || bottom ); | ||
|
||
const convertedMarginTop = useConvertUnitToMobile( | ||
parseFloat( top || 0 ) || MARGIN_CONSTRAINTS[ marginUnit ].min, | ||
marginUnit | ||
); | ||
|
||
const convertedMarginBottom = useConvertUnitToMobile( | ||
parseFloat( bottom || 0 ) || MARGIN_CONSTRAINTS[ marginUnit ].min, | ||
marginUnit | ||
); | ||
|
||
return ( | ||
<> | ||
<HorizontalRule | ||
{ ...useBlockProps() } | ||
style={ { | ||
backgroundColor: color.color, | ||
color: color.color, | ||
marginBottom: convertedMarginBottom, | ||
marginTop: convertedMarginTop, | ||
} } | ||
/> | ||
<SeparatorSettings { ...props } /> | ||
</> | ||
); | ||
} | ||
|
||
export default withColors( 'color', { textColor: 'color' } )( SeparatorEdit ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
.block-editor-block-list__block[data-type="core/separator"] { | ||
// Prevent margin collapsing so the area to select the separator is bigger. | ||
padding-top: 0.1px; | ||
padding-bottom: 0.1px; | ||
.wp-block-separator-wrapper { | ||
display: flex; | ||
|
||
.wp-block-separator { | ||
width: 100%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 105 additions & 2 deletions
107
packages/block-library/src/separator/separator-settings.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,106 @@ | ||
// Mobile has no separator settings at this time, so render nothing | ||
const SeparatorSettings = () => null; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { InspectorControls, PanelColorSettings } from '@wordpress/block-editor'; | ||
import { PanelBody, UnitControl } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { CSS_UNITS, MARGIN_CONSTRAINTS, parseUnit } from './shared'; | ||
|
||
const SeparatorSettings = ( { | ||
color, | ||
setColor, | ||
attributes, | ||
setAttributes, | ||
} ) => { | ||
const { style } = attributes; | ||
const { top, bottom } = style?.spacing?.margin || {}; | ||
|
||
const topUnit = parseUnit( top ); | ||
const bottomUnit = parseUnit( bottom ); | ||
const topValue = top | ||
? parseFloat( top ) | ||
: MARGIN_CONSTRAINTS[ topUnit ].min; | ||
const bottomValue = bottom | ||
? parseFloat( bottom ) | ||
: MARGIN_CONSTRAINTS[ bottomUnit ].min; | ||
|
||
const updateMargins = ( margins ) => { | ||
setAttributes( { | ||
style: { | ||
...style, | ||
spacing: { | ||
...style?.spacing, | ||
margin: margins, | ||
}, | ||
}, | ||
} ); | ||
}; | ||
|
||
const createHandleMarginChange = ( side, unit ) => ( value ) => { | ||
updateMargins( { | ||
...style?.spacing?.margin, | ||
[ side ]: `${ value }${ unit }`, | ||
} ); | ||
}; | ||
|
||
const onUnitChange = ( unit ) => { | ||
updateMargins( { | ||
top: MARGIN_CONSTRAINTS[ unit ].default, | ||
bottom: MARGIN_CONSTRAINTS[ unit ].default, | ||
} ); | ||
}; | ||
|
||
return ( | ||
<InspectorControls> | ||
<PanelColorSettings | ||
title={ __( 'Color settings' ) } | ||
colorSettings={ [ | ||
{ | ||
value: color.color, | ||
onChange: setColor, | ||
label: __( 'Color' ), | ||
}, | ||
] } | ||
/> | ||
<PanelBody title={ __( 'Separator settings' ) }> | ||
<UnitControl | ||
label={ __( 'Top margin' ) } | ||
key={ `separator-top-margin-${ bottomUnit }` } | ||
min={ MARGIN_CONSTRAINTS[ topUnit ].min } | ||
max={ MARGIN_CONSTRAINTS[ topUnit ].max } | ||
value={ topValue || MARGIN_CONSTRAINTS[ topUnit ].min } | ||
unit={ topUnit } | ||
units={ CSS_UNITS } | ||
onChange={ createHandleMarginChange( 'top', topUnit ) } | ||
onUnitChange={ onUnitChange } | ||
decimalNum={ 1 } | ||
step={ topUnit === 'px' ? 1 : 0.1 } | ||
/> | ||
<UnitControl | ||
label={ __( 'Bottom margin' ) } | ||
key={ `separator-bottom-margin-${ bottomUnit }` } | ||
min={ MARGIN_CONSTRAINTS[ bottomUnit ].min } | ||
max={ MARGIN_CONSTRAINTS[ bottomUnit ].max } | ||
value={ | ||
bottomValue || MARGIN_CONSTRAINTS[ bottomUnit ].min | ||
} | ||
unit={ bottomUnit } | ||
units={ CSS_UNITS } | ||
onChange={ createHandleMarginChange( | ||
'bottom', | ||
bottomUnit | ||
) } | ||
onUnitChange={ onUnitChange } | ||
decimalNum={ 1 } | ||
step={ bottomUnit === 'px' ? 1 : 0.1 } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
); | ||
}; | ||
|
||
export default SeparatorSettings; |
Oops, something went wrong.