@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
142 lines (141 loc) • 5.23 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_element = require("@wordpress/element");
let _wordpress_data = require("@wordpress/data");
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_global_styles_engine = require("@wordpress/global-styles-engine");
let react_jsx_runtime = require("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 = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getSettings().styles, []);
const stateCSS = (0, _wordpress_element.useMemo)(() => {
if (!stateStyles) return "";
return (0, _wordpress_block_editor.transformStyles)([{ css: (0, _wordpress_global_styles_engine.__unstableGeneratePreviewStateStyles)(stateStyles, element) }], ".editor-styles-wrapper").join("");
}, [stateStyles, element]);
const editorStyles = (0, _wordpress_element.useMemo)(() => [
...styles ?? [],
{ css: PREVIEW_CSS },
{ css: stateCSS }
], [styles, stateCSS]);
const imageUrl = (0, _wordpress_blocks.getBlockType)("core/image")?.example?.attributes?.url;
let sample;
switch (element) {
case "button":
sample = state && LINK_ONLY_STATES.includes(state) ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
className: "wp-element-button",
href: "#anchor",
children: (0, _wordpress_i18n.__)("Call to action")
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
className: "wp-element-button",
type: "button",
children: (0, _wordpress_i18n.__)("Call to action")
});
break;
case "textInput":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
type: "text",
defaultValue: (0, _wordpress_i18n.__)("Text"),
readOnly: true
});
break;
case "select":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", { children: (0, _wordpress_i18n.__)("Select an option") }) });
break;
case "caption":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("figure", {
className: "wp-block-image",
children: [imageUrl && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
className: "global-styles-ui-element-preview__media",
src: imageUrl,
alt: ""
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("figcaption", {
className: "wp-element-caption",
children: (0, _wordpress_i18n.__)("A caption for the image above")
})]
});
break;
case "cite":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("blockquote", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.__)("In quoting others, we cite ourselves.") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("cite", { children: (0, _wordpress_i18n.__)("Julio Cortázar") })] });
break;
case "link":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
href: "#anchor",
children: (0, _wordpress_i18n.__)("A link")
});
break;
case "heading":
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", { children: (0, _wordpress_i18n.__)("Aa") });
break;
default: {
const Tag = /^h[1-6]$/.test(element) ? element : "p";
sample = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Tag, { children: (0, _wordpress_i18n.__)("Aa") });
}
}
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "global-styles-ui-element-preview",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.__unstableIframe, {
tabIndex: -1,
readonly: true,
title: (0, _wordpress_i18n.__)("Element preview"),
"aria-hidden": "true",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.__unstableEditorStyles, { styles: editorStyles }), sample]
})
});
}
exports.default = ElementPreview;
//# sourceMappingURL=element-preview.cjs.map