-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buttons block: overhaul alignment/justification controls (#23168)
* Buttons block: overhaul alignment/justification controls. * Make icon names consistent with CSS terminology. * Move onClick function inline. * Mobile - Buttons block - Support content justification and ContentJustificationDropdown (#26166) * Fix control title inconsistency. Co-authored-by: Gerardo Pacheco <[email protected]>
- Loading branch information
1 parent
148e2b2
commit a03ea51
Showing
20 changed files
with
412 additions
and
52 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
73 changes: 73 additions & 0 deletions
73
packages/block-library/src/buttons/content-justification-dropdown.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { DropdownMenu } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contentJustificationCenterIcon, | ||
contentJustificationLeftIcon, | ||
contentJustificationRightIcon, | ||
} from './icons'; | ||
|
||
const DEFAULT_ALLOWED_VALUES = [ 'left', 'center', 'right' ]; | ||
|
||
const CONTROLS = { | ||
left: { | ||
icon: contentJustificationLeftIcon, | ||
title: __( 'Justify content left' ), | ||
}, | ||
center: { | ||
icon: contentJustificationCenterIcon, | ||
title: __( 'Justify content center' ), | ||
}, | ||
right: { | ||
icon: contentJustificationRightIcon, | ||
title: __( 'Justify content right' ), | ||
}, | ||
}; | ||
|
||
const DEFAULT_ICON = CONTROLS.center.icon; | ||
|
||
/** | ||
* Dropdown for selecting a content justification option. | ||
* | ||
* @param {Object} props Component props. | ||
* @param {string[]} [props.allowedValues] List of options to include. Default: | ||
* ['left', 'center', 'right']. | ||
* @param {()=>void} props.onChange Callback to run when an option is | ||
* selected in the dropdown. | ||
* @param {Object} props.toggleProps Props to pass to the dropdown toggle. | ||
* @param {string} props.value The current content justification | ||
* value. | ||
* | ||
* @return {WPComponent} The component. | ||
*/ | ||
export default function ContentJustificationDropdown( { | ||
onChange, | ||
allowedValues = DEFAULT_ALLOWED_VALUES, | ||
toggleProps, | ||
value, | ||
} ) { | ||
return ( | ||
<DropdownMenu | ||
icon={ CONTROLS[ value ]?.icon ?? DEFAULT_ICON } | ||
label={ __( 'Change content justification' ) } | ||
controls={ allowedValues.map( ( allowedValue ) => { | ||
return { | ||
...CONTROLS[ allowedValue ], | ||
isActive: value === allowedValue, | ||
role: 'menuitemradio', | ||
onClick: () => | ||
onChange( | ||
value === allowedValue ? undefined : allowedValue | ||
), | ||
}; | ||
} ) } | ||
toggleProps={ toggleProps } | ||
/> | ||
); | ||
} |
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,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
const deprecated = [ | ||
{ | ||
supports: { | ||
align: [ 'center', 'left', 'right' ], | ||
anchor: true, | ||
}, | ||
save() { | ||
return ( | ||
<div> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}, | ||
isEligible( { align } ) { | ||
return align && [ 'center', 'left', 'right' ].includes( align ); | ||
}, | ||
migrate( attributes ) { | ||
return { | ||
...attributes, | ||
align: undefined, | ||
// Floating Buttons blocks shouldn't have been supported in the | ||
// first place. Most users using them probably expected them to | ||
// act like content justification controls, so these blocks are | ||
// migrated to use content justification. | ||
// As for center-aligned Buttons blocks, the content justification | ||
// equivalent will create an identical end result in most cases. | ||
contentJustification: attributes.align, | ||
}; | ||
}, | ||
}, | ||
]; | ||
|
||
export default deprecated; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Path, SVG } from '@wordpress/components'; | ||
|
||
export const contentJustificationLeftIcon = ( | ||
<SVG | ||
width="20" | ||
height="20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
> | ||
<Path d="M11 16v-3h10v-2H11V8l-4 4 4 4zM5 4H3v16h2V4z" /> | ||
</SVG> | ||
); | ||
|
||
export const contentJustificationCenterIcon = ( | ||
<SVG | ||
width="20" | ||
height="20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
> | ||
<Path d="M5 8v3H1v2h4v3l4-4-4-4zm14 8v-3h4v-2h-4V8l-4 4 4 4zM13 4h-2v16h2V4z" /> | ||
</SVG> | ||
); | ||
|
||
export const contentJustificationRightIcon = ( | ||
<SVG | ||
width="20" | ||
height="20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
> | ||
<Path d="M13 8v3H3v2h10v3l4-4-4-4zm8-4h-2v16h2V4z" /> | ||
</SVG> | ||
); |
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
Oops, something went wrong.