Stylus is an awesome CSS pre-processor, which provides much more concise syntax and more powerful feature than its competitors, such as LESS or SCSS.
But now, with more and more features added into Stylus, it seems its syntax become over-weighted. Pitfall come up.
I wish to declare an array of values for box-shadow property. And I can reference them with index:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop-shadows = [
02px10px0 rgba(0, 0, 0, 0.16),
06px20px0 rgba(0, 0, 0, 0.19),
017px50px0 rgba(0, 0, 0, 0.19),
025px55px0 rgba(0, 0, 0, 0.21),
040px77px0 rgba(0, 0, 0, 0.22)
]
drop-shadow(n)
box-shadow shadows[n]
foriin (1..5)
.drop-shadow-{i}
drop-shadow(i)
And expect it generates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.drop-shadow-1 {
box-shadow: 02px10px0rgba(0, 0, 0, 0.16);
}
.drop-shadow-2 {
box-shadow: 06px20px0rgba(0, 0, 0, 0.19);
}
.drop-shadow-3 {
box-shadow: 017px50px0rgba(0, 0, 0, 0.19);
}
.drop-shadow-4 {
box-shadow: 025px55px0rgba(0, 0, 0, 0.21);
}
.drop-shadow-5 {
box-shadow: 040px77px0rgba(0, 0, 0, 0.22;
}
But I found there is not such thing called Array in Stylus!!!! There is only Hash, and Hash doesn’t accept number as key! So finally, I come up something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop-shadows = {
'1': 02px10px0 rgba(0, 0, 0, 0.16),
'2': 06px20px0 rgba(0, 0, 0, 0.19),
'3': 017px50px0 rgba(0, 0, 0, 0.19),
'4': 025px55px0 rgba(0, 0, 0, 0.21),
'5': 040px77px0 rgba(0, 0, 0, 0.22)
}
drop-shadow(n)
box-shadow shadows[n+'']
foriin (1..5)
.drop-shadow-{i}
drop-shadow(i)
In this piece of code, there are a bunch of pitfalls:
Hash doesn’t accept number as key. So 1: 0 2px 10px 0 rgba(0, 0, 0, 0.16) cause compile error.
'1' != 1, so drop-shadows[1] returns null
There is no type conversion function in Stylus, use the same trick as JavaScript. ''+n convert n into string.
Just found Stylus provides something called List, which is pretty much similar to what array in other languages, except the syntax.