Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic fragments in a class name #45

Closed
peterszerzo opened this issue Sep 5, 2015 · 6 comments
Closed

Dynamic fragments in a class name #45

peterszerzo opened this issue Sep 5, 2015 · 6 comments
Labels

Comments

@peterszerzo
Copy link

On a project I am currently working on, I encounter some situations where a class name is built up dynamically based some variable, for example: 'help help--' + props.position, resulting in help help--left, help help--top, help help--right. This library would make this code much more expressive if it supported a syntax such as the following:

var cls = classNames({
    'help': true,
    'help--${0}': [ props.position ]
}, { interpolate: true });

The character 0 inside the syntax notation would indicate that the 0th (first) element of the array should be used.

To improve performance, this behavior can be switched off by not setting the interpolate option to true (no interpolation by default).

Is there an interest to have a feature like this in the library (if so, I am happy to implement it)? Any ideas for a better syntax?

@rbsmidt
Copy link

rbsmidt commented Sep 8, 2015

Hi Pickled-plugins

I'm looking for the exact same in a project, that i'm working on. So yes - there's interest in such a feature, at least from me :-)

Other than that, i'm very pleased with classNames, so thanks to the author for a nice piece of work.

@dcousens
Copy link
Collaborator

dcousens commented Sep 8, 2015

Why not just

classnames({
    'help': true,
    `help--${props.position}`: !!props.position
})

This would already work in ES6 AFAIK.

@rbsmidt
Copy link

rbsmidt commented Sep 8, 2015

Might be a possibility - however i'm currently using JSX with watch-compiler but not ES6.
My issue is, that i want a category-label as a classname in addition to other dynamic classes (active-state e.g.). So what i want to do is:
var classes = classNames({ 'active': active, //where active is a bool {this.props.cat.slug}: true })

I'm still new to React, so maybe i'm trying to use the wrong tool for this operation, or maybe i just doesn't use it right. But if i try your suggestion, my JSX watch task gives me following error:

Parse Error: Line xxx: Unexpected token {

If i wrap the category slug ref in quotes, it obviously just treats my var ref as a string.
Am i trying to bend classNames in directions, that i shouldn't?

@longlho
Copy link
Contributor

longlho commented Sep 8, 2015

@rbsmidt you can read up on template string and have babel compiles your stuff

@dcousens
Copy link
Collaborator

dcousens commented Sep 8, 2015

Closing, this is easily solved by just using ES6.
If you really need it, you can still solve the solution in a classical way.

No point special casing this library just for this.

@dcousens dcousens closed this as completed Sep 8, 2015
@peterszerzo
Copy link
Author

@dcousens thank you for the tip. I did not know that interpolation worked for object keys, which makes your solution the best way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants