Performance: Conditional mixins with @if
This affects CSS generation size and browser style recalculation during rendering.
Jump into concepts and practice - no test required
@mixin button-style($type) { color: black; background: white; border: 1px solid gray; @if $type == 'primary' { background: blue; color: white; } @else if $type == 'secondary' { background: gray; } @else if $type == 'danger' { background: red; color: yellow; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); } .button-danger { @include button-style('danger'); } .button-default { @include button-style('default'); }
@mixin button-style($type) { color: black; background: white; border: 1px solid gray; @if $type == 'primary' { background: blue; color: white; } @if $type == 'secondary' { background: gray; } @if $type == 'danger' { background: red; color: yellow; } } .button-primary { @include button-style('primary'); } .button-secondary { @include button-style('secondary'); } .button-danger { @include button-style('danger'); } .button-default { @include button-style('default'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Conditional mixins with multiple @if blocks | No direct DOM impact | Triggers multiple style recalculations | Higher paint cost due to redundant styles | [X] Bad |
| Conditional mixins with @if/@else if chain | No direct DOM impact | Single style recalculation per element | Lower paint cost with minimal styles | [OK] Good |
@if directive inside a Sass mixin do?@if in Sass mixins@if directive checks a condition and applies styles only when that condition is true.@for or @each, importing uses @import, and defining classes is done with selectors, not @if.@if controls conditional style application [OK]@if inside a Sass mixin?@if syntax in Sass mixins@if followed by a condition without parentheses, then curly braces for the block: @if $flag { ... }.@ before if. @mixin example($flag) { @if ($flag) color: red; } incorrectly uses parentheses and misses braces. @mixin example($flag) { @if $flag: { color: red; } } uses colon incorrectly.@if syntax = @mixin example($flag) { @if $flag { color: red; } } [OK]@mixin color-choice($color) {
@if $color == 'red' {
color: red;
} @else if $color == 'blue' {
color: blue;
} @else {
color: black;
}
}
.test {
@include color-choice('blue');
}.test class have?'blue' as the argument for $color.$color == 'red' which is false. The second condition @else if $color == 'blue' is true, so color: blue; is applied.@mixin size($value) {
@if $value > 10
font-size: 2rem;
@else
font-size: 1rem;
}@if and @else inside mixins, blocks must be wrapped in curly braces { }.@if $value > 10 and @else, causing syntax errors.@if to achieve this?== for comparison and strings must be quoted. @mixin status-bg($status) {
@if $status == 'success' {
background-color: green;
} @else if $status == 'warning' {
background-color: yellow;
} @else if $status == 'error' {
background-color: red;
} @else {
background-color: gray;
}
} uses == and quotes correctly.= which is assignment, not comparison. @mixin status-bg($status) {
@if $status == success {
background-color: green;
} @else if $status == warning {
background-color: yellow;
} @else if $status == error {
background-color: red;
} @else {
background-color: gray;
}
} misses quotes around strings. @mixin status-bg($status) {
@if $status === 'success' {
background-color: green;
} @else if $status === 'warning' {
background-color: yellow;
} @else if $status === 'error' {
background-color: red;
} @else {
background-color: gray;
}
} uses === which is invalid in Sass.