lukeed / clsx

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

feat: support for using functions #87

Closed liuseen-l closed 8 months ago

liuseen-l commented 8 months ago

Sometimes we may encounter more complex scenarios, so we can support functions to deal with such scenarios

let z = 0
let className = clsx(() => {
  if(z === 0) {
     return 'foo'
  } else if (z === 1) {
    return 'baz'
  } else {
    return 'bar'
  }
})
lukeed commented 8 months ago
clsx({
  foo: z === 0,
  bar: z === 1,
  baz: z > 1 || z < 0,
})
// or
clsx(
  z === 0 ? "foo"
  : z === 1 ? "bar"
  : "baz"
);
// or
let z
clsx(
  (function() {
    if(z === 0) {
     return 'foo'
    } else if (z === 1) {
      return 'baz'
    } else {
      return 'bar'
    }
  })()
)