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

BorderBoxControl: fix right border control alignment on small viewports and RTL styles #41254

Merged
merged 6 commits into from
May 24, 2022

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented May 23, 2022

What?

This PR:

The bug fixes in this PR also related to #41166

Why?

Bug fix

How?

The styles used to align the right border are the same used by @aaronrobertshaw in the original PR (#40874).

While adding and testing these changes, I noticed that RTL styles where not applied as expected, and found out a few issues with the way styles are written and assigned.

Testing Instructions

  • Render the BorderBoxControl component (either in Storybook, or in the editor)
  • Unlink the sides, so that 4 border controls are rendered (top, right, bottom, left border)
  • On a small screen (mobile/tablet), make sure that the right border control is aligned correctly with the rest of the UI
  • Using the tools of your choosing, test that styles are applied correctly also when the writing direction is RTL

Screenshots or screencast

Control alignment:

trunk This PR
Screenshot 2022-05-23 at 19 30 23 Screenshot 2022-05-23 at 19 31 30

(the change is subtle, but in this PR, the right border control is a bit further away from the left border control)

RTL styles

border-box-control-rtl.mp4

(first Storybook is trunk, second Storybook is this PR)

@ciampo ciampo added [Type] Bug An existing feature does not function as intended [Package] Components /packages/components [Feature] Component System WordPress component system labels May 23, 2022
@ciampo ciampo requested a review from ajitbohra as a code owner May 23, 2022 17:38
@ciampo ciampo self-assigned this May 23, 2022
Comment on lines +28 to +29
// rtlWatchResult is needed to refresh styles when the writing direction changes
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Contributor Author

@ciampo ciampo May 23, 2022

Choose a reason for hiding this comment

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

Added a bunch of these eslint-disable-next-line rules in preparation for the exhaustive-deps rule (see #41166)

@sarayourfriend , WDYT about the current rtl.watch() approach? Asking because, with the new eslint rule, we may need to have to "ignore" the rule a lot of the times, which defeats the point a little bit

(cc @chad1008 )

Copy link
Contributor

Choose a reason for hiding this comment

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

It does defeat the purpose of adding the exhaustive-deps rule if all components should eventually support RTL. In the interim though probably better to catch a few oversights in the dependency arrays.

Copy link
Contributor Author

@ciampo ciampo May 24, 2022

Choose a reason for hiding this comment

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

I agree. Ignoring the rule for now is a lesser evil compared to not adding the eslint rule at all. Will keep it as is for now

Copy link
Contributor

Choose a reason for hiding this comment

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

Want to confirm I'm following the logic here :)

rtlWatchResult isn't actually called in the useMemo's create function, so it's not technically a dependency from React's POV.

We do, however, want to recalculate classes whenever rtlWatchResult changes, so we add it to the dependency array to trigger the useMemo, which exhaustive-deps will hate for the reason described above?

Copy link
Contributor Author

@ciampo ciampo May 24, 2022

Choose a reason for hiding this comment

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

Correct.

A couple of alternative approaches that come to mind:

  • we could make these "dynamic style" functions accept, as an argument, a isRTL boolean (for example: return cx( styles.borderBoxControlSplitControls( rtlWatchResult ), className );). This would make the computation of RTL styles less "auto-magical", which isn't necessarily a bad thing since we've been missing a few of those!
  • create a hook that forces a re-render of the component when the writing direction changes, and therefore remove it from the list of dependencies of useMemo

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

The first option sounds, to me, like it might make things a little more intuitive, but I'd happily defer to others who've worked more with the current auto-magical approach 😄

Copy link
Member

Choose a reason for hiding this comment

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

I feel like we've discussed this before so sorry if I'm forgetting a crucial piece of the convo, but do we actually have a good reason to memoize these things at all? I searched around but couldn't really find anything that mentioned any performance issues that were observed. The Emotion folks also don't recommend blanket optimizations like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is also a good question. From what I remember, the reason why we memoize Emotion classname computation is because of a test ran by Q before I joined the components squad, where he found out that computing these classnames can be quite expensive.

Since then, quite some time has passed and, with that, libraries (like Emotion and React) also got updated. Therefore, I'm not sure if this assumption still holds true. We should run some tests and take it from there

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw 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 very much for addressing this @ciampo, it had slipped off my radar 🙇

BorderBoxControl functions and looks correctly aligned in the Storybook
✅ Border controls in the editor look and work well in the editor with RTL or small viewports
✅ Unit tests are still passing
✅ No linting errors (other than those addressed by #41259)

LGTM 👍

Comment on lines +28 to +29
// rtlWatchResult is needed to refresh styles when the writing direction changes
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Contributor

Choose a reason for hiding this comment

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

It does defeat the purpose of adding the exhaustive-deps rule if all components should eventually support RTL. In the interim though probably better to catch a few oversights in the dependency arrays.

@ciampo ciampo merged commit d0faa07 into trunk May 24, 2022
@ciampo ciampo deleted the fix/border-box-control-right-alignment branch May 24, 2022 11:27
@github-actions github-actions bot added this to the Gutenberg 13.4 milestone May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Component System WordPress component system [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants