gitmyhub

rbem

JavaScript ★ 5 updated 9y ago

bem__naming-made--easy

A tiny dependency-free JavaScript library that auto-generates BEM-style CSS class names, so you don't have to type long block__element--modifier strings by hand.

JavaScriptsetup: easycomplexity 1/5

Rbem Explanation

Rbem is a tiny utility library that makes it easier to write CSS class names following the BEM naming convention. BEM (Block, Element, Modifier) is a popular system for organizing CSS that uses a specific naming pattern to keep styles organized and predictable. Instead of writing out these long class names by hand every time, Rbem gives you simple functions that generate them for you.

Here's the basic idea: you define a block (like "button"), and then the library helps you create related element and modifier class names automatically. For example, you tell it your block is "button," and it can instantly generate names like "button__icon" (an element inside the button) or "button--primary" (a variation of the button). You don't have to remember or retype the BEM naming rules; the library does it for you.

The library provides two main functions. The first, block, creates a namespace for a single component—think of it as declaring "I'm working with button-related styles." Once you've done that, you can quickly generate element names by calling that function. The second function, applyModifiers, lets you add modifiers (style variations) to either a block or an element. You can pass it a single modifier name, a list of modifiers, or an object where you specify which modifiers should apply based on conditions.

This approach pairs nicely with React components. Imagine you're building a Button component that can be "primary," "active," or both. Instead of manually concatenating strings or maintaining a list of hardcoded class names, you'd use Rbem to generate the full class name based on the component's current state. The library is dependency-free and tiny, so it won't bloat your bundle. It's designed to work smoothly with Webpack and Babel if you're using a modern JavaScript setup.

Where it fits