@wordpress/block-library
Version:
Block library for the WordPress editor.
92 lines (91 loc) • 3.71 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { Button } from "@wordpress/components";
import { useAsyncList, useInstanceId } from "@wordpress/compose";
import { forwardRef, memo, useLayoutEffect, useMemo, useRef } from "@wordpress/element";
import { getScrollContainer } from "@wordpress/dom";
import HtmlRenderer from "../../../utils/html-renderer.mjs";
import { groupIconsByCollection } from "./utils.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/icon/components/custom-inserter/icon-grid.jsx
const BATCH_SIZE = 20;
const IconGridItem = memo(forwardRef(function IconGridItemContent({ icon, isSelected, onChange }, ref) {
return /* @__PURE__ */ jsxs(Button, {
ref,
className: "wp-block-icon__inserter-grid-icons-list-item",
onClick: () => onChange(icon.name),
variant: isSelected ? "primary" : void 0,
__next40pxDefaultSize: true,
children: [/* @__PURE__ */ jsx("span", {
className: "wp-block-icon__inserter-grid-icons-list-item-icon",
children: /* @__PURE__ */ jsx(HtmlRenderer, { html: icon.content })
}), /* @__PURE__ */ jsx("span", {
className: "wp-block-icon__inserter-grid-icons-list-item-title",
children: icon.label
})]
});
}));
function IconGrid({ icons, onChange, value, collections }) {
const instanceId = useInstanceId(IconGrid, "wp-block-icon__inserter-grid");
const { groups, orderedIcons } = useMemo(() => {
if (!icons?.length || !collections) return {
groups: null,
orderedIcons: icons
};
const iconGroups = groupIconsByCollection(icons, collections);
return {
groups: iconGroups,
orderedIcons: iconGroups.flatMap((group) => group.icons)
};
}, [icons, collections]);
const shownIcons = useAsyncList(orderedIcons, { step: BATCH_SIZE });
const selectedIconRef = useRef();
const selectedIndex = orderedIcons?.findIndex((icon) => icon.name === value) ?? -1;
const isReadyToScroll = selectedIndex >= 0 && (shownIcons.length >= selectedIndex + BATCH_SIZE || shownIcons.length === orderedIcons.length);
useLayoutEffect(() => {
const node = selectedIconRef.current;
if (!isReadyToScroll || !node) return;
if (getScrollContainer(node)?.scrollTop) return;
node.scrollIntoView({ block: "center" });
}, [isReadyToScroll]);
const renderIcons = (iconsToRender) => /* @__PURE__ */ jsx("div", {
className: "wp-block-icon__inserter-grid-icons-list",
children: iconsToRender.map((icon) => /* @__PURE__ */ jsx(IconGridItem, {
ref: icon.name === value ? selectedIconRef : void 0,
icon,
isSelected: icon.name === value,
onChange
}, icon.name))
});
const renderGroups = () => {
let offset = 0;
return groups.map((group) => {
const start = offset;
offset += group.icons.length;
const shownGroupIcons = group.icons.slice(0, Math.max(0, shownIcons.length - start));
if (!shownGroupIcons.length) return null;
const headingId = `${instanceId}-${group.slug}`;
return /* @__PURE__ */ jsxs("div", {
role: "group",
"aria-labelledby": headingId,
children: [/* @__PURE__ */ jsx("h2", {
id: headingId,
className: "wp-block-icon__inserter-grid-group-title",
children: group.label
}), renderIcons(shownGroupIcons)]
}, group.slug);
});
};
if (!icons?.length) return /* @__PURE__ */ jsx("div", {
className: "wp-block-icon__inserter-grid",
children: /* @__PURE__ */ jsx("div", {
className: "wp-block-icon__inserter-grid-no-results",
children: /* @__PURE__ */ jsx("p", { children: __("No results found.") })
})
});
return /* @__PURE__ */ jsx("div", {
className: "wp-block-icon__inserter-grid",
children: groups ? renderGroups() : renderIcons(shownIcons)
});
}
export { IconGrid as default };
//# sourceMappingURL=icon-grid.mjs.map