補間
Stylusは、式を囲むために`{}`文字を使用することで補間をサポートします。式は識別子の一部になります。たとえば、`-webkit-{'border' + '-radius'}`は`-webkit-border-radius`と評価されます。
この素晴らしい使用例は、ベンダープレフィックスでプロパティを拡張することです。
vendor(prop, args)
-webkit-{prop} args
-moz-{prop} args
{prop} args
border-radius()
vendor('border-radius', arguments)
box-shadow()
vendor('box-shadow', arguments)
button
border-radius 1px 2px / 3px 4px
vendor(prop, args)
-webkit-{prop} args
-moz-{prop} args
{prop} args
border-radius()
vendor('border-radius', arguments)
box-shadow()
vendor('box-shadow', arguments)
button
border-radius 1px 2px / 3px 4px
出力
button {
-webkit-border-radius: 1px 2px / 3px 4px;
-moz-border-radius: 1px 2px / 3px 4px;
border-radius: 1px 2px / 3px 4px;
}
button {
-webkit-border-radius: 1px 2px / 3px 4px;
-moz-border-radius: 1px 2px / 3px 4px;
border-radius: 1px 2px / 3px 4px;
}
セレクタの補間
補間はセレクタでも機能します。たとえば、以下に示すように、テーブルの最初の5行の`height`プロパティを割り当てるために反復処理を行うことができます。
table
for row in 1 2 3 4 5
tr:nth-child({row})
height: 10px * row
table
for row in 1 2 3 4 5
tr:nth-child({row})
height: 10px * row
出力
table tr:nth-child(1) {
height: 10px;
}
table tr:nth-child(2) {
height: 20px;
}
table tr:nth-child(3) {
height: 30px;
}
table tr:nth-child(4) {
height: 40px;
}
table tr:nth-child(5) {
height: 50px;
}
table tr:nth-child(1) {
height: 10px;
}
table tr:nth-child(2) {
height: 20px;
}
table tr:nth-child(3) {
height: 30px;
}
table tr:nth-child(4) {
height: 40px;
}
table tr:nth-child(5) {
height: 50px;
}
また、文字列を構築して複数のセレクタを1つの変数にまとめて、それらを適切な場所に補間することもできます。
mySelectors = '#foo,#bar,.baz'
{mySelectors}
background: #000
mySelectors = '#foo,#bar,.baz'
{mySelectors}
background: #000
出力
#foo,
#bar,
.baz {
background: #000;
}
#foo,
#bar,
.baz {
background: #000;
}