@wordpress/block-library
Version:
Block library for the WordPress editor.
85 lines (84 loc) • 3.12 kB
JavaScript
// packages/block-library/src/navigation/edit/inner-blocks.js
import { useEntityBlockEditor } from "@wordpress/core-data";
import {
useInnerBlocksProps,
InnerBlocks,
store as blockEditorStore
} from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useMemo } from "@wordpress/element";
import PlaceholderPreview from "./placeholder/placeholder-preview";
import { DEFAULT_BLOCK, PRIORITIZED_INSERTER_BLOCKS } from "../constants";
import { jsx } from "react/jsx-runtime";
function NavigationInnerBlocks({
clientId,
hasCustomPlaceholder,
orientation,
templateLock
}) {
const {
isImmediateParentOfSelectedBlock,
selectedBlockHasChildren,
isSelected,
hasSelectedDescendant
} = useSelect(
(select) => {
const {
getBlockCount,
hasSelectedInnerBlock,
getSelectedBlockClientId
} = select(blockEditorStore);
const selectedBlockId = getSelectedBlockClientId();
return {
isImmediateParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
false
),
selectedBlockHasChildren: !!getBlockCount(selectedBlockId),
hasSelectedDescendant: hasSelectedInnerBlock(clientId, true),
// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock.
isSelected: selectedBlockId === clientId
};
},
[clientId]
);
const [blocks, onInput, onChange] = useEntityBlockEditor(
"postType",
"wp_navigation"
);
const parentOrChildHasSelection = isSelected || isImmediateParentOfSelectedBlock && !selectedBlockHasChildren;
const placeholder = useMemo(() => /* @__PURE__ */ jsx(PlaceholderPreview, {}), []);
const hasMenuItems = !!blocks?.length;
const showPlaceholder = !hasCustomPlaceholder && !hasMenuItems && !isSelected;
const innerBlocksProps = useInnerBlocksProps(
{
className: "wp-block-navigation__container"
},
{
value: blocks,
onInput,
onChange,
prioritizedInserterBlocks: PRIORITIZED_INSERTER_BLOCKS,
defaultBlock: DEFAULT_BLOCK,
directInsert: true,
orientation,
templateLock,
// As an exception to other blocks which feature nesting, show
// the block appender even when a child block is selected.
// This should be a temporary fix, to be replaced by improvements to
// the sibling inserter.
// See https://github.com/WordPress/gutenberg/issues/37572.
renderAppender: isSelected || isImmediateParentOfSelectedBlock && !selectedBlockHasChildren || hasSelectedDescendant || // Show the appender while dragging to allow inserting element between item and the appender.
parentOrChildHasSelection ? InnerBlocks.ButtonBlockAppender : false,
placeholder: showPlaceholder ? placeholder : void 0,
__experimentalCaptureToolbars: true,
__unstableDisableLayoutClassNames: true
}
);
return /* @__PURE__ */ jsx("div", { ...innerBlocksProps });
}
export {
NavigationInnerBlocks as default
};
//# sourceMappingURL=inner-blocks.js.map