Performance: Mixin libraries pattern
This pattern affects CSS bundle size and rendering speed by how styles are generated and reused.
Jump into concepts and practice - no test required
.button { padding: 1rem 2rem; border-radius: 0.5rem; background-color: blue; color: white; } .button1, .button2, .button3 { @extend .button; }
@mixin button-style { padding: 1rem 2rem; border-radius: 0.5rem; background-color: blue; color: white; } .button1 { @include button-style; } .button2 { @include button-style; } .button3 { @include button-style; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Mixin included multiple times | No extra DOM nodes | No extra reflows | Higher paint cost due to larger CSS | [X] Bad |
| Shared class with @extend | No extra DOM nodes | No extra reflows | Lower paint cost due to smaller CSS | [OK] Good |
button-style in Sass?@mixin name { ... }.@include name;.@mixin card-style {
border: 1px solid #ccc;
padding: 1rem;
box-shadow: 0 0 5px rgba(0,0,0,0.1);
}
.card {
@include card-style;
background-color: white;
}.card class?card-style defines border, padding, and box-shadow styles.@include card-style; inserts those styles inside .card, plus the background color.@mixin text-style {
font-size: 1.2rem;
color: #333;
}
.title {
@mixin text-style;
font-weight: bold;
}@mixin but applied with @include.@mixin text-style; inside .title, which is wrong syntax.primary-button that uses the existing button-base mixin and adds a blue background. Which Sass code correctly achieves this?@mixin primary-button { ... } to create the new mixin.@include button-base; to reuse styles.background-color: blue; after including the base mixin.