Skip to content

Conversation

Saeris
Copy link
Contributor

@Saeris Saeris commented Sep 23, 2025

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 inline style 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.

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 inline `style` attribute and a CSS custom property (eg: `--mouse-x`).

Good examples can be found in [this article on replacing React code with CSS](https://www.developerway.com/posts/replacing-react-with-css) and [this interactive guide to :has()](https://ishadeed.com/article/css-has-guide/).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant