@@ -6,7 +6,8 @@ import { BaseControl, Button, ColorIndicator, PanelRow, TextControl, ToggleContr
6
6
import { Icon , warning } from '@wordpress/icons' ;
7
7
import { store as editorStore } from '@wordpress/editor' ;
8
8
import { useDispatch , useSelect } from '@wordpress/data' ;
9
- import { useState } from '@wordpress/element' ;
9
+ import { useEffect , useState } from '@wordpress/element' ;
10
+ import { usePrevious } from '@wordpress/compose' ;
10
11
11
12
const DEFAULT_COLOR = '#f6f6f6' ;
12
13
@@ -22,6 +23,23 @@ function ColorControl( { label, description } ) {
22
23
const hexValue = value . replace ( '#' , '' ) ;
23
24
const isValid = / ^ [ 0 - 9 a - f ] + $ / . test ( hexValue ) && [ 3 , 4 , 6 , 8 ] . includes ( hexValue . length ) ;
24
25
const hasImage = ! ! meta [ 'screenshot-desktop' ] ;
26
+ const prevHasImage = usePrevious ( hasImage ) ;
27
+
28
+ useEffect ( ( ) => {
29
+ // No previous state, initial values OK.
30
+ if ( prevHasImage === undefined ) {
31
+ return ;
32
+ }
33
+ if ( ! prevHasImage && hasImage ) {
34
+ // If an image was added, but we have a custom color, keep the toggle off.
35
+ if ( hexValue !== DEFAULT_COLOR . replace ( '#' , '' ) ) {
36
+ setUseImageColor ( false ) ;
37
+ } else {
38
+ // The color is the default, but clear it so the frontend works.
39
+ onUpdate ( '' ) ;
40
+ }
41
+ }
42
+ } , [ hasImage , hexValue ] ) ; // eslint-disable-line react-hooks/exhaustive-deps
25
43
26
44
return (
27
45
< div className = "wporg-site-color" >
0 commit comments