@wordpress/block-editor
Version:
8 lines (7 loc) • 7.59 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/custom-css.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useMemo } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { useInstanceId } from '@wordpress/compose';\nimport { getBlockType, hasBlockSupport } from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { processCSSNesting } from '@wordpress/global-styles-engine';\nimport { useBlockEditingMode } from '../components/block-editing-mode';\n\n/**\n * Internal dependencies\n */\nimport InspectorControls from '../components/inspector-controls';\nimport AdvancedPanel, {\n\tvalidateCSS,\n} from '../components/global-styles/advanced-panel';\nimport { cleanEmptyObject, useStyleOverride } from './utils';\nimport { store as blockEditorStore } from '../store';\n\n// Stable reference for useInstanceId.\nconst CUSTOM_CSS_INSTANCE_REFERENCE = {};\n\n// Stable empty object reference for useSelect.\nconst EMPTY_STYLE = {};\n\n/**\n * Inspector control for custom CSS.\n *\n * @param {Object} props Component props.\n * @param {string} props.blockName Block name.\n * @param {Function} props.setAttributes Function to set block attributes.\n * @param {Object} props.style Block style attribute.\n */\nfunction CustomCSSControl( { blockName, setAttributes, style } ) {\n\tconst blockEditingMode = useBlockEditingMode();\n\n\tif ( blockEditingMode !== 'default' ) {\n\t\treturn null;\n\t}\n\tconst blockType = getBlockType( blockName );\n\n\tfunction onChange( newStyle ) {\n\t\t// Normalize whitespace-only CSS to undefined so it gets cleaned up.\n\t\tconst css = newStyle?.css?.trim() ? newStyle.css : undefined;\n\t\tsetAttributes( {\n\t\t\tstyle: cleanEmptyObject( { ...newStyle, css } ),\n\t\t} );\n\t}\n\n\tconst cssHelpText = sprintf(\n\t\t// translators: %s: is the name of a block e.g., 'Image' or 'Quote'.\n\t\t__(\n\t\t\t'Add your own CSS to customize the appearance of the %s block. You do not need to include a CSS selector, just add the property and value, e.g. color: red;.'\n\t\t),\n\t\tblockType?.title\n\t);\n\n\treturn (\n\t\t<InspectorControls group=\"advanced\">\n\t\t\t<AdvancedPanel\n\t\t\t\tvalue={ style }\n\t\t\t\tonChange={ onChange }\n\t\t\t\tinheritedValue={ style }\n\t\t\t\thelp={ cssHelpText }\n\t\t\t/>\n\t\t</InspectorControls>\n\t);\n}\n\nfunction CustomCSSEdit( { clientId, name, setAttributes } ) {\n\tconst { style, canEditCSS } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockAttributes, getSettings } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\treturn {\n\t\t\t\tstyle: getBlockAttributes( clientId )?.style || EMPTY_STYLE,\n\t\t\t\tcanEditCSS: getSettings().canEditCSS,\n\t\t\t};\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\t// Don't render the panel if user lacks edit_css capability.\n\tif ( ! canEditCSS ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<CustomCSSControl\n\t\t\tblockName={ name }\n\t\t\tsetAttributes={ setAttributes }\n\t\t\tstyle={ style }\n\t\t/>\n\t);\n}\n\n/**\n * Hook to handle custom CSS for a block in the editor.\n * Generates a unique class and applies scoped CSS via style override.\n *\n * @param {Object} props Block props.\n * @param {Object} props.style Block style attribute.\n * @return {Object} Block props including className for custom CSS scoping.\n */\nfunction useBlockProps( { style } ) {\n\tconst customCSS = style?.css;\n\n\t// Validate CSS is non-empty and passes validation checks.\n\tconst isValidCSS =\n\t\ttypeof customCSS === 'string' &&\n\t\tcustomCSS.trim().length > 0 &&\n\t\tvalidateCSS( customCSS );\n\n\tconst customCSSIdentifier = useInstanceId(\n\t\tCUSTOM_CSS_INSTANCE_REFERENCE,\n\t\t'wp-custom-css'\n\t);\n\n\tconst customCSSSelector = `.${ customCSSIdentifier }`;\n\n\t// Transform the custom CSS using the same logic as global styles.\n\t// Only process if CSS is valid (doesn't contain HTML markup).\n\tconst transformedCSS = useMemo( () => {\n\t\tif ( ! isValidCSS ) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn processCSSNesting( customCSS, customCSSSelector );\n\t}, [ customCSS, customCSSSelector, isValidCSS ] );\n\n\t// Inject the CSS via style override.\n\tuseStyleOverride( { css: transformedCSS } );\n\n\t// Only add the class if there's valid custom CSS.\n\tif ( ! isValidCSS ) {\n\t\treturn {};\n\t}\n\n\treturn {\n\t\tclassName: `has-custom-css ${ customCSSIdentifier }`,\n\t};\n}\n\n/**\n * Adds a marker class to blocks with custom CSS for server-side rendering.\n *\n * @param {Object} props Additional props applied to save element.\n * @param {Object} blockType Block type definition.\n * @param {Object} attributes Block's attributes.\n * @return {Object} Filtered props applied to save element.\n */\nfunction addSaveProps( props, blockType, attributes ) {\n\tif ( ! hasBlockSupport( blockType, 'customCSS', true ) ) {\n\t\treturn props;\n\t}\n\n\tif ( ! attributes?.style?.css?.trim() ) {\n\t\treturn props;\n\t}\n\n\t// Add a class to indicate this block has custom CSS.\n\t// The actual CSS is rendered server-side using the render_block filter.\n\tconst className = props.className\n\t\t? `${ props.className } has-custom-css`\n\t\t: 'has-custom-css';\n\n\treturn {\n\t\t...props,\n\t\tclassName,\n\t};\n}\n\nexport default {\n\tedit: CustomCSSEdit,\n\tuseBlockProps,\n\taddSaveProps,\n\tattributeKeys: [ 'style' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'customCSS', true );\n\t},\n};\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAwB;AACxB,kBAA0B;AAC1B,qBAA8B;AAC9B,oBAA8C;AAC9C,kBAA4B;AAC5B,kCAAkC;AAClC,gCAAoC;AAKpC,gCAA8B;AAC9B,4BAEO;AACP,mBAAmD;AACnD,mBAA0C;AA0CvC;AAvCH,IAAM,gCAAgC,CAAC;AAGvC,IAAM,cAAc,CAAC;AAUrB,SAAS,iBAAkB,EAAE,WAAW,eAAe,MAAM,GAAI;AAChE,QAAM,uBAAmB,+CAAoB;AAE7C,MAAK,qBAAqB,WAAY;AACrC,WAAO;AAAA,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