From f3596c38a206f540c464da84ebbf63f137495189 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Tue, 26 Mar 2024 13:42:26 +1000
Subject: [PATCH] Featured Image: Fix overlay rendering in the editor
---
.../src/post-featured-image/edit.js | 13 ++-
.../post-featured-image/overlay-controls.js | 88 +++++++++++++++
.../src/post-featured-image/overlay.js | 101 +++---------------
3 files changed, 117 insertions(+), 85 deletions(-)
create mode 100644 packages/block-library/src/post-featured-image/overlay-controls.js
diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js
index a37322467d1f52..d1d03c0d4b52a1 100644
--- a/packages/block-library/src/post-featured-image/edit.js
+++ b/packages/block-library/src/post-featured-image/edit.js
@@ -36,6 +36,7 @@ import { store as noticesStore } from '@wordpress/notices';
* Internal dependencies
*/
import DimensionControls from './dimension-controls';
+import OverlayControls from './overlay-controls';
import Overlay from './overlay';
const ALLOWED_MEDIA_TYPES = [ 'image' ];
@@ -181,7 +182,7 @@ export default function PostFeaturedImageEdit( {
const controls = blockEditingMode === 'default' && (
<>
-
>
);
@@ -367,6 +373,11 @@ export default function PostFeaturedImageEdit( {
) : (
image
) }
+
>
);
diff --git a/packages/block-library/src/post-featured-image/overlay-controls.js b/packages/block-library/src/post-featured-image/overlay-controls.js
new file mode 100644
index 00000000000000..34abf4c958f5f1
--- /dev/null
+++ b/packages/block-library/src/post-featured-image/overlay-controls.js
@@ -0,0 +1,88 @@
+/**
+ * WordPress dependencies
+ */
+import {
+ RangeControl,
+ __experimentalToolsPanelItem as ToolsPanelItem,
+} from '@wordpress/components';
+import {
+ InspectorControls,
+ withColors,
+ __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
+ __experimentalUseGradient,
+ __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
+} from '@wordpress/block-editor';
+import { compose } from '@wordpress/compose';
+import { __ } from '@wordpress/i18n';
+
+const Overlay = ( {
+ clientId,
+ attributes,
+ setAttributes,
+ overlayColor,
+ setOverlayColor,
+} ) => {
+ const { dimRatio } = attributes;
+ const { gradientValue, setGradient } = __experimentalUseGradient();
+ const colorGradientSettings = useMultipleOriginColorsAndGradients();
+
+ if ( ! colorGradientSettings.hasColorsOrGradients ) {
+ return null;
+ }
+
+ return (
+
+ ( {
+ overlayColor: undefined,
+ customOverlayColor: undefined,
+ gradient: undefined,
+ customGradient: undefined,
+ } ),
+ },
+ ] }
+ panelId={ clientId }
+ { ...colorGradientSettings }
+ />
+ dimRatio !== undefined }
+ label={ __( 'Overlay opacity' ) }
+ onDeselect={ () => setAttributes( { dimRatio: 0 } ) }
+ resetAllFilter={ () => ( {
+ dimRatio: 0,
+ } ) }
+ isShownByDefault
+ panelId={ clientId }
+ >
+
+ setAttributes( {
+ dimRatio: newDimRatio,
+ } )
+ }
+ min={ 0 }
+ max={ 100 }
+ step={ 10 }
+ required
+ __next40pxDefaultSize
+ />
+
+
+ );
+};
+
+export default compose( [
+ withColors( { overlayColor: 'background-color' } ),
+] )( Overlay );
diff --git a/packages/block-library/src/post-featured-image/overlay.js b/packages/block-library/src/post-featured-image/overlay.js
index 33d7047e814921..fb22196981d780 100644
--- a/packages/block-library/src/post-featured-image/overlay.js
+++ b/packages/block-library/src/post-featured-image/overlay.js
@@ -7,35 +7,21 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import {
- RangeControl,
- __experimentalToolsPanelItem as ToolsPanelItem,
-} from '@wordpress/components';
-import {
- InspectorControls,
withColors,
- __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseGradient,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
__experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
-import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { dimRatioToClass } from './utils';
-const Overlay = ( {
- clientId,
- attributes,
- setAttributes,
- overlayColor,
- setOverlayColor,
-} ) => {
+const Overlay = ( { attributes, overlayColor } ) => {
const { dimRatio } = attributes;
- const { gradientClass, gradientValue, setGradient } =
- __experimentalUseGradient();
+ const { gradientClass, gradientValue } = __experimentalUseGradient();
const colorGradientSettings = useMultipleOriginColorsAndGradients();
const borderProps = useBorderProps( attributes );
@@ -45,79 +31,26 @@ const Overlay = ( {
...borderProps.style,
};
- if ( ! colorGradientSettings.hasColorsOrGradients ) {
+ if ( ! colorGradientSettings.hasColorsOrGradients || ! dimRatio ) {
return null;
}
return (
- <>
- { !! dimRatio && (
-
+
- ( {
- overlayColor: undefined,
- customOverlayColor: undefined,
- gradient: undefined,
- customGradient: undefined,
- } ),
- },
- ] }
- panelId={ clientId }
- { ...colorGradientSettings }
- />
- dimRatio !== undefined }
- label={ __( 'Overlay opacity' ) }
- onDeselect={ () => setAttributes( { dimRatio: 0 } ) }
- resetAllFilter={ () => ( {
- dimRatio: 0,
- } ) }
- isShownByDefault
- panelId={ clientId }
- >
-
- setAttributes( {
- dimRatio: newDimRatio,
- } )
- }
- min={ 0 }
- max={ 100 }
- step={ 10 }
- required
- __next40pxDefaultSize
- />
-
-
- >
+ style={ overlayStyles }
+ />
);
};