classnames icon indicating copy to clipboard operation
classnames copied to clipboard

When no classes are selected, classNames should return null

Open Lovor01 opened this issue 2 years ago • 5 comments

ClassNames with no classes selected return empty string (''). For using it in React with jsx, it would be much better to return null. Example:

<p className={classNames({ first: false, second: false)>
Hello World!
</p>

This results in <p class>Hello World!</p>, while <p>Hello World!</p> would be correct. The second would happen if classNames returned null in such case.

Lovor01 avatar Jul 15 '23 09:07 Lovor01

This is a great idea, however considering this changes the public API, this must be considered a breaking change.

jonkoops avatar Dec 14 '23 16:12 jonkoops

I am adding this to the milestone for a potential version 3.0, which will allow us to introduce some breaking changes.

jonkoops avatar Dec 27 '23 17:12 jonkoops

@dcousens @JedWatson could I get your take on this enhancement? I think it might be a valuable addition, especially for React users, but the pain of having a return value that is possibly undefined might be problematic for others. I am leaning towards introducing this in v3, as we can always revert it as a non-breaking change in case it causes too much frustration for users.

jonkoops avatar Dec 27 '23 17:12 jonkoops

I'm not against this, it is a reasonable breaking change - but many usage patterns have assumed that concatenation is safe and this change might break that workflow quite substantially.

The fix for users should be as simple as ?? '', but, that's the argument against.

dcousens avatar Dec 28 '23 00:12 dcousens

The fix for users should be as simple as ?? '', but, that's the argument against.

Yeah, exactly, this could be an annoying fix to apply. I think we should still go for it, and base our decision on restoring it on user feedback. Perhaps we should roll a couple of beta versions so folks can try it out.

jonkoops avatar Dec 28 '23 11:12 jonkoops