Skip to content

Commit

Permalink
Simplify CustomCSS UI
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 11, 2023
1 parent 9091d91 commit 78052d1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -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 (
<VStack spacing={ 3 }>
<TextareaControl
label={ __( 'Additional CSS' ) }
__nextHasNoMarginBottom
value={ customCSS }
onChange={ ( newValue ) => handleOnChange( newValue ) }
onBlur={ handleOnBlur }
className="edit-site-global-styles__custom-css-input"
spellCheck={ false }
/>
{ cssError && (
<Tooltip text={ cssError }>
<div className="edit-site-global-styles__custom-css-validation-wrapper">
<Icon
icon={ info }
className="edit-site-global-styles__custom-css-validation-icon"
/>
</div>
</Tooltip>
) }
</VStack>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
131 changes: 16 additions & 115 deletions packages/edit-site/src/components/global-styles/custom-css.js
Original file line number Diff line number Diff line change
@@ -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 && (
<Panel>
<PanelBody
title={ __( 'Original Theme Custom CSS' ) }
initialOpen={ false }
>
<pre className="edit-site-global-styles__custom-css-theme-css">
{ originalThemeCustomCSS }
</pre>
</PanelBody>
</Panel>
) }
<VStack spacing={ 3 }>
<Subtitle>{ __( 'Additional CSS' ) }</Subtitle>
<TextareaControl
__nextHasNoMarginBottom
value={
customCSS?.replace( ignoreThemeCustomCSS, '' ) ||
themeCustomCSS
}
onChange={ ( value ) => handleOnChange( value ) }
onBlur={ handleOnBlur }
className="edit-site-global-styles__custom-css-input"
spellCheck={ false }
/>
{ cssError && (
<Tooltip text={ cssError }>
<div className="edit-site-global-styles__custom-css-validation-wrapper">
<Icon
icon={ info }
className="edit-site-global-styles__custom-css-validation-icon"
/>
</div>
</Tooltip>
) }
</VStack>
</>
<StylesAdvancedPanel
value={ style }
onChange={ setStyle }
inheritedValue={ inheritedStyle }
/>
);
}

Expand Down

0 comments on commit 78052d1

Please sign in to comment.