@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
73 lines (72 loc) • 2.47 kB
JavaScript
import { __, _x } from "@wordpress/i18n";
import { Modal, privateApis } from "@wordpress/components";
import { store, useEntityRecords } from "@wordpress/core-data";
import { useSelect } from "@wordpress/data";
import InstalledFonts from "./installed-fonts.mjs";
import FontCollection from "./font-collection.mjs";
import UploadFonts from "./upload-fonts.mjs";
import { unlock } from "../lock-unlock.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/global-styles-ui/src/font-library/modal.tsx
const { Tabs: WCTabs } = unlock(privateApis);
const DEFAULT_TAB = {
id: "installed-fonts",
title: _x("Library", "Font library")
};
const UPLOAD_TAB = {
id: "upload-fonts",
title: _x("Upload", "noun")
};
const tabsFromCollections = (collections) => collections.map(({ slug, name }) => ({
id: slug,
title: collections.length === 1 && slug === "google-fonts" ? __("Install Fonts") : name
}));
function FontLibraryModal({ onRequestClose, defaultTabId = "installed-fonts" }) {
const { records: collections = [] } = useEntityRecords("root", "fontCollection", { _fields: "slug,name,description" });
const canUserCreate = useSelect((select) => {
return select(store).canUser("create", {
kind: "postType",
name: "wp_font_family"
});
}, []);
const tabs = [DEFAULT_TAB];
if (canUserCreate) {
tabs.push(UPLOAD_TAB);
tabs.push(...tabsFromCollections(collections || []));
}
return /* @__PURE__ */ jsx(Modal, {
title: __("Fonts"),
onRequestClose,
isFullScreen: true,
className: "font-library-modal",
children: /* @__PURE__ */ jsxs(WCTabs, {
defaultTabId,
children: [/* @__PURE__ */ jsx("div", {
className: "font-library-modal__tablist-container",
children: /* @__PURE__ */ jsx(WCTabs.TabList, { children: tabs.map(({ id, title }) => /* @__PURE__ */ jsx(WCTabs.Tab, {
tabId: id,
children: title
}, id)) })
}), tabs.map(({ id }) => {
let contents;
switch (id) {
case "upload-fonts":
contents = /* @__PURE__ */ jsx(UploadFonts, {});
break;
case "installed-fonts":
contents = /* @__PURE__ */ jsx(InstalledFonts, {});
break;
default: contents = /* @__PURE__ */ jsx(FontCollection, { slug: id });
}
return /* @__PURE__ */ jsx(WCTabs.TabPanel, {
tabId: id,
focusable: false,
className: "font-library-modal__tab-panel",
children: contents
}, id);
})]
})
});
}
export { FontLibraryModal as default };
//# sourceMappingURL=modal.mjs.map