CSS Modules are often touted as the next step from CSS methodologies like BEM (Block Element Modifier). In this article, I'll explain why BEM should still be used alongside CSS Modules for consistent and intuitive styling.
A Quick Overview
What is BEM?
BEM essentially provides guidelines to structure your CSS selectors in a way that allows you to reuse components (blocks) and manage state changes (modifiers). By clearly defining your components, you have essentially namespaced your CSS to avoid naming conflicts.
Here we have a navigation where the "Home" link is the current page, so we make it blue and bold. This overriding relies on the order of class names, the later class names overriding styles of the previous. So the black turns to blue.
What are CSS Modules?
CSS Modules builds on this and provide scoped selectors. Selectors no longer need to be global, and so we can avoid selector conflicts between components. A pre-processor can take these scoped class names and generate non-conflicting global class names, which can be used on your components.
Notice how the selectors in this example are completely generic. They're allowed to be since they're locally scoped and won't conflict with other CSS Modules which use the same selectors.
CSS Modules without BEM
If you're not using BEM when naming your class names in CSS Modules, you're back to the age-old problem of deciding a suitable naming scheme for your class names. BEM was made to solve this so there's really no reason not to keep using it.
Say we wanted to add another link, "Sign In" which looks like a button. We can do the following without BEM:
We can see the issue here. Every time we introduce a new "type" of Link, we'll need to add another conditional property to the "className" to pick out the right class name from our CSS Module. We can figure out a naming scheme so that we can automatically use the right class name, or we can just use BEM.
CSS Modules with BEM
Because BEM's naming scheme uses Blocks, Elements and Modifiers, we can remap these concepts to React. Blocks are Components, Elements are Child Components and Modifiers are Props/State.
So "Link" is our Block, "active" and "type" are our modifiers where:
Modifiers that are undefined, null or false are not applied.
Modifiers that are boolean and true are applied by key. e.g. "Link --- active"
Modifiers that are strings, numbers, etc. are applied by key and value. e.g. "Link --- type --- button"
In the example above, I'm using my own library "@jahed/bem" which has a "bemModule()" function which:
Wraps the generated class names.
Returns a function to generate a className string by remapping a given state to the generated class names. I've assigned this to "bem".
This way, we can avoid having to rewrite every class name and keep them synchronised. If a modifier doesn't exist in the CSS Module, it's simply ignored.
There are a number of advantages to using CSS Modules with BEM and generated class name assignments. Here's a few:
Enforced BEM conventions. Since mappings are generated, selectors will only work if they follow the expected naming scheme. We can go further and show warnings when CSS Modules have invalid selectors (using bemModule()) and when states aren't assigned styles (using bem)
Consistency and Brevity. Both React components and CSS modules are now a "function" of their state. There's no need for logic in between.
Component Themes and Composition. Since we use consistent naming, we can take a CSS Module as a component prop instead of using an import.
Easier Debugging. Since the component name and state is visible in the DOM during Development, it's easier to see the current state in a Web Inspector and find the available states.
Easier Search and Discovery. Assuming components are named somewhat uniquely, it's easy to run global searches for a given selector rather than binding selectors to where they're imported.
All the benefits of CSS Modules. Locally scoped selectors and shorter generated selectors in Production stylesheets.
All the benefits of BEM. Consistent and intuitive naming.
Not limited to React. bemModule returns strings so you can use the library for server-side templates (Handlebars, EJS) and other frameworks.
@jahed/bem is an open source project, so feel free to contribute with issue tickets and pull-requests.