npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-bem

v1.0.0-beta.8

Published

A TypeScript library for generating BEM class names.

Downloads

0

Readme

Typed BEM

npm license downloads issues pull requests size contributors

Overview

Typed BEM is an extension of the lightweight and proven easy-bem library. While easy-bem efficiently generates BEM (Block-Element-Modifier) class names, Typed BEM enhances it with TypeScript typings to create a type-safe and scalable approach for managing your CSS.

This library not only ensures correctness at compile time but also allows you to use your TypeScript definitions to drive your SCSS architecture, making it a robust solution for creating and maintaining large-scale BEM-based design systems.

Key Features

  • Built on Easy-BEM: Combines the simplicity of easy-bem with the power of TypeScript.
  • Type Safety: Guarantees correct usage of blocks, elements, and modifiers at compile time.
  • SCSS-Driven Approach: Use TypeScript definitions to programmatically generate SCSS files, ensuring consistent styles.
  • Flexible and Scalable: Supports nested elements, complex modifiers, and unified design systems.
  • Set-Based Modifiers: Efficiently handles and validates modifiers using Set<string>.
  • Lightweight: Minimal overhead with no additional dependencies beyond easy-bem.

Installation

Install using npm or pnpm:

Using npm

npm install typed-bem

Using pnpm

pnpm add typed-bem

Usage

Typed BEM introduces a type-safe way to define and generate BEM class names. Define your BEM structures using TypeScript generics, ensuring correctness and preventing invalid combinations.

Example: Single Component

import typedBem from 'typed-bem';

const bem = typedBem<{
	button: {
		modifiers: Set<'primary' | 'secondary'>;
		elements: {
			icon: {
				modifiers: Set<'small' | 'large'>;
			};
			text: {
				modifiers: never; // No modifiers allowed for `text`
			};
		};
	};
}>();

// Block with modifiers
console.log(bem('button', { primary: true }));
// Output: "button button--primary"

// Element with modifiers
console.log(bem('button', 'icon', { small: true }));
// Output: "button__icon button__icon--small"

// Element without modifiers
console.log(bem('button', 'text'));
// Output: "button__text"

API Documentation

typedBem

Function Signature

typedBem<B extends BemBlocks>(): TypedBemFunction<B>;

Returns

The function returns a BEM generator function that accepts:

  • blockName (keyof B): The name of the block.
  • blockModifiersOrElementName:
    • A record of block modifiers.
    • Or the name of an element (keyof B[BlockName]['elements']).
  • elementModifiers (optional): A record of element modifiers, if applicable.

SCSS Integration

Typed BEM allows you to synchronize your TypeScript definitions with your SCSS structure by generating SCSS files programmatically.

SCSS Generator Script

import fs from 'fs';

const bemDefinition = {
	button: {
		modifiers: new Set(['primary', 'secondary']),
		elements: {
			icon: { modifiers: new Set(['small', 'large']) },
			text: { modifiers: null },
		},
	},
	alert: {
		modifiers: new Set(['success', 'error']),
		elements: {
			container: { modifiers: new Set(['padded']) },
		},
	},
} as const;

const generateScss = (bemDef: typeof bemDefinition, outputPath: string) => {
	const scssLines: string[] = [];

	Object.entries(bemDef).forEach(([block, blockDef]) => {
		scssLines.push(`.${block} {`);
		if (blockDef.modifiers) {
			blockDef.modifiers.forEach((modifier) => {
				scssLines.push(`  &--${modifier} {}`);
			});
		}
		if (blockDef.elements) {
			Object.entries(blockDef.elements).forEach(([element, elementDef]) => {
				scssLines.push(`  &__${element} {`);
				if (elementDef.modifiers) {
					elementDef.modifiers.forEach((modifier) => {
						scssLines.push(`    &--${modifier} {}`);
					});
				}
				scssLines.push(`  }`);
			});
		}
		scssLines.push(`}`);
	});

	fs.writeFileSync(outputPath, scssLines.join('\n'), 'utf8');
};

// Generate SCSS file
generateScss(bemDefinition, './bem-structure.scss');

Example Output (bem-structure.scss)

.button {
	&--primary {
	}
	&--secondary {
	}
	&__icon {
		&--small {
		}
		&--large {
		}
	}
	&__text {
	}
}

.alert {
	&--success {
	}
	&--error {
	}
	&__container {
		&--padded {
		}
	}
}

Why Use typed-bem?

  • Built on Easy-BEM: Combines the simplicity of easy-bem with strict TypeScript typing.
  • SCSS Synchronization: Generate SCSS files from your TypeScript definitions for consistency.
  • Type Safety: Catch invalid combinations of blocks, elements, and modifiers at compile time.
  • Scalable Design Systems: Perfect for large projects with multiple components.

License

Typed BEM is licensed under the MIT License.