DEV Community

Cover image for Package: react-style-classes
Patrik Mäsiar
Patrik Mäsiar

Posted on

1 3

Package: react-style-classes

Every one of us knows the case when you need to add more class names to style your component depending on some conditions, but you do not want to have negative boolean values in className prop.

In Kontentino, we had the same issue.

So, what are you going to do if you need to solve a problem like this?
Probably, the first option that are you going to do is find the library which will help you.
Another option is to write your own library. This is what we did and also what we do! We develop our own libraries.

We wanted to solve the issue, that if we have class names depending on the condition of state like:

state = { isModalShown: false };
...
<div className={[!this.state.isModalShown && style.item, this.state.isModalShown && style.activeItem]} />
Enter fullscreen mode Exit fullscreen mode

The result of this example is ["item", false]. It is something that you do not want to have in className prop. So we needed to remove boolean values if it equals false, and put class name if the condition applies.

We coded our own package!

One of our first libraries is named react-style-classes and this library solves the problem with boolean values in an array of styles. It is a package for merge arguments of component style class names.

If you are interested in how we wrote this helper, you can join us on Github. https://github.com/kontentino/react-style-classes

More interesting articles coming soon at kontentino.engineering

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay