@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
90 lines (89 loc) • 3.45 kB
JavaScript
import clsx from "clsx";
import { useContext, useEffect, useRef, useState } from "@wordpress/element";
import { Skeleton } from "@wordpress/ui";
import { FontLibraryContext } from "./context.mjs";
import { getFacePreviewStyle, getFamilyPreviewStyle } from "./utils/preview-styles.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/global-styles-ui/src/font-library/font-demo.tsx
const loadedPreviews = /* @__PURE__ */ new Set();
function getPreviewUrl(fontFace) {
if (fontFace.preview) return fontFace.preview;
if (fontFace.src) return Array.isArray(fontFace.src) ? fontFace.src[0] : fontFace.src;
}
function getDisplayFontFace(font) {
if ("fontStyle" in font && font.fontStyle || "fontWeight" in font && font.fontWeight) return font;
if ("fontFace" in font && font.fontFace && font.fontFace.length) return font.fontFace.find((face) => face.fontStyle === "normal" && face.fontWeight === "400") || font.fontFace[0];
return {
fontStyle: "normal",
fontWeight: "400",
fontFamily: font.fontFamily
};
}
function FontDemo({ font, text }) {
const ref = useRef(null);
const fontFace = getDisplayFontFace(font);
const style = getFamilyPreviewStyle(font);
text = text || ("name" in font ? font.name : "");
const previewUrl = font.preview ?? getPreviewUrl(fontFace);
const isPreviewImage = Boolean(previewUrl && /\.(png|jpg|jpeg|gif|svg)$/i.test(previewUrl));
const [isIntersecting, setIsIntersecting] = useState(false);
const [isFontLoaded, setIsFontLoaded] = useState(false);
const [resolvedUrl, setResolvedUrl] = useState();
const { loadFontFaceAsset } = useContext(FontLibraryContext);
const isAssetLoaded = isPreviewImage ? !!previewUrl && (loadedPreviews.has(previewUrl) || resolvedUrl === previewUrl) : isFontLoaded;
const estimatedImageWidth = Math.min(Math.max(text.length * 12, 48), 300);
const faceStyles = getFacePreviewStyle(fontFace);
const textDemoStyle = {
fontSize: "18px",
lineHeight: 1,
opacity: isAssetLoaded ? "1" : "0",
...style,
...faceStyles
};
useEffect(() => {
if (isPreviewImage) return;
const observer = new window.IntersectionObserver(([entry]) => {
setIsIntersecting(entry.isIntersecting);
}, {});
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, [isPreviewImage]);
useEffect(() => {
const loadAsset = async () => {
if (isIntersecting && !isPreviewImage) {
if (fontFace.src) await loadFontFaceAsset(fontFace);
setIsFontLoaded(true);
}
};
loadAsset();
}, [
fontFace,
isIntersecting,
loadFontFaceAsset,
isPreviewImage
]);
return /* @__PURE__ */ jsx("div", {
ref,
className: "font-library__font-demo",
children: isPreviewImage ? /* @__PURE__ */ jsxs(Fragment, { children: [!isAssetLoaded && /* @__PURE__ */ jsx(Skeleton, {
className: "font-library__font-variant-demo-skeleton",
style: { width: estimatedImageWidth }
}), /* @__PURE__ */ jsx("img", {
src: previewUrl,
loading: "lazy",
alt: text,
onLoad: () => {
if (previewUrl) loadedPreviews.add(previewUrl);
setResolvedUrl(previewUrl);
},
onError: () => setResolvedUrl(previewUrl),
className: clsx("font-library__font-variant_demo-image", { "is-loading": !isAssetLoaded })
})] }) : /* @__PURE__ */ jsx("span", {
style: textDemoStyle,
className: "font-library__font-variant_demo-text",
children: text
})
});
}
export { FontDemo as default };
//# sourceMappingURL=font-demo.mjs.map