Skip to content

Resolves #37695 - Use clsx instead of classnames#37708

Merged
tbradsha merged 15 commits into
trunkfrom
fix/37695/classnames_to_clsx
Jun 10, 2024
Merged

Resolves #37695 - Use clsx instead of classnames#37708
tbradsha merged 15 commits into
trunkfrom
fix/37695/classnames_to_clsx

Conversation

@tbradsha

@tbradsha tbradsha commented Jun 4, 2024

Copy link
Copy Markdown
Contributor

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

@anomiex anomiex left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread projects/plugins/jetpack/extensions/blocks/videopress/deprecated/v2/utils.js Outdated
Comment thread projects/plugins/jetpack/extensions/blocks/videopress/deprecated/v2/utils.js Outdated
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>

@anomiex anomiex left a comment

Copy link
Copy Markdown
Contributor

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

DaniGuardiola commented Jun 5, 2024

Copy link
Copy Markdown

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

anomiex commented Jun 5, 2024

Copy link
Copy Markdown
Contributor

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

DaniGuardiola commented Jun 5, 2024

Copy link
Copy Markdown

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

anomiex commented Jun 5, 2024

Copy link
Copy Markdown
Contributor

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

tbradsha commented Jun 5, 2024

Copy link
Copy Markdown
Contributor Author

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

@renatoagds renatoagds left a comment

Copy link
Copy Markdown
Contributor

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
@tbradsha tbradsha deleted the fix/37695/classnames_to_clsx branch June 10, 2024 16:06
@github-actions github-actions Bot removed [Status] Needs Review This PR is ready for review. [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jun 10, 2024
@tbradsha

tbradsha commented Jun 11, 2024

Copy link
Copy Markdown
Contributor Author

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 Info [Block] Donations [Block] Eventbrite [Block] Form Form block (also see Form package label) [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] Subscribe [Block] Tiled Gallery [Block] Top Posts [Block] VideoPress [Extension] AI Assistant Plugin [Extension] Publicize Block editor plugin [Extension] SEO [Feature] Forms [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