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: Stack on mobile option on Media & Text block. #10969

Merged
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
24 changes: 19 additions & 5 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { Component, Fragment } from '@wordpress/element';
import {
PanelBody,
TextareaControl,
ToggleControl,
Toolbar,
} from '@wordpress/components';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -112,12 +112,19 @@ class MediaTextEdit extends Component {
setAttributes,
setBackgroundColor,
} = this.props;
const { mediaAlt, mediaPosition, mediaType, mediaWidth } = attributes;
const {
isStackedOnMobile,
mediaAlt,
mediaPosition,
mediaType,
mediaWidth,
} = attributes;
const temporaryMediaWidth = this.state.mediaWidth;
const classNames = classnames( className, {
'has-media-on-the-right': 'right' === mediaPosition,
'is-selected': isSelected,
[ backgroundColor.class ]: backgroundColor.class,
'is-stacked-on-mobile': isStackedOnMobile,
} );
const widthString = `${ temporaryMediaWidth || mediaWidth }%`;
const style = {
Expand All @@ -143,14 +150,21 @@ class MediaTextEdit extends Component {
const onMediaAltChange = ( newMediaAlt ) => {
setAttributes( { mediaAlt: newMediaAlt } );
};
const mediaTextGeneralSettings = mediaType === 'image' && (
const mediaTextGeneralSettings = (
<PanelBody title={ __( 'Media & Text Settings' ) }>
<TextareaControl
<ToggleControl
label={ __( 'Stack on mobile' ) }
checked={ isStackedOnMobile }
onChange={ () => setAttributes( {
isStackedOnMobile: ! isStackedOnMobile,
} ) }
/>
{ mediaType === 'image' && ( <TextareaControl
label={ __( 'Alt Text (Alternative Text)' ) }
value={ mediaAlt }
onChange={ onMediaAltChange }
help={ __( 'Alternative text describes your image to people who can’t see it. Add a short description with its key details.' ) }
/>
/> ) }
</PanelBody>
);
return (
Expand Down
17 changes: 12 additions & 5 deletions packages/block-library/src/media-text/editor.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
.wp-block-media-text,
.wp-block-media-text.aligncenter {
.wp-block-media-text {
grid-template-areas:
"media-text-media media-text-content"
"resizer resizer";
}

.wp-block-media-text.has-media-on-the-right,
.wp-block-media-text.has-media-on-the-right.aligncenter {
.wp-block-media-text.has-media-on-the-right {
grid-template-areas:
"media-text-content media-text-media"
"resizer resizer";
Expand Down Expand Up @@ -51,8 +49,17 @@ figure.block-library-media-text__media-container {
display: none;
}

.wp-block-media-text.is-selected {
.wp-block-media-text.is-selected:not(.is-stacked-on-mobile) {
.editor-media-container__resizer .components-resizable-box__handle {
display: block;
}
}

@include break-small {
.wp-block-media-text.is-selected.is-stacked-on-mobile {
.editor-media-container__resizer .components-resizable-box__handle {
display: block;
}
}
}

6 changes: 6 additions & 0 deletions packages/block-library/src/media-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const settings = {
type: 'number',
default: 50,
},
isStackedOnMobile: {
type: 'boolean',
default: false,
},
},

supports: {
Expand All @@ -82,6 +86,7 @@ export const settings = {
const {
backgroundColor,
customBackgroundColor,
isStackedOnMobile,
mediaAlt,
mediaPosition,
mediaType,
Expand All @@ -97,6 +102,7 @@ export const settings = {
const className = classnames( {
'has-media-on-the-right': 'right' === mediaPosition,
[ backgroundClass ]: backgroundClass,
'is-stacked-on-mobile': isStackedOnMobile,
} );

let gridTemplateColumns;
Expand Down
31 changes: 25 additions & 6 deletions packages/block-library/src/media-text/style.scss
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.wp-block-media-text,
.wp-block-media-text.aligncenter {
.wp-block-media-text {
display: grid;
}

.wp-block-media-text {
grid-template-rows: auto;
grid-template-areas: "media-text-media media-text-content";
align-items: center;
grid-template-areas: "media-text-media media-text-content";
grid-template-columns: 50% auto;
.has-media-on-the-right {
grid-template-areas: "media-text-content media-text-media";
grid-template-columns: auto 50%;
}
}
Expand All @@ -23,13 +23,32 @@
grid-area: media-text-content;
padding: 0 8% 0 8%;
}
.wp-block-media-text.has-media-on-the-right {
grid-template-areas: "media-text-content media-text-media";
}

.wp-block-media-text > figure > img,
.wp-block-media-text > figure > video {
max-width: unset;
width: 100%;
vertical-align: middle;
}

/*
* Here we here not able to use a mobile first CSS approach.
* Custom widths are set using inline styles, and on mobile,
* we need 100% width, so we use important to overwrite the inline style.
* If the style were set on mobile first, on desktop styles,
* we would have no way of setting the style again to the inline style.
*/
@media (max-width: #{ ($break-small) }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, we do the opposite. Build for mobile first and use break-small or break-medium to add the desktop styles. Isn't this possible here?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, in this case, we can't follow that approach because custom widths are set using an inline style, and on mobile, we need to overwrite this inline style so we need to use "!important". After having an important set on mobile styles it would be impossible than on the desktop sizes to make it use the inline styles again.
I should have added a comment with this explanation I will add them now.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good answer, but it's also a good question. Can you add an inline comment to explain basically the same?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @jasmussen I already added a CSS comment there specifying the reason for this, let me know if you find it hard to understand or you would prefer some improvement.

Copy link
Contributor

Choose a reason for hiding this comment

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

Argh sorry, I missed that. No worries then, you're many steps ahead of me as usual.

.wp-block-media-text.is-stacked-on-mobile {
grid-template-columns: 100% !important;
grid-template-areas:
"media-text-media"
"media-text-content";
}

.wp-block-media-text.is-stacked-on-mobile.has-media-on-the-right {
grid-template-areas:
"media-text-content"
"media-text-media";
}
}
3 changes: 2 additions & 1 deletion test/integration/full-content/fixtures/core__media-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"mediaId": 17985,
"mediaUrl": "http://localhost/wp-content/uploads/2018/09/1600px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg",
"mediaType": "image",
"mediaWidth": 50
"mediaWidth": 50,
"isStackedOnMobile": false
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"mediaId": 17985,
"mediaUrl": "http://localhost/wp-content/uploads/2018/09/1600px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg",
"mediaType": "image",
"mediaWidth": 50
"mediaWidth": 50,
"isStackedOnMobile": false
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- wp:media-text {"mediaId":17897,"mediaType":"video", "isStackedOnMobile": true} -->
<div class="wp-block-media-text alignwide is-stacked-on-mobile">
<figure class="wp-block-media-text__media">
<video controls src="http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4"></video>
</figure>
<div class="wp-block-media-text__content">
<!-- wp:paragraph {"placeholder":"Content…","fontSize":"large"} -->
<p class="has-large-font-size">My Content</p>
<!-- /wp:paragraph -->
</div>
</div>
<!-- /wp:media-text -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"clientId": "_clientId_0",
"name": "core/media-text",
"isValid": true,
"attributes": {
"align": "wide",
"mediaAlt": "",
"mediaPosition": "left",
"mediaId": 17897,
"mediaUrl": "http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4",
"mediaType": "video",
"mediaWidth": 50,
"isStackedOnMobile": true
},
"innerBlocks": [
{
"clientId": "_clientId_0",
"name": "core/paragraph",
"isValid": true,
"attributes": {
"content": "My Content",
"dropCap": false,
"placeholder": "Content…",
"fontSize": "large"
},
"innerBlocks": [],
"originalContent": "<p class=\"has-large-font-size\">My Content</p>"
}
],
"originalContent": "<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\">\n\t<figure class=\"wp-block-media-text__media\">\n\t\t<video controls src=\"http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4\"></video>\n\t</figure>\n\t<div class=\"wp-block-media-text__content\">\n\t\t\n\t</div>\n</div>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"blockName": "core/media-text",
"attrs": {
"mediaId": 17897,
"mediaType": "video",
"isStackedOnMobile": true
},
"innerBlocks": [
{
"blockName": "core/paragraph",
"attrs": {
"placeholder": "Content…",
"fontSize": "large"
},
"innerBlocks": [],
"innerHTML": "\n\t\t<p class=\"has-large-font-size\">My Content</p>\n\t\t"
}
],
"innerHTML": "\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\">\n\t<figure class=\"wp-block-media-text__media\">\n\t\t<video controls src=\"http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4\"></video>\n\t</figure>\n\t<div class=\"wp-block-media-text__content\">\n\t\t\n\t</div>\n</div>\n"
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:media-text {"mediaId":17897,"mediaType":"video","isStackedOnMobile":true} -->
<div class="wp-block-media-text alignwide is-stacked-on-mobile"><figure class="wp-block-media-text__media"><video controls src="http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4"></video></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…","fontSize":"large"} -->
<p class="has-large-font-size">My Content</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"mediaId": 17897,
"mediaUrl": "http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4",
"mediaType": "video",
"mediaWidth": 41
"mediaWidth": 41,
"isStackedOnMobile": false
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"mediaId": 17897,
"mediaUrl": "http://localhost/wp-content/uploads/2018/09/Jul-26-2018-11-34-54.mp4",
"mediaType": "video",
"mediaWidth": 50
"mediaWidth": 50,
"isStackedOnMobile": false
},
"innerBlocks": [
{
Expand Down