@wordpress/block-library
Version:
Block library for the WordPress editor.
62 lines (61 loc) • 2.91 kB
JavaScript
import { useMemo, useState } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import { store } from "@wordpress/notices";
import { useDispatch } from "@wordpress/data";
import { __experimentalBlockPatternsList } from "@wordpress/block-editor";
import { SearchControl, __experimentalHStack } from "@wordpress/components";
import { useAlternativeBlockPatterns, useAlternativeTemplateParts } from "./utils/hooks.mjs";
import { mapTemplatePartToBlockPattern } from "./utils/map-template-part-to-block-pattern.mjs";
import { searchPatterns } from "../../utils/search-patterns.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/template-part/edit/selection-modal.jsx
function TemplatePartSelectionModal({ setAttributes, onClose, templatePartId = null, area, clientId }) {
const [searchValue, setSearchValue] = useState("");
const { templateParts } = useAlternativeTemplateParts(area, templatePartId);
const filteredTemplateParts = useMemo(() => {
const partsAsPatterns = templateParts.map((templatePart) => mapTemplatePartToBlockPattern(templatePart));
return searchPatterns(partsAsPatterns, searchValue);
}, [templateParts, searchValue]);
const blockPatterns = useAlternativeBlockPatterns(area, clientId);
const filteredBlockPatterns = useMemo(() => {
return searchPatterns(blockPatterns, searchValue);
}, [blockPatterns, searchValue]);
const { createSuccessNotice } = useDispatch(store);
const onTemplatePartSelect = (templatePart) => {
setAttributes({
slug: templatePart.slug,
theme: templatePart.theme,
area: void 0
});
createSuccessNotice(sprintf(__("Template Part \"%s\" inserted."), templatePart.title?.rendered || templatePart.slug), { type: "snackbar" });
onClose();
};
const hasTemplateParts = !!filteredTemplateParts.length;
const hasBlockPatterns = !!filteredBlockPatterns.length;
return /* @__PURE__ */ jsxs("div", {
className: "block-library-template-part__selection-content",
children: [
/* @__PURE__ */ jsx("div", {
className: "block-library-template-part__selection-search",
children: /* @__PURE__ */ jsx(SearchControl, {
onChange: setSearchValue,
value: searchValue,
label: __("Search"),
placeholder: __("Search")
})
}),
hasTemplateParts && /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h2", { children: __("Existing template parts") }), /* @__PURE__ */ jsx(__experimentalBlockPatternsList, {
blockPatterns: filteredTemplateParts,
onClickPattern: (pattern) => {
onTemplatePartSelect(pattern.templatePart);
}
})] }),
!hasTemplateParts && !hasBlockPatterns && /* @__PURE__ */ jsx(__experimentalHStack, {
alignment: "center",
children: /* @__PURE__ */ jsx("p", { children: __("No results found.") })
})
]
});
}
export { TemplatePartSelectionModal as default };
//# sourceMappingURL=selection-modal.mjs.map