Skip to content

Extend className manipulation #12099

Closed
Closed
@romulof

Description

@romulof

Currently we're doing string manipulation outside React (Starring classnames) and it works perfectly, but after a while writing code, it feels a little verbose:

<div className={classnames({ ... })}> ... </div>

The prop className is a plain string because it's directly mapped to DOM Element property.

Let's not create a polymorphic Frankenstein here. Keep it as close to pure JS/DOM as possible. My suggestion is not about changing, but adding.

Since a long time ago (not for Microsoft), DOM Element has the classList method. It works greatly for direct manipulation, but it wouldn't fit JSX declarative syntax.

How about extending the components with a classMap prop, that would work pretty much like classnames, mapping class names keys to conditional values.

Upon rendering that component, React would merge classNames and classMap into a single string to be passed down the render as the good old plain string classNames.

Angular has something like this for a long time:

  • class prop is a plain string;
  • ngClass directive makes the magic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions