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

Add option to resize Cover Block #17143

Merged
merged 7 commits into from
Aug 26, 2019
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
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"focalPoint": {
"type": "object"
},
"minHeight": {
"type": "number"
senadir marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
123 changes: 91 additions & 32 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import {
ToggleControl,
Toolbar,
withNotices,
ResizableBox,
BaseControl,
} from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { compose, withInstanceId } from '@wordpress/compose';
import {
BlockControls,
BlockIcon,
Expand All @@ -39,6 +41,8 @@ import icon from './icon';
import {
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
COVER_MIN_HEIGHT,
COVER_DEFAULT_HEIGHT,
backgroundImageStyles,
dimRatioToClass,
} from './shared';
Expand Down Expand Up @@ -90,8 +94,10 @@ class CoverEdit extends Component {

render() {
const {
instanceId,
attributes,
setAttributes,
isSelected,
className,
noticeUI,
overlayColor,
Expand All @@ -104,6 +110,7 @@ class CoverEdit extends Component {
hasParallax,
id,
url,
minHeight = COVER_DEFAULT_HEIGHT,
} = attributes;
const onSelectMedia = ( media ) => {
if ( ! media || ! media.url ) {
Expand Down Expand Up @@ -161,7 +168,7 @@ class CoverEdit extends Component {
if ( focalPoint ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}

const inputId = `block-cover-height-input-${ instanceId }`;
const controls = (
<>
<BlockControls>
Expand Down Expand Up @@ -205,6 +212,28 @@ class CoverEdit extends Component {
onChange={ ( value ) => setAttributes( { focalPoint: value } ) }
/>
) }
<BaseControl label={ __( 'Height in pixels' ) } id={ inputId }>
<input
type="number"
id={ inputId }
onChange={ ( event ) => {
let coverMinHeight = parseInt( event.target.value, 10 );
this.setState( { coverMinHeight } );
if ( isNaN( coverMinHeight ) ) {
// Set cover min height to default size and input box to empty string
this.setState( { coverMinHeight: COVER_DEFAULT_HEIGHT } );
coverMinHeight = COVER_DEFAULT_HEIGHT;
} else if ( coverMinHeight < COVER_MIN_HEIGHT ) {
// Set cover min height to minimum size
coverMinHeight = COVER_MIN_HEIGHT;
}
setAttributes( { minHeight: coverMinHeight } );
} }
value={ this.state.coverMinHeight || minHeight }
min={ COVER_MIN_HEIGHT }
step="10"
/>
</BaseControl>
<PanelColorSettings
title={ __( 'Overlay' ) }
initialOpen={ true }
Expand Down Expand Up @@ -268,39 +297,68 @@ class CoverEdit extends Component {
return (
<>
{ controls }
<div
data-url={ url }
style={ style }
className={ classes }
<ResizableBox
className={ classnames(
'block-library-cover__resize-container',
{ 'is-selected': isSelected },
) }
size={ {
height: minHeight,
} }
minHeight={ COVER_MIN_HEIGHT }
enable={ {
top: false,
right: false,
bottom: true,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false,
} }
onResizeStop={ ( event, direction, elt, delta ) => {
const coverHeight = parseInt( minHeight + delta.height, 10 );
this.setState( { coverMinHeight: coverHeight } );
setAttributes( {
minHeight: coverHeight,
} );
} }
>
{ IMAGE_BACKGROUND_TYPE === backgroundType && (

<div
data-url={ url }
style={ style }
className={ classes }
>
{ IMAGE_BACKGROUND_TYPE === backgroundType && (
// Used only to programmatically check if the image is dark or not
<img
ref={ this.imageRef }
aria-hidden
alt=""
style={ {
display: 'none',
} }
src={ url }
/>
) }
{ VIDEO_BACKGROUND_TYPE === backgroundType && (
<video
ref={ this.videoRef }
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
/>
) }
<div className="wp-block-cover__inner-container">
<InnerBlocks
template={ INNER_BLOCKS_TEMPLATE }
/>
<img
ref={ this.imageRef }
aria-hidden
alt=""
style={ {
display: 'none',
} }
src={ url }
/>
) }
{ VIDEO_BACKGROUND_TYPE === backgroundType && (
<video
ref={ this.videoRef }
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
/>
) }
<div className="wp-block-cover__inner-container">
<InnerBlocks
template={ INNER_BLOCKS_TEMPLATE }
/>
</div>
</div>
</div>
</ResizableBox>
</>
);
}
Expand Down Expand Up @@ -372,4 +430,5 @@ class CoverEdit extends Component {
export default compose( [
withColors( { overlayColor: 'background-color' } ),
withNotices,
withInstanceId,
] )( CoverEdit );
2 changes: 2 additions & 0 deletions packages/block-library/src/cover/editor.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.wp-block-cover-image,
.wp-block-cover {
min-height: auto;

&.components-placeholder h2 {
color: inherit;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function save( { attributes } ) {
hasParallax,
overlayColor,
url,
minHeight,
} = attributes;
const overlayColorClass = getColorClassName( 'background-color', overlayColor );
const style = backgroundType === IMAGE_BACKGROUND_TYPE ?
Expand All @@ -41,6 +42,7 @@ export default function save( { attributes } ) {
if ( focalPoint && ! hasParallax ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}
style.minHeight = minHeight || undefined;

const classes = classnames(
dimRatioToClass( dimRatio ),
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/cover/shared.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const IMAGE_BACKGROUND_TYPE = 'image';
export const VIDEO_BACKGROUND_TYPE = 'video';

export const COVER_MIN_HEIGHT = 50;
export const COVER_DEFAULT_HEIGHT = 430;
export function backgroundImageStyles( url ) {
return url ?
{ backgroundImage: `url(${ url })` } :
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/cover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
background-size: cover;
background-position: center center;
min-height: 430px;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
Expand Down