Components: remove hardcoded classnames and move away from Sass styles #43083
Labels
[Package] Components
/packages/components
[Type] Code Quality
Issues or PRs that relate to code quality
[Type] Enhancement
A suggestion for improvement.
[Type] Tracking Issue
Tactical breakdown of efforts across the codebase and/or tied to Overview issues.
Context
Hardcoded classnames have been historically used in the
@wordpress/components
package as the main way to style component while writing styles in Sass.But while hardcoded classnames have (almost) never been intended to be part of the public API of a component, they've been often used by consumers of the
@wordpress/components
package (both in the Gutenberg repo, and outside of it) as a way to override the components' styles with custom styles.As @nerrad pointed out, the project does have existing documentation about backwards compatibility around class names and DOM updates.
This practice, while allowing infinite customization options, is actually quite bad for the components library itself: styling a component using its internal, hardcoded classnames (or by relying on its implicit DOM structure) is an extremely fragile technique, prone to breakage as soon as a component's implementation details change.
And since we, maintainers of the library, are very mindful about not causing breaking changes, the more hardcoded classnames are used the harder it is for us to improve components iteratively without causing such breaking changes.
The added benefit of using CSS-in-JS is better style encapsulation compared to the current Sass styles (although I am aware that there are other technologies that also offer style encapsulation, but that's a different topic)
The plan
For each component still using
.component-*
classnames:@wordpress/components
packageclassName
prop) and use it as a way to assign custom styles to the component.We should also update the CONTRIBUTING guidelines.
Risks
As well summarized by @mirka in #42789 (comment)
Removing hardcoded classnames and reducing the amount of style overrides will also give us a better idea of how components are used "in the wild", and potentially inform future decisions.
The text was updated successfully, but these errors were encountered: