From 78052d15878649ad7e48b684bfcbb4df16e1b812 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 11 Apr 2023 13:58:46 +0100 Subject: [PATCH] Simplify CustomCSS UI --- .../global-styles/advanced-panel.js | 82 +++++++++++ .../src/components/global-styles/index.js | 1 + .../components/global-styles/custom-css.js | 131 +++--------------- 3 files changed, 99 insertions(+), 115 deletions(-) create mode 100644 packages/block-editor/src/components/global-styles/advanced-panel.js diff --git a/packages/block-editor/src/components/global-styles/advanced-panel.js b/packages/block-editor/src/components/global-styles/advanced-panel.js new file mode 100644 index 00000000000000..69930152d92042 --- /dev/null +++ b/packages/block-editor/src/components/global-styles/advanced-panel.js @@ -0,0 +1,82 @@ +/** + * WordPress dependencies + */ +import { + TextareaControl, + Tooltip, + __experimentalVStack as VStack, +} from '@wordpress/components'; +import { useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { Icon, info } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import { default as transformStyles } from '../../utils/transform-styles'; + +export default function AdvancedPanel( { + value, + onChange, + inheritedValue = value, +} ) { + // Custom CSS + const [ cssError, setCSSError ] = useState( null ); + const customCSS = inheritedValue?.css; + function handleOnChange( newValue ) { + onChange( { + ...value, + css: newValue, + } ); + if ( cssError ) { + const [ transformed ] = transformStyles( + [ { css: value } ], + '.editor-styles-wrapper' + ); + if ( transformed ) { + setCSSError( null ); + } + } + } + function handleOnBlur( event ) { + if ( ! event?.target?.value ) { + setCSSError( null ); + return; + } + + const [ transformed ] = transformStyles( + [ { css: event.target.value } ], + '.editor-styles-wrapper' + ); + + setCSSError( + transformed === null + ? __( 'There is an error with your CSS structure.' ) + : null + ); + } + + return ( + + handleOnChange( newValue ) } + onBlur={ handleOnBlur } + className="edit-site-global-styles__custom-css-input" + spellCheck={ false } + /> + { cssError && ( + +
+ +
+
+ ) } +
+ ); +} diff --git a/packages/block-editor/src/components/global-styles/index.js b/packages/block-editor/src/components/global-styles/index.js index c738b861884d50..7e41c11c507e78 100644 --- a/packages/block-editor/src/components/global-styles/index.js +++ b/packages/block-editor/src/components/global-styles/index.js @@ -19,3 +19,4 @@ export { default as BorderPanel, useHasBorderPanel } from './border-panel'; export { default as ColorPanel, useHasColorPanel } from './color-panel'; export { default as EffectsPanel, useHasEffectsPanel } from './effects-panel'; export { default as FiltersPanel, useHasFiltersPanel } from './filters-panel'; +export { default as AdvancedPanel } from './advanced-panel'; diff --git a/packages/edit-site/src/components/global-styles/custom-css.js b/packages/edit-site/src/components/global-styles/custom-css.js index afc721b81d5e02..897c17633700e4 100644 --- a/packages/edit-site/src/components/global-styles/custom-css.js +++ b/packages/edit-site/src/components/global-styles/custom-css.js @@ -1,130 +1,31 @@ /** * WordPress dependencies */ -import { useState } from '@wordpress/element'; -import { - TextareaControl, - Panel, - PanelBody, - __experimentalVStack as VStack, - Tooltip, - Icon, -} from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; -import { - privateApis as blockEditorPrivateApis, - transformStyles, -} from '@wordpress/block-editor'; -import { info } from '@wordpress/icons'; +import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; /** * Internal dependencies */ import { unlock } from '../../private-apis'; -import Subtitle from './subtitle'; -const { useGlobalStyle } = unlock( blockEditorPrivateApis ); -function CustomCSSControl( { blockName } ) { - // If blockName is defined, we are customizing CSS at the block level: - // styles.blocks.blockName.css - const block = !! blockName ? blockName : null; - - const [ customCSS, setCustomCSS ] = useGlobalStyle( 'css', block ); - const [ themeCSS ] = useGlobalStyle( 'css', block, 'base' ); - const [ cssError, setCSSError ] = useState( null ); - const ignoreThemeCustomCSS = '/* IgnoreThemeCustomCSS */'; - - // If there is custom css from theme.json show it in the edit box - // so the user can selectively overwrite it, rather than have the user CSS - // completely overwrite the theme CSS by default. - const themeCustomCSS = - ! customCSS && themeCSS - ? `/* ${ __( - 'Theme Custom CSS start' - ) } */\n${ themeCSS }\n/* ${ __( 'Theme Custom CSS end' ) } */` - : undefined; - - function handleOnChange( value ) { - // If there is theme custom CSS, but the user clears the input box then save the - // ignoreThemeCustomCSS string so that the theme custom CSS is not re-applied. - if ( themeCSS && value === '' ) { - setCustomCSS( ignoreThemeCustomCSS ); - return; - } - setCustomCSS( value ); - if ( cssError ) { - const [ transformed ] = transformStyles( - [ { css: value } ], - '.editor-styles-wrapper' - ); - if ( transformed ) { - setCSSError( null ); - } - } - } +const { useGlobalStyle, AdvancedPanel: StylesAdvancedPanel } = unlock( + blockEditorPrivateApis +); - function handleOnBlur( event ) { - if ( ! event?.target?.value ) { - setCSSError( null ); - return; - } - - const [ transformed ] = transformStyles( - [ { css: event.target.value } ], - '.editor-styles-wrapper' - ); - - setCSSError( - transformed === null - ? __( 'There is an error with your CSS structure.' ) - : null - ); - } - - const originalThemeCustomCSS = - themeCSS && customCSS && themeCustomCSS !== customCSS - ? themeCSS - : undefined; +function CustomCSSControl( { blockName } ) { + const [ style ] = useGlobalStyle( '', blockName, 'user', { + shouldDecodeEncode: false, + } ); + const [ inheritedStyle, setStyle ] = useGlobalStyle( '', blockName, 'all', { + shouldDecodeEncode: false, + } ); return ( - <> - { originalThemeCustomCSS && ( - - -
-							{ originalThemeCustomCSS }
-						
-
-
- ) } - - { __( 'Additional CSS' ) } - handleOnChange( value ) } - onBlur={ handleOnBlur } - className="edit-site-global-styles__custom-css-input" - spellCheck={ false } - /> - { cssError && ( - -
- -
-
- ) } -
- + ); }