Back to directory
salsify avatar

ember-css-modules

CSS Modules for ambitious applications. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/salsify/ember-css-modules.git

282

Stars

50

Forks

36

Watchers

MIT

License

Ember CSS Modules

This repository contains several packages for working with CSS Modules in Ember.js.

Dependency-free Usage

Historically, ember-css-modules was the core means of using CSS Modules in Ember, but do note that with modern tooling you don't necessarily need any custom packages at all!

If your bundler is configured to support CSS Modules, then you can just import the class name mapping and reference it directly in your components:

// my-component.gjs
import styles from './my-component.module.css';

<template>
  <div class="some-global-name {{styles.some-local-name}}">
    Hello!
  </div>
</template>
Bundler Configuration Examples

v1 app with ember-auto-import

  const app = new EmberApp(defaults, {
    ...config,
    autoImport: {
      allowAppImports: ['**/*.module.css'],
      cssLoaderOptions: {
        modules: { auto: true },
      },
    },
  })

v1 app with @embroider/compat and @embroider/webpack

  return compatBuild(app, Webpack, {
    // ...
    packagerOptions: {
      cssLoaderOptions: {
        modules: { auto: true },
      },
    },
  });

v2 app with Vite

Nothing required! Vite supports CSS Modules out of the box.

local-class Syntax

The main thing ember-css-modules provides that doesn't come with out-of-the-box bundler support is special local-class syntax for referring to classes from a component's corresponding CSS Module. If you still wish to make use of this syntax, you can use ember-local-class or glimmer-local-class-transform to write the equivalent of the above GJS example:

<template>
  <div class="some-global-name" local-class="some-local-name">
    Hello!
  </div>
</template>

See the READMEs for ember-local-class and glimmer-local-class-transform, as well as the Migration Guides below, for further details on getting set up with those packages.

CSS Modules in Libraries

When publishing libraries (e.g. v2 addons) that use CSS Modules as an implementation detail, you ideally want to publish standard CSS that the consuming application's bundler can package appropriately, without requiring CSS Module support from that bundler.

The rollup-plugin-preprocess-css-modules enables this by preprocessing CSS Modules to standards-compliant JS + CSS at publish time for your library.

Migration Guides

Releases

Mar 23, 2022

Download .zip

Fixed Avoid triggering ember.js#19392 when we produce synthetic class AttrNodes.

Nov 22, 2021

Download .zip

This major release of Ember CSS Modules primarily removes support for deprecated patterns and updates our minimum support for other elements of the ecosystem. Compatibility Ember CSS Modules is now te...

Nov 19, 2021

Download .zip

Added

You can now pass patchClassicComponent: false in your ECM config to opt out of the deprecated monkeypatching of Ember.Component that will be removed entirely in 2.0 (thanks @SergeAstapov!)

Fix...