• Stars
    star
    612
  • Rank 70,406 (Top 2 %)
  • Language
  • Created almost 9 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interoperable CSS β€” a standard for loadable, linkable CSS

Interoperable CSS (ICSS)

This document describes the specification of the low-level file format that enabled CSS Modules. This is designed for loader-implementers, not for end-users. For the high-level specification, see the full CSS Modules spec.

Rationale

As JavaScript workflows have trended towards building collections of components, CSS workflows have followed suit. However, any progress on the CSS front has been purely conventional, not supported by the language. The most visible example of this is the BEM methodology, but the argument is common to many approaches:

  • Styles should be scoped to a single component
  • All CSS selectors are global
  • ∴ Develop a convention for ensuring globally-unique selectors

In BEM, that takes the form .block-name__element-name--modifier-name, or .BlockName__elementName--modifierName.

Explicit cross-language dependencies

One of the fundamental features of the Webpack loader (which is also core to JSPM and easily possible with Browserify) is the ability to explicitly describe each file's dependencies regardless of the type of source file. For CSS in a component workflow, that takes the following form:

// Marks the CSS as being a dependency of this JS.
// Depending on the loader, the CSS is either injected
// into the DOM or bundled into a separate CSS package.
require( './my-component.css' );
var MyComponent = // component definition
module.exports = MyComponent;

Now, whenever my-component.js is loaded or bundled, the corresponding CSS is guaranteed to be present, just like any other dependency. This convention leads us to a new capability, and necessitates a new specification.

CSS - JS interoperability

By treating the CSS as a dependency of our JS, we have the opportunity to do something hitherto impossible – pass variables from CSS to JS. For example, instead of this:

// loads the CSS as a side-effect
require( './my-component.css' );

we can now pass arbitrary information to our loader:

// loads the CSS as a side-effect and returns
// something we can use in rendering our component.
var styles = require( './my-component.css' );
// this might be a dynamically-generated classname:
elem.addClass( styles.elemClass );

This is the key capability that is new to modern multi-format loaders like Webpack, JSPM and Browserify, and the CSS Modules Specification is an opinionated proposal of new CSS techniques this enables. However, at the fundamental level, we need a specification that describes the mechanism by which these symbols are passed around.

Specification

Interoperable CSS (ICSS) is a superset of standard CSS, making use of two additional pseudo-selectors:

:import("path/to/dep.css") {
  localAlias: keyFromDep;
  /* ... */
}
:export {
  exportedKey: exportedValue;
	/* ... */
}

:export

An :export block defines the symbols that are going to be exported to the consumer. It can be thought of functionally equivalent to the following JS:

module.exports = {
	"exportedKey": "exportedValue"
}

The following restrictions are placed on the :export syntax:

  • It must be at the top level, but can be anywhere in the file.
  • If there is more than one in a file, the keys and values are combined and exported together.
  • If a particular exportedKey is duplicated, the last (in source order) takes precedence.
  • An exportedValue may contain any character valid for CSS declaration values (including spaces).
  • An exportedValue does not need to be quoted, it is already treated as a literal string.

The following are desirable for output readability, but not enforced:

  • There should be only one :export block
  • It should be located at the top of the file, but after any :import blocks

:import

An :import statement allows importing variables from other CSS files. It performs the following operations:

  • Fetch & process the dependency
  • Resolve the dependency's exports against the imported tokens, and match them up to a localAlias
  • Find and replace the usages of localAlias in certain places (described below) within the current file with the dependency's exportedValue.

The places within the CSS file that are checked for localAlias are:

  • In any declaration value: e.g. border: 1px solid localAlias;
  • In any selector: e.g. .localAlias .MyComponent {}
  • In a media query argument: e.g. @media screen and localAlias

This allows considerable flexibility about what can be imported and used in a file. It also demands that a particular local alias is distinct enough to not cause false positives during the replacement process. The following restrictions apply:

  • It must be at the top level
  • Each file can import from multiple dependencies, and import many symbols
  • A local alias must be a single term, consisting of only alphanumeric characters, underscores and dashes. It must also be unique for the file to avoid clashes.

And the following properties are desirable for readability but not enforced:

  • There should be one import per dependency
  • All imports should be at the top of the file
  • Local aliases should be prefixed with double-underscore

Contributions

Edit this file and make your change to the spec, then send a PR with your argument for why the change should be made. All contributions are welcome.

Acknowledgements

With thanks to:

  • Mark Dalgleish
  • Tobias Koppers
  • Ben Smithett
  • Guy Bedford

Glen Maddern, 2015.

More Repositories

1

css-modules

Documentation about css-modules
17,378
star
2

webpack-demo

Working demo of CSS Modules, using Webpack's css-loader in module mode
JavaScript
1,487
star
3

css-modules-require-hook

A require hook to compile CSS Modules in runtime
JavaScript
485
star
4

css-modulesify

A browserify plugin to load CSS Modules
JavaScript
406
star
5

postcss-icss-values

Pass arbitrary constants between your module files
JavaScript
202
star
6

css-modules-loader-core

A loader-agnostic CSS Modules implementation, based on PostCSS
JavaScript
92
star
7

browserify-demo

A working demo of CSS Modules, using css-modulesify
JavaScript
87
star
8

css-selector-tokenizer

Parses and stringifies CSS selectors.
JavaScript
84
star
9

postcss-icss-composes

A CSS Modules transform to extract export statements from local-scope classes
JavaScript
55
star
10

jspm-demo

CSS Modules & JSPM
JavaScript
52
star
11

postcss-modules-extract-imports

A CSS Modules transform to extract local aliases for inline imports
JavaScript
40
star
12

postcss-modules-values

Pass arbitrary constants between your module files
JavaScript
35
star
13

postcss-modules-scope

A CSS Modules transform to extract export statements from local-scope classes
JavaScript
23
star
14

icss-utils

Search & replace tokens during the linking stage of ICSS loading
JavaScript
22
star
15

postcss-modules-local-by-default

PostCSS plugin for css modules to local-scope classes and ids
JavaScript
20
star
16

cssm-rails

WIP of a Rails plugin for CSS Modules
Ruby
17
star
17

generic-names

Helper for building generic names, similar to webpack
JavaScript
15
star
18

postcss-modules-resolve-imports

Resolves ICSS imports
JavaScript
9
star
19

postcss-modules-lint

PostCSS plugin to verify the scoping of selectors in CSS Modules
JavaScript
8
star
20

logos

Official CSS Modules logos
5
star
21

postcss-icss-keyframes

PostCSS plugin for css-modules to local-scope keyframes
JavaScript
5
star
22

postcss-icss

Postcss plugin to process css modules and extract tokens
JavaScript
4
star
23

postcss-icss-import

PostCSS plugin for css-modules to convert @import statements to ICSS
JavaScript
2
star
24

postcss-icss-url

JavaScript
2
star