UNPKG

@wordpress/global-styles-ui

Version:
98 lines (97 loc) • 3.69 kB
import { BlockPreview } from "@wordpress/block-editor"; import { getBlockFromExample, getBlockType } from "@wordpress/blocks"; import { __experimentalSpacer } from "@wordpress/components"; import { useMemo } from "@wordpress/element"; import { __unstableGeneratePreviewStateStyles, privateApis } from "@wordpress/global-styles-engine"; import { getVariationClassName } from "./utils.mjs"; import { unlock } from "./lock-unlock.mjs"; import { jsx } from "react/jsx-runtime"; // packages/global-styles-ui/src/block-preview-panel.tsx const { getViewportBreakpoints, getViewportBreakpointValueInPixels } = unlock(privateApis); const DEFAULT_PREVIEW_WIDTH_BY_VIEWPORT = { default: 783, "@tablet": 600, "@mobile": 480 }; function getPreviewWidthByViewport(selectedViewport, viewportSettings) { const breakpoints = getViewportBreakpoints(viewportSettings); const mobileWidth = getViewportBreakpointValueInPixels(breakpoints.mobile); if (selectedViewport === "@mobile" && mobileWidth) return mobileWidth; const tabletWidth = getViewportBreakpointValueInPixels(breakpoints.tablet); if (selectedViewport === "@tablet" && tabletWidth) { if (!mobileWidth) return tabletWidth; if (breakpoints.mobile === "480px" && breakpoints.tablet === "782px") return DEFAULT_PREVIEW_WIDTH_BY_VIEWPORT["@tablet"]; return Math.round((mobileWidth + tabletWidth) / 2); } if (selectedViewport === "default") { const desktopBreakpoint = tabletWidth ?? mobileWidth; if (desktopBreakpoint) return desktopBreakpoint + 1; } return DEFAULT_PREVIEW_WIDTH_BY_VIEWPORT[selectedViewport]; } const BlockPreviewPanel = ({ name, variation = "", selectedViewport = "default", selectedState = "default", stateStyles, viewportSettings }) => { const blockExample = getBlockType(name)?.example; const blocks = useMemo(() => { if (!blockExample) return null; const example = { ...blockExample, attributes: { ...blockExample.attributes, style: void 0, className: variation ? getVariationClassName(variation) : blockExample.attributes?.className } }; return getBlockFromExample(name, example); }, [ name, blockExample, variation ]); const stateCSS = useMemo(() => { if (selectedState === "default" || !stateStyles) return ""; return __unstableGeneratePreviewStateStyles(stateStyles, name); }, [ selectedState, stateStyles, name ]); if (!blockExample) return null; const viewportWidth = getPreviewWidthByViewport(selectedViewport, viewportSettings) ?? blockExample.viewportWidth ?? 500; const normalizedViewportWidth = blockExample.viewportWidth ?? 500; const previewScale = Math.max(viewportWidth / normalizedViewportWidth, 1); const previewPadding = 24 * previewScale; const previewHeight = 144; const scale = 235 / viewportWidth; const minHeight = scale !== 0 && scale < 1 && previewHeight ? previewHeight / scale : previewHeight; return /* @__PURE__ */ jsx(__experimentalSpacer, { marginX: 4, marginBottom: 4, children: /* @__PURE__ */ jsx("div", { className: "global-styles-ui__block-preview-panel", style: { maxHeight: previewHeight, boxSizing: "initial" }, children: /* @__PURE__ */ jsx(BlockPreview, { blocks, viewportWidth, minHeight: previewHeight, additionalStyles: [{ css: ` body{ padding: ${previewPadding}px; min-height:${Math.round(minHeight)}px; display:flex; } .is-root-container { width: ${100 / previewScale}%; transform: scale(${previewScale}); transform-origin: top left; } ${stateCSS} ` }] }) }) }); }; export { BlockPreviewPanel as default }; //# sourceMappingURL=block-preview-panel.mjs.map