2017-01-17 3 views
0

Ich erstelle Hilfsklassen, die die Farbe in vordefinierte Variablen ändern. In den meisten Fällen ist der Code derselbe. Ich werde spezifischere Selektoren hinzufügen, wie kann ich das einfacher machen?Sass - Wiederholungscode, wie kann ich das prägnanter machen

.fg-text { 
    color: $colorText; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
    color: $colorText; 
    border-color: $colorText; 
    } 
} 
.fg-foreground { 
    color: $colorForeground; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
    color: $colorForeground; 
    border-color: $colorForeground; 
    } 
} 
.fg-alternate { 
    color: $colorAlternate; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
    color: $colorAlternate; 
    border-color: $colorAlternate; 
    } 
} 
.fg-background { 
    color: $colorBackground; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
    color: $colorBackground; 
    border-color: $colorBackground; 
    } 
} 
.fg-highlight { 
    color: $colorHighlight; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
    color: $colorHighlight; 
    border-color: $colorHighlight; 
    } 
} 
+1

Sie können eine mixin machen. – sobolevn

+0

Ja, ich denke, das ist ziemlich offensichtlich. Ich habe einige Leute gesehen, die mit Gittern komplexe Dinge gemacht haben und wahrscheinlich übertrieben haben. – Buts

Antwort

1

Sie einen @each directive

@each $name, $color in (text: $colorText, foreground: $colorForeground ...) { 
    .fg-#{$name} { 
    color: $color; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
     color: $color; 
     border-color: $color; 
    } 
    } 
} 
verwenden könnte
+1

Zusätzliche Anmerkung: Sie können möglicherweise die Dinge noch weiter vereinfachen, indem Sie die Farbe nur für das Element '.fg-XYZ' festlegen und den Wert 'currentColor' für die Farbe und die Rahmenfarbe der Schaltflächen und Formularfelder verwenden. Siehe: https://css-tricks.com/currentcolor/ –

0

Ich habe es zu einem mixin kondensiert:

@mixin fg($name, $color) { 
    .fg-#{$name} { 
    color: $color; 
    button, .button, 
    input[type="button"], 
    input[type="submit"], 
    input[type="reset"], 
    select { 
     color: $color; 
     border-color: $color; 
    } 
    } 
} 
@include fg(brand, $colorBrand); 
@include fg(foreground, $colorForeground); 
@include fg(background, $colorBackground); 
@include fg(text, $colorText); 
@include fg(alternate, $colorAlternate); 
@include fg(highlight, $colorHighlight); 
Verwandte Themen