@wordpress/block-library
Version:
Block library for the WordPress editor.
119 lines (118 loc) • 5.42 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let _wordpress_dom = require("@wordpress/dom");
let _________utils_html_renderer_jsx = require("../../../utils/html-renderer.cjs");
_________utils_html_renderer_jsx = __toESM(_________utils_html_renderer_jsx);
let __utils_js = require("./utils.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/icon/components/custom-inserter/icon-grid.jsx
const BATCH_SIZE = 20;
const IconGridItem = (0, _wordpress_element.memo)((0, _wordpress_element.forwardRef)(function IconGridItemContent({ icon, isSelected, onChange }, ref) {
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Button, {
ref,
className: "wp-block-icon__inserter-grid-icons-list-item",
onClick: () => onChange(icon.name),
variant: isSelected ? "primary" : void 0,
__next40pxDefaultSize: true,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: "wp-block-icon__inserter-grid-icons-list-item-icon",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_________utils_html_renderer_jsx.default, { html: icon.content })
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: "wp-block-icon__inserter-grid-icons-list-item-title",
children: icon.label
})]
});
}));
function IconGrid({ icons, onChange, value, collections }) {
const instanceId = (0, _wordpress_compose.useInstanceId)(IconGrid, "wp-block-icon__inserter-grid");
const { groups, orderedIcons } = (0, _wordpress_element.useMemo)(() => {
if (!icons?.length || !collections) return {
groups: null,
orderedIcons: icons
};
const iconGroups = (0, __utils_js.groupIconsByCollection)(icons, collections);
return {
groups: iconGroups,
orderedIcons: iconGroups.flatMap((group) => group.icons)
};
}, [icons, collections]);
const shownIcons = (0, _wordpress_compose.useAsyncList)(orderedIcons, { step: BATCH_SIZE });
const selectedIconRef = (0, _wordpress_element.useRef)();
const selectedIndex = orderedIcons?.findIndex((icon) => icon.name === value) ?? -1;
const isReadyToScroll = selectedIndex >= 0 && (shownIcons.length >= selectedIndex + BATCH_SIZE || shownIcons.length === orderedIcons.length);
(0, _wordpress_element.useLayoutEffect)(() => {
const node = selectedIconRef.current;
if (!isReadyToScroll || !node) return;
if ((0, _wordpress_dom.getScrollContainer)(node)?.scrollTop) return;
node.scrollIntoView({ block: "center" });
}, [isReadyToScroll]);
const renderIcons = (iconsToRender) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "wp-block-icon__inserter-grid-icons-list",
children: iconsToRender.map((icon) => /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsxs)("div", {
role: "group",
"aria-labelledby": headingId,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
id: headingId,
className: "wp-block-icon__inserter-grid-group-title",
children: group.label
}), renderIcons(shownGroupIcons)]
}, group.slug);
});
};
if (!icons?.length) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "wp-block-icon__inserter-grid",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "wp-block-icon__inserter-grid-no-results",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.__)("No results found.") })
})
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "wp-block-icon__inserter-grid",
children: groups ? renderGroups() : renderIcons(shownIcons)
});
}
exports.default = IconGrid;
//# sourceMappingURL=icon-grid.cjs.map