typestyle / typestyle

Making CSS Typesafe 🌹
https://typestyle.github.io
MIT License
3.07k stars 87 forks source link

New feature: custom prefixes for class names #39

Closed mzygmunt closed 7 years ago

mzygmunt commented 7 years ago

Something like this:

const gridStyle: string = style({
    color: "red"
}, {prefix: "grid"});

Then the style name in the HTML code would look like this:

<div class="grid_f1d5jrmm">
...
</div>

It will be useful in some cases, because the source of the page will be more readable.

basarat commented 7 years ago

Don't use classNames to do debugging. Two ways:

Hope that helps 🌹

rcchen commented 7 years ago

So I have a different use case for this, that is not debugging related.

Let's say for example that I have an app with two components from independent packages that both use Typestyle. Is there some way to ensure that there aren't class name collisions between two components, which were styled with different instances of Typestyle?

My first instinct was to add a prefix somehow, or (potentially more awesomely) there's a guarantee that any permutation of styles maps to a unique class name.

Curious to hear thoughts on this.

basarat commented 7 years ago

which were styled with different instances of Typestyle

Have typestyle as a peerDependency :rose:

notoriousb1t commented 7 years ago

The underlying hashing function for generating classnames is djb2. It uses the property names and values of the style to create a unique class name. While the chances of class name collisions in a single instance of TypeStyle is very very small, that algorithm is not guaranteed to be collision-free. This is a trade-off between speed and exactness.

If two styles were to collide for some reason, adding a custom property or using the $debugName property would resolve the issue. A custom property would change the resulting hash, and $debugName is a prefix to the hashname, so that would resolve it as well.

Adding a second TypeStyle instance does not increase the chance of a collision.

rcchen commented 7 years ago

OK sounds like we'll be ok then, thanks for the guidance @basarat @notoriousb1t 😄