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

Improve cover z-index solution #66249

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
6 changes: 4 additions & 2 deletions packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ $z-layers: (
".interface-interface-skeleton__header": 30,
".interface-interface-skeleton__content": 20,
".edit-widgets-header": 30,
".wp-block-cover__inner-container": 1, // InnerBlocks area inside cover image block.

".wp-block-cover.is-placeholder .components-placeholder.is-large": 1, // Cover block resizer component inside a large placeholder.
// These z-index are now used only for a deprecated version of the cover block.
".wp-block-cover__inner-container": 1, // InnerBlocks area inside cover image block.
".wp-block-cover.has-background-dim::before": 1, // Overlay area inside block cover need to be higher than the video background.
".block-library-cover__padding-visualizer": 2, // BoxControl visualizer needs to be +1 higher than .wp-block-cover.has-background-dim::before
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm removing it again because the change discussed here was reverted.

".wp-block-cover__image-background": 0, // Image background inside cover block.
".wp-block-cover__video-background": 0, // Video background inside cover block.

".wp-block-template-part__placeholder-preview-filter-input": 1,

// Fixed position appender:
Expand Down
168 changes: 164 additions & 4 deletions packages/block-library/src/cover/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const v8ToV11BlockAttributes = {
},
};

const v12BlockAttributes = {
const v12toV13BlockAttributes = {
...v8ToV11BlockAttributes,
useFeaturedImage: {
type: 'boolean',
Expand All @@ -176,6 +176,13 @@ const v12BlockAttributes = {
},
};

const v14BlockAttributes = {
...v12toV13BlockAttributes,
sizeSlug: {
type: 'string',
},
};

const v7toV11BlockSupports = {
anchor: true,
align: true,
Expand Down Expand Up @@ -244,9 +251,162 @@ const v12BlockSupports = {
},
};

// Deprecation for blocks that have z-index.
const v14 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deprecation will need a fixture to cover it. Here's an example for an earlier deprecation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion and the example! ♥️
I'll try to fix it today!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed this documentation, and based on your example, I added the new fixture: 9ba72c0 (created the HTML and let the command generate the others)

I'm still struggling a bit to fix the others. I let you know here when it's done.

Copy link
Contributor Author

@renatho renatho Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed most of the issues (0ae6273), but in the fixture tests it's trying to add the 'has-no-zindex-in-inner-blocks' as a className attribute.

Not sure if it's a bug with the fixture tests, or if adding a class to the wrapper element wouldn't be recommended. 🤔

I thought of not using the sibling class, as you suggested to fix this, but it would mean a few new classes instead of just one. Another approach would be adding an additional wrapper with a class, it would break a few styles (we have some using > that we would need to refactor to do it, so I think it would be risky).

I'm out of ideas now. I need to think more.

And I noticed that I also need to update some mobile snapshots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, I'll review those shortly.

Regarding the sibling selectors though, why would we need additional classes?

We should be able to rely on the order of the elements to have the correct sibling selector apply because the old and new versions moved the overlay element. When there aren't multiple elements there, we wouldn't need to juggle their z-index.

I'm a little short on bandwidth at the moment to code something up but I could take a run at it later this week perhaps if you don't beat me to it.

It could be worth pursuing this, even if it is ruled out as an option, as it would limit the number of blocks needing migration and avoid the slightly awkward class on the wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! 👋

Thank you for your suggestions! I followed your suggestion and applied it with this commit: 0e25fed.

attributes: v14BlockAttributes,
supports: v12BlockSupports,
save( { attributes } ) {
const {
backgroundType,
gradient,
contentPosition,
customGradient,
customOverlayColor,
dimRatio,
focalPoint,
useFeaturedImage,
hasParallax,
isDark,
isRepeated,
overlayColor,
url,
alt,
id,
minHeight: minHeightProp,
minHeightUnit,
tagName: Tag,
sizeSlug,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
overlayColor
);
const gradientClass = __experimentalGetGradientClass( gradient );
const minHeight =
minHeightProp && minHeightUnit
? `${ minHeightProp }${ minHeightUnit }`
: minHeightProp;

const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType;
const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType;

const isImgElement = ! ( hasParallax || isRepeated );

const style = {
minHeight: minHeight || undefined,
};

const bgStyle = {
backgroundColor: ! overlayColorClass
? customOverlayColor
: undefined,
background: customGradient ? customGradient : undefined,
};

const objectPosition =
// prettier-ignore
focalPoint && isImgElement
? mediaPosition(focalPoint)
: undefined;

const backgroundImage = url ? `url(${ url })` : undefined;

const backgroundPosition = mediaPosition( focalPoint );

const classes = clsx(
{
'is-light': ! isDark,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
'has-custom-content-position':
! isContentPositionCenter( contentPosition ),
},
getPositionClassName( contentPosition )
);

const imgClasses = clsx(
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null,
{
[ `size-${ sizeSlug }` ]: sizeSlug,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
}
);

const gradientValue = gradient || customGradient;

return (
<Tag { ...useBlockProps.save( { className: classes, style } ) }>
<span
aria-hidden="true"
className={ clsx(
'wp-block-cover__background',
overlayColorClass,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ bgStyle }
/>

{ ! useFeaturedImage &&
isImageBackground &&
url &&
( isImgElement ? (
<img
className={ imgClasses }
alt={ alt }
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) : (
<div
role={ alt ? 'img' : undefined }
aria-label={ alt ? alt : undefined }
className={ imgClasses }
style={ { backgroundPosition, backgroundImage } }
/>
) ) }
{ isVideoBackground && url && (
<video
className={ clsx(
'wp-block-cover__video-background',
'intrinsic-ignore'
) }
autoPlay
muted
loop
playsInline
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) }
<div
{ ...useInnerBlocksProps.save( {
className: 'wp-block-cover__inner-container',
} ) }
/>
</Tag>
);
},
};

// Deprecation for blocks that does not have the aria-label when the image background is fixed or repeated.
const v13 = {
attributes: v12BlockAttributes,
attributes: v12toV13BlockAttributes,
supports: v12BlockSupports,
save( { attributes } ) {
const {
Expand Down Expand Up @@ -396,7 +556,7 @@ const v13 = {

// Deprecation for blocks to prevent auto overlay color from overriding previously set values.
const v12 = {
attributes: v12BlockAttributes,
attributes: v12toV13BlockAttributes,
supports: v12BlockSupports,
isEligible( attributes ) {
return (
Expand Down Expand Up @@ -1824,4 +1984,4 @@ const v1 = {
},
};

export default [ v13, v12, v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
export default [ v14, v13, v12, v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
45 changes: 24 additions & 21 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,27 +528,6 @@ function CoverEdit( {
data-url={ url }
>
{ resizeListener }
{ showOverlay && (
<span
aria-hidden="true"
className={ clsx(
'wp-block-cover__background',
dimRatioToClass( dimRatio ),
{
[ overlayColor.class ]: overlayColor.class,
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ { backgroundImage: gradientValue, ...bgStyle } }
/>
) }

{ ! url && useFeaturedImage && (
<Placeholder
Expand Down Expand Up @@ -590,7 +569,31 @@ function CoverEdit( {
style={ mediaStyle }
/>
) }

{ showOverlay && (
<span
aria-hidden="true"
className={ clsx(
'wp-block-cover__background',
dimRatioToClass( dimRatio ),
{
[ overlayColor.class ]: overlayColor.class,
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ { backgroundImage: gradientValue, ...bgStyle } }
/>
) }

{ isUploadingMedia && <Spinner /> }

<CoverPlaceholder
disableMediaButtons
onSelectMedia={ onSelectMedia }
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/cover/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
width: 100%;
z-index: 1;
}

.wp-block-cover__inner-container {
z-index: 2;
}
}

// Shown while media is being uploaded
.components-spinner {
position: absolute;
z-index: z-index(".wp-block-cover__inner-container");
top: 50%;
left: 50%;
transform: translate(-50%, -50%); // Account for spinner dimensions
Expand Down
41 changes: 21 additions & 20 deletions packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,6 @@ export default function save( { attributes } ) {

return (
<Tag { ...useBlockProps.save( { className: classes, style } ) }>
<span
aria-hidden="true"
className={ clsx(
'wp-block-cover__background',
overlayColorClass,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ bgStyle }
/>

{ ! useFeaturedImage &&
isImageBackground &&
url &&
Expand Down Expand Up @@ -162,6 +142,27 @@ export default function save( { attributes } ) {
data-object-position={ objectPosition }
/>
) }

<span
aria-hidden="true"
className={ clsx(
'wp-block-cover__background',
overlayColorClass,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ bgStyle }
/>

<div
{ ...useInnerBlocksProps.save( {
className: 'wp-block-cover__inner-container',
Expand Down
Loading
Loading