Skip to content

Commit 4681a88

Browse files
Jon Quachdiegohaz
andcommitted
Cover: Customizing Alignment with Reakit Composite (#21333)
* WIP: Attempt to use Composite from Reakit * Fix issues and clean up composite code * Refactor Composite integration and update tests * Update snapshots * Fix keyboard navigation for AlignmentMatrixControl in Toolbar * Fix keyboard interaction to open Alignment position on keyboard down Co-authored-by: Haz <hazdiego@gmail.com>
1 parent c024ec1 commit 4681a88

File tree

12 files changed

+416
-704
lines changed

12 files changed

+416
-704
lines changed

packages/block-editor/src/components/block-alignment-matrix-toolbar/index.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,60 @@ import { noop } from 'lodash';
66
* WordPress dependencies
77
*/
88
import { __ } from '@wordpress/i18n';
9+
import { DOWN } from '@wordpress/keycodes';
910
import {
10-
Toolbar,
11+
Button,
12+
Dropdown,
13+
ToolbarGroup,
1114
__experimentalAlignmentMatrixControl as AlignmentMatrixControl,
1215
} from '@wordpress/components';
1316

14-
const POPOVER_PROPS = {
15-
className: 'block-editor-block-alignment-matrix-toolbar',
16-
position: 'bottom right',
17-
};
18-
1917
export function BlockAlignmentMatrixToolbar( props ) {
2018
const {
21-
isCollapsed = true,
2219
label = __( 'Change matrix alignment' ),
2320
onChange = noop,
2421
value = 'center',
2522
} = props;
2623

2724
const icon = <AlignmentMatrixControl.Icon value={ value } />;
25+
const className = 'block-editor-block-alignment-matrix-toolbar';
2826

2927
return (
30-
<Toolbar
31-
isCollapsed={ isCollapsed }
32-
icon={ icon }
33-
label={ label }
34-
popoverProps={ POPOVER_PROPS }
35-
>
36-
{ () => {
28+
<Dropdown
29+
position="bottom right"
30+
className={ className }
31+
popoverProps={ { className } }
32+
renderToggle={ ( { onToggle, isOpen } ) => {
33+
const openOnArrowDown = ( event ) => {
34+
if ( ! isOpen && event.keyCode === DOWN ) {
35+
event.preventDefault();
36+
event.stopPropagation();
37+
onToggle();
38+
}
39+
};
40+
3741
return (
38-
<AlignmentMatrixControl
39-
hasFocusBorder={ false }
40-
onChange={ onChange }
41-
value={ value }
42-
/>
42+
<ToolbarGroup>
43+
<Button
44+
onClick={ onToggle }
45+
aria-haspopup="true"
46+
aria-expanded={ isOpen }
47+
onKeyDown={ openOnArrowDown }
48+
label={ label }
49+
icon={ icon }
50+
showTooltip
51+
/>
52+
</ToolbarGroup>
4353
);
4454
} }
45-
</Toolbar>
55+
renderContent={ () => (
56+
<AlignmentMatrixControl
57+
hasFocusBorder={ false }
58+
onChange={ onChange }
59+
value={ value }
60+
/>
61+
) }
62+
/>
4663
);
4764
}
4865

packages/block-library/src/cover/shared.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import { __experimentalAlignmentMatrixControl as AlignmentMatrixControl } from '@wordpress/components';
5-
6-
const { __getAlignmentIndex: getAlignmentIndex } = AlignmentMatrixControl;
7-
8-
const POSITION_CLASSNAMES = [
9-
'is-position-top-left',
10-
'is-position-top-center',
11-
'is-position-top-right',
12-
'is-position-center-left',
13-
'is-position-center-center',
14-
'is-position-center-right',
15-
'is-position-bottom-left',
16-
'is-position-bottom-center',
17-
'is-position-bottom-right',
18-
];
1+
const POSITION_CLASSNAMES = {
2+
'top left': 'is-position-top-left',
3+
'top center': 'is-position-top-center',
4+
'top right': 'is-position-top-right',
5+
'center left': 'is-position-center-left',
6+
'center center': 'is-position-center-center',
7+
'center right': 'is-position-center-right',
8+
'bottom left': 'is-position-bottom-left',
9+
'bottom center': 'is-position-bottom-center',
10+
'bottom right': 'is-position-bottom-right',
11+
};
1912

2013
export const IMAGE_BACKGROUND_TYPE = 'image';
2114
export const VIDEO_BACKGROUND_TYPE = 'video';
@@ -79,9 +72,7 @@ export function attributesFromMedia( setAttributes ) {
7972
export function getPositionClassName( contentPosition ) {
8073
if ( contentPosition === undefined ) return '';
8174

82-
const index = getAlignmentIndex( contentPosition );
83-
84-
return POSITION_CLASSNAMES[ index ];
75+
return POSITION_CLASSNAMES[ contentPosition ];
8576
}
8677

8778
export function isContentPositionCenter( contentPosition ) {
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { unstable_CompositeItem as CompositeItem } from 'reakit/Composite';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import VisuallyHidden from '../visually-hidden';
10+
111
/**
212
* Internal dependencies
313
*/
@@ -8,14 +18,12 @@ import {
818

919
export default function Cell( { isActive = false, value, ...props } ) {
1020
return (
11-
<CellView
12-
aria-selected={ isActive }
13-
aria-label={ value }
14-
role="option"
15-
tabIndex={ -1 }
16-
{ ...props }
17-
>
21+
<CompositeItem as={ CellView } role="gridcell" { ...props }>
22+
{ /* VoiceOver needs a text content to be rendered within grid cell,
23+
otherwise it'll announce the content as "blank". So we use a visually
24+
hidden element instead of aria-label. */ }
25+
<VisuallyHidden>{ value }</VisuallyHidden>
1826
<Point isActive={ isActive } role="presentation" />
19-
</CellView>
27+
</CompositeItem>
2028
);
2129
}

packages/components/src/alignment-matrix-control/icon.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
export default function AlignmentMatrixControlIcon( {
1717
className,
18+
disablePointerEvents = true,
1819
height,
1920
size: sizeProp = 24,
2021
value = 'center',
@@ -33,14 +34,15 @@ export default function AlignmentMatrixControlIcon( {
3334
<Root
3435
{ ...props }
3536
className={ classes }
37+
disablePointerEvents={ disablePointerEvents }
3638
role="presentation"
3739
size={ size }
3840
>
3941
{ ALIGNMENTS.map( ( align, index ) => {
4042
const isActive = alignIndex === index;
4143

4244
return (
43-
<Cell key={ align } tabIndex={ -1 }>
45+
<Cell key={ align }>
4446
<Point isActive={ isActive } />
4547
</Cell>
4648
);

0 commit comments

Comments
 (0)