classnames lib - Object Syntax
See original GitHub issueThis isn’t a bug or a problem that requires fixing.
I noticed that this project uses classnames
, which is great, but in many places the project is using ternary operators for toggling classes when the object style eliminates this need completely.
Example:
const componentClassnames = classnames([
styles.component,
isVisible ? styles.isVisible : null,
flipHorizontal ? styles.flipHorizontal : null,
flipVertical ? styles.flipVertical : null,
]);
- could instead be
const componentClassnames = classnames(styles.component, {
[styles.isVisible]: isVisible,
[styles.flipHorizontal]: flipHorizontal,
[styles.flipVertical]: flipVertical,
});
when classnames
receives an object it concatenates the keys for any values that are true.
I think the latter reads much cleaner.
This isn’t critical but I figured it was worth mentioning. If there was a specific reason this style was avoided, then I apologize in advance. If this is something everyone likes, I will be happy to put together a PR for these changes.
Love all the work 🙂
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
classnames - npm
A simple utility for conditionally joining classNames together. Latest version: 2.3.2, last published: 3 months ago. Start using classnames ...
Read more >You don't know the classNames library - Arek Nawo
The usefulness of the classNames library comes from its syntax. All it is is a single function, taking different types of values as ......
Read more >classnames-bind.md - gists · GitHub
Thankfully, the classnames library ships with an alternative method for ... That syntax is the only way to reference object keys with disallowed...
Read more >Classnames by JedWatson
A simple javascript utility for conditionally joining classNames together. Install with npm or Bower. ... The classNames function takes any number of arguments ......
Read more >Using the classnames library for conditional CSS in React
Classnames is a simple yet versatile javascript utility that joins CSS class names based on a set of conditions.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@daniloprates yes I think so
@lrosa007 that’s an awesome contribution. Is the PR ready to be reviewed?
Thanks, Danilo