Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featured Image: Fix overlay rendering in the editor #60187

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading