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

UnitControl: Add support for unit step per unit type #30376

Merged
merged 13 commits into from
May 11, 2021

Conversation

rmorse
Copy link
Contributor

@rmorse rmorse commented Mar 30, 2021

Description

The UnitControl component does not support a "step" value. Therefor it always increments exactly by 1.

This is probably not desired for certain unit types (i.e., rem, em, etc) in certain situations.

This update, allows for adding a "step" property to the units argument - so it can be controlled on a per unit type basis:

So we can now do this:

    const units = [
    	{ value: 'px', label: 'px', default: 0, step: 1 },
    	{ value: '%', label: '%', default: 10, step: 1 },
    	{ value: 'em', label: 'em', default: 0, step: 0.1 },
    ];

It defaults to a value of 1, if the unit type does not contain a step (for backwards compatibility).

Personally, I wanted to use this as an improved "Line height" control, that also supports units, and the first unit is blank:
{ value: '', label: '', default: 1, step: 0.1 },
But I think there is a good argument for supporting different steps for different use cases on a per unit basis.

How has this been tested?

I've tested the component by following the example, by suppling a step, and without.

All incremements behave as expected.

I have not tested the React Native stuff - but I have updated the code. I checked, and it looks like RangeCell and StepperCell both have a step property so this should work - can someone test this? (I'm not sure how to)

Screenshots

unitcontrol-step
Don't worry about the value reset on unit change - this is not hooked up to state.
This example uses the following props for unit types (notice 0.1 step on em, rem, and empty)

const unitTypes = [
	{ value: '', label: '', default: 1, step: 0.1 },
	{ value: 'px', label: 'px', default: 0 },
	{ value: '%', label: '%', default: 0 },
	{ value: 'em', label: 'em', default: 0, step: 0.1 },
	{ value: 'rem', label: 'rem', default: 0, step: 0.1 },
	{ value: 'vw', label: 'vw', default: 0 },
	{ value: 'vh', label: 'vh', default: 0 },
];

Types of changes

Minor improvement - we check the unit types argument, for a step value and pass that into our number input, if it is not present, default to a step of 1

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • 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 (please manually search all *.native.js files for terms that need renaming or removal).

*Update - I guess this also builds on: #31314

@@ -22,6 +22,7 @@ import { Root, ValueInput } from './styles/unit-control-styles';
import UnitSelectControl from './unit-select-control';
import { CSS_UNITS, getParsedValue, getValidParsedUnit } from './utils';
import { useControlledState } from '../utils/hooks';
// import { active } from '@wp-g2/styles/types';
Copy link
Contributor Author

@rmorse rmorse Mar 30, 2021

Choose a reason for hiding this comment

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

This was causing my local build to fail, and it wasn't in use - so I've commented it for now

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need anything here?

@rmorse
Copy link
Contributor Author

rmorse commented Mar 30, 2021

@ItsJonQ - thought I would flag you on this as you've been working on the previous iterations and will have a sense if this is in the right direction.

@paaljoachim
Copy link
Contributor

@aristath @gziolo @mcsf Can you take a look at this PR?
Thanks!

@aristath
Copy link
Member

What are all the changes to package-lock.json for?

@rmorse
Copy link
Contributor Author

rmorse commented Apr 13, 2021

Hmm there shouldn't have been, maybe a bad merge on my side, I'll double check later on today and see if I can get this tidied up.

@mcsf mcsf added [Package] Components /packages/components [Type] Enhancement A suggestion for improvement. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Apr 15, 2021
@mcsf
Copy link
Contributor

mcsf commented Apr 15, 2021

@rmorse: thanks for the PR. Ensuring that certain units, like em, can default to stepping by 0.1 makes sense as far as the user is concerned. However, I wanted to correct the opening assumption:

The UnitControl component does not support a "step" value. Therefor it always increments exactly by 1.

The component does accept step as a prop, which it will pass down until it reaches NumberControl. I agree that this isn't clear, and the inheritance model used by the *Control components makes it hard to determine what the full API is.

@nosolosw: is the addition of units to line-height compatible with what you're doing in Global Styles?

If so, then @rmorse, we could go with your suggestion to provide step defaults for each unit in CSS_UNITS, as long as we don't override any incoming step prop:

let step = stepProp;
if ( ! step ) {
  const activeUnit = units.find( ( option ) => option.value === unit );
  step = activeUnit?.step ? activeUnit.step : 1;
}

My last note is to treat the non-unit, '', locally in LineHeightControl and not in UnitControl. That's because, in other contexts, it may refer to larger or strictly integer values (e.g. columns: 2).

@oandregal
Copy link
Member

Things that relate to this from the "theme.json" perspective are:

@rmorse
Copy link
Contributor Author

rmorse commented Apr 18, 2021

@mcsf - I've updated that. To confirm logic:

  • The UnitControl can accept a step prop, and that should supercede the step value associated with units
  • It is expected that this prop, is applied to all units when passed

I've added your suggestion to the PR (and also added a condition to default to the first unit type, if somehow one wasn't found)

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Thanks for cleaning up. Here's my review.

packages/components/src/unit-control/index.js Outdated Show resolved Hide resolved
packages/components/src/unit-control/index.js Outdated Show resolved Hide resolved
packages/components/src/unit-control/index.js Outdated Show resolved Hide resolved
@@ -22,6 +22,7 @@ import { Root, ValueInput } from './styles/unit-control-styles';
import UnitSelectControl from './unit-select-control';
import { CSS_UNITS, getParsedValue, getValidParsedUnit } from './utils';
import { useControlledState } from '../utils/hooks';
// import { active } from '@wp-g2/styles/types';
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need anything here?

@mcsf mcsf requested a review from gziolo April 19, 2021 17:17
Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Hey, @rmorse, we're almost there! Please apply these last fixes, and we should be good to go.

packages/components/src/unit-control/index.native.js Outdated Show resolved Hide resolved
packages/components/src/unit-control/index.js Outdated Show resolved Hide resolved
packages/components/src/unit-control/index.native.js Outdated Show resolved Hide resolved
@rmorse
Copy link
Contributor Author

rmorse commented May 11, 2021

Thanks @mcsf , now done - I didn't understand those fails on units being able to be false - should have spotted the bad variable name though ;)

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

🚢

Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

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

LGTM. I tested the changes for native on an iPhone 11 simulator and Samsung Galaxy S20 to test for any regressions.

I also opened wordpress-mobile/gutenberg-mobile#3479 to allow additional native CI tests to run for this change. I'd recommend awaiting those to pass before merging.

@dcalhoun
Copy link
Member

The current failures in wordpress-mobile/gutenberg-mobile#3479 are unrelated to the changes in this PR and have been resolved in wordpress-mobile/gutenberg-mobile#3477. We should be good to go for merging this work. 👍🏻

@mcsf mcsf merged commit 132fec1 into WordPress:trunk May 11, 2021
@mcsf
Copy link
Contributor

mcsf commented May 11, 2021

Thanks for everyone’s help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants