Skip to content

Commit 4e148a5

Browse files
committed
Handle adding images after updating the custom color
1 parent 96aab65 commit 4e148a5

File tree

1 file changed

+19
-1
lines changed
  • source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/components

1 file changed

+19
-1
lines changed

source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/components/color-control.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { BaseControl, Button, ColorIndicator, PanelRow, TextControl, ToggleContr
66
import { Icon, warning } from '@wordpress/icons';
77
import { store as editorStore } from '@wordpress/editor';
88
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';
1011

1112
const DEFAULT_COLOR = '#f6f6f6';
1213

@@ -22,6 +23,23 @@ function ColorControl( { label, description } ) {
2223
const hexValue = value.replace( '#', '' );
2324
const isValid = /^[0-9a-f]+$/.test( hexValue ) && [ 3, 4, 6, 8 ].includes( hexValue.length );
2425
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
2543

2644
return (
2745
<div className="wporg-site-color">

0 commit comments

Comments
 (0)