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

Components: Fix RTL on Flex component #33729

Merged
merged 11 commits into from
Aug 16, 2021
Merged

Conversation

walbo
Copy link
Member

@walbo walbo commented Jul 28, 2021

Description

The flex component has some margins to add gaps. These need to be reversed on RTL sites.

How has this been tested?

Tested different location that the component is used with Arabic and English.

Screenshots

Before After
Skjermbilde 2021-07-28 kl  15 02 42
Skjermbilde 2021-07-28 kl  15 03 35
Skjermbilde 2021-07-28 kl  14 56 36
Skjermbilde 2021-07-28 kl  15 00 58

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@skorasaurus skorasaurus added the [Package] Components /packages/components label Jul 28, 2021
@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Aug 3, 2021
Copy link
Contributor

@sarayourfriend sarayourfriend 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 the fix!

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, @walbo !

For ease of review, noting that the only significant code changes are in the in the useFlex hook, where the marginLeft and marginRight CSS properties are being wrapped in the rtl() utility function (which is able to switch left and right CSS props when a RTL language is detected at component "mount time").

Every other code change is caused by auto-formatting (and consequently, by spacing updates in the unit test snapshots).

For completeness, it would probably be good to replace instances of the words "left" and "right" in the folder, so that the documentation also doesn't assume LTR languages. A few instances I found:

Finally, should we add a unit test for RTL styles? (maybe we could mock the isRTL function for that specific test, in order to force rtl styles to be applied?)

@walbo
Copy link
Member Author

walbo commented Aug 4, 2021

Looks like the left in the readme/types.ts refers to an example that doesn’t exists. I guess those can be removed.

I'll take a look at adding an RTL unit test later today (or tomorrow). @ciampo to you know if there is an existing test that has mocked RTL for reference?

@ciampo
Copy link
Contributor

ciampo commented Aug 4, 2021

do you know if there is an existing test that has mocked RTL for reference?

Not that I know — but I'd probably look into mocking the isRtl function, maybe like this:

jest.mock( '@wordpress/i18n', () => ( {
	...jest.requireActual( '@wordpress/i18n' ),
	isRTL: jest.fn( () => true ),
} ) );

and then, maybe, later in the single test, change the implementation of isRtl to return true? Something like

isRTL.mockImplementationOnce( () => true );

... and either do a snapshot test of a component rendered with RTL locale, or maybe even do a diff snapshot between the LTR and the RTL version of the same snippet?

@sarayourfriend
Copy link
Contributor

To be fair we don't do these kinds of tests literally anywhere else in Gutenberg, do we? I'm not sure they bring that much value to something as declarative as styles 🤷 The rtl utility itself should be tested and implementations of it should be able not to worry about that implementation detail (indeed I'd argue testing those styles would be testing an implementation detail).

The best way to test that RTL styles are working properly is probably something like visual regression testing with various locales. 🤔 But that's not something we have the infrastructure for at the moment.

@walbo
Copy link
Member Author

walbo commented Aug 4, 2021

I agree with @sarayourfriend about the test. Adding ex a snapshot test will only confirm that the rtl function works and rtl has is own test that should throw if anything doesn't work. I can add a test tomorrow if you still want it.


Added rtl.watch() but seems like rtl still needs to be executed.

packages/components/src/flex/flex/hook.js:88:14 - error TS2769: No overload matches this call.
  Overload 1 of 2, '(template: TemplateStringsArray, ...args: CSSInterpolation[]): SerializedStyles', gave the following error.
    Argument of type '() => SerializedStyles' is not assignable to parameter of type 'CSSInterpolation'.
      Type '() => SerializedStyles' is missing the following properties from type 'ArrayCSSInterpolation': pop, push, concat, join, and 27 more.
  Overload 2 of 2, '(...args: CSSInterpolation[]): SerializedStyles', gave the following error.
    Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'CSSInterpolation'.
      Type 'TemplateStringsArray' is not assignable to type 'CSSObject'.
        Types of property 'filter' are incompatible.
          Type '{ <S extends string>(predicate: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (predicate: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is not assignable to type '"inherit" | "none" | (string & {}) | "-moz-initial" | "initial" | "revert" | "unset" | ("inherit" | "none" | (string & {}) | "-moz-initial" | "initial" | "revert" | "unset" | undefined)[] | Filter[] | undefined'.
            Type '{ <S extends string>(predicate: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (predicate: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is missing the following properties from type 'Filter[]': pop, push, concat, join, and 27 more.

@ciampo
Copy link
Contributor

ciampo commented Aug 4, 2021

To be fair we don't do these kinds of tests literally anywhere else in Gutenberg, do we?
...
I agree with @sarayourfriend about the test.

Sure, let's drop the unit tests.

Added rtl.watch() but seems like rtl still needs to be executed.

Is this Argument of type '() => SerializedStyles' is not assignable to parameter of type 'CSSInterpolation'. error happening even when the callback returned by rtl() is immediately executed (i.e. rtl({ ...styles })()) ? I can't see this error in the CI checks.

@walbo
Copy link
Member Author

walbo commented Aug 4, 2021

Is this Argument of type '() => SerializedStyles' is not assignable to parameter of type 'CSSInterpolation'. error happening even when the callback returned by rtl() is immediately executed (i.e. rtl({ ...styles })()) ? I can't see this error in the CI checks.

It work if I run rtl( { ...styles } )(), but without the final () (rtl( { ...styles } )) is fails. The PR still has the final () so not showing up here.

The example added in #33882 fails.

@ciampo
Copy link
Contributor

ciampo commented Aug 5, 2021

It work if I run rtl( { ...styles } )(), but without the final () (rtl( { ...styles } )) is fails. The PR still has the final () so not showing up here.

The example added in #33882 fails.

This is expected. As mentioned in an earlier comment, interpolated functions only work for styled components.

Since in this case we're using the css function, we have to keep the final () for it to work properly. We're mitigating this "limitation" by adding the rtl.watch() to the list of useMemo dependencies, to ensure that styles are recalculated in case the locale's writing direction changes.

@walbo
Copy link
Member Author

walbo commented Aug 5, 2021

Thanks for the clarification @ciampo

I belive this is ready then.

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.

Code changes LGTM and test well as per instructions! Thank you @walbo for working on this 🚀

One last thing, before merging — could you add an entry for this bug fix in the "Unreleased" section of packages/components/CHANGELOG.md?

@walbo walbo merged commit 8c623e2 into WordPress:trunk Aug 16, 2021
@walbo walbo deleted the fix/flex-rtl branch August 16, 2021 09:05
@github-actions github-actions bot added this to the Gutenberg 11.4 milestone Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[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.

5 participants