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: Add custom unit support for height controls #20888

Merged
merged 31 commits into from Mar 30, 2020

Conversation

@ItsJonQ
Copy link
Contributor

@ItsJonQ ItsJonQ commented Mar 13, 2020

Description

Screen Shot 2020-03-13 at 4 05 01 PM

This update adds a new (experimental) UnitControl that enables values beyond pixels! For Cover block, this control unlocks the ability to set px, em, rem, vw and vh values.

(Note: Although % is supported, it's omitted from the Cover block control as % height does not render predictably).

How has this been tested?

  • New (experimental) UnitControl and NumberControl were tested in Storybook and Gutenberg
  • UnitControl x Cover block integration was developed and tested in local Gutenberg

Screenshots

Screen Capture on 2020-03-13 at 15-35-36

The GIF above showcases the flow for changing values and unit values for the Cover block.

Types of changes

UnitControl (and NumberControl)

Screen Shot 2020-03-13 at 3 58 26 PM

These new experimental components have been added to @wordpress/components. The UnitControl is the component that powers the input for setting the height for the Cover block.

Shift Increment

One of the features with this new control is to jump values when holding down shift while pressing up or down (default is by 10). This interaction is common for other Design applications like Figma, Sketch, Photoshop, etc...

Note: This feature can be customized and even disabled in the base component.

Switching Units

Screen Shot 2020-03-13 at 4 05 01 PM

The original behaviour defaults to a min-height of 50px, but only if the user makes a change. This interaction is preserved in this update.

To improve the canvas rendering of Cover, switching units would "reset" the Cover to predetermined values for each unit. This is to prevent the Cover block's height from unexpectedly jumping to 3000px if they switch to a vh or vw unit.

Drag Resize Handling

Dragging to resize will "reset" the unit back to px. This interaction decision was made as it is very difficult to accurately translate pixel by pixel drag values to their respective vh or em values.

Simulated Height Rendering

Cover's canvas rendering is simulated as best as possible given the value and unit type. For example, vh and vw can be accurately rendered. Whereas we have to take the best guess for units like em or rem.

Tests

These updates have been tested locally in Gutenberg. Unit tests need to be written for the new UnitControl and NumberControl components.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

Resolves:
#20567 (comment)

@github-actions
Copy link

@github-actions github-actions bot commented Mar 13, 2020

Size Change: +1.99 kB (0%)

Total Size: 863 kB

Filename Size Change
build/annotations/index.js 3.44 kB +1 B
build/api-fetch/index.js 3.39 kB +1 B
build/block-directory/index.js 6.03 kB +4 B (0%)
build/block-editor/index.js 102 kB +136 B (0%)
build/block-editor/style-rtl.css 11 kB +6 B (0%)
build/block-editor/style.css 11 kB +6 B (0%)
build/block-library/index.js 110 kB +196 B (0%)
build/block-serialization-default-parser/index.js 1.65 kB +1 B
build/blocks/index.js 57.5 kB +2 B (0%)
build/components/index.js 191 kB +1.61 kB (0%)
build/core-data/index.js 10.6 kB +1 B
build/data-controls/index.js 1.03 kB -3 B (0%)
build/data/index.js 8.26 kB +4 B (0%)
build/date/index.js 5.36 kB -4 B (0%)
build/edit-site/index.js 8.63 kB +3 B (0%)
build/edit-widgets/index.js 4.43 kB -1 B
build/editor/index.js 42.8 kB +11 B (0%)
build/element/index.js 4.44 kB +2 B (0%)
build/format-library/index.js 6.95 kB -2 B (0%)
build/hooks/index.js 1.92 kB -2 B (0%)
build/i18n/index.js 3.57 kB +1 B
build/is-shallow-equal/index.js 712 B +2 B (0%)
build/keyboard-shortcuts/index.js 2.31 kB +7 B (0%)
build/keycodes/index.js 1.7 kB -2 B (0%)
build/media-utils/index.js 4.84 kB +3 B (0%)
build/notices/index.js 1.57 kB -2 B (0%)
build/nux/index.js 3.01 kB +3 B (0%)
build/plugins/index.js 2.54 kB +3 B (0%)
build/primitives/index.js 1.5 kB -1 B
build/redux-routine/index.js 2.84 kB +1 B
build/rich-text/index.js 14.5 kB +2 B (0%)
build/server-side-render/index.js 2.55 kB -3 B (0%)
build/shortcode/index.js 1.7 kB +1 B
build/url/index.js 4.01 kB +2 B (0%)
build/warning/index.js 1.14 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 998 B 0 B
build/autop/index.js 2.58 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 760 B 0 B
build/block-library/editor-rtl.css 7.21 kB 0 B
build/block-library/editor.css 7.21 kB 0 B
build/block-library/style-rtl.css 7.5 kB 0 B
build/block-library/style.css 7.51 kB 0 B
build/block-library/theme-rtl.css 669 B 0 B
build/block-library/theme.css 671 B 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/components/style-rtl.css 15.8 kB 0 B
build/components/style.css 15.7 kB 0 B
build/compose/index.js 6.2 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.06 kB 0 B
build/edit-post/index.js 92.3 kB 0 B
build/edit-post/style-rtl.css 8.35 kB 0 B
build/edit-post/style.css 8.34 kB 0 B
build/edit-site/style-rtl.css 3.44 kB 0 B
build/edit-site/style.css 3.44 kB 0 B
build/edit-widgets/style-rtl.css 2.57 kB 0 B
build/edit-widgets/style.css 2.57 kB 0 B
build/editor/editor-styles-rtl.css 423 B 0 B
build/editor/editor-styles.css 426 B 0 B
build/editor/style-rtl.css 3.38 kB 0 B
build/editor/style.css 3.38 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/list-reusable-blocks/index.js 2.99 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/priority-queue/index.js 781 B 0 B
build/token-list/index.js 1.28 kB 0 B
build/viewport/index.js 1.61 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@ItsJonQ ItsJonQ requested a review from ellatrix as a code owner Mar 14, 2020
@mcsf
Copy link
Contributor

@mcsf mcsf commented Mar 16, 2020

Hi! I may have missed a detail, but was wondering: what are some intended use cases for allowing the customisation of the unit set beyond px, em, rem, vw, vh?

* @return array Filtered editor settings.
*/
function gutenberg_extend_settings_custom_units( $settings ) {
$settings['__experimentalDisableCustomUnits'] = get_theme_support( 'disable-custom-units' );

This comment has been minimized.

@mtias

mtias Mar 16, 2020
Contributor

Should this support a second argument where the theme can say which should be the unit shown / used? Like get_theme_support( 'disable-custom-units', 'rem' ) would force rem to be used in all unit-based selectors.

This comment has been minimized.

@ItsJonQ

ItsJonQ Mar 17, 2020
Author Contributor

Good suggestion! Let me make that adjustment

@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 17, 2020

Hi! I may have missed a detail, but was wondering: what are some intended use cases for allowing the customisation of the unit set beyond px, em, rem, vw, vh?

@mcsf Haii!! There are many other units beyond the defaults I've set for the base UnitControl. My intention was to keep it open, which may allow blocks (core or 3rd party) to specify the units they wish to support. Some other custom units (that are slowly gaining popularity) are things like vmin or vmax.

Hope this makes sense 🙏

@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 17, 2020

@mtias I just pushed an update! This adds support for additional parameters for add_theme_support( 'disable-custom-units').

For example:

// This will disable all custom units
add_theme_support( 'disable-custom-units' );

// This will enable only rem
add_theme_support( 'disable-custom-units', 'rem' );

// This will enable only rem and em
add_theme_support( 'disable-custom-units', 'rem', 'em' );

If, say, only rem and em are enabled, a control would look like this:

Screen Shot 2020-03-17 at 4 33 28 PM

Lemme know how this API feels for you :)


In other news...

There may be race conditions where a block (by default) expects a px value. However, the moment the units are forced to using only rem, it may make the block confused.

We have a bit of this in the case of Cover. To test...

  • Add a Cover block
  • Save
  • Add add_theme_support( 'disable-custom-units', 'rem' );
  • Change Cover block's height

@mtias
Copy link
Contributor

@mtias mtias commented Mar 18, 2020

This looks good. Yes, there will be several things to tweak to account for units being a "thing" now across blocks, but we'll get there.

It might make sense to rename the theme support from disable-custom-units to experimental-custom-units initially in case we need to make tweaks before releasing.

@ItsJonQ ItsJonQ force-pushed the try/cover-height-custom-units branch from 4eecd7b to 7feead3 Mar 18, 2020
@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 19, 2020

It might make sense to rename the theme support from disable-custom-units to experimental-custom-units initially in case we need to make tweaks before releasing.

@mtias Done 👍

@mcsf
mcsf approved these changes Mar 19, 2020
Copy link
Contributor

@mcsf mcsf left a comment

I've left some notes, but other than that let's keep it moving!

@@ -32,6 +32,9 @@
"minHeight": {
"type": "number"
},
"minHeightUnit": {
"type": "string"

This comment has been minimized.

@mcsf

mcsf Mar 19, 2020
Contributor

I assume from this that it is preferred to let components like CoverHeightInput decide based on an undefined value, correct? Otherwise we might add a default value here.

style.minHeight = minHeight || undefined;
style.minHeight = minHeightUnit
? `${ minHeight }${ minHeightUnit }`
: minHeight || undefined;

This comment has been minimized.

@mcsf

mcsf Mar 19, 2020
Contributor

I for one had to go check the operator precedence table for logical OR and ternary conditional to make sure I was reading this correctly. :) I recommend parentheses in this situations.

export const BASE = {
black: '#000',
white: '#fff',
};

export const G2 = {

This comment has been minimized.

@mcsf

mcsf Mar 19, 2020
Contributor

Maybe add TODO-type comment explaining what G2 means (succinctly, focused on why this object is separate from the other colors) and with a note on when it would be a good time to consolidate all of this. Otherwise other devs in a few months won't have a clue of what this means. :)

@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 19, 2020

@mcsf Thank you so much for your feedback 🙏 ! Great suggestions! I just pushed them up

@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 20, 2020

@mcsf Hmm! It looks like adding the default px value for minHeightUnit breaks a couple of the fixture based tests 😅

I'm not sure how to fix it. It looks like the fixture (.json) files are autogenerated. I tried running npm run fixtures:generate, but it doesn't look like that's helping.

🤔

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Mar 20, 2020

It also means you need a deprecated version for the block, otherwise, it's going to invalidate existing blocks.

export const COLORS = {
...BASE,
darkGray: DARK_GRAY,
darkGray: merge( DARK_GRAY, G2.darkGray ),

This comment has been minimized.

@aduth

aduth Mar 20, 2020
Member

This is mutating DARK_GRAY, such that every reference to DARK_GRAY would subsequently contain properties of G2.darkGray. I don't think that's the intention?

Suggested change
darkGray: merge( DARK_GRAY, G2.darkGray ),
darkGray: merge( {}, DARK_GRAY, G2.darkGray ),

A few other instances in this file as well.

This comment has been minimized.

@ItsJonQ

ItsJonQ Mar 20, 2020
Author Contributor

Oh! I didn't know it mutates. Thank you! Will fix those up

@ItsJonQ ItsJonQ force-pushed the try/cover-height-custom-units branch from 0a99fa5 to dd9e79d Mar 23, 2020
@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 23, 2020

Rebased with latest master

@ItsJonQ ItsJonQ force-pushed the try/cover-height-custom-units branch from dd9e79d to d1469b6 Mar 24, 2020
@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 24, 2020

It also means you need a deprecated version for the block, otherwise, it's going to invalidate existing blocks.

@youknowriad Haiii! I'm following up on this PR :).

I'm not sure how to proceed with deprecation. For this particular change, would adding a new minHeightUnit qualify for us to deprecate the previous version of Cover?

Thank you!

@ItsJonQ ItsJonQ mentioned this pull request Mar 25, 2020
3 of 11 tasks
@ItsJonQ ItsJonQ force-pushed the try/cover-height-custom-units branch from 6d1f7d8 to 95a7024 Mar 30, 2020
@ItsJonQ
Copy link
Contributor Author

@ItsJonQ ItsJonQ commented Mar 30, 2020

Yay! Looks like Travis green and happy!
Thanks for the reviews, thoughts, and feedback everyone!

Excited for this one to land.

Merging it up!

@ItsJonQ ItsJonQ merged commit 328655b into master Mar 30, 2020
3 checks passed
3 checks passed
@github-actions
build
Details
@github-actions
pull-request-automation
Details
@travis-ci
Travis CI - Pull Request Build Passed
Details
@ItsJonQ ItsJonQ deleted the try/cover-height-custom-units branch Mar 30, 2020
@github-actions github-actions bot added this to the Gutenberg 7.9 milestone Mar 30, 2020

const baseUnitLabelStyles = ( props ) => {
return css`
appearance: none;

This comment has been minimized.

@oandregal

oandregal Apr 13, 2020
Member

I'm testing this on Firefox 75 in a Linux OS and the input control in Firefox includes the stepper arrows buttons:

2020-04-13-191008-height-control-input-firefox

while in Chrome it doesn't:

2020-04-13-191037-height-control-input-chrome

For what is worth, I've tried with -moz-appearance: textfield and, as far as my testing went, it worked as expected (arrow up/down with the keyboard still increases/decreases the number) and the steppers are not shown.

cc @ItsJonQ @mcsf

This comment has been minimized.

@mcsf

mcsf Apr 13, 2020
Contributor

I'm testing this on Firefox 75 in a Linux OS

Firefox (76) on macOS is also affected.

This comment has been minimized.

@oandregal

oandregal Apr 14, 2020
Member

Potential fix at #21571

@ellatrix ellatrix mentioned this pull request Jun 16, 2020
10 of 12 tasks
@mems
Copy link

@mems mems commented Nov 11, 2020

For people like me who don't know why this option is not available in the editor of cover block and find that PR, it's because the theme didn't enable custom units:

add_theme_support( 'custom-units' ); // this allow the post author to select other CSS units than "px"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment