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

Cover: Customizing Alignment of inner content #21091

Merged
merged 35 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
54cd584
Add initial AlignmentControl
Mar 20, 2020
0d1560b
Refactor AlignmentControl utils. Add basic tests for utils
Mar 20, 2020
569e33d
Move AlignmentControl styled components to dedicated file
Mar 20, 2020
49e11ad
Add AlignControl icon. Refactor and share styles between main Control…
Mar 23, 2020
4ab514f
Update Docs Manifest
Mar 23, 2020
ccd4cde
Export AlignControl as __experimentalAlignControl
Mar 23, 2020
a1d4c8e
Fix export for AlignmentControl (__experimentalAlignmentControl)
Mar 23, 2020
5a747a0
Update packages/components/src/alignment-control/utils.js
Mar 24, 2020
d5bc9c3
Integrate AlignmentControl with Cover block
Mar 25, 2020
37f4eb8
Update Docs manifest and Storyshot
Mar 25, 2020
4651a81
Improve AlignmentMatrix rendering in Edit and Save functions
Mar 25, 2020
1c27b72
Prevent rendering of position className if undefined
Mar 25, 2020
69fe802
Update Cover content position control label
Mar 27, 2020
22073f6
Add padding to Cover (rather than inner content)
Mar 27, 2020
eaa7cc4
Adding a11y and RTL support to AlignmentMatrixControl
Mar 27, 2020
74e0f95
Add basic tests for keyboard movements
Mar 27, 2020
ccabb0a
Cover: Customizing Alignment with Reakit Composite (#21333)
Apr 3, 2020
50ed904
Update comments in AlignmentMatrixControl
Apr 3, 2020
ef5b7a6
Remove unnecessary fragment in Story
Apr 6, 2020
19b3f7c
Refactor styles. Add knobs to AlignmentMatrixControl story
Apr 6, 2020
0c95094
Adjust icon style
Apr 6, 2020
604fb66
Add Tooltip to AlignmentMatrixControl cell
Apr 6, 2020
1d9ed4b
Remove onBlur handling and border styles
Apr 6, 2020
67184bf
Account for center position
Apr 8, 2020
0259d71
Adding code comment for width/height props in AlignmentMatrixControlIcon
Apr 14, 2020
fa3d6c5
Add prefix support in useInstanceId
Apr 14, 2020
f25d306
Add code comment regarding icon export from AlignmentMatrixControl
Apr 14, 2020
4f8f1ca
Remove export of AlignmentMatrixControl icon element
Apr 16, 2020
13551d8
Update stories + snapshot
Apr 16, 2020
655efc6
Simplify nested array flattening
Apr 16, 2020
af7f487
Remove padding from Cover editor.scss
May 11, 2020
79dd122
Adjust BlockAlignmentMatrixToolbar popover className
May 11, 2020
6dabde1
Move padding styles to top of wp-block-cover selector
May 11, 2020
96674de
Fix AlignmentMatrixControl tests for window.focus in JSDOM
May 13, 2020
53f4457
Replace Array.flat with lodash.flattenDeep
May 13, 2020
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 docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@
"markdown_source": "../packages/components/README.md",
"parent": null
},
{
"title": "AlignmentMatrixControl",
"slug": "alignment-matrix-control",
"markdown_source": "../packages/components/src/alignment-matrix-control/README.md",
"parent": "components"
},
{
"title": "AnglePickerControl",
"slug": "angle-picker-control",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { DOWN } from '@wordpress/keycodes';
import {
Button,
Dropdown,
ToolbarGroup,
__experimentalAlignmentMatrixControl as AlignmentMatrixControl,
} from '@wordpress/components';

export function BlockAlignmentMatrixToolbar( props ) {
const {
label = __( 'Change matrix alignment' ),
onChange = noop,
value = 'center',
} = props;

const icon = <AlignmentMatrixControl.Icon value={ value } />;
const className = 'block-editor-block-alignment-matrix-toolbar';
const popoverClassName = `${ className }__popover`;

return (
<Dropdown
position="bottom right"
className={ className }
popoverProps={ { className: popoverClassName } }
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
event.stopPropagation();
onToggle();
}
};

return (
<ToolbarGroup>
<Button
onClick={ onToggle }
aria-haspopup="true"
aria-expanded={ isOpen }
onKeyDown={ openOnArrowDown }
label={ label }
icon={ icon }
showTooltip
/>
</ToolbarGroup>
);
} }
renderContent={ () => (
<AlignmentMatrixControl
hasFocusBorder={ false }
onChange={ onChange }
value={ value }
/>
) }
/>
);
}

