-
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 flex layout to Buttons block and new layout type.
- Loading branch information
1 parent
154b34a
commit edb0d3d
Showing
8 changed files
with
197 additions
and
68 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
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,152 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { justifyLeft, justifyCenter, justifyRight } from '@wordpress/icons'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { appendSelectors } from './utils'; | ||
import useSetting from '../components/use-setting'; | ||
import { BlockControls, JustifyContentControl } from '../components'; | ||
|
||
const justifyContentMap = { | ||
left: 'flex-start', | ||
right: 'flex-end', | ||
center: 'center', | ||
}; | ||
|
||
export default { | ||
name: 'column', | ||
label: __( 'Column' ), | ||
inspectorControls: function FlexLayoutInspectorControls( { | ||
layout = {}, | ||
onChange, | ||
} ) { | ||
return ( | ||
<FlexLayoutJustifyContentControl | ||
layout={ layout } | ||
onChange={ onChange } | ||
/> | ||
); | ||
}, | ||
toolBarControls: function FlexLayoutToolbarControls( { | ||
layout = {}, | ||
onChange, | ||
layoutBlockSupport, | ||
} ) { | ||
if ( layoutBlockSupport?.allowSwitching ) { | ||
return null; | ||
} | ||
return ( | ||
<BlockControls group="block" __experimentalShareWithChildBlocks> | ||
<FlexLayoutJustifyContentControl | ||
layout={ layout } | ||
onChange={ onChange } | ||
isToolbar | ||
/> | ||
</BlockControls> | ||
); | ||
}, | ||
save: function FlexLayoutStyle( { selector, layout } ) { | ||
const blockGapSupport = useSetting( 'spacing.blockGap' ); | ||
const hasBlockGapStylesSupport = blockGapSupport !== null; | ||
const alignItems = | ||
justifyContentMap[ layout.justifyContent ] || 'flex-start'; | ||
return ( | ||
<style>{ ` | ||
${ appendSelectors( selector ) } { | ||
display: flex; | ||
flex-direction: column; | ||
gap: ${ | ||
hasBlockGapStylesSupport | ||
? 'var( --wp--style--block-gap, 0.5em )' | ||
: '0.5em' | ||
}; | ||
flex-wrap: wrap; | ||
align-items: ${ alignItems }; | ||
} | ||
${ appendSelectors( selector, '> *' ) } { | ||
margin: 0; | ||
} | ||
` }</style> | ||
); | ||
}, | ||
getOrientation() { | ||
return 'vertical'; | ||
}, | ||
getAlignments() { | ||
return []; | ||
}, | ||
}; | ||
|
||
const justificationOptions = [ | ||
{ | ||
value: 'left', | ||
icon: justifyLeft, | ||
label: __( 'Justify items left' ), | ||
}, | ||
{ | ||
value: 'center', | ||
icon: justifyCenter, | ||
label: __( 'Justify items center' ), | ||
}, | ||
{ | ||
value: 'right', | ||
icon: justifyRight, | ||
label: __( 'Justify items right' ), | ||
}, | ||
]; | ||
function FlexLayoutJustifyContentControl( { | ||
layout, | ||
onChange, | ||
isToolbar = false, | ||
} ) { | ||
const { justifyContent = 'left' } = layout; | ||
const onJustificationChange = ( value ) => { | ||
onChange( { | ||
...layout, | ||
justifyContent: value, | ||
} ); | ||
}; | ||
if ( isToolbar ) { | ||
return ( | ||
<JustifyContentControl | ||
allowedControls={ [ | ||
'left', | ||
'center', | ||
'right', | ||
'space-between', | ||
] } | ||
value={ justifyContent } | ||
onChange={ onJustificationChange } | ||
popoverProps={ { | ||
position: 'bottom right', | ||
isAlternate: true, | ||
} } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<fieldset className="block-editor-hooks__flex-layout-justification-controls"> | ||
<legend>{ __( 'Justification' ) }</legend> | ||
<div> | ||
{ justificationOptions.map( ( { value, icon, label } ) => { | ||
return ( | ||
<Button | ||
key={ value } | ||
label={ label } | ||
icon={ icon } | ||
isPressed={ justifyContent === value } | ||
onClick={ () => onJustificationChange( value ) } | ||
/> | ||
); | ||
} ) } | ||
</div> | ||
</fieldset> | ||
); | ||
} |
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
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
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