@wordpress/block-editor
Version:
78 lines (77 loc) • 2.57 kB
JavaScript
// packages/block-editor/src/components/html-element-control/index.js
import { __, sprintf } from "@wordpress/i18n";
import {
SelectControl,
Notice,
__experimentalVStack as VStack
} from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store as blockEditorStore } from "../../store";
import { htmlElementMessages } from "./messages";
import { jsx, jsxs } from "react/jsx-runtime";
function HTMLElementControl({
tagName,
onChange,
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" }
]
}) {
const checkForMainTag = !!clientId && options.some((option) => option.value === "main");
const hasMainElementElsewhere = useSelect(
(select) => {
if (!checkForMainTag) {
return false;
}
const { getClientIdsWithDescendants, getBlockAttributes } = select(blockEditorStore);
return getClientIdsWithDescendants().some((id) => {
if (id === clientId) {
return false;
}
return getBlockAttributes(id)?.tagName === "main";
});
},
[clientId, checkForMainTag]
);
const modifiedOptions = options.map((option) => {
if (option.value === "main" && hasMainElementElsewhere && tagName !== "main") {
return {
...option,
disabled: true,
label: sprintf(
/* translators: %s: HTML element name */
__("%s (Already in use)"),
option.label
)
};
}
return option;
});
return /* @__PURE__ */ jsxs(VStack, { spacing: 2, className: "block-editor-html-element-control", children: [
/* @__PURE__ */ jsx(
SelectControl,
{
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
label: __("HTML element"),
options: modifiedOptions,
value: tagName,
onChange,
help: htmlElementMessages[tagName]
}
),
tagName === "main" && hasMainElementElsewhere && /* @__PURE__ */ jsx(Notice, { status: "warning", isDismissible: false, children: __(
"Multiple <main> elements detected. The duplicate may be in your content or template. This is not valid HTML and may cause accessibility issues. Please change this HTML element."
) })
] });
}
export {
HTMLElementControl as default
};
//# sourceMappingURL=index.js.map