Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core Columns Block - adds ability to vertically align all the columns #13899

Merged
merged 47 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d9240a8
Adds ability to valign all the columns via parent
getdave Feb 15, 2019
a2293a2
Adds ability to v-align individual Columns
getdave Feb 15, 2019
1f3acbf
Adds ability to select individual Column Blocks to access Block Toolbar
getdave Feb 18, 2019
ea81e28
Adds Material UI icons
getdave Feb 18, 2019
964ea00
Updates to add i18n to text strings
getdave Feb 18, 2019
7b389a8
Extracts reusable VAlignToolbar to DRY up implementation
getdave Feb 18, 2019
21b0cc8
Fixes alignment attribute not updating
getdave Feb 18, 2019
c26aa42
Updates Toolbar API to observe Single Responsibility
getdave Feb 18, 2019
639ce69
Extract deprecations for readability
getdave Feb 18, 2019
3ea9144
Updates valignment default to no alignment specified
getdave Feb 19, 2019
b6c628c
Adds ability to toggle alignment settings on and off
getdave Feb 19, 2019
8cbe359
Adds editor preview of vertical alignment
getdave Feb 19, 2019
cf84290
Adds reusable valign toolbar to editor package components
getdave Feb 20, 2019
b76cb6d
Updates Columns Block to utilise new editor package toolbar component
getdave Feb 20, 2019
63a1b32
Adds documentation to the BlockVerticalAlignmentToolbar
getdave Feb 20, 2019
c15b8df
Updates to use simplified arrow function syntax
getdave Feb 20, 2019
635b8be
Fixes deprecation save definition back to original version
getdave Feb 20, 2019
3ff8179
Updates known classes to use simplified classnames util style
getdave Feb 21, 2019
206fd15
remove extraneous file with icons
marekhrabe Feb 21, 2019
119c771
Fixes icon vars to use standard coding style
getdave Feb 21, 2019
884e101
Updates test to use `it` instead of `test`
getdave Feb 21, 2019
5fa2181
Fix spelling and grammatical errors
getdave Feb 21, 2019
84257b2
Removes duplicate dependencies comment
getdave Feb 21, 2019
9bd7645
Updates code readability and self documentation
getdave Feb 21, 2019
9719b7d
Consolidates selectors for improve readability/comprehension
getdave Feb 21, 2019
014384b
Updates to consolidate similar CSS rules
getdave Feb 21, 2019
3ede33f
Fixes spelling error
getdave Feb 21, 2019
6a7deca
Removes unecessary type checking in favour of coercion rules
getdave Feb 21, 2019
452f386
update icon sizes to match others in Toolbar
marekhrabe Feb 21, 2019
c83a44e
Fixes bottom alignment on front end
getdave Feb 21, 2019
fcc6441
Fix failing e2e testing due to additional of toolbar
getdave Feb 22, 2019
68710d5
Fixes toolbar snapshot unit test
getdave Feb 22, 2019
c8821a6
Adds force update of child columns when parent setting changes
getdave Feb 22, 2019
8c7f720
Updates changelog with `BlockVerticalAlignmentToolbar` feature
getdave Feb 25, 2019
d861e6d
Adds valign feature to changelog for Block Library
getdave Feb 25, 2019
764b1e0
Creates dedicated files for large code blocks
getdave Feb 25, 2019
77badda
Updates to avoid tests depending on erroneous _id prop
getdave Mar 7, 2019
e52e000
Removes usage of unused attribute introduced as experiment
getdave Mar 7, 2019
5e05de1
Updates example to run without erroring due to missing var
getdave Mar 7, 2019
8e65b6b
Fixes comment to match coding style guidelines
getdave Mar 7, 2019
a4f58fb
Fixes to avoid mutation in render method
getdave Mar 7, 2019
2905a82
Fixes single Column Block breadcrumb to RHS of boundary
getdave Mar 12, 2019
af56d2e
Restores pass through click behaviour on single Column Block
getdave Mar 19, 2019
a65d18c
Updates to tidy CSS selectors for improved readability
getdave Mar 19, 2019
46a9b12
Updates to move VAlign Toolbar into Block Editor package
getdave Mar 19, 2019
c8713f5
Automated README update
getdave Mar 19, 2019
56b838f
Updates changlogs to reflect release status
getdave Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ Undocumented declaration.

Undocumented declaration.

### BlockVerticalAlignmentToolbar

