WIP: test(css): Proof of concept for possible approach to using custom CSS class names #1371
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.
DO NOT MERGE
I've been thinking a lot about the future of CSS on this project, if it's ever going to go beyond Bootstrap classes. Here's a proof of concept that handles a small but common component, the Paginator.
Goal
The goal here is to gradually move away from Bootstrap class names to custom class names, while still using the styles that Bootstrap provides, with the possible ultimate goal of remove Bootstrap as a dependency entirely. Bootstrap doesn't provide an off-ramp as far as I can tell, and most articles either say "Don't use Bootstrap" or show someone rewriting an entire codebase.
Benefits
1. Reusability
Right now, every time we want to make a component that looks like another component that uses, say, four Bootstrap class names to get its styles, that new component needs to use all four of those classes. If a class is added to the original component for some small change, a developer will have to know to find every similar-looking component and update those class names as well.
2. Custom theming
Greasemonkey-style plugins and, more importantly, use stylesheets (for people with vision or mental impairments, for instance) have a really hard time latching on to collections of multiple classnames on a single component, so customizability is very difficult with Bootstrap classes only.
Approach
1. Use
@extend
on Bootstrap classes for complex stylesThis is the shortest route to getting off of Bootstrap classes in the HTML, but it adds some well-documented overhead to the compiled CSS (see below). But for something like
.btn
, Bootstrap unfortunately doesn't provide any mixins.2. Use Bootstrap Sass vars
This is possible for smaller utility classes, like margins and colors, and is more "future-proof," as vars like
$spacer
can be easily copied into a custom codebase3. Use BEM
BEM is a common naming methodology that works well for complex projects with a lot of people touching the same codebase.
4. One component at a time
Rather than throwing hundreds of CSS classes at the codebase and hoping that something is eventually done with them, doing this on a component-by-component basis will allow any shortcomings to surface earlier to allow for better course-correction. Lessons will be learned as components are rewritten, lessons in terms of naming, file structure,
@extend
s and vars, etc.Obstacles
1. Style inconsistencies
One of the benefits of Bootstrap is that it provides a fully consistent UI; there's no easy way to just put an arbitrary 17px margin on something, for instance, and this can be a good thing.
Moving to separate stylesheets will invite the temptation to add these arbitrary values, which could quickly result in a lot of UI inconsistencies.
2. Bloat
Using
@extend
a lot can seriously bloat a stylesheet, but gzip can crunch this down pretty efficiently.