Add CSS over JS guideline #10
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a new guideline to the Performance section that recommends the use of CSS features for conditionally applying styles instead of relying on JavaScript.
The idea behind this guideline is that where possible, developers should be writing only as much JavaScript as necessary.
Modern CSS features such as
:has()
are extremely powerful and can be leveraged to greatly reduce overall code needed. By leveraging these features for state-dependent styles, those styles can be statically defined and thus reduce unnecessary re-renders. Often times in cases such as form elements, state can be derived from HTML attributes, negating the need for additional conditionally applied classes in JS. In rare cases where a value must come from JS (such as a mouse cursor position), that value can be passed using an inlinestyle
attribute and a CSS custom property (eg:--mouse-x
).Good examples can be found in this article on replacing React code with CSS and this interactive guide to :has().
Please feel free to adjust the wording of this new guideline. I attempted to be concise, but it may be too vague.