FontSizePicker: Use components instead of helper functions#44891
Conversation
Simplify how FontSizePicker is implemented by using nested components instead of helper functions. Components are easier to reason about and much easier to type.
There was a problem hiding this comment.
Thank you for working on this, @noisysocks !
This has been a much deeper review than anticipated 😅
Apart from the inline comments, could we also rename:
toggle-group.tsxtofont-size-picker-toggle-group.tsxselect.tsxtofont-size-picker-select.tsx
The following observations are also present on trunk, but I thought I'd mention them anyway:
- I noticed that the user can still enter a custom value even if
disableCustomFontSizesis set totrue, whenwithSlideris also set totrue - The logic around
fallbackFontSize,withResetandwithSlideris very confusing and should be reviewed. - The component can get into a weird state if:
i. Click on the toggle to show the custom font size controls
ii. Change thedisableCustomFontSizestotrue
iii. The UI disappears and the user is stuck
|
Thanks for the feedback @ciampo!
I think #44598 should accidentally fix this!
The whole component is very confusing 😂. I'd be keen to deprecate |
|
Feedback addressed or responded to! Thanks again. |
|
Thank you! I will try to have another look at this before EOW |
ciampo
left a comment
There was a problem hiding this comment.
There's a couple of comments still in need of addressing, but in the meantime I gave the component a look on Storybook and it looks fine!
I still think that we could add more tests to FontSizePicker, given its complexity and the number of edge cases (an example of the need for more coverage is that my manual code review flagged at least 3 instances in which the original logic was not refactored correctly). It would be a good investment , since from what I understand this component will be changed/improved further in the upcoming weeks.
(ps: for the same reasons, more manual testing is welcome too!)
|
I rebased this and addressed all the inline feedback. I'll improve the |
…y-font-size-picker
|
I stacked this PR onto #45298. |
ciampo
left a comment
There was a problem hiding this comment.
How are you feeling @ciampo?
Feeling pretty good about this change and the newly added test — they definitely give us more confidence when making changes moving forward!
Although I still found some discrepancies between this PR and trunk, which I outlined in one of the inline conversations. I believe that once that's solved, we'll be ready to merge!
|
Nice catch @ciampo, I've fixed that and added regression tests. |
I tricked myself, I think :) |
| } ); | ||
| } | ||
|
|
||
| export function getSelectedOption( |
There was a problem hiding this comment.
Heads up, it's possible that I'll need this again for #44967
Maybe not. Either way, it's going to be a gnarly rebase if this goes in first.
🐎
There was a problem hiding this comment.
Oh wait, I can probably use selectedFontSize or something similar. Never mind me.
There was a problem hiding this comment.
Yeah selectedFontSize should work. Or you can fontSizes.find(). Happy to help you with the rebase if it's too gnarly.
ciampo
left a comment
There was a problem hiding this comment.
LGTM 🚀
Great, great work here. Thank you so much!
What?
Simplify how
FontSizePickeris implemented by using nested components for theCustomSelectControlandToggleGroupControllogic instead of helper functions.Why?
While adding types to
FontSizePickerin #44449 I ran into difficulty typinggetFontSizeOptionsas its return type depends on the runtime value ofuseSelectControl. See #44449 (comment) for some relevant discussion there.The end result is lots of yucky
ascasts scattered throughout the component:gutenberg/packages/components/src/font-size-picker/index.tsx
Line 218 in 3983fee
In addition to being difficult to type, it's just plain confusing. @ramonjd learned this the other day when trying to make a simple change to
FontSizePickerin #44791. He only needed to change one line but the design of this component tricked him into thinking it was more complicated than it was. (Sorry to pick on you @ramonjd.)To do away with all of this, I've settled on embracing that
FontSizePickerworks differently depending on if there are more than 5 font sizes or not. I've split out theCustomSelectControl(>5 font sizes) andToggleGroupControl(≤5 font sizes) into their own components. Now each component can do whatever it needs to do without having to worry about the other. We can then remove the difficult-to-type helper functions altogether.How?
FontSizePickerSelectcomponent: this contains theCustomSelectControland logic for when there are > 5 font sizes.FontSizePickerToggleGroupcomponent: this contains theToggleGroupControland logic for when there are ≤5 font sizes.utils.ts.Testing Instructions
Either:
FontSizePickerin Storybook, or;The font size picker should work exactly as it does in
trunkand tests should pass.