functions-each.less 1.74 KB
Newer Older
liang ce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
@selectors: blue, green, red;
@list: a b c d;

each(@selectors, {
  .sel-@{value} {
    a: b;
  }
});

.each {
  each(@list, {
    index+: @index;
    item@{index}: @value;
  });

  // nested each
  each(10px 15px, 20px 25px; { 
    // demonstrates nesting of each()
    each(@value; #(@v, @k, @i) {
      nest-@{i}-@{index}: @v @k;
    });
  });

  // nested anonymous mixin
  .nest-anon {
    each(a b, .(@v;@i) {
      each(c d, .(@vv;@ii) {
        nest-@{i}-@{ii}: @v @vv;
      });
    });
  }

  // vector math
  each(1 2 3 4, {
    padding+_: (@value * 10px);
  });
}

@set: {
  one: blue;
  // skip comments
  two: green;
  /** and these
  */
  three: red; //and this
}
.set {
  each(@set, {
    @{key}: @value;
  });
}
.set-2() {
  one: blue;
  two: green;
  three: red;
}
.set-2 {
  each(.set-2(), .(@v, @k, @i) {
    @{k}-@{i}: @v;
  });
}

.pick(@a) when (@a = 4) {
  val3: @a;
}
.single {
  each(true, {
    val: @value;
  });
  @exp: 1 + 1;
  each(@exp, {
    val2: @value;
  });
  each(1 2 3 4, {
    .pick(@value);
  });
}

@columns: range(4);
.column-list {
  list: @columns;
}

each(@columns, .(@val) {
  .col-@{val} {
    width: (100% / length(@columns));
  }
});

each(range(10px, 30px, 10px), .(@val, @index) {
  .row-@{index} {
    width: @val;
  }
});

@list: a b c d;
.box {
  each(@list, {
    -less-log: extract(@list, @index);
  })
}

// https://github.com/less/less.js/issues/3325

@color-schemes: {
  @primary: {
    @color: blue;
  }
  @secondary: {
    @color: red;
  }
}
.test {
  each(primary secondary, .(@color-name) {
    @scheme: @color-schemes[@@color-name]; // e.g. @color-name = primary
    color: @scheme[@color];
  });
}

@one: {
  @two: {
    foo: red;
    bar: blue;
  };
};

each(@one[@two], {
  .@{key} {
    content: @value;
  }
});