Skip to content

Commit

Permalink
Featured Image: Fix overlay rendering in the editor (#60187)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored Mar 26, 2024
1 parent 80a0b12 commit 1d7407b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 85 deletions.
13 changes: 12 additions & 1 deletion packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ];
Expand Down Expand Up @@ -181,7 +182,7 @@ export default function PostFeaturedImageEdit( {

const controls = blockEditingMode === 'default' && (
<>
<Overlay
<OverlayControls
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
Expand Down Expand Up @@ -262,6 +263,11 @@ export default function PostFeaturedImageEdit( {
) : (
placeholder()
) }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</div>
</>
);
Expand Down Expand Up @@ -367,6 +373,11 @@ export default function PostFeaturedImageEdit( {
) : (
image
) }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</figure>
</>
);
Expand Down
88 changes: 88 additions & 0 deletions packages/block-library/src/post-featured-image/overlay-controls.js
Original file line number Diff line number Diff line change
@@ -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 (
<InspectorControls group="color">
<ColorGradientSettingsDropdown
__experimentalIsRenderedInSidebar
settings={ [
{
colorValue: overlayColor.color,
gradientValue,
label: __( 'Overlay' ),
onColorChange: setOverlayColor,
onGradientChange: setGradient,
isShownByDefault: true,
resetAllFilter: () => ( {
overlayColor: undefined,
customOverlayColor: undefined,
gradient: undefined,
customGradient: undefined,
} ),
},
] }
panelId={ clientId }
{ ...colorGradientSettings }
/>
<ToolsPanelItem
hasValue={ () => dimRatio !== undefined }
label={ __( 'Overlay opacity' ) }
onDeselect={ () => setAttributes( { dimRatio: 0 } ) }
resetAllFilter={ () => ( {
dimRatio: 0,
} ) }
isShownByDefault
panelId={ clientId }
>
<RangeControl
__nextHasNoMarginBottom
label={ __( 'Overlay opacity' ) }
value={ dimRatio }
onChange={ ( newDimRatio ) =>
setAttributes( {
dimRatio: newDimRatio,
} )
}
min={ 0 }
max={ 100 }
step={ 10 }
required
__next40pxDefaultSize
/>
</ToolsPanelItem>
</InspectorControls>
);
};

export default compose( [
withColors( { overlayColor: 'background-color' } ),
] )( Overlay );
101 changes: 17 additions & 84 deletions packages/block-library/src/post-featured-image/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -45,79 +31,26 @@ const Overlay = ( {
...borderProps.style,
};

if ( ! colorGradientSettings.hasColorsOrGradients ) {
if ( ! colorGradientSettings.hasColorsOrGradients || ! dimRatio ) {
return null;
}

return (
<>
{ !! dimRatio && (
<span
aria-hidden="true"
className={ classnames(
'wp-block-post-featured-image__overlay',
dimRatioToClass( dimRatio ),
{
[ overlayColor.class ]: overlayColor.class,
'has-background-dim': dimRatio !== undefined,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
},
borderProps.className
) }
style={ overlayStyles }
/>
<span
aria-hidden="true"
className={ classnames(
'wp-block-post-featured-image__overlay',
dimRatioToClass( dimRatio ),
{
[ overlayColor.class ]: overlayColor.class,
'has-background-dim': dimRatio !== undefined,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
},
borderProps.className
) }
<InspectorControls group="color">
<ColorGradientSettingsDropdown
__experimentalIsRenderedInSidebar
settings={ [
{
colorValue: overlayColor.color,
gradientValue,
label: __( 'Overlay' ),
onColorChange: setOverlayColor,
onGradientChange: setGradient,
isShownByDefault: true,
resetAllFilter: () => ( {
overlayColor: undefined,
customOverlayColor: undefined,
gradient: undefined,
customGradient: undefined,
} ),
},
] }
panelId={ clientId }
{ ...colorGradientSettings }
/>
<ToolsPanelItem
hasValue={ () => dimRatio !== undefined }
label={ __( 'Overlay opacity' ) }
onDeselect={ () => setAttributes( { dimRatio: 0 } ) }
resetAllFilter={ () => ( {
dimRatio: 0,
} ) }
isShownByDefault
panelId={ clientId }
>
<RangeControl
__nextHasNoMarginBottom
label={ __( 'Overlay opacity' ) }
value={ dimRatio }
onChange={ ( newDimRatio ) =>
setAttributes( {
dimRatio: newDimRatio,
} )
}
min={ 0 }
max={ 100 }
step={ 10 }
required
__next40pxDefaultSize
/>
</ToolsPanelItem>
</InspectorControls>
</>
style={ overlayStyles }
/>
);
};

Expand Down

0 comments on commit 1d7407b

Please sign in to comment.