diff --git a/packages/block-editor/src/components/block-list/index.js b/packages/block-editor/src/components/block-list/index.js index c74ed80bb074b7..f1fba4a7bc4752 100644 --- a/packages/block-editor/src/components/block-list/index.js +++ b/packages/block-editor/src/components/block-list/index.js @@ -30,17 +30,36 @@ const appenderNodesMap = new Map(); */ const BLOCK_ANIMATION_THRESHOLD = 200; -function BlockList( - { - className, - rootClientId, - renderAppender, - __experimentalTagName = 'div', - __experimentalAppenderTagName, - __experimentalPassedProps = {}, - }, - ref -) { +function BlockList( { className, rootClientId, renderAppender }, ref ) { + const Container = rootClientId ? 'div' : RootContainer; + const fallbackRef = useRef(); + const wrapperRef = ref || fallbackRef; + + return ( + + + + + + ); +} + +function Items( { + rootClientId, + renderAppender, + __experimentalAppenderTagName, + wrapperRef, +} ) { function selector( select ) { const { getBlockOrder, @@ -76,12 +95,8 @@ function BlockList( isDraggingBlocks, } = useSelect( selector, [ rootClientId ] ); - const fallbackRef = useRef(); - const element = __experimentalPassedProps.ref || ref || fallbackRef; - - const Container = rootClientId ? __experimentalTagName : RootContainer; const dropTargetIndex = useBlockDropZone( { - element, + element: wrapperRef, rootClientId, } ); @@ -89,72 +104,60 @@ function BlockList( dropTargetIndex === blockClientIds.length && isDraggingBlocks; return ( - - - { blockClientIds.map( ( clientId, index ) => { - const isBlockInSelection = hasMultiSelection - ? multiSelectedBlockClientIds.includes( clientId ) - : selectedBlockClientId === clientId; - - const isDropTarget = - dropTargetIndex === index && isDraggingBlocks; - - return ( - - - - ); + <> + { blockClientIds.map( ( clientId, index ) => { + const isBlockInSelection = hasMultiSelection + ? multiSelectedBlockClientIds.includes( clientId ) + : selectedBlockClientId === clientId; + + const isDropTarget = + dropTargetIndex === index && isDraggingBlocks; + + return ( + + + + ); + } ) } + - - + /> + ); } -const ForwardedBlockList = forwardRef( BlockList ); - -// This component needs to always be synchronous -// as it's the one changing the async mode -// depending on the block selection. -export default forwardRef( ( props, ref ) => { +export function BlockListItems( props ) { + // This component needs to always be synchronous as it's the one changing + // the async mode depending on the block selection. return ( - + ); -} ); +} + +export default forwardRef( BlockList ); diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index 7719bfff230e89..f9b04319ec9b47 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -32,7 +32,10 @@ export { default as __experimentalGradientPickerPanel } from './gradient-picker/ export { default as __experimentalColorGradientControl } from './colors-gradients/control'; export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings'; export { default as __experimentalImageSizeControl } from './image-size-control'; -export { default as InnerBlocks } from './inner-blocks'; +export { + default as InnerBlocks, + useInnerBlocksProps as __experimentalUseInnerBlocksProps, +} from './inner-blocks'; export { default as InspectorAdvancedControls } from './inspector-advanced-controls'; export { default as InspectorControls } from './inspector-controls'; export { default as __experimentalLinkControl } from './link-control'; diff --git a/packages/block-editor/src/components/inner-blocks/index.js b/packages/block-editor/src/components/inner-blocks/index.js index c676d941340bc8..48bb8274636aad 100644 --- a/packages/block-editor/src/components/inner-blocks/index.js +++ b/packages/block-editor/src/components/inner-blocks/index.js @@ -23,7 +23,7 @@ import getBlockContext from './get-block-context'; /** * Internal dependencies */ -import BlockList from '../block-list'; +import { BlockListItems } from '../block-list'; import { BlockContextProvider } from '../block-context'; import { useBlockEditContext } from '../block-edit/context'; import useBlockSync from '../provider/use-block-sync'; @@ -42,31 +42,27 @@ function UncontrolledInnerBlocks( props ) { allowedBlocks, template, templateLock, - forwardedRef, + wrapperRef, templateInsertUpdatesSelection, __experimentalCaptureToolbars: captureToolbars, + __experimentalAppenderTagName, + renderAppender, orientation, } = props; - const isSmallScreen = useViewportMatch( 'medium', '<' ); + useNestedSettingsUpdate( + clientId, + allowedBlocks, + templateLock, + captureToolbars, + orientation + ); - const hasOverlay = useSelect( - ( select ) => { - const { - getBlockName, - isBlockSelected, - hasSelectedInnerBlock, - isNavigationMode, - } = select( 'core/block-editor' ); - const enableClickThrough = isNavigationMode() || isSmallScreen; - return ( - getBlockName( clientId ) !== 'core/template' && - ! isBlockSelected( clientId ) && - ! hasSelectedInnerBlock( clientId, true ) && - enableClickThrough - ); - }, - [ clientId, isSmallScreen ] + useInnerBlockTemplateSync( + clientId, + template, + templateLock, + templateInsertUpdatesSelection ); const context = useSelect( @@ -83,42 +79,18 @@ function UncontrolledInnerBlocks( props ) { [ clientId ] ); - useNestedSettingsUpdate( - clientId, - allowedBlocks, - templateLock, - captureToolbars, - orientation - ); - - useInnerBlockTemplateSync( - clientId, - template, - templateLock, - templateInsertUpdatesSelection - ); - - const classes = classnames( { - 'has-overlay': hasOverlay, - 'is-capturing-toolbar': captureToolbars, - } ); - - const blockList = ( + // This component needs to always be synchronous as it's the one changing + // the async mode depending on the block selection. + return ( - ); - - if ( props.__experimentalTagName ) { - return blockList; - } - - return
{ blockList }
; } /** @@ -135,27 +107,78 @@ function ControlledInnerBlocks( props ) { return ; } +const ForwardedInnerBlocks = forwardRef( ( props, ref ) => { + const innerBlocksProps = useInnerBlocksProps( { ref }, props ); + return ( +
+
+
+ ); +} ); + /** - * Wrapped InnerBlocks component which detects whether to use the controlled or - * uncontrolled variations of the InnerBlocks component. This is the component - * which should be used throughout the application. + * This hook is used to lightly mark an element as an inner blocks wrapper + * element. Call this hook and pass the returned props to the element to mark as + * an inner blocks wrapper, automatically rendering inner blocks as children. If + * you define a ref for the element, it is important to pass the ref to this + * hook, which the hook in turn will pass to the component through the props it + * returns. Optionally, you can also pass any other props through this hook, and + * they will be merged and returned. + * + * @param {Object} props Optional. Props to pass to the element. Must contain + * the ref if one is defined. + * @param {Object} options Optional. Inner blocks options. + * + * @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/inner-blocks/README.md */ -const ForwardedInnerBlocks = forwardRef( ( props, ref ) => { - const { clientId } = useBlockEditContext(); +export function useInnerBlocksProps( props = {}, options = {} ) { const fallbackRef = useRef(); + const { clientId } = useBlockEditContext(); + const isSmallScreen = useViewportMatch( 'medium', '<' ); + const hasOverlay = useSelect( + ( select ) => { + const { + getBlockName, + isBlockSelected, + hasSelectedInnerBlock, + isNavigationMode, + } = select( 'core/block-editor' ); + const enableClickThrough = isNavigationMode() || isSmallScreen; + return ( + getBlockName( clientId ) !== 'core/template' && + ! isBlockSelected( clientId ) && + ! hasSelectedInnerBlock( clientId, true ) && + enableClickThrough + ); + }, + [ clientId, isSmallScreen ] + ); - const allProps = { - clientId, - forwardedRef: ref || fallbackRef, + const ref = props.ref || fallbackRef; + const InnerBlocks = + options.value && options.onChange + ? ControlledInnerBlocks + : UncontrolledInnerBlocks; + + return { ...props, + ref, + className: classnames( + props.className, + 'block-editor-block-list__layout', + { + 'has-overlay': hasOverlay, + } + ), + children: ( + + ), }; - - // Detects if the InnerBlocks should be controlled by an incoming value. - if ( props.value && props.onChange ) { - return ; - } - return ; -} ); +} // Expose default appender placeholders as components. ForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender; diff --git a/packages/block-library/src/buttons/edit.js b/packages/block-library/src/buttons/edit.js index 97c4d4fede1687..6cc9d69f8efb13 100644 --- a/packages/block-library/src/buttons/edit.js +++ b/packages/block-library/src/buttons/edit.js @@ -3,7 +3,7 @@ */ import { __experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider, - InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, useBlockProps, } from '@wordpress/block-editor'; @@ -22,15 +22,14 @@ const alignmentHooksSetting = { function ButtonsEdit() { const blockProps = useBlockProps(); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_BLOCKS, + template: BUTTONS_TEMPLATE, + orientation: 'horizontal', + } ); return ( - +
); } diff --git a/packages/block-library/src/column/edit.js b/packages/block-library/src/column/edit.js index 0a57cbac145a3d..70e1e669bf5118 100644 --- a/packages/block-library/src/column/edit.js +++ b/packages/block-library/src/column/edit.js @@ -12,6 +12,7 @@ import { BlockVerticalAlignmentToolbar, InspectorControls, useBlockProps, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, } from '@wordpress/block-editor'; import { PanelBody, @@ -58,6 +59,12 @@ function ColumnEdit( { className: classes, style: width ? { flexBasis: width } : undefined, } ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + templateLock: false, + renderAppender: hasChildBlocks + ? undefined + : InnerBlocks.ButtonBlockAppender, + } ); return ( <> @@ -89,14 +96,7 @@ function ColumnEdit( { /> - +
); } diff --git a/packages/block-library/src/columns/edit.js b/packages/block-library/src/columns/edit.js index 32a51f9ee202dd..0f9eb104484ca6 100644 --- a/packages/block-library/src/columns/edit.js +++ b/packages/block-library/src/columns/edit.js @@ -12,7 +12,7 @@ import { PanelBody, RangeControl, Notice } from '@wordpress/components'; import { InspectorControls, - InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, BlockControls, BlockVerticalAlignmentToolbar, __experimentalBlockVariationPicker, @@ -66,6 +66,11 @@ function ColumnsEditContainer( { const blockProps = useBlockProps( { className: classes, } ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_BLOCKS, + orientation: 'horizontal', + renderAppender: false, + } ); return ( <> @@ -93,13 +98,7 @@ function ColumnsEditContainer( { ) } - +
); } diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index e6860c83afacce..f8b2b1c7fec8f8 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -26,13 +26,13 @@ import { compose, withInstanceId, useInstanceId } from '@wordpress/compose'; import { BlockControls, BlockIcon, - InnerBlocks, InspectorControls, MediaPlaceholder, MediaReplaceFlow, withColors, ColorPalette, useBlockProps, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, __experimentalUseGradient, __experimentalPanelColorGradientSettings as PanelColorGradientSettings, __experimentalUnitControl as UnitControl, @@ -430,6 +430,12 @@ function CoverEdit( { ); const blockProps = useBlockProps(); + const innerBlocksProps = useInnerBlocksProps( + { + className: 'wp-block-cover__inner-container', + }, + { template: INNER_BLOCKS_TEMPLATE } + ); if ( ! hasBackground ) { const placeholderIcon = ; @@ -554,13 +560,7 @@ function CoverEdit( { /> ) } { isBlogUrl && } - +
); diff --git a/packages/block-library/src/group/edit.js b/packages/block-library/src/group/edit.js index fd8f6e0466c03e..ecf56d13f73dc1 100644 --- a/packages/block-library/src/group/edit.js +++ b/packages/block-library/src/group/edit.js @@ -2,7 +2,11 @@ * WordPress dependencies */ import { useSelect } from '@wordpress/data'; -import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; +import { + InnerBlocks, + useBlockProps, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, +} from '@wordpress/block-editor'; import { __experimentalBoxControl as BoxControl } from '@wordpress/components'; const { __Visualizer: BoxControlVisualizer } = BoxControl; @@ -17,6 +21,16 @@ function GroupEdit( { attributes, clientId } ) { ); const blockProps = useBlockProps(); const { tagName: TagName = 'div' } = attributes; + const innerBlocksProps = useInnerBlocksProps( + { + className: 'wp-block-group__inner-container', + }, + { + renderAppender: hasInnerBlocks + ? undefined + : InnerBlocks.ButtonBlockAppender, + } + ); return ( @@ -24,15 +38,7 @@ function GroupEdit( { attributes, clientId } ) { values={ attributes.style?.spacing?.padding } showValues={ attributes.style?.visualizers?.padding } /> - +
); } diff --git a/packages/block-library/src/media-text/edit.js b/packages/block-library/src/media-text/edit.js index fd83c4d830e7eb..a837b246fd2352 100644 --- a/packages/block-library/src/media-text/edit.js +++ b/packages/block-library/src/media-text/edit.js @@ -13,7 +13,7 @@ import { useState } from '@wordpress/element'; import { BlockControls, BlockVerticalAlignmentToolbar, - InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, InspectorControls, useBlockProps, __experimentalImageURLInputUI as ImageURLInputUI, @@ -285,6 +285,16 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) { style, } ); + const innerBlocksProps = useInnerBlocksProps( + { + className: 'wp-block-media-text__content', + }, + { + template: TEMPLATE, + templateInsertUpdatesSelection: false, + } + ); + return ( <> { mediaTextGeneralSettings } @@ -329,14 +339,7 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) { mediaWidth, } } /> - +
); diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 2b6d2339ea3346..3791d7e103cdc4 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -29,6 +29,7 @@ import { __, sprintf } from '@wordpress/i18n'; import { BlockControls, InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, InspectorControls, RichText, __experimentalLinkControl as LinkControl, @@ -262,6 +263,29 @@ function NavigationLinkEdit( { }, } ); + const innerBlocksProps = useInnerBlocksProps( + { + className: classnames( 'wp-block-navigation__container', { + 'is-parent-of-selected-block': + isParentOfSelectedBlock && + // Don't select as parent of selected block while dragging. + ! isDraggingBlocks, + } ), + }, + { + allowedBlocks: [ 'core/navigation-link' ], + renderAppender: + ( isSelected && hasDescendants ) || + ( isImmediateParentOfSelectedBlock && + ! selectedBlockHasDescendants ) || + // Show the appender while dragging to allow inserting element between item and the appender. + ( isDraggingBlocks && hasDescendants ) + ? InnerBlocks.DefaultAppender + : false, + __experimentalAppenderTagName: 'li', + } + ); + return ( @@ -420,31 +444,7 @@ function NavigationLinkEdit( { ) } - +
    ); diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js index 64564b70cc7f02..6822883685f101 100644 --- a/packages/block-library/src/navigation/edit.js +++ b/packages/block-library/src/navigation/edit.js @@ -10,6 +10,7 @@ import classnames from 'classnames'; import { useState } from '@wordpress/element'; import { InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, InspectorControls, BlockControls, useBlockProps, @@ -51,6 +52,33 @@ function Navigation( { clientId ); + const innerBlocksProps = useInnerBlocksProps( + { + className: 'wp-block-navigation__container', + }, + { + allowedBlocks: [ + 'core/navigation-link', + 'core/search', + 'core/social-links', + ], + orientation: attributes.orientation || 'horizontal', + renderAppender: + ( isImmediateParentOfSelectedBlock && + ! selectedBlockHasDescendants ) || + isSelected + ? InnerBlocks.DefaultAppender + : false, + templateInsertUpdatesSelection: false, + __experimentalAppenderTagName: 'li', + __experimentalCaptureToolbars: true, + // Template lock set to false here so that the Nav + // Block on the experimental menus screen does not + // inherit templateLock={ 'all' }. + templateLock: false, + } + ); + if ( isPlaceholderShown ) { return (
    @@ -150,32 +178,7 @@ function Navigation( { blockClassNames ) } > - +
      ); diff --git a/packages/block-library/src/social-links/edit.js b/packages/block-library/src/social-links/edit.js index eef8507bcc5668..44be658e7fbd11 100644 --- a/packages/block-library/src/social-links/edit.js +++ b/packages/block-library/src/social-links/edit.js @@ -5,7 +5,7 @@ import { Fragment } from '@wordpress/element'; import { - InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, useBlockProps, InspectorControls, } from '@wordpress/block-editor'; @@ -33,6 +33,13 @@ export function SocialLinksEdit( props ) { setAttributes, } = props; const blockProps = useBlockProps(); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_BLOCKS, + templateLock: false, + template: TEMPLATE, + orientation: 'horizontal', + __experimentalAppenderTagName: 'li', + } ); return ( @@ -46,15 +53,7 @@ export function SocialLinksEdit( props ) { /> - +
        ); } diff --git a/packages/block-library/src/template-part/edit/inner-blocks.js b/packages/block-library/src/template-part/edit/inner-blocks.js index a2295a3de6eb40..9b5e2af21428b0 100644 --- a/packages/block-library/src/template-part/edit/inner-blocks.js +++ b/packages/block-library/src/template-part/edit/inner-blocks.js @@ -2,7 +2,10 @@ * WordPress dependencies */ import { useEntityBlockEditor } from '@wordpress/core-data'; -import { InnerBlocks } from '@wordpress/block-editor'; +import { + InnerBlocks, + __experimentalUseInnerBlocksProps as useInnerBlocksProps, +} from '@wordpress/block-editor'; export default function TemplatePartInnerBlocks( { postId: id, @@ -13,15 +16,16 @@ export default function TemplatePartInnerBlocks( { 'wp_template_part', { id } ); - return ( - + const innerBlocksProps = useInnerBlocksProps( + {}, + { + value: blocks, + onInput, + onChange, + renderAppender: hasInnerBlocks + ? undefined + : InnerBlocks.ButtonBlockAppender, + } ); + return
        ; }