[src/index.js#L15-L15](src/index.js#L15-L15)

Undocumented declaration.

### ColorPalette

[src/index.js#L15-L15](src/index.js#L15-L15)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
BlockVerticalAlignmentToolbar
=============================

`BlockVerticalAlignmentToolbar` is a simple `Toolbar` component designed to provide _vertical_ alignment UI controls for use within the editor `BlockControls` toolbar.

This builds upon similar patterns to the [`BlockAlignmentToolbar`](https://github.com/WordPress/gutenberg/tree/master/packages/editor/src/components/block-alignment-toolbar) but is focused on vertical alignment only.

## Usage

In a block's `edit` implementation, render a `<BlockControls />` component. Then inside of this add the `<BlockVerticalAlignmentToolbar />` where required.


```jsx
import { registerBlockType } from '@wordpress/blocks';
import { Fragment } from '@wordpress/element';
import {
BlockControls,
BlockVerticalAlignmentToolbar,
} from '@wordpress/block-editor';

registerBlockType( 'my-plugin/my-block', {
// ...

attributes: {
// other attributes here
// ...

verticalAlignment: {
type: 'string',
},
},

edit( { attributes, setAttributes } ) {

const { verticalAlignment } = attributes;

// Change handler to set Block `attributes`
const onChange = ( alignment ) => setAttributes( { verticalAlignment: alignment } );

return (
<Fragment>
<BlockControls>
<BlockVerticalAlignmentToolbar
onChange={ onChange }
value={ verticalAlignment }
/>
</BlockControls>
<div>
// your Block here
</div>
</Fragment>
);
}
} );
```

_Note:_ by default if you do not provide an initial `value` prop for the current alignment value, then no value will be selected (ie: there is no default alignment set).

_Note:_ the user can repeatedly click on the toolbar buttons to toggle the alignment values on/off. This is handled within the component.

## Props

### `value`
* **Type:** `String`
* **Default:** `undefined`
* **Options:**: `top`, `center`, `bottom`

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


### `onChange`
* **Type:** `Function`

A callback function invoked when the toolbar's alignment value is changed via an interaction with any of the toolbar's buttons. Called with the new alignment value (ie: `top`, `center`, `bottom`, `undefined`) as the only argument.

Note: the value may be `undefined` if the user has toggled the component "off".

```js
const onChange = ( alignment ) => setAttributes( { verticalAlignment: alignment } );
```

## Examples

The [Core Columns](https://github.com/WordPress/gutenberg/tree/master/packages/block-library/src/columns) Block utilises the `BlockVerticalAlignmentToolbar`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { Path, SVG } from '@wordpress/components';

export const alignBottom = (
<SVG xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M16 13h-3V3h-2v10H8l4 4 4-4zM4 19v2h16v-2H4z" />
</SVG>
);

export const alignCenter = (
<SVG xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M8 19h3v4h2v-4h3l-4-4-4 4zm8-14h-3V1h-2v4H8l4 4 4-4zM4 11v2h16v-2H4z"
/>
</SVG>
);

export const alignTop = (
<SVG xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M8 11h3v10h2V11h3l-4-4-4 4zM4 3v2h16V3H4z" />
</SVG>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* WordPress dependencies
*/
import { _x } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';
import { withViewportMatch } from '@wordpress/viewport';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { withBlockEditContext } from '../block-edit/context';
import { alignTop, alignCenter, alignBottom } from './icons';

const BLOCK_ALIGNMENTS_CONTROLS = {
top: {
icon: alignTop,
title: _x( 'Vertically Align Top', 'Block vertical alignment setting' ),
},
center: {
icon: alignCenter,
title: _x( 'Vertically Align Middle', 'Block vertical alignment setting' ),
},
bottom: {
icon: alignBottom,
title: _x( 'Vertically Align Bottom', 'Block vertical alignment setting' ),
},
};

const DEFAULT_CONTROLS = [ 'top', 'center', 'bottom' ];
const DEFAULT_CONTROL = 'top';

export function BlockVerticalAlignmentToolbar( { isCollapsed, value, onChange, controls = DEFAULT_CONTROLS } ) {
function applyOrUnset( align ) {
return () => onChange( value === align ? undefined : align );
}

const activeAlignment = BLOCK_ALIGNMENTS_CONTROLS[ value ];
const defaultAlignmentControl = BLOCK_ALIGNMENTS_CONTROLS[ DEFAULT_CONTROL ];

return (
<Toolbar
isCollapsed={ isCollapsed }
icon={ activeAlignment ? activeAlignment.icon : defaultAlignmentControl.icon }
label={ _x( 'Change Alignment', 'Block vertical alignment setting label' ) }
controls={
controls.map( ( control ) => {
return {
...BLOCK_ALIGNMENTS_CONTROLS[ control ],
isActive: value === control,
onClick: applyOrUnset( control ),
};
} )
}
/>
);
}

export default compose(
withBlockEditContext( ( { clientId } ) => {
return {
clientId,
};
} ),
withViewportMatch( { isLargeViewport: 'medium' } ),
withSelect( ( select, { clientId, isLargeViewport, isCollapsed } ) => {
const { getBlockRootClientId, getEditorSettings } = select( 'core/editor' );
return {
isCollapsed: isCollapsed || ! isLargeViewport || (
! getEditorSettings().hasFixedToolbar &&
getBlockRootClientId( clientId )
),
};
} ),
)( BlockVerticalAlignmentToolbar );
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BlockVerticalAlignmentToolbar should match snapshot 1`] = `
<Toolbar
controls={
Array [
Object {
"icon": <SVG
height="20"
viewBox="0 0 24 24"
width="20"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M0 0h24v24H0V0z"
fill="none"
/>
<Path
d="M8 11h3v10h2V11h3l-4-4-4 4zM4 3v2h16V3H4z"
/>
</SVG>,
"isActive": true,
"onClick": [Function],
"title": "Vertically Align Top",
},
Object {
"icon": <SVG
height="20"
viewBox="0 0 24 24"
width="20"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M0 0h24v24H0V0z"
fill="none"
/>
<Path
d="M8 19h3v4h2v-4h3l-4-4-4 4zm8-14h-3V1h-2v4H8l4 4 4-4zM4 11v2h16v-2H4z"
/>
</SVG>,
"isActive": false,
"onClick": [Function],
"title": "Vertically Align Middle",
},
Object {
"icon": <SVG
height="20"
viewBox="0 0 24 24"
width="20"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M0 0h24v24H0V0z"
fill="none"
/>
<Path
d="M16 13h-3V3h-2v10H8l4 4 4-4zM4 19v2h16v-2H4z"
/>
</SVG>,
"isActive": false,
"onClick": [Function],
"title": "Vertically Align Bottom",
},
]
}
icon={
<SVG
height="20"
viewBox="0 0 24 24"
width="20"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M0 0h24v24H0V0z"
fill="none"
/>
<Path
d="M8 11h3v10h2V11h3l-4-4-4 4zM4 3v2h16V3H4z"
/>
</SVG>
}
label="Change Alignment"
/>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { BlockVerticalAlignmentToolbar } from '../';

describe( 'BlockVerticalAlignmentToolbar', () => {
const alignment = 'top';
const onChange = jest.fn();

const wrapper = shallow( <BlockVerticalAlignmentToolbar value={ alignment } onChange={ onChange } /> );

const controls = wrapper.props().controls;

afterEach( () => {
onChange.mockClear();
} );

it( 'should match snapshot', () => {
expect( wrapper ).toMatchSnapshot();
} );

it( 'should call onChange with undefined, when the control is already active', () => {
const activeControl = controls.find( ( { title } ) => title.toLowerCase().includes( alignment ) );
activeControl.onClick();

expect( activeControl.isActive ).toBe( true );
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( undefined );
} );

it( 'should call onChange with alignment value when the control is inactive', () => {
// note "middle" alias for "center"
const inactiveCenterControl = controls.find( ( { title } ) => title.toLowerCase().includes( 'middle' ) );

inactiveCenterControl.onClick();

expect( inactiveCenterControl.isActive ).toBe( false );
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( 'center' );
} );
} );
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as BlockEdit } from './block-edit';
export { default as BlockFormatControls } from './block-format-controls';
export { default as BlockNavigationDropdown } from './block-navigation/dropdown';
export { default as BlockIcon } from './block-icon';
export { default as BlockVerticalAlignmentToolbar } from './block-vertical-alignment-toolbar';
export { default as ColorPalette } from './color-palette';
export { default as withColorContext } from './color-palette/with-color-context';
export * from './colors';
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.0 (Unreleased)

- Add vertical alignment controls to `columns` Block ([#13899](https://github.com/WordPress/gutenberg/pull/13899/)).

## 2.3.0 (2019-03-06)

### New Feature
Expand Down
Loading