DEV Community

Cover image for I built a CSS framework where every class name is an emoji
Tom Hayes
Tom Hayes

Posted on

I built a CSS framework where every class name is an emoji

I got into one too many Tailwind vs BEM arguments this month and decided the correct response was to build something that makes both sides equally uncomfortable.

Meet BEMoji: a production-grade CSS framework where every class name is an emoji.

<article class="๐Ÿƒ">
  <div class="๐Ÿƒ__๐Ÿ–ผ๏ธ ๐Ÿƒ__๐Ÿ–ผ๏ธ--๐ŸŒŸ">
    <img src="hero.jpg" alt="...">
  </div>
  <div class="๐Ÿƒ__๐Ÿ“">
    <h2 class="๐Ÿƒ__๐Ÿ” ">Card Title</h2>
  </div>
  <footer class="๐Ÿƒ__๐Ÿฆถ">
    <button class="๐Ÿ”˜ ๐Ÿ”˜--๐ŸŒŸ">Primary</button>
    <button class="๐Ÿ”˜ ๐Ÿ”˜--๐Ÿ‘ป">Disabled</button>
  </footer>
</article>
Enter fullscreen mode Exit fullscreen mode

That is valid, production HTML. It works in every modern browser. I am not sorry.

Why does this exist

The core BEM pattern is block__element--modifier. BEMoji keeps that structure exactly, just swapping string names for emoji tokens. The double-underscore and double-hyphen delimiters are preserved, so the class names are still machine-parseable even if they're completely illegible to humans.

It started as a joke and then I accidentally made it real.

It's actually kind of a full framework

This is the part that got out of hand. BEMoji ships with:

  • 143 canonical emoji tokens across blocks, elements, modifiers and utilities
  • A complete design token system (yes, using emoji as CSS custom property names: --๐Ÿ“-4, --๐ŸŒ‘-md, --โญ•-full)
  • 24 pre-built components (cards, buttons, badges, alerts, modals, tables, the lot)
  • A PostCSS plugin so you can write readable BEM in your source and get emoji compiled at build time
  • A Vite plugin with HMR support
  • An ESLint plugin that enforces correct token usage
  • A React bem() helper
  • A CLI with init, compile, audit, decode, encode and export commands

The CLI's decode command is probably my favourite part:

npx bemoji decode "๐Ÿƒ__๐Ÿ–ผ๏ธ--๐ŸŒŸ"
# card__image--featured
Enter fullscreen mode Exit fullscreen mode

The PostCSS workflow

You don't have to write emoji by hand in your source files. The PostCSS plugin lets you use a bracket shorthand:

.[card] {
  border-radius: var(--โญ•-md);
  box-shadow: var(--๐ŸŒ‘-sm);
}

.[card__image--featured] {
  outline: 2px solid var(--๐ŸŸก);
}
Enter fullscreen mode Exit fullscreen mode

Which compiles to:

.๐Ÿƒ {
  border-radius: var(--โญ•-md);
  box-shadow: var(--๐ŸŒ‘-sm);
}

.๐Ÿƒ__๐Ÿ–ผ๏ธ--๐ŸŒŸ {
  outline: 2px solid var(--๐ŸŸก);
}
Enter fullscreen mode Exit fullscreen mode

So the output is emoji soup but your source stays readable. Or as readable as it was before, anyway.

The React helper

import { useBem } from 'bemoji/react';

function Card({ featured }) {
  const b = useBem('card');

  return (
    <article className={b()}>
      <div className={b('image', { featured })}>
        ...
      </div>
    </article>
  );
}

// When featured=true, className becomes "๐Ÿƒ__๐Ÿ–ผ๏ธ ๐Ÿƒ__๐Ÿ–ผ๏ธ--๐ŸŒŸ"
Enter fullscreen mode Exit fullscreen mode

There are legitimate arguments for this

I feel compelled to make the honest case, because it genuinely surprised me:

Free obfuscation. Your production CSS is meaningless to anyone without the config file. You get the obfuscation benefits of CSS Modules without the build complexity.

Enforced vocabulary. Every UI concept maps to one canonical emoji. The config file is your design system contract. Changing a concept's emoji is a one-line config change that propagates everywhere.

It's faster to type than you think. Once you have emoji picker shortcuts set up, ๐Ÿƒ is two keystrokes. .card__image--featured is 24. The maths eventually works out.

None of this makes it a good idea. But they are real arguments.

The config

// bemoji.config.js
export default {
  blocks: {
    card:    '๐Ÿƒ',
    navbar:  '๐Ÿงญ',
    modal:   '๐ŸชŸ',
    alert:   '๐Ÿ””',
  },
  elements: {
    image:  '๐Ÿ–ผ๏ธ',
    title:  '๐Ÿ” ',
    body:   '๐Ÿ“',
    button: '๐Ÿ”˜',
  },
  modifiers: {
    primary:  '๐ŸŒŸ',
    danger:   '๐Ÿ”ด',
    disabled: '๐Ÿ‘ป',
    loading:  'โณ',
  },
};
Enter fullscreen mode Exit fullscreen mode

Install

npm install bemoji
npx bemoji init
Enter fullscreen mode Exit fullscreen mode

The init command scaffolds a config, imports the base CSS, and wires up PostCSS.

Links

My personal favourite token is ๐Ÿฆถ for footer. If you find a better use for it than I have, open a PR.

Top comments (1)

Collapse
 
francistrdev profile image
๐Ÿ‘พ FrancisTRแด…แด‡แด  ๐Ÿ‘พ

That's crazy lol. Imagine creating documentation for a large code-base that uses emojis. Readability is gonna go oof. Great work!