-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathadvanced-panel.js
82 lines (77 loc) · 2.01 KB
/
advanced-panel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* WordPress dependencies
*/
import {
TextareaControl,
Notice,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* 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 ) {
// Check if the new value is valid CSS, and pass a wrapping selector
// to ensure that `transformStyles` validates the CSS. Note that the
// wrapping selector here is not used in the actual output of any styles.
const [ transformed ] = transformStyles(
[ { css: newValue } ],
'.for-validation-only'
);
if ( transformed ) {
setCSSError( null );
}
}
}
function handleOnBlur( event ) {
if ( ! event?.target?.value ) {
setCSSError( null );
return;
}
// Check if the new value is valid CSS, and pass a wrapping selector
// to ensure that `transformStyles` validates the CSS. Note that the
// wrapping selector here is not used in the actual output of any styles.
const [ transformed ] = transformStyles(
[ { css: event.target.value } ],
'.for-validation-only'
);
setCSSError(
transformed === null
? __( 'There is an error with your CSS structure.' )
: null
);
}
return (
<VStack spacing={ 3 }>
{ cssError && (
<Notice status="error" onRemove={ () => setCSSError( null ) }>
{ cssError }
</Notice>
) }
<TextareaControl
label={ __( 'Additional CSS' ) }
__nextHasNoMarginBottom
value={ customCSS }
onChange={ ( newValue ) => handleOnChange( newValue ) }
onBlur={ handleOnBlur }
className="block-editor-global-styles-advanced-panel__custom-css-input"
spellCheck={ false }
/>
</VStack>
);
}