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

Cover: Alternate approach to handling border support in editor #40774

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 14 additions & 1 deletion packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@
"padding": true
}
},
"__experimentalBorder": {
"color": true,
"radius": true,
"style": true,
"width": true,
"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"color": true,
"radius": true,
"style": true,
"width": true
}
},
"color": {
"__experimentalDuotone": "> .wp-block-cover__image-background, > .wp-block-cover__video-background",
"__experimentalDuotone": "> .wp-block-cover__image-background, > .wp-block-cover__video-background, > .block-library-cover__border-wrapper > .wp-block-cover__video-background, > .block-library-cover__border-wrapper > .wp-block-cover__image-background",
"text": false,
"background": false
}
Expand Down
97 changes: 54 additions & 43 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useSetting,
useInnerBlocksProps,
__experimentalUseGradient,
__experimentalUseBorderProps as useBorderProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -97,6 +98,8 @@ function CoverEdit( {
templateLock,
} = attributes;

const borderProps = useBorderProps( attributes );

const [ featuredImage ] = useEntityProp(
'postType',
postType,
Expand Down Expand Up @@ -326,53 +329,61 @@ function CoverEdit( {
showHandle={ isSelected }
/>

<span
aria-hidden="true"
<div
className={ classnames(
'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,
}
'block-library-cover__border-wrapper',
borderProps.className
) }
style={ { backgroundImage: gradientValue, ...bgStyle } }
/>

{ url && isImageBackground && isImgElement && (
<img
ref={ mediaElement }
className="wp-block-cover__image-background"
alt={ alt }
src={ url }
style={ mediaStyle }
style={ { ...borderProps.style } }
>
<span
aria-hidden="true"
className={ classnames(
'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 && isVideoBackground && (
<video
ref={ mediaElement }
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
style={ mediaStyle }

{ url && isImageBackground && isImgElement && (
<img
ref={ mediaElement }
className="wp-block-cover__image-background"
alt={ alt }
src={ url }
style={ mediaStyle }
/>
) }
{ url && isVideoBackground && (
<video
ref={ mediaElement }
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
style={ mediaStyle }
/>
) }
{ isUploadingMedia && <Spinner /> }
<CoverPlaceholder
disableMediaButtons
onSelectMedia={ onSelectMedia }
onError={ onUploadError }
/>
) }
{ isUploadingMedia && <Spinner /> }
<CoverPlaceholder
disableMediaButtons
onSelectMedia={ onSelectMedia }
onError={ onUploadError }
/>
<div { ...innerBlocksProps } />
<div { ...innerBlocksProps } />
</div>
</div>
</>
);
Expand Down
59 changes: 59 additions & 0 deletions packages/block-library/src/cover/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* Extra specificity needed because the reset.css applied in the editor context is overriding this rule. */
.editor-styles-wrapper & {
box-sizing: border-box;
overflow: initial;
}

// Override default cover styles
Expand Down Expand Up @@ -98,3 +99,61 @@
.block-editor-block-patterns-list__list-item .has-parallax.wp-block-cover {
background-attachment: scroll;
}

.block-library-cover__border-wrapper {
// Absolute positioning is needed to handle padding that might be applied
// to the main block wrapper via global styles.
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
// Hide backgrounds so they don't overflow the border.
overflow: hidden;
// Re-establish layout and handle custom positions.
display: flex;
justify-content: center;
align-items: center;
// Inherits padding from main block wrapper.
padding: inherit;

// Position: Top
.is-position-top-left & {
align-items: flex-start;
justify-content: flex-start;
}
.is-position-top-center & {
align-items: flex-start;
justify-content: center;
}
.is-position-top-right & {
align-items: flex-start;
justify-content: flex-end;
}
// Position: Center
.is-position-center-left & {
align-items: center;
justify-content: flex-start;
}
.is-position-center-center & {
align-items: center;
justify-content: center;
}
.is-position-center-right & {
align-items: center;
justify-content: flex-end;
}
// Position: Bottom
.is-position-bottom-left & {
align-items: flex-end;
justify-content: flex-start;
}
.is-position-bottom-center & {
align-items: flex-end;
justify-content: center;
}
.is-position-bottom-right & {
align-items: flex-end;
justify-content: flex-end;
}
}
6 changes: 5 additions & 1 deletion packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getColorClassName,
__experimentalGetGradientClass,
useBlockProps,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
} from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -59,12 +60,14 @@ export default function save( { attributes } ) {
const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType;

const isImgElement = ! ( hasParallax || isRepeated );
const borderProps = getBorderClassesAndStyles( attributes );

const style = {
...( isImageBackground && ! isImgElement && ! useFeaturedImage
? backgroundImageStyles( url )
: {} ),
minHeight: minHeight || undefined,
...borderProps.style,
};

const bgStyle = {
Expand All @@ -87,7 +90,8 @@ export default function save( { attributes } ) {
contentPosition
),
},
getPositionClassName( contentPosition )
getPositionClassName( contentPosition ),
borderProps.className
);

const gradientValue = gradient || customGradient;
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/cover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
padding: 1em;
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;
// Prevent overflow of background when block has border radius.
overflow: hidden;

&.has-parallax {
background-attachment: fixed;
Expand Down