export default BlockAlignmentMatrixToolbar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.block-editor-block-alignment-matrix-toolbar__popover {
.components-popover__content {
min-width: 0;
padding: $grid-unit;
width: auto;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious to know why we need custom styling and whether these could be abstracted somehow in Dropdown.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently needed as there's no other way to style the dropdown menu.
It would be lovely to abstract it to Dropdown - perhaps out of scope for this PR?

}
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 @@ -8,6 +8,7 @@ export * from './font-sizes';
export { default as AlignmentToolbar } from './alignment-toolbar';
export { default as Autocomplete } from './autocomplete';
export { default as BlockAlignmentToolbar } from './block-alignment-toolbar';
export { default as __experimentalBlockAlignmentMatrixToolbar } from './block-alignment-matrix-toolbar';
export { default as BlockBreadcrumb } from './block-breadcrumb';
export { BlockContextProvider } from './block-context';
export { default as BlockControls } from './block-controls';
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Only add styles for components that are used inside the editing canvas here:

@import "./autocompleters/style.scss";
@import "./components/block-alignment-matrix-toolbar/style.scss";
@import "./components/block-icon/style.scss";
@import "./components/block-inspector/style.scss";
@import "./components/block-list/style.scss";
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
},
"customGradient": {
"type": "string"
},
"contentPosition": {
"type": "string"
}
}
}
35 changes: 27 additions & 8 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
__experimentalUseGradient,
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
__experimentalUnitControl as UnitControl,
__experimentalBlockAlignmentMatrixToolbar as BlockAlignmentMatrixToolbar,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { withDispatch } from '@wordpress/data';
Expand All @@ -49,6 +50,8 @@ import {
CSS_UNITS,
backgroundImageStyles,
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
} from './shared';

