lukeed / clsx

A tiny (239B) utility for constructing `className` strings conditionally.
MIT License
8.28k stars 143 forks source link

Empty `class` attribute due to empty string #45

Closed felixmosh closed 2 years ago

felixmosh commented 2 years ago

Hi,

First of all thank you for this awesome package 🙏🏼.

I've noticed that when the calculation of clsx returning an empty string, React renders an empty class attribute.

image A working example: https://codesandbox.io/s/clsx-empty-string-bug-rrqu40

Should I make a PR for returning undefined in case of an empty string?

lukeed commented 2 years ago

Hey thanks,

no this is intentional as clsx is meant to be a 1:1 drop in replacement for the classnames package, which has been heavily utilized by the react ecosystem for years. Plus if you really want to hide the attr, you can wrap the library as your own utility:

import clsx from 'clsx';

export default function () {
  return clsx.apply(0, arguments) || undefined;
}

Hope that helps!