@wordpress/block-library
Version:
Block library for the WordPress editor.
83 lines (82 loc) • 3.39 kB
JavaScript
import { useDispatch, useSelect } from "@wordpress/data";
import { createBlocksFromInnerBlocksTemplate, store } from "@wordpress/blocks";
import { useState } from "@wordpress/element";
import { __experimentalBlockVariationPicker, store as store$1, useBlockProps } from "@wordpress/block-editor";
import { Button, Placeholder } from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import { useResizeObserver } from "@wordpress/compose";
import { useScopedBlockVariations } from "../utils.mjs";
import { useBlockPatterns } from "./pattern-selection.mjs";
import QueryToolbar from "./query-toolbar.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/query/edit/query-placeholder.jsx
function QueryPlaceholder({ attributes, clientId, name, openPatternSelectionModal, isSelected }) {
const [isStartingBlank, setIsStartingBlank] = useState(false);
const [containerWidth, setContainerWidth] = useState(0);
const resizeObserverRef = useResizeObserver(([entry]) => {
setContainerWidth(entry.contentRect.width);
});
const isSmallContainer = containerWidth > 0 && containerWidth < 160;
const { blockType, activeBlockVariation } = useSelect((select) => {
const { getActiveBlockVariation, getBlockType } = select(store);
return {
blockType: getBlockType(name),
activeBlockVariation: getActiveBlockVariation(name, attributes)
};
}, [name, attributes]);
const hasPatterns = !!useBlockPatterns(clientId, attributes).length;
const icon = activeBlockVariation?.icon?.src || activeBlockVariation?.icon || blockType?.icon?.src;
const label = activeBlockVariation?.title || blockType?.title;
const blockProps = useBlockProps({ ref: resizeObserverRef });
if (isStartingBlank) return /* @__PURE__ */ jsx(QueryVariationPicker, {
clientId,
attributes,
icon,
label
});
return /* @__PURE__ */ jsxs("div", {
...blockProps,
children: [isSelected && /* @__PURE__ */ jsx(QueryToolbar, {
clientId,
attributes,
hasInnerBlocks: false
}), /* @__PURE__ */ jsxs(Placeholder, {
className: "block-editor-media-placeholder",
icon: !isSmallContainer && icon,
label: !isSmallContainer && label,
instructions: !isSmallContainer && __("Choose a pattern for the query loop or start blank."),
withIllustration: isSmallContainer,
children: [!!hasPatterns && !isSmallContainer && /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
onClick: openPatternSelectionModal,
children: __("Choose")
}), !isSmallContainer && /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "secondary",
onClick: () => {
setIsStartingBlank(true);
},
children: __("Start blank")
})]
})]
});
}
function QueryVariationPicker({ clientId, attributes, icon, label }) {
const scopeVariations = useScopedBlockVariations(attributes);
const { replaceInnerBlocks } = useDispatch(store$1);
const blockProps = useBlockProps();
return /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(__experimentalBlockVariationPicker, {
icon,
label,
variations: scopeVariations,
onSelect: (variation) => {
if (variation.innerBlocks) replaceInnerBlocks(clientId, createBlocksFromInnerBlocksTemplate(variation.innerBlocks), false);
}
})
});
}
export { QueryPlaceholder as default };
//# sourceMappingURL=query-placeholder.mjs.map