Performance: State class generation (hover, active, disabled)
This affects the browser's style calculation and paint phases by adding or removing CSS classes that change element states like hover, active, or disabled.
@mixin button-states-good { button { &.hover { background-color: blue; } &.active { background-color: darkblue; } &.disabled { background-color: gray; cursor: not-allowed; } } } @include button-states-good; // JavaScript toggles classes on events instead of relying on :hover/:active pseudo-classes
@mixin button-states-bad { button { &:hover { background-color: blue; } &:active { background-color: darkblue; } &:disabled { background-color: gray; cursor: not-allowed; } } } @include button-states-bad;
| Pattern | DOM Operations | Style Recalc | Paint Cost | Verdict |
|---|---|---|---|---|
| Using :hover/:active/:disabled pseudo-classes | No extra DOM ops | Triggers style recalc and repaint on every hover/active | High paint cost on interaction | [X] Bad |
| Using explicit .hover/.active/.disabled classes toggled by JS | Class toggling on state change | Single style recalc per class toggle | Lower paint cost, batched updates | [OK] Good |
:hover, :active, and .disabled in Sass?:hover and :active change how elements look when users interact with them.@mixin name { ... } and nested selectors use &:state for pseudo-classes.&:hover and &:active inside the mixin.@mixin states {
&:hover { color: green; }
&:active { color: orange; }
&.disabled { color: gray; cursor: not-allowed; }
}
.button {
color: black;
@include states;
}color: green on :hover, color: orange on :active, and gray with disabled class.:hover style applies, changing text color to green.@mixin states {
&:hover { color: blue; }
&:active { color: red; }
&:disabled { color: gray; cursor: not-allowed; }
}
.button {
@include states;
}:disabled is a pseudo-class for form elements, but here disabled is a class, so &.disabled is correct.opacity: 0.5 and prevent clicks using pointer-events: none.&.disabled is correct for a class. Cursor should be not-allowed to show disabled status.&.disabled, sets opacity to 0.5, disables pointer events, and sets cursor properly.Pass the test and this pattern moves unknown