{"version":3,"file":"element-preview.mjs","names":["blockEditorStore","generatePreviewStateStyles","Iframe","EditorStyles"],"sources":["../src/element-preview.tsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { useMemo } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { getBlockType } from '@wordpress/blocks';\nimport {\n\t__unstableIframe as Iframe,\n\t__unstableEditorStyles as EditorStyles,\n\tstore as blockEditorStore,\n\ttransformStyles,\n} from '@wordpress/block-editor';\nimport { __unstableGeneratePreviewStateStyles as generatePreviewStateStyles } from '@wordpress/global-styles-engine';\nimport type { GlobalStylesStyles } from '@wordpress/global-styles-engine';\n\ninterface ElementPreviewProps {\n\telement: string;\n\t/**\n\t * The selected state, such as `:hover`, if any.\n\t */\n\tstate?: string;\n\t/**\n\t * The styles of the selected state, such as `:hover`, if any. The preview\n\t * is inert and can't trigger the state, so it shows these styles on top of\n\t * the sample's own.\n\t */\n\tstateStyles?: GlobalStylesStyles;\n}\n\n/*\n * Centres the sample and strips the canvas chrome. Everything the sample\n * actually looks like comes from the site's own stylesheet.\n */\nconst PREVIEW_CSS = `\n\tbody {\n\t\tmargin: 0;\n\t\tpadding: 16px;\n\t\tmin-height: 100px;\n\t\tdisplay: flex;\n\t\talign-items: center;\n\t\tjustify-content: center;\n\t\ttext-align: center;\n\t\toverflow: hidden;\n\t}\n\tfigure.wp-block-image img.global-styles-ui-element-preview__media {\n\t\tdisplay: block;\n\t\twidth: 100%;\n\t\theight: 44px;\n\t\tmax-height: 44px;\n\t\tborder-radius: 2px;\n\t\tobject-fit: cover;\n\t\taspect-ratio: auto;\n\t}\n\tfigure {\n\t\tmargin: 0;\n\t\tmax-width: 160px;\n\t}\n\tblockquote {\n\t\tmargin: 0;\n\t\tmax-width: 220px;\n\t}\n\tblockquote p {\n\t\tmargin: 0 0 4px;\n\t}\n`;\n\n// The states only a link can match.\nconst LINK_ONLY_STATES = [ ':link', ':any-link', ':visited' ];\n\n/**\n * Renders a sample of an element using the site's own styles.\n *\n * The sample is real markup carrying the classes the element's selector\n * targets, rendered in the editor's iframe with the generated stylesheet, so it\n * picks up every panel, the theme's fonts and the cascade, rather than a list of\n * properties copied across by hand.\n *\n * @param props\n * @param props.element     The element being previewed.\n * @param props.state       The selected state, if any.\n * @param props.stateStyles The styles of the selected state, if any.\n */\nexport default function ElementPreview( {\n\telement,\n\tstate,\n\tstateStyles,\n}: ElementPreviewProps ) {\n\tconst styles = useSelect(\n\t\t( select ) => select( blockEditorStore ).getSettings().styles,\n\t\t[]\n\t);\n\n\t// Show the selected state by rendering its styles as the element's own.\n\t// The link element's selector (0,0,1) is weaker than a theme's `:link` and\n\t// `:any-link` rules (0,1,0), which the sample always matches, so scope the\n\t// state's styles to the canvas body to outrank them.\n\tconst stateCSS = useMemo( () => {\n\t\tif ( ! stateStyles ) {\n\t\t\treturn '';\n\t\t}\n\t\treturn transformStyles(\n\t\t\t[ { css: generatePreviewStateStyles( stateStyles, element ) } ],\n\t\t\t'.editor-styles-wrapper'\n\t\t).join( '' );\n\t}, [ stateStyles, element ] );\n\n\tconst editorStyles = useMemo(\n\t\t() => [ ...( styles ?? [] ), { css: PREVIEW_CSS }, { css: stateCSS } ],\n\t\t[ styles, stateCSS ]\n\t);\n\n\t// Show a caption on the image the Image block uses for its own example,\n\t// rather than keeping a second copy of that URL here.\n\tconst imageUrl = getBlockType( 'core/image' )?.example?.attributes?.url as\n\t\tstring | undefined;\n\n\tlet sample;\n\tswitch ( element ) {\n\t\tcase 'button':\n\t\t\t// The class is half of the element's selector, so a button only\n\t\t\t// picks up the element's styles when it carries it. Button blocks\n\t\t\t// are links and form buttons, such as the Search block's, are\n\t\t\t// `<button>`s. Only links match the link-only states, such as\n\t\t\t// `:visited`, so show a link for those and a form button otherwise.\n\t\t\tsample =\n\t\t\t\tstate && LINK_ONLY_STATES.includes( state ) ? (\n\t\t\t\t\t<a className=\"wp-element-button\" href=\"#anchor\">\n\t\t\t\t\t\t{ __( 'Call to action' ) }\n\t\t\t\t\t</a>\n\t\t\t\t) : (\n\t\t\t\t\t<button className=\"wp-element-button\" type=\"button\">\n\t\t\t\t\t\t{ __( 'Call to action' ) }\n\t\t\t\t\t</button>\n\t\t\t\t);\n\t\t\tbreak;\n\t\tcase 'textInput':\n\t\t\tsample = (\n\t\t\t\t<input type=\"text\" defaultValue={ __( 'Text' ) } readOnly />\n\t\t\t);\n\t\t\tbreak;\n\t\tcase 'select':\n\t\t\tsample = (\n\t\t\t\t<select>\n\t\t\t\t\t<option>{ __( 'Select an option' ) }</option>\n\t\t\t\t</select>\n\t\t\t);\n\t\t\tbreak;\n\t\tcase 'caption':\n\t\t\tsample = (\n\t\t\t\t<figure className=\"wp-block-image\">\n\t\t\t\t\t{ imageUrl && (\n\t\t\t\t\t\t<img\n\t\t\t\t\t\t\tclassName=\"global-styles-ui-element-preview__media\"\n\t\t\t\t\t\t\tsrc={ imageUrl }\n\t\t\t\t\t\t\talt=\"\"\n\t\t\t\t\t\t/>\n\t\t\t\t\t) }\n\t\t\t\t\t<figcaption className=\"wp-element-caption\">\n\t\t\t\t\t\t{ __( 'A caption for the image above' ) }\n\t\t\t\t\t</figcaption>\n\t\t\t\t</figure>\n\t\t\t);\n\t\t\tbreak;\n\t\tcase 'cite':\n\t\t\tsample = (\n\t\t\t\t<blockquote>\n\t\t\t\t\t<p>{ __( 'In quoting others, we cite ourselves.' ) }</p>\n\t\t\t\t\t<cite>{ __( 'Julio Cortázar' ) }</cite>\n\t\t\t\t</blockquote>\n\t\t\t);\n\t\t\tbreak;\n\t\tcase 'link':\n\t\t\tsample = <a href=\"#anchor\">{ __( 'A link' ) }</a>;\n\t\t\tbreak;\n\t\tcase 'heading':\n\t\t\t// The shared heading entry has no tag of its own; any level shows\n\t\t\t// what it sets, and h2 is the most common in content.\n\t\t\tsample = <h2>{ __( 'Aa' ) }</h2>;\n\t\t\tbreak;\n\t\tdefault: {\n\t\t\t// The heading level elements are named after their tag.\n\t\t\tconst Tag = ( /^h[1-6]$/.test( element ) ? element : 'p' ) as\n\t\t\t\t'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p';\n\t\t\tsample = <Tag>{ __( 'Aa' ) }</Tag>;\n\t\t}\n\t}\n\n\treturn (\n\t\t<div className=\"global-styles-ui-element-preview\">\n\t\t\t<Iframe\n\t\t\t\ttabIndex={ -1 }\n\t\t\t\treadonly\n\t\t\t\ttitle={ __( 'Element preview' ) }\n\t\t\t\taria-hidden=\"true\"\n\t\t\t>\n\t\t\t\t<EditorStyles styles={ editorStyles } />\n\t\t\t\t{ sample }\n\t\t\t</Iframe>\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;;AA+BA,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCpB,MAAM,mBAAmB;CAAE;CAAS;CAAa;AAAW;;;;;;;;;;;;;;AAe5D,SAAwB,eAAgB,EACvC,SACA,OACA,eACwB;CACxB,MAAM,SAAS,WACZ,WAAY,OAAQA,KAAiB,CAAC,CAAC,YAAY,CAAC,CAAC,QACvD,CAAC,CACF;CAMA,MAAM,WAAW,cAAe;EAC/B,IAAK,CAAE,aACN,OAAO;EAER,OAAO,gBACN,CAAE,EAAE,KAAKC,qCAA4B,aAAa,OAAQ,EAAE,CAAE,GAC9D,wBACD,CAAC,CAAC,KAAM,EAAG;CACZ,GAAG,CAAE,aAAa,OAAQ,CAAE;CAE5B,MAAM,eAAe,cACd;EAAE,GAAK,UAAU,CAAC;EAAK,EAAE,KAAK,YAAY;EAAG,EAAE,KAAK,SAAS;CAAE,GACrE,CAAE,QAAQ,QAAS,CACpB;CAIA,MAAM,WAAW,aAAc,YAAa,CAAC,EAAE,SAAS,YAAY;CAGpE,IAAI;CACJ,QAAS,SAAT;EACC,KAAK;GAMJ,SACC,SAAS,iBAAiB,SAAU,KAAM,IACzC,oBAAC,KAAD;IAAG,WAAU;IAAoB,MAAK;IACnC,UAAA,GAAI,gBAAiB;GACrB,CAAA,IAEH,oBAAC,UAAD;IAAQ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