Performance: @if conditional logic
This affects CSS compilation time and the size of the generated CSS file, impacting initial page load speed.
@mixin button-style($type) { color: black; background: white; @if $type == 'primary' { background: blue; color: white; } @else if $type == 'secondary' { background: gray; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); }
@mixin button-style($type) { color: black; background: white; @if $type == 'primary' { background: blue; color: white; } @if $type == 'secondary' { background: gray; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using multiple @if blocks generating duplicated CSS | N/A | N/A | Higher due to larger CSS | [X] Bad |
| Using @if with else-if to generate minimal CSS | N/A | N/A | Lower due to smaller CSS | [OK] Good |
@if directive do in Sass?@if@if directive checks a condition and applies styles only when that condition is true.@for or @each, importing with @import, and variables are defined with $.@if in Sass?== to compare values in @if conditions, not =, ===, or =>.@if $variable == value { ... } without parentheses around the condition.$theme: dark;
body {
@if $theme == light {
background: white;
color: black;
} @else if $theme == dark {
background: black;
color: white;
} @else {
background: gray;
color: black;
}
}$theme is set to dark.$theme == light is false, so it skips to @else if $theme == dark, which is true. So it applies background: black; and color: white;.$size: 10px;
.box {
@if $size > 5 {
width: $size;
} @else if $size < 5 {
width: 5px;
}
}10px > 5 in @if conditions.$status that can be 'success', 'warning', or 'error'. Which Sass code correctly uses @if conditional logic to apply green for success, yellow for warning, and red for error, with a default gray if none match?==, not = or ===.=== which is invalid in Sass.Pass the test and this pattern moves unknown