UNPKG

@wordpress/block-library

Version:
113 lines (112 loc) • 3.77 kB
import { useDispatch, useSelect } from "@wordpress/data"; import { InnerBlocks, InspectorControls, privateApis, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { useState } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import GroupPlaceHolder, { useShouldShowPlaceHolder } from "./placeholder.mjs"; import { unlock } from "../lock-unlock.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/group/edit.jsx const { HTMLElementControl } = unlock(privateApis); /** * Render inspector controls for the Group block. * * @param {Object} props Component props. * @param {string} props.tagName The HTML tag name. * @param {Function} props.onSelectTagName onChange function for the SelectControl. * @param {string} props.clientId The client ID of the current block. * * @return {React.JSX.Element} The control group. */ function GroupEditControls({ tagName, onSelectTagName, clientId }) { return /* @__PURE__ */ jsx(InspectorControls, { group: "advanced", children: /* @__PURE__ */ jsx(HTMLElementControl, { tagName, onChange: onSelectTagName, clientId, options: [ { label: __("Default (<div>)"), value: "div" }, { label: "<header>", value: "header" }, { label: "<main>", value: "main" }, { label: "<section>", value: "section" }, { label: "<article>", value: "article" }, { label: "<aside>", value: "aside" }, { label: "<footer>", value: "footer" } ] }) }); } function GroupEdit({ attributes, name, setAttributes, clientId }) { const { hasInnerBlocks, themeSupportsLayout } = useSelect((select) => { const { getBlock, getSettings } = select(store); const block = getBlock(clientId); return { hasInnerBlocks: !!(block && block.innerBlocks.length), themeSupportsLayout: getSettings()?.supportsLayout }; }, [clientId]); const { tagName: TagName = "div", templateLock, allowedBlocks, layout = {} } = attributes; const { type = "default" } = layout; const layoutSupportEnabled = themeSupportsLayout || type === "flex" || type === "grid"; const [dropZoneElement, setDropZoneElement] = useState(null); const blockProps = useBlockProps({ ref: setDropZoneElement }); const [showPlaceholder, setShowPlaceholder] = useShouldShowPlaceHolder({ attributes, usedLayoutType: type, hasInnerBlocks }); let renderAppender; if (showPlaceholder) renderAppender = false; else if (!hasInnerBlocks) renderAppender = InnerBlocks.ButtonBlockAppender; const innerBlocksProps = useInnerBlocksProps(layoutSupportEnabled ? blockProps : { className: "wp-block-group__inner-container" }, { dropZoneElement, templateLock, allowedBlocks, renderAppender }); const { selectBlock } = useDispatch(store); const selectVariation = (nextVariation) => { setAttributes(nextVariation.attributes); selectBlock(clientId, -1); setShowPlaceholder(false); }; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(GroupEditControls, { tagName: TagName, onSelectTagName: (value) => setAttributes({ tagName: value }), clientId }), showPlaceholder && /* @__PURE__ */ jsxs("div", { children: [innerBlocksProps.children, /* @__PURE__ */ jsx(GroupPlaceHolder, { name, onSelect: selectVariation })] }), layoutSupportEnabled && !showPlaceholder && /* @__PURE__ */ jsx(TagName, { ...innerBlocksProps }), !layoutSupportEnabled && !showPlaceholder && /* @__PURE__ */ jsx(TagName, { ...blockProps, children: /* @__PURE__ */ jsx("div", { ...innerBlocksProps }) }) ] }); } export { GroupEdit as default }; //# sourceMappingURL=edit.mjs.map