-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Migrating BlockPatternSetup
to use updated Composite
implementation
#55425
Changes from all commits
72270c1
1461398
01ad516
d4cb67c
8969e9d
ebad31e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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, | ||
|
@@ -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' ], | ||
|
@@ -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' ) } | ||
|
@@ -67,7 +75,6 @@ const SetupContent = ( { | |
key={ pattern.name } | ||
pattern={ pattern } | ||
onSelect={ onBlockPatternSelect } | ||
composite={ composite } | ||
showTitles={ showTitles } | ||
/> | ||
) ) } | ||
|
@@ -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 | ||
|
@@ -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 } | ||
|
@@ -133,7 +145,7 @@ function BlockPatternSlide( { className, pattern, minHeight } ) { | |
{ description } | ||
</VisuallyHidden> | ||
) } | ||
</li> | ||
</div> | ||
); | ||
} | ||
|
||
|
@@ -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 ) | ||
); | ||
Comment on lines
+198
to
+200
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that previous/next buttons are no longer disabled (see |
||
} } | ||
onBlockPatternSelect={ () => { | ||
onPatternSelectCallback( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
|
@@ -85,7 +88,7 @@ | |
align-items: center; | ||
justify-content: space-between; | ||
border-top: 1px solid $gray-300; | ||
align-self: flex-end; | ||
align-self: stretch; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems to make the toolbar more stable, preventing it from jumping around as slide height varies. |
||
|
||
.block-editor-block-pattern-setup__display-controls { | ||
display: flex; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that previous/next buttons are no longer disabled (see
CarouselNavigation
), this is necessary to keep the active slide within bounds.