When no classes are selected, classNames should return null
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.
This is a great idea, however considering this changes the public API, this must be considered a breaking change.
I am adding this to the milestone for a potential version 3.0, which will allow us to introduce some breaking changes.
@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.
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.
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.