-
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.
Buttons block: overhaul alignment/justification controls.
- Loading branch information
1 parent
01fab7e
commit 9637656
Showing
19 changed files
with
347 additions
and
40 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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
{ | ||
"name": "core/buttons", | ||
"category": "design", | ||
"attributes": { | ||
"contentJustification": { | ||
"type": "string" | ||
} | ||
}, | ||
"supports": { | ||
"align": true, | ||
"alignWide": false, | ||
"align": [ "wide", "full" ], | ||
"lightBlockWrapper": true | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
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,67 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { DropdownMenu } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { justifyCenterIcon, justifyLeftIcon, justifyRightIcon } from './icons'; | ||
|
||
const DEFAULT_ALLOWED_VALUES = [ 'left', 'center', 'right' ]; | ||
|
||
const CONTROLS = { | ||
left: { | ||
icon: justifyLeftIcon, | ||
title: __( 'Justify items left' ), | ||
}, | ||
center: { | ||
icon: justifyCenterIcon, | ||
title: __( 'Justify items center' ), | ||
}, | ||
right: { | ||
icon: justifyRightIcon, | ||
title: __( 'Justify items 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 {string} props.value The current content justification | ||
* value. | ||
* | ||
* @return {WPComponent} The component. | ||
*/ | ||
export default function ContentJustificationDropdown( { | ||
onChange, | ||
allowedValues = DEFAULT_ALLOWED_VALUES, | ||
value, | ||
} ) { | ||
function applyOrUnset( align ) { | ||
return () => onChange( value === align ? undefined : align ); | ||
} | ||
|
||
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: applyOrUnset( allowedValue ), | ||
}; | ||
} ) } | ||
/> | ||
); | ||
} |
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 { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
const deprecated = [ | ||
{ | ||
supports: { | ||
align: [ 'center', 'left', 'right' ], | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Path, SVG } from '@wordpress/components'; | ||
|
||
export const justifyLeftIcon = ( | ||
<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 justifyCenterIcon = ( | ||
<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 justifyRightIcon = ( | ||
<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
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
Oops, something went wrong.