lukeed/clsxPublic

A tiny (239B) utility for constructing `className` strings conditionally.

Combine variables without spaces

LO

Hi! Is there a way to have conditional variables add onto the class string without spaces? If there is a way, I'm not understanding implementation from documentation. Want to update the state, but cannot do this if a spaces exists. Current expected behavior: ``` <div className={ clsx( 'foo barr example_', { 'zoo': !place, [place as string]: place } )}> // 'foo barr example_ zoo' ``` Desired behavior: ``` <div className={ clsx( 'foo barr', 'example_' + { 'zoo': !place, [place as string]: place } )}> desired output // 'foo barr example_zoo' actual output // 'foo barr example_[Object Object]' ``` I can have a crack at contributing if something like this doesn't exist! Thanks for any info or for a work around.

  1. LU

    You have to wrap the object in a clsx call. What you pasted in the second snippet natively evaluates to “example_[object Object]” before clsx even sees it

  2. LO
    Author

    Thanks for the quick response! Brilliant, I didn't realize that was possible. This worked like a charm: ``` <div className={ clsx( 'foo barr', 'example_' + clsx({ 'zoo': !place, [place as string]: place }) )}> output // 'foo barr example_zoo' ```

  3. LU

    Haha great, glad that was it

  4. lukeedclosed4 years ago on GitHub

  5. longevitytinareferenced this in feat: add readme update #564 years ago on GitHub