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

FontSizePicker: Use components instead of helper functions #44891

Merged
merged 30 commits into from
Nov 2, 2022

Conversation

noisysocks
Copy link
Member

What?

Simplify how FontSizePicker is implemented by using nested components for the CustomSelectControl and ToggleGroupControl logic instead of helper functions.

Why?

While adding types to FontSizePicker in #44449 I ran into difficulty typing getFontSizeOptions as its return type depends on the runtime value of useSelectControl. See #44449 (comment) for some relevant discussion there.

The end result is lots of yucky as casts scattered throughout the component:

options={ options as FontSizeSelectOption[] }

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 FontSizePicker in #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 FontSizePicker works differently depending on if there are more than 5 font sizes or not. I've split out the CustomSelectControl (>5 font sizes) and ToggleGroupControl (≤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?

  • New FontSizePickerSelect component: this contains the CustomSelectControl and logic for when there are > 5 font sizes.
  • New FontSizePickerToggleGroup component: this contains the ToggleGroupControl and logic for when there are ≤5 font sizes.
  • Removed no-longer-necessary helpers from utils.ts.

Testing Instructions

Either:

  • Look at FontSizePicker in Storybook, or;
  • Go to Site Editor → Global Styles → Typography → Text → Size.

The font size picker should work exactly as it does in trunk and tests should pass.

Simplify how FontSizePicker is implemented by using nested components
instead of helper functions. Components are easier to reason about and
much easier to type.
@noisysocks noisysocks added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Oct 12, 2022
@ciampo ciampo requested a review from mirka October 12, 2022 09:46
@ciampo ciampo requested a review from chad1008 October 12, 2022 09:46
@ciampo ciampo added this to In progress (owned) ⏳ in WordPress Components via automation Oct 12, 2022
Copy link
Contributor

@ciampo ciampo left a 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, @noisysocks !

This has been a much deeper review than anticipated 😅

Apart from the inline comments, could we also rename:

  • toggle-group.tsx to font-size-picker-toggle-group.tsx
  • select.tsx to font-size-picker-select.tsx

The following observations are also present on trunk, but I thought I'd mention them anyway:

  1. I noticed that the user can still enter a custom value even if disableCustomFontSizes is set to true, when withSlider is also set to true
  2. The logic around fallbackFontSize, withReset and withSlider is very confusing and should be reviewed.
  3. The component can get into a weird state if:
    i. Click on the toggle to show the custom font size controls
    ii. Change the disableCustomFontSizes to true
    iii. The UI disappears and the user is stuck

packages/components/src/font-size-picker/types.ts Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/types.ts Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/select.tsx Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/index.tsx Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/index.tsx Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/select.tsx Outdated Show resolved Hide resolved
packages/components/src/font-size-picker/toggle-group.tsx Outdated Show resolved Hide resolved
@noisysocks
Copy link
Member Author

Thanks for the feedback @ciampo!

  1. I noticed that the user can still enter a custom value even if disableCustomFontSizes is set to true, when withSlider is also set to true

I think #44598 should accidentally fix this!

  1. The logic around fallbackFontSize, withReset and withSlider is very confusing and should be reviewed.

The whole component is very confusing 😂. I'd be keen to deprecate withReset. fallbackFontSize is named poorly (suggestion: defaultSliderValue) but probably needs to be there.

@noisysocks
Copy link
Member Author

Feedback addressed or responded to! Thanks again.

@ciampo
Copy link
Contributor

ciampo commented Oct 13, 2022

Thank you! I will try to have another look at this before EOW

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

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!)

packages/components/src/font-size-picker/test/index.tsx Outdated Show resolved Hide resolved
@noisysocks
Copy link
Member Author

I rebased this and addressed all the inline feedback. I'll improve the FontSizePicker tests tomorrow in a seperate PR so that we can merge that one first.

@noisysocks noisysocks changed the base branch from trunk to update/font-size-picker-unit-tests November 1, 2022 05:25
@noisysocks
Copy link
Member Author

I stacked this PR onto #45298.

Base automatically changed from update/font-size-picker-unit-tests to trunk November 1, 2022 06:20
@noisysocks
Copy link
Member Author

Cool, the unit tests added in #45298 pass! That should reassure us that this refactor doesn't break FontSizePicker. How are you feeling @ciampo?

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

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!

@noisysocks
Copy link
Member Author

Nice catch @ciampo, I've fixed that and added regression tests.

@ramonjd
Copy link
Member

ramonjd commented Nov 2, 2022

He only needed to change one line but the design of this component tricked him into thinking it was more complicated than it was.

I tricked myself, I think :)

} );
}

export function getSelectedOption(
Copy link
Member

Choose a reason for hiding this comment

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

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.

🐎

Copy link
Member

Choose a reason for hiding this comment

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

Oh wait, I can probably use selectedFontSize or something similar. Never mind me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah selectedFontSize should work. Or you can fontSizes.find(). Happy to help you with the rebase if it's too gnarly.

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Great, great work here. Thank you so much!

@noisysocks noisysocks merged commit 3081460 into trunk Nov 2, 2022
WordPress Components automation moved this from In progress (owned) ⏳ to Done 🎉 Nov 2, 2022
@noisysocks noisysocks deleted the update/simplify-font-size-picker branch November 2, 2022 20:55
@github-actions github-actions bot added this to the Gutenberg 14.6 milestone Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants