Skip to content

Commit

Permalink
Light block: introduce useBlockWrapperProps (#23034)
Browse files Browse the repository at this point in the history
* useBlockProps

* Restore delete behaviour

* Rename

* Address feedback

* Avoid inline hook calls

* Convert recently added blocks

* Rebase errors
  • Loading branch information
ellatrix committed Sep 21, 2020
1 parent 7491f3c commit aa0abb9
Show file tree
Hide file tree
Showing 34 changed files with 472 additions and 338 deletions.
378 changes: 200 additions & 178 deletions packages/block-editor/src/components/block-list/block-wrapper.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*/
import ELEMENTS from './block-wrapper-elements';

export function useBlockWrapperProps( props = {} ) {
return props;
}

const ExtendedBlockComponent = ELEMENTS.reduce( ( acc, element ) => {
acc[ element ] = element;
return acc;
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -210,7 +211,7 @@ function BlockListBlock( {
name,
mode,
blockTitle: blockType.title,
wrapperProps,
wrapperProps: omit( wrapperProps, [ 'data-align' ] ),
};
const memoizedValue = useMemo( () => value, Object.values( value ) );

Expand Down
5 changes: 4 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export { default as __experimentalPreviewOptions } from './preview-options';
export { default as __experimentalUseResizeCanvas } from './use-resize-canvas';
export { default as BlockInspector } from './block-inspector';
export { default as BlockList } from './block-list';
export { Block as __experimentalBlock } from './block-list/block-wrapper';
export {
Block as __experimentalBlock,
useBlockWrapperProps as __experimentalUseBlockWrapperProps,
} from './block-list/block-wrapper';
export { default as BlockMover } from './block-mover';
export { default as BlockPreview } from './block-preview';
export { default as BlockSelectionClearer } from './block-selection-clearer';
Expand Down
5 changes: 4 additions & 1 deletion packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export { default as BlockStyles } from './block-styles';
export { default as DefaultBlockAppender } from './default-block-appender';
export { default as __unstableEditorStyles } from './editor-styles';
export { default as Inserter } from './inserter';
export { Block as __experimentalBlock } from './block-list/block-wrapper';
export {
Block as __experimentalBlock,
useBlockWrapperProps as __experimentalUseBlockWrapperProps,
} from './block-list/block-wrapper';
export { default as FloatingToolbar } from './floating-toolbar';

// State Related Components
Expand Down
35 changes: 34 additions & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ function RichTextWrapper(
__unstableEmbedURLOnPaste,
__unstableDisableFormats: disableFormats,
disableLineBreaks,
unstableOnFocus,
__unstableAllowPrefixTransformations,
__unstableMultilineRootTag,
// Native props.
__unstableMobileNoFocusOnMount,
deleteEnter,
placeholderTextColor,
textAlign,
selectionColor,
tagsToEliminate,
rootTagsToEliminate,
disableEditingMenu,
fontSize,
fontFamily,
fontWeight,
fontStyle,
...props
},
forwardedRef
Expand Down Expand Up @@ -528,7 +544,6 @@ function RichTextWrapper(

const content = (
<RichText
{ ...props }
clientId={ clientId }
identifier={ identifier }
ref={ ref }
Expand Down Expand Up @@ -563,6 +578,11 @@ function RichTextWrapper(
disabled={ disabled }
start={ startAttr }
reversed={ reversed }
unstableOnFocus={ unstableOnFocus }
__unstableAllowPrefixTransformations={
__unstableAllowPrefixTransformations
}
__unstableMultilineRootTag={ __unstableMultilineRootTag }
// Native props.
onCaretVerticalPositionChange={ onCaretVerticalPositionChange }
blockIsSelected={
Expand All @@ -571,6 +591,18 @@ function RichTextWrapper(
: blockIsSelected
}
shouldBlurOnUnmount={ shouldBlurOnUnmount }
__unstableMobileNoFocusOnMount={ __unstableMobileNoFocusOnMount }
deleteEnter={ deleteEnter }
placeholderTextColor={ placeholderTextColor }
textAlign={ textAlign }
selectionColor={ selectionColor }
tagsToEliminate={ tagsToEliminate }
rootTagsToEliminate={ rootTagsToEliminate }
disableEditingMenu={ disableEditingMenu }
fontSize={ fontSize }
fontFamily={ fontFamily }
fontWeight={ fontWeight }
fontStyle={ fontStyle }
>
{ ( {
isSelected: nestedIsSelected,
Expand Down Expand Up @@ -599,6 +631,7 @@ function RichTextWrapper(
{ ( { listBoxId, activeId, onKeyDown } ) => (
<TagName
{ ...editableProps }
{ ...props }
aria-autocomplete={
listBoxId ? 'list' : undefined
}
Expand Down
12 changes: 6 additions & 6 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
MediaPlaceholder,
MediaReplaceFlow,
RichText,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand All @@ -41,7 +41,7 @@ function AudioEdit( {
insertBlocksAfter,
} ) {
const { id, autoplay, caption, loop, preload, src } = attributes;

const blockWrapperProps = useBlockWrapperProps();
const mediaUpload = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return getSettings().mediaUpload;
Expand Down Expand Up @@ -116,7 +116,7 @@ function AudioEdit( {
}
if ( ! src ) {
return (
<Block.div>
<div { ...blockWrapperProps }>
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectAudio }
Expand All @@ -127,7 +127,7 @@ function AudioEdit( {
notices={ noticeUI }
onError={ onUploadError }
/>
</Block.div>
</div>
);
}

Expand Down Expand Up @@ -175,7 +175,7 @@ function AudioEdit( {
/>
</PanelBody>
</InspectorControls>
<Block.figure>
<figure { ...blockWrapperProps }>
{ /*
Disable the audio tag so the user clicking on it won't play the
file or change the position slider when the controls are enabled.
Expand All @@ -197,7 +197,7 @@ function AudioEdit( {
}
/>
) }
</Block.figure>
</figure>
</>
);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
BlockControls,
InspectorControls,
RichText,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
__experimentalLinkControl as LinkControl,
__experimentalUseEditorFeature as useEditorFeature,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -197,11 +197,12 @@ function ButtonEdit( props ) {
);

const colorProps = getColorAndStyleProps( attributes, colors, true );
const blockWrapperProps = useBlockWrapperProps();

return (
<>
<ColorEdit { ...props } />
<Block.div>
<div { ...blockWrapperProps }>
<RichText
placeholder={ placeholder || __( 'Add text…' ) }
value={ text }
Expand Down Expand Up @@ -231,7 +232,7 @@ function ButtonEdit( props ) {
onMerge={ mergeBlocks }
identifier="text"
/>
</Block.div>
</div>
<URLPicker
url={ url }
setAttributes={ setAttributes }
Expand Down
7 changes: 4 additions & 3 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
__experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider,
InnerBlocks,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

/**
Expand All @@ -21,16 +21,17 @@ const alignmentHooksSetting = {
};

function ButtonsEdit() {
const blockWrapperProps = useBlockWrapperProps();
return (
<Block.div>
<div { ...blockWrapperProps }>
<AlignmentHookSettingsProvider value={ alignmentHooksSetting }>
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ BUTTONS_TEMPLATE }
orientation="horizontal"
/>
</AlignmentHookSettingsProvider>
</Block.div>
</div>
);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/block-library/src/code/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { __ } from '@wordpress/i18n';
*/
import {
PlainText,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

export default function CodeEdit( { attributes, setAttributes } ) {
const blockWrapperProps = useBlockWrapperProps();
return (
<Block.pre>
<pre { ...blockWrapperProps }>
<PlainText
__experimentalVersion={ 2 }
tagName="code"
Expand All @@ -22,6 +23,6 @@ export default function CodeEdit( { attributes, setAttributes } ) {
placeholder={ __( 'Write code…' ) }
aria-label={ __( 'Code' ) }
/>
</Block.pre>
</pre>
);
}
23 changes: 18 additions & 5 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
MediaReplaceFlow,
withColors,
ColorPalette,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
__experimentalUseGradient,
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
__experimentalUnitControl as UnitControl,
Expand Down Expand Up @@ -426,14 +426,22 @@ function CoverEdit( {
</>
);

const blockWrapperProps = useBlockWrapperProps();

if ( ! hasBackground ) {
const placeholderIcon = <BlockIcon icon={ icon } />;
const label = __( 'Cover' );

return (
<>
{ controls }
<Block.div className="is-placeholder">
<div
{ ...blockWrapperProps }
className={ classnames(
'is-placeholder',
blockWrapperProps.className
) }
>
<MediaPlaceholder
icon={ placeholderIcon }
labels={ {
Expand All @@ -460,7 +468,7 @@ function CoverEdit( {
/>
</div>
</MediaPlaceholder>
</Block.div>
</div>
</>
);
}
Expand All @@ -484,7 +492,12 @@ function CoverEdit( {
return (
<>
{ controls }
<Block.div className={ classes } data-url={ url } style={ style }>
<div
{ ...blockWrapperProps }
className={ classnames( classes, blockWrapperProps.className ) }
style={ { ...style, ...blockWrapperProps.style } }
data-url={ url }
>
<BoxControlVisualizer
values={ styleAttribute?.spacing?.padding }
showValues={ styleAttribute?.visualizers?.padding }
Expand Down Expand Up @@ -543,7 +556,7 @@ function CoverEdit( {
} }
template={ INNER_BLOCKS_TEMPLATE }
/>
</Block.div>
</div>
</>
);
}
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
BlockControls,
BlockIcon,
MediaPlaceholder,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -272,10 +272,15 @@ export function ImageEdit( {
[ `size-${ sizeSlug }` ]: sizeSlug,
} );

const blockWrapperProps = useBlockWrapperProps( {
ref,
className: classes,
} );

return (
<>
{ controls }
<Block.figure ref={ ref } className={ classes }>
<figure { ...blockWrapperProps }>
{ url && (
<Image
attributes={ attributes }
Expand All @@ -290,7 +295,7 @@ export function ImageEdit( {
/>
) }
{ mediaPlaceholder }
</Block.figure>
</figure>
</>
);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RichText,
BlockControls,
RichTextShortcut,
__experimentalBlock as Block,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';
import { ToolbarGroup } from '@wordpress/components';
import {
Expand Down Expand Up @@ -154,13 +154,15 @@ export default function ListEdit( {
</>
);

const blockWrapperProps = useBlockWrapperProps();

return (
<>
<RichText
identifier="values"
multiline="li"
__unstableMultilineRootTag={ tagName }
tagName={ Block[ tagName ] }
tagName={ tagName }
onChange={ ( nextValues ) =>
setAttributes( { values: nextValues } )
}
Expand All @@ -178,6 +180,7 @@ export default function ListEdit( {
start={ start }
reversed={ reversed }
type={ type }
{ ...blockWrapperProps }
>
{ controls }
</RichText>
Expand Down
Loading

0 comments on commit aa0abb9

Please sign in to comment.