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

Animate: Use CSS-in-JS #25754

Closed
wants to merge 5 commits into from
Closed

Animate: Use CSS-in-JS #25754

wants to merge 5 commits into from

Conversation

sarayourfriend
Copy link
Contributor

@sarayourfriend sarayourfriend commented Sep 30, 2020

Description

This PR updates the Animate component to use CSS-in-JS similar to how newer components do like the AnglePickerControl and Text.

How has this been tested?

I've compared my changes in storybook to the production storybook and confirmed there are no behavioral or visual differences.

Types of changes

This might introduce a breaking change in that the old classnames will no longer appear on the animated component–however it seems to me like it would be an anti-pattern to target generated classnames rather than adding ones own classnames, so I think we should accept this (if it is indeed a breaking change).

This also deprecates the middle value for the yAxis part of origin for the appear type of animation. I chose to do this because it simplified the logic for selecting the correct Y and X axis of transform-origin while also getting rid of a non-standard value for origin that wasn't even being used. Now any valid transform-origin value is actually going to be used correctly when passed as origin. I thought for a minute about removing the little parsing logic we do; however, this logic maintains some specific default values for the Y and X axis so I chose to keep it in.

Otherwise this PR maintains the ability for children to be a function and keeps the className argument null-safe.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • 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.

@sarayourfriend sarayourfriend marked this pull request as ready for review September 30, 2020 22:01
@sarayourfriend
Copy link
Contributor Author

/cc @ItsJonQ

[ 'is-from-' + yAxis ]: yAxis !== 'middle',
} ),
} );
if ( yAxis === 'middle' ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this value was used for consistency with the Popover position props but I may be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, should it be kept and still filtered out then? It does appear to line up with the Popover position props.

@youknowriad youknowriad added [Feature] UI Components Impacts or related to the UI component system [Type] Code Quality Issues or PRs that relate to code quality labels Oct 1, 2020
@youknowriad
Copy link
Contributor

I've seen that you opened two PRs about CSS in JS refactors, I have nothing against these but I'm curious about the motivation.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Did we try on RTL locales, I know CSS-in-JS need special care there compared to CSS.

@sarayourfriend
Copy link
Contributor Author

@youknowriad Maybe I should open an issue to open discussion about this, but my motivation here is to make it possible to progressively load the CSS for each of these components. Right now the CSS for the entire package is bundled as a single file during the build, rather than allowing each component to progressively load the styles as needed. By moving the styles into CSS-in-JS it will make it possible for the styles for each component to be reduced (which is a benefit even if for some reason we cannot eventually fully remove the built stylesheet).

Does this need more discussion before I can continue this work, shall I open an issue? My understanding is that this was already the direction we were heading in with newer components, so I figured I would take advantage of that and uplift as many of the existing ones as I could.

@ItsJonQ
Copy link

ItsJonQ commented Oct 1, 2020

@saramarcondes Haiii!!

Thank for you for helping out with this! ❤️
To touch upon @youknowriad 's point with RTL...

⏭ ⏪ RTL Handling

As a whole, RTL in CSS-in-JS is tricky, and isn't standardized (compared to other CSS-in-JS approaches).

The current CSS-in-JS implementation within Gutenberg is incomplete - at least compared to Sass which includes RTL conversion via PostCSS during build.

With runtime compiled styles (e.g. Emotion or Styled Components), we have the ability to adjust styles using a "plugin" system (not unlike how PostCSS does this with modules).

Ideally, the CSS-in-JS solution within Gutenberg would have this setup, so that we don't have to manually do things when we need to write styles.

An automated solution may look similar to the one I've added for G2 Components:
https://github.com/ItsJonQ/g2/blob/master/packages/create-styles/src/createCompiler/plugins/index.js#L14

Note: It's using CSS Janus, one of the go to solutions for RTL conversion. Unfortunately, it's not GPL compatible... so we'll need to figure something out there 🙃. I'd try to write one myself, but my REGEX game is weak sauce 😂 .

🤷‍♀️ So, what's to do?

Since this auto adjustment mechanic is missing... I've added an [rtl](https://github.com/WordPress/gutenberg/blob/master/packages/components/src/utils/rtl.js#L84) function within the Gutenberg project that can help with this.

It gets us over the hump of not having RTL handling at all. However, you can imagine it's quite cumbersome and "verbose" - not too pleasant from a developer experience perspective.


Hopefully this helps! ❤️

I can help submit some code tomorrow with the rtl functions in there.
Although... I'm not sure if it's smart enough to understand translateX based values (negative vs positive) 🤔

(It was a simple and naive solution. Apologies! 🙏 )

@youknowriad
Copy link
Contributor

@saramarcondes Thanks for the explanation, I don't think an issue is needed, I was just curious and this is one of the main reasons we decided to use CSS-in-JS in components so I think these efforts are good in general.

Aside: It seems the bundle size check only works for folks with the rights in the repo (not for fork PRs) and I'm curious to see the impact of these PRs. I don't expect it to create a big difference but just want to at least have an idea.

@sarayourfriend
Copy link
Contributor Author

It seems the bundle size check only works for folks with the rights in the repo (not for fork PRs) and I'm curious to see the impact of these PRs

@youknowriad Any suggestions for how we can address that?

@sarayourfriend
Copy link
Contributor Author

@ItsJonQ I took a shot at trying to make this RTL safe, maybe you can review and let me know if I'm on the right track?


${ reduceMotion( 'animation' ) }

transform-origin: ${ ( p ) => p.yAxis } ${ getRTLSafeXAxis };
Copy link

Choose a reason for hiding this comment

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

@saramarcondes Haii! It looks like you have the RTL figured out :).

My suggestion here would be to abstract the transform-origin logic into another function.
My thinking for that is because having all inline is tricky to read (at least for me).

Abstracting it will allow us to write something like this:

${ appearTransformOrigin };

(or something similar)

And the prop interpolation + RTL would be handled in that :)

The same could apply for the SliderWrapper.

P.S. I found that outputting the styles in the abstracted function works best using the css function from emotion, example:

https://github.com/WordPress/gutenberg/blob/master/packages/components/src/input-control/styles/input-control-styles.js#L148

It seems to play nicer with editors, prettier, and linters.

Note: It could be used with string interpolation or as a function css({ ...}).

<3

@ItsJonQ
Copy link

ItsJonQ commented Oct 2, 2020

@ItsJonQ I left a comment for you! Thanks again for your efforts 🙏

Copy link

@ItsJonQ ItsJonQ left a comment

Choose a reason for hiding this comment

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

🚀 from me! Thank you for working on this @saramarcondes (and for following up with the suggestions!) <3

@sarayourfriend
Copy link
Contributor Author

Closing this PR as this approach is no longer possible given the recent refactor of this component in #26063

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants