@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
137 lines (136 loc) • 4.26 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { useMemo } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { getBlockType } from "@wordpress/blocks";
import { __unstableEditorStyles, __unstableIframe, store, transformStyles } from "@wordpress/block-editor";
import { __unstableGeneratePreviewStateStyles } from "@wordpress/global-styles-engine";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/global-styles-ui/src/element-preview.tsx
const PREVIEW_CSS = `
body {
margin: 0;
padding: 16px;
min-height: 100px;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
overflow: hidden;
}
figure.wp-block-image img.global-styles-ui-element-preview__media {
display: block;
width: 100%;
height: 44px;
max-height: 44px;
border-radius: 2px;
object-fit: cover;
aspect-ratio: auto;
}
figure {
margin: 0;
max-width: 160px;
}
blockquote {
margin: 0;
max-width: 220px;
}
blockquote p {
margin: 0 0 4px;
}
`;
const LINK_ONLY_STATES = [
":link",
":any-link",
":visited"
];
/**
* Renders a sample of an element using the site's own styles.
*
* The sample is real markup carrying the classes the element's selector
* targets, rendered in the editor's iframe with the generated stylesheet, so it
* picks up every panel, the theme's fonts and the cascade, rather than a list of
* properties copied across by hand.
*
* @param props
* @param props.element The element being previewed.
* @param props.state The selected state, if any.
* @param props.stateStyles The styles of the selected state, if any.
*/
function ElementPreview({ element, state, stateStyles }) {
const styles = useSelect((select) => select(store).getSettings().styles, []);
const stateCSS = useMemo(() => {
if (!stateStyles) return "";
return transformStyles([{ css: __unstableGeneratePreviewStateStyles(stateStyles, element) }], ".editor-styles-wrapper").join("");
}, [stateStyles, element]);
const editorStyles = useMemo(() => [
...styles ?? [],
{ css: PREVIEW_CSS },
{ css: stateCSS }
], [styles, stateCSS]);
const imageUrl = getBlockType("core/image")?.example?.attributes?.url;
let sample;
switch (element) {
case "button":
sample = state && LINK_ONLY_STATES.includes(state) ? /* @__PURE__ */ jsx("a", {
className: "wp-element-button",
href: "#anchor",
children: __("Call to action")
}) : /* @__PURE__ */ jsx("button", {
className: "wp-element-button",
type: "button",
children: __("Call to action")
});
break;
case "textInput":
sample = /* @__PURE__ */ jsx("input", {
type: "text",
defaultValue: __("Text"),
readOnly: true
});
break;
case "select":
sample = /* @__PURE__ */ jsx("select", { children: /* @__PURE__ */ jsx("option", { children: __("Select an option") }) });
break;
case "caption":
sample = /* @__PURE__ */ jsxs("figure", {
className: "wp-block-image",
children: [imageUrl && /* @__PURE__ */ jsx("img", {
className: "global-styles-ui-element-preview__media",
src: imageUrl,
alt: ""
}), /* @__PURE__ */ jsx("figcaption", {
className: "wp-element-caption",
children: __("A caption for the image above")
})]
});
break;
case "cite":
sample = /* @__PURE__ */ jsxs("blockquote", { children: [/* @__PURE__ */ jsx("p", { children: __("In quoting others, we cite ourselves.") }), /* @__PURE__ */ jsx("cite", { children: __("Julio Cortázar") })] });
break;
case "link":
sample = /* @__PURE__ */ jsx("a", {
href: "#anchor",
children: __("A link")
});
break;
case "heading":
sample = /* @__PURE__ */ jsx("h2", { children: __("Aa") });
break;
default: {
const Tag = /^h[1-6]$/.test(element) ? element : "p";
sample = /* @__PURE__ */ jsx(Tag, { children: __("Aa") });
}
}
return /* @__PURE__ */ jsx("div", {
className: "global-styles-ui-element-preview",
children: /* @__PURE__ */ jsxs(__unstableIframe, {
tabIndex: -1,
readonly: true,
title: __("Element preview"),
"aria-hidden": "true",
children: [/* @__PURE__ */ jsx(__unstableEditorStyles, { styles: editorStyles }), sample]
})
});
}
export { ElementPreview as default };
//# sourceMappingURL=element-preview.mjs.map