-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Unit Control: Always display current unit value if valid #34768
Unit Control: Always display current unit value if valid #34768
Conversation
Size Change: -238 B (0%) Total Size: 1.06 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! Definitely an interesting problem 🤔 To me this approach strikes a fairer balance between preventing broken UI and respecting the configuration in theme.json
than enforcing 'required' units for the block.
I tested:
- In Twenty Twenty, '%' unit is selected when adding a 30/70 (or 25/50/25 etc) configuration
- With '%' still selected, value is treated as a percentage even if changed
- If the unit is changed, '%' is no longer available in the dropdown
- '%' is not available as an option for other configurations that don't set it by default (eg 50/50)
- '%' is always available when enabled in the theme.json
- Test coverage looks great 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this! (and special thanks for adding unit tests ❤️ ).
I've tested the changes and everything seems to work as expected!
I've also left a few inline comments, hope you don't mind!
* | ||
* @param {number|string} currentValue Selected value to parse. | ||
* @param {string} legacyUnit Legacy unit value, if currentValue needs it appended. | ||
* @param {Array<Object>} units List of available units. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It'd be great if we could leverage the @typedef
and @property
JSDocs tags to type the Units object better in this file (here's an example of how this syntax is used )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added in a @typedef
at the top of the file to create a definition of the unit object. Let me know if it needs further changes — I see there's a proposal to add in support for the keyword any
for the step
value in #34542. Since that isn't supported yet, I haven't added it to the type, but happy to add it in if you think we should get it in earlier.
eea67d9
to
f2d44a3
Compare
Thanks for the feedback @ciampo, it's very much appreciated! I've had a go at adding in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this, it's a great improvement!
Code looks good and clean, tested and works as advertised 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for addressing the feedback!
Longer-term it'd be great for us to move this component to TypeScript, too — I'll write myself a note in case I get spare time at any point further down the track, and if no one beats me to it 🙂
I had a quick look at inspecting the code and I can already spot a few inconsistencies — mainly related to the fact that the unit is typed as string|number
. A quick way to spot these is to change the extension of the file to .ts
and re-run the ts-check
The conversion to TypeScript would be a welcome one, and seeing the bugs just listed above, probably should be treated with a higher priority than we initially estimated.
Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com>
Thanks again for the detailed notes @ciampo! I've updated the README, and great point about the inconsistencies when we switch the utils file to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @andrewserong for working on this and addressing all feedback! Code changes LGTM and test well as per instructions 🚀
I'm happy to start taking a look at refactoring this component to TypeScript later on next week if it doesn't need to be done immediately, I'm keen to get the TS practice 🙂
That would be fantastic. Please keep me in the loop when you do, I'm definitely happy to help and review!
Description
Fixes #34096
In #34096 it was reported that in themes that don't include the
%
unit in the theme.jsonsettings.spacing.units
list, that percentage width column blocks display the current value aspx
instead of as%
. This PR explores ensuring that if there is a current value, that its unit is displayed correctly in the UnitControl's drop-down.The idea here is that whether or not we wish to hide or disable a particular unit, if the current value has somehow managed to be a valid CSS unit, we should still do our best to display it correctly and allow it to be editable. Once the user switches to another unit, then the provided list of allowed units will take precedence.
For ease of testing, the TT1-Blocks theme is a good candidate, as it does not list the
%
unit, and the Columns block is a good block to test, as its placeholder variations use percentage units.How has this been tested?
Test that % units are displayed correctly in TT1-Blocks
%
%
should workpx
)%
Test that the % unit doesn't show up where it isn't expected
%
10%
and hit ENTER. It should not let you commit the value as a percentage.Run tests
Screenshots
Before
Note that with TT1-Blocks active, after selecting the Columns block's 70 / 30 variation, the current value is displayed as if it's in
px
units. (If you switch to the code view, you can see that the attribute's value is currently in%
).After
After selecting the Columns block's 70 / 30 variation, the current value is correctly rendered with the
%
unit. Switching away from that unit will hide the availability of the unit, as the theme is still attempting to keep it hidden. Here, this PR attempts to respect the settings, while still displaying an accurate value.Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist:
*.native.js
files for terms that need renaming or removal). (This change only applies to the web-based component)