-
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
Cover
block: stop using UnitControl
's deprecated unit
prop
#39522
Conversation
Size Change: +8 B (0%) Total Size: 1.16 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.
Thanks for these PRs @ciampo!
Is there also an opportunity in this PR to remove the temporaryInput
internal state of the CoverHeightInput
component? I'm struggling a bit to understand what it's doing currently, but apologies if I'm missing something obvious! Since it's slightly outside the scope of the change to deprecate the unit
prop, we can always look at it separately, though.
For the moment, this change is testing well for me, I think we might possibly need to add unit
to a dependency array, though.
I had a quick go at seeing if we can remove temporaryInput
in the below diff, which wound up requiring adding unit
to the dependency array.
Attempt to remove `temporaryInput` internal state
diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js
index 285e232616..d49e660a8b 100644
--- a/packages/block-library/src/cover/edit.js
+++ b/packages/block-library/src/cover/edit.js
@@ -103,8 +103,6 @@ function CoverHeightInput( {
unit = 'px',
value = '',
} ) {
- const [ temporaryInput, setTemporaryInput ] = useState( null );
-
const instanceId = useInstanceId( UnitControl );
const inputId = `block-cover-height-input-${ instanceId }`;
const isPx = unit === 'px';
@@ -127,27 +125,15 @@ function CoverHeightInput( {
: undefined;
if ( isNaN( inputValue ) && inputValue !== undefined ) {
- setTemporaryInput( unprocessedValue );
return;
}
- setTemporaryInput( null );
onChange( inputValue );
};
- const handleOnBlur = () => {
- if ( temporaryInput !== null ) {
- setTemporaryInput( null );
- }
- };
-
const computedValue = useMemo( () => {
- const inputValue = temporaryInput !== null ? temporaryInput : value;
- const [ parsedQuantity ] = parseQuantityAndUnitFromRawValue(
- inputValue
- );
-
+ const [ parsedQuantity ] = parseQuantityAndUnitFromRawValue( value );
return [ parsedQuantity, unit ].join( '' );
- }, [ temporaryInput, value ] );
+ }, [ value, unit ] );
const min = isPx ? COVER_MIN_HEIGHT : 0;
@@ -157,7 +143,6 @@ function CoverHeightInput( {
id={ inputId }
isResetValueOnUnitChange
min={ min }
- onBlur={ handleOnBlur }
onChange={ handleOnChange }
onUnitChange={ onUnitChange }
style={ { maxWidth: 80 } }
What do you think? I know these components are a bit of a rabbit hole, so I totally understand if you'd just like to get the simpler change landed first!
); | ||
|
||
return [ parsedQuantity, unit ].join( '' ); | ||
}, [ temporaryInput, value ] ); |
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.
Do we need to add unit
to the dependency array here? In this change, I couldn't find any issues that came up from not including it, but if we were to refactor to remove the temporaryInput
internal state, then I was wondering if it could be an issue.
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.
Do we need to add
unit
to the dependency array here?
Absolutely, I definitely missed adding it on my end.
(Also, that's why I hope we can soon add the exhaustive-deps
eslint rule to the repo).
cc6df7a
to
61ff1e0
Compare
Thank you for your review, @andrewserong !
I am not familiar with the code in blocks in general (as I tend to mostly work in the components package), but I took a look at the I went ahead and pushed changes to remove 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.
Thanks for including those changes @ciampo, much appreciated! It's so nice when we can simplify things a bit with these refactors 😀
✅ Manually inputing values works the same as trunk
✅ Switching units works correctly, and resets the value to the default for that unit
✅ Empty values are treated as empty
✅ Dragging the resizer switches to the px
unit
LGTM! 🎉
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 is testing well on the web for me 👍
✅ Inputting values worked as expected
✅ Clearing values works
✅ Switching units correctly applies default for that unit
✅ Resizing via the resizable box's drag handles works and enforces px
units.
My only question is should this PR remove the unit prop from the native controls as well?
Thank you both, @andrewserong and @aaronrobertshaw ! Regarding the native controls, the unit A couple of weeks ago I had a chat with @geriux and agreed that he would help aligning the native component with the web component — he should continue the work in this draft PR. Until that's done, we won't be able to update the native consumers of |
…dPress#39522) * Cover block: stop using `UnitControl` s deprecated `unit` prop * Add `unit` to the list of `computedValue` memo dependencies * Remove `temporaryValue`
What?
Related to #39503 , this PR refactors the
Cover
blockmin height
controls to avoid using the deprecatedunit
prop from theUnitControl
component.Why?
The
unit
prop is marked as deprecated, the component's docs recommend passing the unit directly through thevalue
propHow?
Refactor the code to pass the unit directly through the
value
propTesting Instructions
min height
(change unit, change value by typing, pressing arrows up/down, mouse dragging...) and make sure it behaves like in productionmin height
in a unit different thanpx
px