-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share the block style selector code (#14383)
* share the block style selector code * Memoized the style previews to prevent them from rerendering * remove unused CSS * Remove unused class * Dont show block previews unless they are supported * use the internal memo if it exists * don't try to render the style selector if there's no valid block Co-authored-by: Paul Bunkham <[email protected]>
- Loading branch information
Showing
5 changed files
with
124 additions
and
135 deletions.
There are no files selected for viewing
72 changes: 0 additions & 72 deletions
72
extensions/blocks/calendly/blockStylesPreviewAndSelector.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
extensions/shared/components/block-styles-selector/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { isEqual } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { memo } from '@wordpress/element'; | ||
import { getBlockType, getBlockFromExample, createBlock } from '@wordpress/blocks'; | ||
import { BlockPreview } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { ENTER, SPACE } from '@wordpress/keycodes'; | ||
|
||
const StylePreview = ( { attributes, styleOption, viewportWidth, blockName } ) => { | ||
const type = getBlockType( blockName ); | ||
|
||
return ( | ||
<div className="block-editor-block-styles__item-preview"> | ||
<BlockPreview | ||
viewportWidth={ viewportWidth } | ||
blocks={ | ||
type.example | ||
? getBlockFromExample( blockName, { | ||
attributes: { ...type.example.attributes, style: styleOption.value }, | ||
innerBlocks: type.example.innerBlocks, | ||
} ) | ||
: createBlock( type, attributes ) | ||
} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const StylePreviewComponent = memo | ||
? memo( StylePreview, ( prevProps, nextProps ) => isEqual( prevProps, nextProps ) ) | ||
: StylePreview; | ||
|
||
export default function BlockStylesSelector( { | ||
attributes, | ||
clientId, | ||
styleOptions, | ||
onSelectStyle, | ||
activeStyle, | ||
viewportWidth, | ||
} ) { | ||
let block; | ||
if ( useSelect ) { | ||
block = useSelect( select => { | ||
const { getBlock } = select( 'core/block-editor' ); | ||
return getBlock( clientId ); | ||
} ); | ||
} | ||
|
||
return ( | ||
<div className="block-editor-block-styles"> | ||
{ styleOptions.map( styleOption => { | ||
const optionAttributes = { | ||
...attributes, | ||
style: styleOption.value, | ||
}; | ||
return ( | ||
<div | ||
key={ styleOption.value } | ||
className={ classnames( 'block-editor-block-styles__item', { | ||
'is-active': styleOption.value === activeStyle, | ||
} ) } | ||
onClick={ () => { | ||
onSelectStyle( { style: styleOption.value } ); | ||
} } | ||
onKeyDown={ event => { | ||
if ( ENTER === event.keyCode || SPACE === event.keyCode ) { | ||
event.preventDefault(); | ||
onSelectStyle( { style: styleOption.value } ); | ||
} | ||
} } | ||
role="button" | ||
tabIndex="0" | ||
aria-label={ styleOption.label } | ||
> | ||
{ useSelect && block && ( | ||
<StylePreviewComponent | ||
blockName={ block.name } | ||
styleOption={ styleOption } | ||
attributes={ optionAttributes } | ||
viewportWidth={ viewportWidth } | ||
/> | ||
) } | ||
<div className="block-editor-block-styles__item-label">{ styleOption.label }</div> | ||
</div> | ||
); | ||
} ) } | ||
</div> | ||
); | ||
} |