Skip to content

Instantly share code, notes, and snippets.

@watilde
Created March 24, 2015 07:15
Show Gist options
  • Save watilde/67df7206d1c1c04c3a56 to your computer and use it in GitHub Desktop.
Save watilde/67df7206d1c1c04c3a56 to your computer and use it in GitHub Desktop.
.className = {
text-align: center,
color: #000,
&.hasClassName: {
color: red
}
};
@watilde
Copy link
Author

watilde commented Mar 24, 2015

Sassで書いてもツラいところは同じことになりそうですね><
変数とテンプレートリテラル使って展開したら短く書けそうですね

e.g.

var child = function (n) { return `nth-child(${n})`; };
var after = "::after";
var notChild = function (n) { return `not(:nth-child(${n}))`; };

.className = {
  `.child${child(n)}`: { prop : value },
  `.child${after}`: { prop : value }
  `.child${notChild(n)})`: { prop : value }
};

CSSの文脈に限り、テンプレートリテラルに必要な ` を省略する機能も提供できそう

@oti
Copy link

oti commented Mar 24, 2015

LGTM!
変数を使うのはありですねー。でもJSってオブジェクトのキー側にfunctionは使えるんでしたっけ?
Sassだとインターポレーションでセレクタやプロパティに変数をうめこむのができますけども。

@watilde
Copy link
Author

watilde commented Mar 24, 2015

可能にします 😼

この構文をトランスパイルしてJSにして、実行結果のObjectをCSSにするイメージなので
なんとかできそう。あとは良い名前を考えてじわじわやっていきます〜

@oti
Copy link

oti commented Mar 24, 2015

可能にします 😼

カッコイイ...///

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment