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

Resolves #37695 - Use clsx instead of classnames #37708

Merged
merged 15 commits into from
Jun 10, 2024

Conversation

tbradsha
Copy link
Contributor

@tbradsha tbradsha commented Jun 4, 2024

Resolves #37695 - Use clsx instead of classnames

Proposed changes:

clsx is faster and smaller than classnames. Also, it is now used in Calypso (Automattic/wp-calypso#91408), in Gutenberg (WordPress/gutenberg#61138), and in @wordpress packages.

For consistency, this PR matches those changes:

Caveats:

  • jetpack-mu-wpcom has its own implementation of classNames; I've left that intact for now. If desired, we can take a look in a separate PR.
  • @automattic/social-previews is a dependency, and the latest published version (2.0.1-beta.13) still has a classnames dependency. This was removed in the aforementioned Calypso PR, but as of yet has not been published.

Internal reference: p4TIVU-aYc-p2

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

Check the code for any typos or mistakes. Pay particularly close attention to the dedupe refactor.

@tbradsha tbradsha force-pushed the fix/37695/classnames_to_clsx branch from cd2abf7 to 7fd3546 Compare June 5, 2024 14:40
@tbradsha tbradsha enabled auto-merge (squash) June 5, 2024 16:21
Copy link
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. A few suggestions for simplifying the dedupe replacement inline.

tbradsha and others added 2 commits June 5, 2024 11:20
Co-authored-by: Brad Jorsch <anomiex@users.noreply.github.com>
Co-authored-by: Brad Jorsch <anomiex@users.noreply.github.com>
Copy link
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

LGTM. Not hitting "Approve" just yet to give Agora a chance to review.

@DaniGuardiola
Copy link

DaniGuardiola commented Jun 5, 2024

Just a quick drive-by comment, I was the one who originally started this transition and was personally responsible for the Gutenberg PR. I managed to replace the dedupe usages without much complex logic or the new dependency you're using here. The code affected seems very similar, probably copy-pasted. I suggest you take a look at what I did there in case it helps you simplify this PR and achieve better consistency.

Note that I don't remember the exact approach I took, but I might have decided to simply not dedupe at all in some cases, since it doesn't really affect any functionality and the trade-off of doing the computation is probably not worth it in the first place.

Just a suggestion :) thanks for applying this transition here <3

@anomiex
Copy link
Contributor

anomiex commented Jun 5, 2024

but I might have decided to simply not dedupe at all, since it doesn't really affect any functionality

You may want to double-check that.

$ node -e 'const classnames = require( "classnames" ); console.log( classnames( "aa bb cc", { bb: false } ) );'
aa bb cc
$ node -e 'const clsx = require( "clsx" ); console.log( clsx( "aa bb cc", { bb: false } ) );'
aa bb cc
$ node -e 'const classnames = require( "classnames/dedupe" ); console.log( classnames( "aa bb cc", { bb: false } ) );'
aa cc

@DaniGuardiola
Copy link

DaniGuardiola commented Jun 5, 2024

Ah, yep I replaced that behavior with a very very simple alternative. If you dig in my PR you'll find my approach. With that sentence in my comment I meant cases where the resulting class name is something like "class class".

@anomiex
Copy link
Contributor

anomiex commented Jun 5, 2024

Glancing at your PR, the following seem likely to be broken to me

Also, in https://github.com/WordPress/gutenberg/blob/200f7c7a210c35eb9c004da9a3e4f1062f471d57/packages/block-library/src/embed/util.js#L191-L193 you might mangle things if a class in outputClassNames has one of the classes in aspectRatioClassNames as a substring. For example, pass in "foo wp-has-aspect-ratio-xxx bar", you'll get back "foo -xxx bar".

@tbradsha
Copy link
Contributor Author

tbradsha commented Jun 5, 2024

I managed to replace the dedupe usages without much complex logic or the new dependency you're using here.

The one dedupe instance here required negation (removal of various classes). We don't use clsx or classnames for it. I'm not sure what new dependency you're referring to?

The code affected seems very similar, probably copy-pasted. I suggest you take a look at what I did there in case it helps you simplify this PR and achieve better consistency.

I can't speak to the origin of the code, but in this case it was an independent refactor; looking now at that other PR it seems the goal is slightly different, but luckily it isn't too complex!

renatoagds
renatoagds previously approved these changes Jun 7, 2024
Copy link
Contributor

@renatoagds renatoagds left a comment

Choose a reason for hiding this comment

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

Just got an eyes on VideoPress and AI stuffs. LGTM

@tbradsha tbradsha merged commit 40a8492 into trunk Jun 10, 2024
71 of 72 checks passed
@tbradsha tbradsha deleted the fix/37695/classnames_to_clsx branch June 10, 2024 16:06
@github-actions github-actions bot removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jun 10, 2024
dilirity pushed a commit that referenced this pull request Jun 11, 2024
* Add rule to disallow `classnames`

* Initial pass

* `classnames/dedupe` → `@wordpress/token-list`

* Add changelog

* Use valid changelog types

* Remove file from eslint exclusions

* Change post-rebase use of classnames

* More changelog format compliance fixes

* Bump versions
@tbradsha
Copy link
Contributor Author

tbradsha commented Jun 11, 2024

Internal announcement: pdWQjU-Mt-p2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Admin Page React-powered dashboard under the Jetpack menu [Block] AI Assistant [Block] Blogging Prompt [Block] Blogroll [Block] Business Hours [Block] Button [Block] Calendly [Block] Contact Form Form block (also see Contact Form label) [Block] Contact Info [Block] Donations [Block] Eventbrite [Block] GIF [Block] Google Docs Embed [Block] Latest Instagram Posts [Block] Mailchimp [Block] Map [Block] Markdown [Block] OpenTable [Block] Paid Content aka Premium Content [Block] Pay With Paypal aka Simple Payments [Block] Payment Buttons [Block] Podcast Player [Block] Repeat Visitor [Block] Send a message [Block] Sharing Button [Block] Sharing Buttons [Block] Slideshow [Block] Story [Block] Subscriptions [Block] Tiled Gallery [Block] Top Posts [Block] VideoPress [Extension] AI Assistant Plugin [Extension] Publicize Block editor plugin [Extension] SEO [Feature] Contact Form [JS Package] AI Client [JS Package] Components [JS Package] Connection [JS Package] Licensing [JS Package] Partner Coupon [JS Package] Publicize Components [Package] Ad aka WordAds [Package] Forms [Package] My Jetpack [Package] Search Contains core Search functionality for Jetpack and Search plugins [Package] VideoPress [Plugin] Automattic For Agencies Client [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] CRM Issues about the Jetpack CRM plugin [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Social Issues about the Jetpack Social plugin RNA [Type] Janitorial
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Monorepo tooling: replace classnames with clsx
6 participants