Skip to content

Commit

Permalink
Template part selection component - fix keyboard controls. (#25881)
Browse files Browse the repository at this point in the history
  • Loading branch information
Addison-Stavlo authored Oct 8, 2020
1 parent ea63a16 commit d592d6d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"memize": "^1.1.0",
"moment": "^2.22.1",
"react-easy-crop": "^3.0.0",
"reakit": "1.1.0",
"tinycolor2": "^1.4.1"
},
"publishConfig": {
Expand Down
22 changes: 20 additions & 2 deletions packages/block-library/src/template-part/edit/selection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
*/
import { __experimentalSearchForm as SearchForm } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import TemplatePartPreviews from './template-part-previews';

const preventArrowKeysPropagation = ( event ) => {
if (
[ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].includes( event.keyCode )
) {
// Stop the key event from propagating up to ObserveTyping.startTypingInTextField.
event.stopPropagation();
}
};
const stopKeyPropagation = ( event ) => event.stopPropagation();

// Disable reason (no-static-element-interactions): Navigational key-presses within
// the menu are prevented from triggering WritingFlow and ObserveTyping interactions.
/* eslint-disable jsx-a11y/no-static-element-interactions */
export default function TemplatePartSelection( { setAttributes, onClose } ) {
const [ filterValue, setFilterValue ] = useState( '' );
return (
<>
<div
onKeyPress={ stopKeyPropagation }
onKeyDown={ preventArrowKeysPropagation }
>
<SearchForm
value={ filterValue }
onChange={ setFilterValue }
Expand All @@ -24,6 +41,7 @@ export default function TemplatePartSelection( { setAttributes, onClose } ) {
onClose={ onClose }
/>
</div>
</>
</div>
);
}
/* eslint-enable jsx-a11y/no-static-element-interactions */
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ import { useAsyncList } from '@wordpress/compose';
* External dependencies
*/
import { groupBy, uniq, deburr } from 'lodash';
import { Composite, useCompositeState, CompositeItem } from 'reakit';

function PreviewPlaceholder() {
return (
<div className="wp-block-template-part__selection-preview-item is-placeholder" />
<div
className="wp-block-template-part__selection-preview-item is-placeholder"
tabIndex={ 0 }
/>
);
}

function TemplatePartItem( { templatePart, setAttributes, onClose } ) {
function TemplatePartItem( {
templatePart,
setAttributes,
onClose,
composite,
} ) {
const {
id,
slug,
Expand Down Expand Up @@ -49,9 +58,9 @@ function TemplatePartItem( { templatePart, setAttributes, onClose } ) {
}, [ id, slug, theme ] );

return (
<div
<CompositeItem
className="wp-block-template-part__selection-preview-item"
role="button"
role="listitem"
onClick={ onClick }
onKeyDown={ ( event ) => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
Expand All @@ -60,12 +69,13 @@ function TemplatePartItem( { templatePart, setAttributes, onClose } ) {
} }
tabIndex={ 0 }
aria-label={ templatePart.slug }
{ ...composite }
>
<BlockPreview blocks={ blocks } />
<div className="wp-block-template-part__selection-preview-item-title">
{ templatePart.slug }
</div>
</div>
</CompositeItem>
);
}

Expand All @@ -85,7 +95,12 @@ function PanelGroup( { title, icon, children } ) {
);
}

function TemplatePartsByTheme( { templateParts, setAttributes, onClose } ) {
function TemplatePartsByTheme( {
templateParts,
setAttributes,
onClose,
composite,
} ) {
const templatePartsByTheme = useMemo( () => {
return Object.values( groupBy( templateParts, 'meta.theme' ) );
}, [ templateParts ] );
Expand All @@ -103,6 +118,7 @@ function TemplatePartsByTheme( { templateParts, setAttributes, onClose } ) {
templatePart={ templatePart }
setAttributes={ setAttributes }
onClose={ onClose }
composite={ composite }
/>
) : (
<PreviewPlaceholder key={ templatePart.id } />
Expand All @@ -117,6 +133,7 @@ function TemplatePartSearchResults( {
setAttributes,
filterValue,
onClose,
composite,
} ) {
const filteredTPs = useMemo( () => {
// Filter based on value.
Expand Down Expand Up @@ -168,6 +185,7 @@ function TemplatePartSearchResults( {
templatePart={ templatePart }
setAttributes={ setAttributes }
onClose={ onClose }
composite={ composite }
/>
) : (
<PreviewPlaceholder key={ templatePart.id } />
Expand All @@ -181,6 +199,7 @@ export default function TemplateParts( {
filterValue,
onClose,
} ) {
const composite = useCompositeState();
const templateParts = useSelect( ( select ) => {
const publishedTemplateParts = select( 'core' ).getEntityRecords(
'postType',
Expand Down Expand Up @@ -216,20 +235,34 @@ export default function TemplateParts( {

if ( filterValue ) {
return (
<TemplatePartSearchResults
templateParts={ templateParts }
setAttributes={ setAttributes }
filterValue={ filterValue }
onClose={ onClose }
/>
<Composite
{ ...composite }
role="list"
aria-label={ __( 'List of template parts' ) }
>
<TemplatePartSearchResults
templateParts={ templateParts }
setAttributes={ setAttributes }
filterValue={ filterValue }
onClose={ onClose }
composite={ composite }
/>
</Composite>
);
}

return (
<TemplatePartsByTheme
templateParts={ templateParts }
setAttributes={ setAttributes }
onClose={ onClose }
/>
<Composite
{ ...composite }
role="list"
aria-label={ __( 'List of template parts' ) }
>
<TemplatePartsByTheme
templateParts={ templateParts }
setAttributes={ setAttributes }
onClose={ onClose }
composite={ composite }
/>
</Composite>
);
}
2 changes: 2 additions & 0 deletions packages/block-library/src/template-part/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
margin-top: $grid-unit-20;
transition: all 0.05s ease-in-out;
border: $border-width solid transparent;
width: 100%;
background-color: $white;

&:hover {
border: $border-width solid var(--wp-admin-theme-color);
Expand Down

0 comments on commit d592d6d

Please sign in to comment.