andreypopp / react-css-components

Define React presentational components with CSS
677 stars 23 forks source link

Support variables #11

Open gingur opened 8 years ago

gingur commented 8 years ago

This looks great, any plans for variable support?

Label {
  color: @label.color
}

Label:hover {
  color: @label.hover
}

or

Label {
  color: @prop:color;
}

Label:hover {
  color: @prop:hover;
}
andreypopp commented 8 years ago

Interesting!

So there are two types of variables I can see could be implemented.

Compile time variables

Those are only available at compile time (like LESS/SASS variables). We can get the for free by using some PostCSS plugin or by using LESS/SASS directly. So there's not much work to be done here, we need just to document how to insert PostCSS plugins in the pipeline or how to use LESS/SASS with react-css-components

Runtime variables

Those are more interesting. Basically we want some styles be influenced by variables supplied in runtime. That means that we need to split some styles from being statically compiled into CSS to styles set via style= prop.

Your example:

Label {
  color: @prop:color;
}

Label:hover {
  color: @prop:hover;
}

show probably exactly that usage scenarios for variables, right?

I'd go with a more standard syntax though:

Label {
  color: prop(colorRegular)
}

Label:hover {
  color: prop(colorHover, 'red')
}

Then usage:

<Label colorRegular="#000" /> // colorHover is not provided, used "red" as default
<Label colorRegular="#000"  colorHover="yellow" />

The issues with that approach are:

gingur commented 8 years ago

yep, or instead of "prop" could use "var" which is following https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables

maybe a radium like approach to inject an actual style tag into the doucument could help the specificity issue of inline styles?

vyorkin commented 8 years ago

I'm also interested in this feature, at least in Compile time variables. Concerning Runtime variables I think prop could be a good name, because it reflects that value comes from a component property and not from css (PostCSS) variables.

For now it would be great to be able to do smth like this:

@import "styles/theme";

/* stylelint-disable */

Button {
  base: button;
  background-color: var(--bg1);

  &:hover {
    background-color: var(--color-danger);
  }
}

/* stylelint-enable */

(but I have no idea how to implement this myself)

andreypopp commented 8 years ago

@vyorkin compile time variables are already supported via sass/less/stylus or postcss. See the example with sass: https://github.com/andreypopp/react-css-components/tree/master/examples/sass

caesarsol commented 7 years ago

Two observations:

This library seems to be so much better than styled-components, which is recently trending. CSS modules for the win! :)

andreypopp commented 7 years ago

compile time variables: I always wanted to get them from a JS file, do you think that's possible with this library + postcss?

Yep! See examples, you can use any postcss plugin with it. Just add postcss-loader.