Skip to content

Commit

Permalink
Share the block style selector code (#14383)
Browse files Browse the repository at this point in the history
* 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
scruffian and pablinos authored Jan 30, 2020
1 parent 7500db5 commit 36c4883
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 135 deletions.
72 changes: 0 additions & 72 deletions extensions/blocks/calendly/blockStylesPreviewAndSelector.js

This file was deleted.

31 changes: 17 additions & 14 deletions extensions/blocks/calendly/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import attributeDetails from './attributes';
import { getValidatedAttributes } from '../../shared/get-validated-attributes';
import SubmitButton from '../../shared/submit-button';
import { getAttributesFromEmbedCode } from './utils';
import BlockStylesPreviewAndSelector from './blockStylesPreviewAndSelector';
import BlockStylesSelector from '../../shared/components/block-styles-selector';

export default function CalendlyEdit( { attributes, className, clientId, setAttributes } ) {
const validatedAttributes = getValidatedAttributes( attributeDetails, attributes );
Expand Down Expand Up @@ -179,8 +179,8 @@ export default function CalendlyEdit( { attributes, className, clientId, setAttr
};

const styleOptions = [
{ name: 'inline', label: __( 'Inline', 'jetpack' ) },
{ name: 'link', label: __( 'Link', 'jetpack' ) },
{ value: 'inline', label: __( 'Inline', 'jetpack' ) },
{ value: 'link', label: __( 'Link', 'jetpack' ) },
];

const blockControls = (
Expand All @@ -192,8 +192,8 @@ export default function CalendlyEdit( { attributes, className, clientId, setAttr
label={ __( 'Style', 'jetpack' ) }
controls={ styleOptions.map( styleOption => ( {
title: styleOption.label,
isActive: styleOption.name === style,
onClick: () => setAttributes( { style: styleOption.name } ),
isActive: styleOption.value === style,
onClick: () => setAttributes( { style: styleOption.value } ),
} ) ) }
popoverProps={ { className: 'is-calendly' } }
/>
Expand All @@ -204,15 +204,18 @@ export default function CalendlyEdit( { attributes, className, clientId, setAttr
const inspectorControls = (
<InspectorControls>
{ url && (
<PanelBody title={ __( 'Styles', 'jetpack' ) }>
<BlockStylesPreviewAndSelector
clientId={ clientId }
styleOptions={ styleOptions }
onSelectStyle={ setAttributes }
activeStyle={ style }
attributes={ attributes }
/>
</PanelBody>
<>
<PanelBody title={ __( 'Styles', 'jetpack' ) }>
<BlockStylesSelector
clientId={ clientId }
styleOptions={ styleOptions }
onSelectStyle={ setAttributes }
activeStyle={ style }
attributes={ attributes }
viewportWidth={ 500 }
/>
</PanelBody>
</>
) }
<PanelBody title={ __( 'Calendar Settings', 'jetpack' ) } initialOpen={ false }>
<form onSubmit={ parseEmbedCode } className={ `${ className }-embed-form-sidebar` }>
Expand Down
37 changes: 9 additions & 28 deletions extensions/blocks/opentable/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { ENTER, SPACE } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import './editor.scss';
import icon from './icon';
import RestaurantPicker from './restaurant-picker';
import BlockStylesSelector from '../../shared/components/block-styles-selector';

import {
getStyleOptions,
Expand Down Expand Up @@ -189,33 +189,14 @@ export default function OpenTableEdit( { attributes, setAttributes, className, c
</InspectorAdvancedControls>
<InspectorControls>
<PanelBody title={ __( 'Styles', 'jetpack' ) }>
<div className="block-editor-block-styles">
{ styleOptions.map( styleOption => {
return (
<div
key={ styleOption.value }
className={ classnames( 'block-editor-block-styles__item is-opentable', {
'is-active': styleOption.value === style,
} ) }
onClick={ () => updateStyle( styleOption.value ) }
onKeyDown={ event => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
event.preventDefault();
updateStyle( styleOption.value );
}
} }
role="button"
tabIndex="0"
aria-label={ styleOption.label }
>
<div className="block-editor-block-styles__item-preview is-opentable">
{ blockPreview( styleOption.value ) }
</div>
<div className="block-editor-block-styles__item-label">{ styleOption.label }</div>
</div>
);
} ) }
</div>
<BlockStylesSelector
clientId={ clientId }
styleOptions={ styleOptions }
onSelectStyle={ setAttributes }
activeStyle={ style }
attributes={ attributes }
viewportWidth={ 150 }
/>
</PanelBody>
<PanelBody title={ __( 'Settings', 'jetpack' ) }>
<RestaurantPicker rids={ rid } onChange={ onPickerSubmit } />
Expand Down
23 changes: 2 additions & 21 deletions extensions/blocks/opentable/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,12 @@

}

.components-toggle-control.is-opentable ,
.block-editor-block-styles__item.is-opentable {
.components-toggle-control.is-opentable {
padding-top: 6px;
}


.block-editor-block-styles__item-preview.is-opentable {
height: 100px;
margin-top: 0;
padding-top: 0;
position: relative;

> iframe {
transform-origin: 0 0;
transform: scale( 0.7 );
width: 143%;
height: 143%;
position: absolute;
z-index: 1;
overflow: hidden;
}
}

.is-opentable {
.is-active {
button.is-active {
font-weight: bold;
}
}
96 changes: 96 additions & 0 deletions extensions/shared/components/block-styles-selector/index.js
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>
);
}

0 comments on commit 36c4883

Please sign in to comment.