Skip to content

Commit

Permalink
Add support for placeholder in AlignmentToolbar component
Browse files Browse the repository at this point in the history
  • Loading branch information
porada committed May 19, 2021
1 parent dcd1dbb commit 8225d47
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ _Note:_ In this example that we render `AlignmentControl` as a child of the `Blo

The current value of the alignment setting. You may only choose from the `Options` listed above.

### `placeholder`
- **Type:** `String`
- **Default:** `left` for left-to-right languages, `right` for right-to-left languages
- **Options:**: `left`, `center`, `right`

The placeholder alignment value that will be reflected by the toolbar icon when the value is `undefined`. If the content inside your block is aligned to center when no specific `value` is defined, you should set `placeholder` to `center`.

### `onChange`

- **Type:** `Function`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AlignmentUI should allow a custom placeholder to be specified 1`] = `
<ToolbarDropdownMenu
controls={
Array [
Object {
"align": "left",
"icon": <SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M4 19.8h8.9v-1.5H4v1.5zm8.9-15.6H4v1.5h8.9V4.2zm-8.9 7v1.5h16v-1.5H4z"
/>
</SVG>,
"isActive": false,
"onClick": [Function],
"role": "menuitemradio",
"title": "Align text left",
},
Object {
"align": "center",
"icon": <SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M16.4 4.2H7.6v1.5h8.9V4.2zM4 11.2v1.5h16v-1.5H4zm3.6 8.6h8.9v-1.5H7.6v1.5z"
/>
</SVG>,
"isActive": false,
"onClick": [Function],
"role": "menuitemradio",
"title": "Align text center",
},
Object {
"align": "right",
"icon": <SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M11.1 19.8H20v-1.5h-8.9v1.5zm0-15.6v1.5H20V4.2h-8.9zM4 12.8h16v-1.5H4v1.5z"
/>
</SVG>,
"isActive": false,
"onClick": [Function],
"role": "menuitemradio",
"title": "Align text right",
},
]
}
icon={
<SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M16.4 4.2H7.6v1.5h8.9V4.2zM4 11.2v1.5h16v-1.5H4zm3.6 8.6h8.9v-1.5H7.6v1.5z"
/>
</SVG>
}
label="Align"
popoverProps={
Object {
"isAlternate": true,
"position": "bottom right",
}
}
toggleProps={
Object {
"describedBy": "Change text alignment",
}
}
/>
`;

exports[`AlignmentUI should allow custom alignment controls to be specified 1`] = `
<ToolbarGroup
controls={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ describe( 'AlignmentUI', () => {
expect( onChangeSpy ).toHaveBeenCalledWith( 'center' );
} );

test( 'should allow a custom placeholder to be specified', () => {
const wrapperPlaceholder = shallow(
<AlignmentUI placeholder="center" />
);
expect( wrapperPlaceholder ).toMatchSnapshot();

const wrapperIllegalPlaceholder = () =>
shallow( <AlignmentUI placeholder="illegal-value" /> );
expect( wrapperIllegalPlaceholder ).not.toThrowError();
} );

test( 'should allow custom alignment controls to be specified', () => {
const wrapperCustomControls = shallow(
<AlignmentUI
Expand Down
8 changes: 8 additions & 0 deletions packages/block-editor/src/components/alignment-control/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const POPOVER_PROPS = {

function AlignmentUI( {
value,
placeholder,
onChange,
alignmentControls = DEFAULT_ALIGNMENT_CONTROLS,
label = __( 'Align' ),
Expand All @@ -51,8 +52,15 @@ function AlignmentUI( {
( control ) => control.align === value
);

const placeholderAlignment = find(
alignmentControls,
( control ) => control.align === placeholder
);

function setIcon() {
if ( activeAlignment ) return activeAlignment.icon;
if ( placeholderAlignment ) return placeholderAlignment.icon;

return isRTL() ? alignRight : alignLeft;
}

Expand Down

0 comments on commit 8225d47

Please sign in to comment.