UNPKG

@wordpress/block-library

Version:
113 lines (112 loc) • 5.57 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_data = require("@wordpress/data"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_components = require("@wordpress/components"); let _wordpress_element = require("@wordpress/element"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/group/placeholder.jsx /** * Returns a custom variation icon. * * @param {string} name The block variation name. * * @return {React.JSX.Element} The SVG element. */ const getGroupPlaceholderIcons = (name = "group") => { return { group: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.SVG, { xmlns: "http://www.w3.org/2000/svg", width: "48", height: "48", viewBox: "0 0 48 48", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Path, { d: "M0 10a2 2 0 0 1 2-2h44a2 2 0 0 1 2 2v28a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V10Z" }) }), "group-row": /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.SVG, { xmlns: "http://www.w3.org/2000/svg", width: "48", height: "48", viewBox: "0 0 48 48", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Path, { d: "M0 10a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v28a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V10Zm25 0a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v28a2 2 0 0 1-2 2H27a2 2 0 0 1-2-2V10Z" }) }), "group-stack": /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.SVG, { xmlns: "http://www.w3.org/2000/svg", width: "48", height: "48", viewBox: "0 0 48 48", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Path, { d: "M0 10a2 2 0 0 1 2-2h44a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V10Zm0 17a2 2 0 0 1 2-2h44a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V27Z" }) }), "group-grid": /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.SVG, { xmlns: "http://www.w3.org/2000/svg", width: "48", height: "48", viewBox: "0 0 48 48", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Path, { d: "M0 10a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V10Zm25 0a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H27a2 2 0 0 1-2-2V10ZM0 27a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V27Zm25 0a2 2 0 0 1 2-2h19a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H27a2 2 0 0 1-2-2V27Z" }) }) }[name]; }; /** * A custom hook to tell the Group block whether to show the variation placeholder. * * @param {Object} props Arguments to pass to hook. * @param {Object} [props.attributes] The block's attributes. * @param {string} [props.usedLayoutType] The block's current layout type. * @param {boolean} [props.hasInnerBlocks] Whether the block has inner blocks. * * @return {[boolean, Function]} A state value and setter function. */ function useShouldShowPlaceHolder({ attributes = { style: void 0, backgroundColor: void 0, textColor: void 0, fontSize: void 0 }, usedLayoutType = "", hasInnerBlocks = false }) { const { style, backgroundColor, textColor, fontSize } = attributes; const [showPlaceholder, setShowPlaceholder] = (0, _wordpress_element.useState)(!hasInnerBlocks && !backgroundColor && !fontSize && !textColor && !style && usedLayoutType !== "flex" && usedLayoutType !== "grid"); if (showPlaceholder && (!!hasInnerBlocks || !!backgroundColor || !!fontSize || !!textColor || !!style || usedLayoutType === "flex")) setShowPlaceholder(false); return [showPlaceholder, setShowPlaceholder]; } /** * Display group variations if none is selected. * * @param {Object} props Component props. * @param {string} props.name The block's name. * @param {Function} props.onSelect Function to set block's attributes. * * @return {React.JSX.Element} The placeholder. */ function GroupPlaceHolder({ name, onSelect }) { const variations = (0, _wordpress_data.useSelect)((select) => select(_wordpress_blocks.store).getBlockVariations(name, "block"), [name]); const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: "wp-block-group__placeholder" }); (0, _wordpress_element.useEffect)(() => { if (variations && variations.length === 1) onSelect(variations[0]); }, [onSelect, variations]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...blockProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Placeholder, { instructions: (0, _wordpress_i18n.__)("Group blocks together. Select a layout:"), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", { role: "list", className: "wp-block-group-placeholder__variations", "aria-label": (0, _wordpress_i18n.__)("Block variations"), children: variations.map((variation) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, variant: "tertiary", icon: getGroupPlaceholderIcons(variation.name), iconSize: 48, onClick: () => onSelect(variation), className: "wp-block-group-placeholder__variation-button", label: `${variation.title}: ${variation.description}` }) }, variation.name)) }) }) }); } exports.default = GroupPlaceHolder; exports.useShouldShowPlaceHolder = useShouldShowPlaceHolder; //# sourceMappingURL=placeholder.cjs.map