Simplifying Repetitive LESS

前端 未结 1 734
萌比男神i
萌比男神i 2020-12-21 13:01

I am creating a themeing system for a WordPress network that supports multiple layout themes that can support color schemes for a variety of universities. To do so, I perio

1条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 13:49

    You can simplify it by making use of array loops. All you have to modify in case of a new addition would be to modify the array variables at the end.

    .loop-column(@index) when (@index > 0) { /* Recursive Mixin with Guard condition. Mixin is processed only when the condition is satisfied */
      .loop-column(@index - 1); /* Call the mixin again with a decremented counter */
      @ctype:  extract(@type, @index); /* Extract the type value corresponding to the index from the array */
      @color:  extract(@colors, @index); /* Extract the color value corresponding to the index from the array */
    
      /* Form and Output the necessary classes and properties */
      .@{ctype}-lighter-text { /* Selector interpolation to dynamically form the selector */
        color: lighten(@color,20);
      }
      .@{ctype}-bg {
        background-color: @color;
      }
      .@{ctype}-border{
        border-color: @color;
      }  
    }
    
    .loop-column(length(@type));
    
    @type: primary, sec, tert; /* The color types array */
    @colors:#fff, #777, #000; /* The color value array for each type */
    /* If required the colors can be kept as separate variables also. Refer 2nd demo. */
    

    Demo | Demo 2

    Update: (Based on comments from Andrew Cafourek and seven-phases-max)

    Since LessPHP is outdated, the following line should be added and the length(@type) should be replaced with the actual count.

    .loop-column(0) {};
    .loop-column(4);
    

    0 讨论(0)
提交回复
热议问题