/**
Expand Down Expand Up @@ -233,6 +236,7 @@ function CoverEdit( {
noticeOperations,
} ) {
const {
contentPosition,
id,
backgroundType,
dimRatio,
Expand Down Expand Up @@ -293,6 +297,13 @@ function CoverEdit( {
const controls = (
<>
<BlockControls>
<BlockAlignmentMatrixToolbar
label={ __( 'Change content position' ) }
value={ contentPosition }
onChange={ ( nextPosition ) =>
setAttributes( { contentPosition: nextPosition } )
}
/>
{ hasBackground && (
<MediaReplaceFlow
mediaId={ id }
Expand Down Expand Up @@ -439,14 +450,22 @@ function CoverEdit( {
);
}

const classes = classnames( className, dimRatioToClass( dimRatio ), {
'is-dark-theme': isDark,
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ overlayColor.class ]: overlayColor.class,
'has-background-gradient': gradientValue,
[ gradientClass ]: ! url && gradientClass,
} );
const classes = classnames(
className,
dimRatioToClass( dimRatio ),
{
'is-dark-theme': isDark,
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ overlayColor.class ]: overlayColor.class,
'has-background-gradient': gradientValue,
[ gradientClass ]: ! url && gradientClass,
'has-custom-content-position': ! isContentPositionCenter(
contentPosition
),
},
getPositionClassName( contentPosition )
);

return (
<>
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import {
VIDEO_BACKGROUND_TYPE,
backgroundImageStyles,
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
} from './shared';

export default function save( { attributes } ) {
const {
backgroundType,
gradient,
contentPosition,
customGradient,
customOverlayColor,
dimRatio,
Expand Down Expand Up @@ -70,7 +73,11 @@ export default function save( { attributes } ) {
'has-parallax': hasParallax,
'has-background-gradient': gradient || customGradient,
[ gradientClass ]: ! url && gradientClass,
}
'has-custom-content-position': ! isContentPositionCenter(
contentPosition
),
},
getPositionClassName( contentPosition )
);

return (
Expand Down
27 changes: 27 additions & 0 deletions packages/block-library/src/cover/shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const POSITION_CLASSNAMES = {
'top left': 'is-position-top-left',
'top center': 'is-position-top-center',
'top right': 'is-position-top-right',
'center left': 'is-position-center-left',
'center center': 'is-position-center-center',
center: 'is-position-center-center',
'center right': 'is-position-center-right',
'bottom left': 'is-position-bottom-left',
'bottom center': 'is-position-bottom-center',
'bottom right': 'is-position-bottom-right',
};

export const IMAGE_BACKGROUND_TYPE = 'image';
export const VIDEO_BACKGROUND_TYPE = 'video';
export const COVER_MIN_HEIGHT = 50;
Expand Down Expand Up @@ -56,3 +69,17 @@ export function attributesFromMedia( setAttributes ) {
} );
};
}

export function getPositionClassName( contentPosition ) {
if ( contentPosition === undefined ) return '';

return POSITION_CLASSNAMES[ contentPosition ];
}

export function isContentPositionCenter( contentPosition ) {
return (
! contentPosition ||
contentPosition === 'center center' ||
contentPosition === 'center'
);
}
49 changes: 49 additions & 0 deletions packages/block-library/src/cover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
justify-content: center;
align-items: center;
overflow: hidden;
padding: $grid-unit-20;

&.has-parallax {
background-attachment: fixed;

Expand Down Expand Up @@ -108,6 +110,53 @@
color: inherit;
}
}

// Position: Top
&.is-position-top-left {
align-items: flex-start;
justify-content: flex-start;
}
&.is-position-top-center {
align-items: flex-start;
justify-content: center;
}
&.is-position-top-right {
align-items: flex-start;
justify-content: flex-end;
}
// Position: Center
&.is-position-center-left {
align-items: center;
justify-content: flex-start;
}
&.is-position-center-center {
align-items: center;
justify-content: center;
}
&.is-position-center-right {
align-items: center;
justify-content: flex-end;
}
// Position: Bottom
&.is-position-bottom-left {
align-items: flex-end;
justify-content: flex-start;
}
&.is-position-bottom-center {
align-items: flex-end;
justify-content: center;
}
&.is-position-bottom-right {
align-items: flex-end;
justify-content: flex-end;
}

&.has-custom-content-position.has-custom-content-position {
.wp-block-cover__inner-container {
margin: 0;
width: auto;
}
}
}

.wp-block-cover__video-background {
Expand Down
18 changes: 18 additions & 0 deletions packages/components/src/alignment-matrix-control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# AlignmentMatrixControl

AlignmentMatrixControl components let adjust horizontal and vertical alignments for UI.

## Usage

```jsx
import { AlignmentMatrixControl } from '@wordpress/components';
import { useState } from '@wordpress/elememt';

const Example = () => {
const [ alignment, setAlignment ] = useState( 'center center' );

return (
<AlignmentMatrixControl value={ alignment } onChange={ setAlignment } />
);
};
```
35 changes: 35 additions & 0 deletions packages/components/src/alignment-matrix-control/cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { unstable_CompositeItem as CompositeItem } from 'reakit/Composite';

/**
* Internal dependencies
*/
import Tooltip from '../tooltip';
import VisuallyHidden from '../visually-hidden';

/**
* Internal dependencies
*/
import { ALIGNMENT_LABEL } from './utils';
import {
Cell as CellView,
Point,
} from './styles/alignment-matrix-control-styles';

export default function Cell( { isActive = false, value, ...props } ) {
const tooltipText = ALIGNMENT_LABEL[ value ];

return (
<Tooltip text={ tooltipText }>
<CompositeItem as={ CellView } role="gridcell" { ...props }>
{ /* VoiceOver needs a text content to be rendered within grid cell,
otherwise it'll announce the content as "blank". So we use a visually
hidden element instead of aria-label. */ }
<VisuallyHidden>{ value }</VisuallyHidden>
<Point isActive={ isActive } role="presentation" />
</CompositeItem>
</Tooltip>
);
}
Loading