Skip to content

Commit

Permalink
Fix extract function to run only for Editor (not preview)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q committed Jul 7, 2020
1 parent 91a7aa6 commit cec9121
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ function CoverEdit( {
*/
const { customColors } = useCoverColorExtract( {
color: backgroundColorValue,
isSelected,
onChange: setOverlayColor,
src: url,
} );
Expand Down Expand Up @@ -558,8 +559,14 @@ function CoverEdit( {
);
}

function useCoverColorExtract( { backgroundColor, onChange = noop, src } ) {
function useCoverColorExtract( {
backgroundColor,
onChange = noop,
isSelected = false,
src,
} ) {
const [ customExtractedColor, setCustomExtractedColor ] = useState( null );
const [ didSelect, setDidSelect ] = useState( false );

const updateCustomOverlayColor = ( value ) => {
onChange( value );
Expand All @@ -573,10 +580,15 @@ function useCoverColorExtract( { backgroundColor, onChange = noop, src } ) {
} );

useEffect( () => {
extractColor().then( ( [ value ] ) => {
setCustomExtractedColor( value );
} );
}, [] );
// Extracts color to add to color palette.
// Run when the block is first selected.
if ( ! didSelect && isSelected ) {
extractColor().then( ( [ value ] ) => {
setCustomExtractedColor( value );
} );
setDidSelect( true );
}
}, [ isSelected, didSelect ] );

let customColors = [];
if ( customExtractedColor ) {
Expand Down

0 comments on commit cec9121

Please sign in to comment.