-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Value on the display is truncated on blur #41276
Comments
After some testing, my impression is that this is a bug somewhere in process where the block commits the value to a block attribute. In the screen recording, it looks like the block attribute isn't updated at all on blur. On the component side, it seems to be working as expected. Decimal precision is dictated by these gutenberg/packages/components/src/unit-control/utils.ts Lines 14 to 26 in 77d5d91
And the CleanShot.2022-05-31.at.23.49.32.mp4If anything, I would think the component's behavior (i.e. respect the |
Thank you for the test! However, I'm concerned that users might be confused if the block attributes are adjusted to the component specifications, because the values that could previously be entered on the block side will now be truncated. For example, if the unit is a percentage, some users might enter two decimal places as shown below, but the component will remove the two decimal places.
If the attributes of the block are to be adapted to the component's specifications, how about a little finer precision in the |
First of all, there is a bug (likely in the blocks) that doesn't respect the canonical value given by the component. So this should be investigated and fixed. Should this issue here be about fixing this bug specifically? For any custom usage of UnitControl, consumers are free to pass their own array of units, including a If you would like to propose changes in the |
As you explained, I would like to solve the following two problems in this issue:
Since it is complicated to address both issues at the same place, should I close this issue and propose each as a separate issue or PR? |
Sounds good! |
at some point, UnitControl appears to have broken the units value spec which uses an array of objects described in the documentation, and all units defined in such a way are treated as "px" with this step truncating. https://developer.wordpress.org/block-editor/reference-guides/components/unit-control/ const Example = () => {
const [ value, setValue ] = useState( '10px' );
const units = [
{ value: 'px', label: 'px', default: 0 },
{ value: '%', label: '%', default: 10 },
{ value: 'em', label: 'em', default: 0 },
];
return <UnitControl onChange={ setValue } value={ value } units={units} />;
}; Both % and em values are rounded to an integer on blur when units are specified in this way. Is this a bug or does the documentation need to be updated? Possibly deserving of a separate issue report... |
I'm not sure I follow about the spec having changed, but please do open a PR or issue if you think the docs can be improved. Is this specifically about the |
Well, I'm not sure. If you implement the example I copied above from the developer.wordpress.org docs, both the % and em units are rounded on blur to an integer (so, i suppose, step: 1). It seems odd that % and em are treated as "custom" rather than inheriting properties they're missing, but this seems like a recent change. I have blocks using UnitControl with units defined similarly which did not round floats on blur for those units until recently (not sure if it was a 6.0 change, but that seems logical). I guess the question is, is this intended behavior? If so, then sure, I'll see about opening an issue to update the docs at developer.wordpress.org. I've actually never seen the documentation link you posted, and don't know where I'd have found that otherwise... |
@ciampo Do you remember if the I wonder if that perceived change in behavior is due to the bug fix in #39186, where values are now (properly) validated/rounded on blur. |
I don't think so — from what I know, I've never seen
That behavior / utility function was introduced in #34768 — but it looks like even before that PR, the general behavior of the
That could well be the case, good thinking! We also need to understand the potential mis-alignment of documentation between the READMEs and Storybook examples in this package, and the docs on developer.wordpress.com |
i'd say it's less that it inherited anything, more that it's only recently we've seen UnitControl with a units prop defined like the developer.wordpress.com doc examples rounding the units we often use floats with, like em/rem/% |
That's my understanding, too @ciampo — I don't think there was ever any merging of missing properties in the |
Description
When the value is placed in
UnitControl
and moved away from the field, the decimal point value specified in the step of each unit is truncated.However, the decimal point values are retained as attributes, and the input values are displayed correctly when the browser is reloaded.
This behavior may lead to the misunderstanding that the specified decimal point cannot be entered.
I think that the decimal point should not be truncated on blur to display the same value as the attributes.
Step-by-step reproduction instructions
UnitControl
(e.g. column block).UnitControl
, set numbers and units that include a decimal point as follows:10.1111 px
10.1111 %
10.1111 em
10.1111 px
>10 px
10.1111 %
>10.1 %
10.1111 em
>10.11 em
Screenshots, screen recording, code snippet
a2d48445f35f6b5839bdeb7a39e5ebfa.mp4
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: