|
1 | 1 | /** |
2 | 2 | * WordPress dependencies |
3 | 3 | */ |
4 | | -import { useState } from '@wordpress/element'; |
5 | | -import { |
6 | | - TextareaControl, |
7 | | - Panel, |
8 | | - PanelBody, |
9 | | - __experimentalVStack as VStack, |
10 | | - Tooltip, |
11 | | - Icon, |
12 | | -} from '@wordpress/components'; |
13 | | -import { __ } from '@wordpress/i18n'; |
14 | | -import { |
15 | | - privateApis as blockEditorPrivateApis, |
16 | | - transformStyles, |
17 | | -} from '@wordpress/block-editor'; |
18 | | -import { info } from '@wordpress/icons'; |
| 4 | +import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; |
19 | 5 |
|
20 | 6 | /** |
21 | 7 | * Internal dependencies |
22 | 8 | */ |
23 | 9 | import { unlock } from '../../private-apis'; |
24 | | -import Subtitle from './subtitle'; |
25 | 10 |
|
26 | | -const { useGlobalStyle } = unlock( blockEditorPrivateApis ); |
27 | | -function CustomCSSControl( { blockName } ) { |
28 | | - // If blockName is defined, we are customizing CSS at the block level: |
29 | | - // styles.blocks.blockName.css |
30 | | - const block = !! blockName ? blockName : null; |
31 | | - |
32 | | - const [ customCSS, setCustomCSS ] = useGlobalStyle( 'css', block ); |
33 | | - const [ themeCSS ] = useGlobalStyle( 'css', block, 'base' ); |
34 | | - const [ cssError, setCSSError ] = useState( null ); |
35 | | - const ignoreThemeCustomCSS = '/* IgnoreThemeCustomCSS */'; |
36 | | - |
37 | | - // If there is custom css from theme.json show it in the edit box |
38 | | - // so the user can selectively overwrite it, rather than have the user CSS |
39 | | - // completely overwrite the theme CSS by default. |
40 | | - const themeCustomCSS = |
41 | | - ! customCSS && themeCSS |
42 | | - ? `/* ${ __( |
43 | | - 'Theme Custom CSS start' |
44 | | - ) } */\n${ themeCSS }\n/* ${ __( 'Theme Custom CSS end' ) } */` |
45 | | - : undefined; |
46 | | - |
47 | | - function handleOnChange( value ) { |
48 | | - // If there is theme custom CSS, but the user clears the input box then save the |
49 | | - // ignoreThemeCustomCSS string so that the theme custom CSS is not re-applied. |
50 | | - if ( themeCSS && value === '' ) { |
51 | | - setCustomCSS( ignoreThemeCustomCSS ); |
52 | | - return; |
53 | | - } |
54 | | - setCustomCSS( value ); |
55 | | - if ( cssError ) { |
56 | | - const [ transformed ] = transformStyles( |
57 | | - [ { css: value } ], |
58 | | - '.editor-styles-wrapper' |
59 | | - ); |
60 | | - if ( transformed ) { |
61 | | - setCSSError( null ); |
62 | | - } |
63 | | - } |
64 | | - } |
| 11 | +const { useGlobalStyle, AdvancedPanel: StylesAdvancedPanel } = unlock( |
| 12 | + blockEditorPrivateApis |
| 13 | +); |
65 | 14 |
|
66 | | - function handleOnBlur( event ) { |
67 | | - if ( ! event?.target?.value ) { |
68 | | - setCSSError( null ); |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - const [ transformed ] = transformStyles( |
73 | | - [ { css: event.target.value } ], |
74 | | - '.editor-styles-wrapper' |
75 | | - ); |
76 | | - |
77 | | - setCSSError( |
78 | | - transformed === null |
79 | | - ? __( 'There is an error with your CSS structure.' ) |
80 | | - : null |
81 | | - ); |
82 | | - } |
83 | | - |
84 | | - const originalThemeCustomCSS = |
85 | | - themeCSS && customCSS && themeCustomCSS !== customCSS |
86 | | - ? themeCSS |
87 | | - : undefined; |
| 15 | +function CustomCSSControl( { blockName } ) { |
| 16 | + const [ style ] = useGlobalStyle( '', blockName, 'user', { |
| 17 | + shouldDecodeEncode: false, |
| 18 | + } ); |
| 19 | + const [ inheritedStyle, setStyle ] = useGlobalStyle( '', blockName, 'all', { |
| 20 | + shouldDecodeEncode: false, |
| 21 | + } ); |
88 | 22 |
|
89 | 23 | return ( |
90 | | - <> |
91 | | - { originalThemeCustomCSS && ( |
92 | | - <Panel> |
93 | | - <PanelBody |
94 | | - title={ __( 'Original Theme Custom CSS' ) } |
95 | | - initialOpen={ false } |
96 | | - > |
97 | | - <pre className="edit-site-global-styles__custom-css-theme-css"> |
98 | | - { originalThemeCustomCSS } |
99 | | - </pre> |
100 | | - </PanelBody> |
101 | | - </Panel> |
102 | | - ) } |
103 | | - <VStack spacing={ 3 }> |
104 | | - <Subtitle>{ __( 'Additional CSS' ) }</Subtitle> |
105 | | - <TextareaControl |
106 | | - __nextHasNoMarginBottom |
107 | | - value={ |
108 | | - customCSS?.replace( ignoreThemeCustomCSS, '' ) || |
109 | | - themeCustomCSS |
110 | | - } |
111 | | - onChange={ ( value ) => handleOnChange( value ) } |
112 | | - onBlur={ handleOnBlur } |
113 | | - className="edit-site-global-styles__custom-css-input" |
114 | | - spellCheck={ false } |
115 | | - /> |
116 | | - { cssError && ( |
117 | | - <Tooltip text={ cssError }> |
118 | | - <div className="edit-site-global-styles__custom-css-validation-wrapper"> |
119 | | - <Icon |
120 | | - icon={ info } |
121 | | - className="edit-site-global-styles__custom-css-validation-icon" |
122 | | - /> |
123 | | - </div> |
124 | | - </Tooltip> |
125 | | - ) } |
126 | | - </VStack> |
127 | | - </> |
| 24 | + <StylesAdvancedPanel |
| 25 | + value={ style } |
| 26 | + onChange={ setStyle } |
| 27 | + inheritedValue={ inheritedStyle } |
| 28 | + /> |
128 | 29 | ); |
129 | 30 | } |
130 | 31 |
|
|
0 commit comments