Skip to content

Commit

Permalink
Migrating BlockPatternSetup to use updated Composite implementati…
Browse files Browse the repository at this point in the history
…on (#55425)

 - Removes `__unstableComposite` imports from `@wordpress/components`
 - Adds private `Composite*` exports from `@wordpress/components`
 - Refactors `BlockPatternSetup` and subcomponents to use updated `Composite` components
 - Additionally adds image semantics, and ensures buttons are available when disabled
  • Loading branch information
Andrew Hayward authored Nov 20, 2023
1 parent 8eb63dd commit 0b153a4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
60 changes: 38 additions & 22 deletions packages/block-editor/src/components/block-pattern-setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { useDispatch } from '@wordpress/data';
import { cloneBlock } from '@wordpress/blocks';
import {
VisuallyHidden,
__unstableComposite as Composite,
__unstableUseCompositeState as useCompositeState,
__unstableCompositeItem as CompositeItem,
privateApis as componentsPrivateApis,
} from '@wordpress/components';

import { useState } from '@wordpress/element';
Expand All @@ -22,6 +20,13 @@ import BlockPreview from '../block-preview';
import SetupToolbar from './setup-toolbar';
import usePatternsSetup from './use-patterns-setup';
import { VIEWMODES } from './constants';
import { unlock } from '../../lock-unlock';

const {
CompositeV2: Composite,
CompositeItemV2: CompositeItem,
useCompositeStoreV2: useCompositeStore,
} = unlock( componentsPrivateApis );

const SetupContent = ( {
viewMode,
Expand All @@ -30,8 +35,9 @@ const SetupContent = ( {
onBlockPatternSelect,
showTitles,
} ) => {
const composite = useCompositeState();
const compositeStore = useCompositeStore();
const containerClass = 'block-editor-block-pattern-setup__container';

if ( viewMode === VIEWMODES.carousel ) {
const slideClass = new Map( [
[ activeSlide, 'active-slide' ],
Expand All @@ -41,23 +47,25 @@ const SetupContent = ( {
return (
<div className="block-editor-block-pattern-setup__carousel">
<div className={ containerClass }>
<ul className="carousel-container">
<div className="carousel-container">
{ patterns.map( ( pattern, index ) => (
<BlockPatternSlide
active={ index === activeSlide }
className={ slideClass.get( index ) || '' }
key={ pattern.name }
pattern={ pattern }
/>
) ) }
</ul>
</div>
</div>
</div>
);
}

return (
<div className="block-editor-block-pattern-setup__grid">
<Composite
{ ...composite }
store={ compositeStore }
role="listbox"
className={ containerClass }
aria-label={ __( 'Patterns list' ) }
Expand All @@ -67,7 +75,6 @@ const SetupContent = ( {
key={ pattern.name }
pattern={ pattern }
onSelect={ onBlockPatternSelect }
composite={ composite }
showTitles={ showTitles }
/>
) ) }
Expand All @@ -76,24 +83,27 @@ const SetupContent = ( {
);
};

function BlockPattern( { pattern, onSelect, composite, showTitles } ) {
function BlockPattern( { pattern, onSelect, showTitles } ) {
const baseClassName = 'block-editor-block-pattern-setup-list';
const { blocks, description, viewportWidth = 700 } = pattern;
const descriptionId = useInstanceId(
BlockPattern,
`${ baseClassName }__item-description`
);
return (
<div
className={ `${ baseClassName }__list-item` }
aria-label={ pattern.title }
aria-describedby={ pattern.description ? descriptionId : undefined }
>
<div className={ `${ baseClassName }__list-item` }>
<CompositeItem
render={
<div
aria-describedby={
description ? descriptionId : undefined
}
aria-label={ pattern.title }
className={ `${ baseClassName }__item` }
/>
}
id={ `${ baseClassName }__pattern__${ pattern.name }` }
role="option"
as="div"
{ ...composite }
className={ `${ baseClassName }__item` }
onClick={ () => onSelect( blocks ) }
>
<BlockPreview
Expand All @@ -115,14 +125,16 @@ function BlockPattern( { pattern, onSelect, composite, showTitles } ) {
);
}

function BlockPatternSlide( { className, pattern, minHeight } ) {
function BlockPatternSlide( { active, className, pattern, minHeight } ) {
const { blocks, title, description } = pattern;
const descriptionId = useInstanceId(
BlockPatternSlide,
'block-editor-block-pattern-setup-list__item-description'
);
return (
<li
<div
aria-hidden={ ! active }
role="img"
className={ `pattern-slide ${ className }` }
aria-label={ title }
aria-describedby={ description ? descriptionId : undefined }
Expand All @@ -133,7 +145,7 @@ function BlockPatternSlide( { className, pattern, minHeight } ) {
{ description }
</VisuallyHidden>
) }
</li>
</div>
);
}

Expand Down Expand Up @@ -178,10 +190,14 @@ const BlockPatternSetup = ( {
activeSlide={ activeSlide }
totalSlides={ patterns.length }
handleNext={ () => {
setActiveSlide( ( active ) => active + 1 );
setActiveSlide( ( active ) =>
Math.min( active + 1, patterns.length - 1 )
);
} }
handlePrevious={ () => {
setActiveSlide( ( active ) => active - 1 );
setActiveSlide( ( active ) =>
Math.max( active - 1, 0 )
);
} }
onBlockPatternSelect={ () => {
onPatternSelectCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const CarouselNavigation = ( {
label={ __( 'Previous pattern' ) }
onClick={ handlePrevious }
disabled={ activeSlide === 0 }
__experimentalIsFocusable
/>
<Button
icon={ chevronRight }
label={ __( 'Next pattern' ) }
onClick={ handleNext }
disabled={ activeSlide === totalSlides - 1 }
__experimentalIsFocusable
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
}

.block-editor-block-pattern-setup-list__item {
scroll-margin: 5px 0;

&:hover .block-editor-block-preview__container {
box-shadow: 0 0 0 2px var(--wp-admin-theme-color);
}
Expand All @@ -44,6 +46,7 @@
color: var(--wp-admin-theme-color);
}
}

.block-editor-block-pattern-setup-list__list-item {
break-inside: avoid-column;
margin-bottom: $grid-unit-30;
Expand Down Expand Up @@ -85,7 +88,7 @@
align-items: center;
justify-content: space-between;
border-top: 1px solid $gray-300;
align-self: flex-end;
align-self: stretch;

.block-editor-block-pattern-setup__display-controls {
display: flex;
Expand Down

0 comments on commit 0b153a4

Please sign in to comment.