Skip to content

Commit

Permalink
Switch to callback-based API.
Browse files Browse the repository at this point in the history
Edit implementation:

<InnerBlocks
    __experimentalItemCallback={
        ( item ) => <div>{ item }</div>
    }
/>

Save implementation:

<InnerBlocks.Content
    __experimentalItemCallback={
        ( item ) => <div>{ item }</div>
    }
/>
  • Loading branch information
ZebulanStanphill committed Aug 5, 2020
1 parent 87f98c3 commit b1c0e08
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 26 deletions.
6 changes: 3 additions & 3 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function BlockListBlock( {
toggleSelection,
index,
enableAnimation,
__experimentalWrapper: Wrapper,
__experimentalCallback: wrapperCallback,
} ) {
// In addition to withSelect, we should favor using useSelect in this
// component going forward to avoid leaking new props to the public API
Expand Down Expand Up @@ -198,8 +198,8 @@ function BlockListBlock( {
);
}

if ( Wrapper ) {
blockEdit = <Wrapper>{ blockEdit }</Wrapper>;
if ( wrapperCallback ) {
blockEdit = wrapperCallback( blockEdit );
}

const value = {
Expand Down
6 changes: 4 additions & 2 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function BlockList(
className,
rootClientId,
renderAppender,
__experimentalItemWrapper,
__experimentalItemCallback,
__experimentalTagName = 'div',
__experimentalAppenderTagName,
__experimentalPassedProps = {},
Expand Down Expand Up @@ -111,7 +111,9 @@ function BlockList(
isDropTarget &&
orientation === 'horizontal',
} ) }
__experimentalWrapper={ __experimentalItemWrapper }
__experimentalCallback={
__experimentalItemCallback
}
/>
</AsyncModeProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class BlockList extends Component {
parentWidth,
marginVertical = styles.defaultBlock.marginTop,
marginHorizontal = styles.defaultBlock.marginLeft,
__experimentalItemWrapper,
__experimentalItemCallback,
} = this.props;
return (
<BlockListItem
Expand All @@ -280,7 +280,7 @@ export class BlockList extends Component {
onCaretVerticalPositionChange={
this.onCaretVerticalPositionChange
}
__experimentalWrapper={ __experimentalItemWrapper }
__experimentalCallback={ __experimentalItemCallback }
/>
);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function UncontrolledInnerBlocks( props ) {
const {
clientId,
allowedBlocks,
__experimentalItemWrapper: itemWrapper,
__experimentalItemCallback: itemCallback,
template,
templateLock,
forwardedRef,
Expand Down Expand Up @@ -94,7 +94,7 @@ function UncontrolledInnerBlocks( props ) {
{ ...props }
ref={ forwardedRef }
rootClientId={ clientId }
__experimentalItemWrapper={ itemWrapper }
__experimentalItemCallback={ itemCallback }
className={ classes }
/>
);
Expand Down Expand Up @@ -159,8 +159,10 @@ ForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender;
ForwardedInnerBlocks.ButtonBlockAppender = ButtonBlockAppender;

ForwardedInnerBlocks.Content = withBlockContentContext(
( { BlockContent, __experimentalItemWrapper } ) => (
<BlockContent __experimentalItemWrapper={ __experimentalItemWrapper } />
( { BlockContent, __experimentalItemCallback } ) => (
<BlockContent
__experimentalItemCallback={ __experimentalItemCallback }
/>
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function UncontrolledInnerBlocks( props ) {
marginHorizontal,
horizontalAlignment,
filterInnerBlocks,
__experimentalItemWrapper,
__experimentalItemCallback,
} = props;

const block = useSelect(
Expand Down Expand Up @@ -83,7 +83,7 @@ function UncontrolledInnerBlocks( props ) {
onAddBlock={ onAddBlock }
onDeleteBlock={ onDeleteBlock }
filterInnerBlocks={ filterInnerBlocks }
__experimentalItemWrapper={ __experimentalItemWrapper }
__experimentalItemCallback={ __experimentalItemCallback }
/>
);

Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ function GroupEdit( { attributes, className, clientId } ) {
: () => <InnerBlocks.ButtonBlockAppender />
}
__experimentalTagName="div"
__experimentalItemWrapper="section"
__experimentalItemCallback={ ( item ) => (
<>
<section>{ item }</section>
<p>You can do cool/weird stuff like this now.</p>
</>
) }
__experimentalPassedProps={ {
className: 'wp-block-group__inner-container',
} }
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/group/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ export default function save( { attributes } ) {
return (
<Tag>
<div className="wp-block-group__inner-container">
<InnerBlocks.Content __experimentalItemWrapper="section" />
<InnerBlocks.Content
__experimentalItemCallback={ ( item ) => (
<>
<section>{ item }</section>
<p>You can do cool/weird stuff like this now.</p>
</>
) }
/>
</div>
</Tag>
);
Expand Down
18 changes: 9 additions & 9 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import BlockContentProvider from '../block-content-provider';
/**
* @typedef {Object} WPBlockSerializationOptions Serialization Options.
*
* @property {boolean} isInnerBlocks Whether we are serializing
* inner blocks.
* @property {WPElement} [__experimentalWrapper] Wrapper for block. (Outside of
* comment delimiters.)
* @property {boolean} isInnerBlocks Whether we are serializing
* inner blocks.
* @property {WPElement} [__experimentalCallback] Callback to define HTML
* surrounding block, outside of
* the comment delimiters. Used
* by InnerBlocks API.
*/

/**
Expand Down Expand Up @@ -319,7 +321,7 @@ export function getCommentDelimitedContent(
*/
export function serializeBlock(
block,
{ isInnerBlocks = false, __experimentalWrapper: Wrapper } = {}
{ isInnerBlocks = false, __experimentalCallback: wrapperCallback } = {}
) {
const blockName = block.name;
const saveContent = getBlockContent( block );
Expand All @@ -346,11 +348,9 @@ export function serializeBlock(
);
}

if ( Wrapper ) {
if ( wrapperCallback ) {
return renderToString(
<Wrapper>
<RawHTML>{ unwrappedContent }</RawHTML>
</Wrapper>
wrapperCallback( <RawHTML>{ unwrappedContent }</RawHTML> )
);
}
return unwrappedContent;
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/block-content-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const { Consumer, Provider } = createContext( () => {} );
* @return {WPComponent} Element with BlockContent injected via context.
*/
const BlockContentProvider = ( { children, innerBlocks } ) => {
const BlockContent = ( { __experimentalItemWrapper } ) => {
const BlockContent = ( { __experimentalItemCallback } ) => {
// Value is an array of blocks, so defer to block serializer
const html = serialize( innerBlocks, {
isInnerBlocks: true,
__experimentalWrapper: __experimentalItemWrapper,
__experimentalCallback: __experimentalItemCallback,
} );

// Use special-cased raw HTML tag to avoid default escaping
Expand Down

0 comments on commit b1c0e08

Please sign in to comment.