• Stars
    star
    577
  • Rank 77,363 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 8 years ago

Reviews

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

Repository Details

✨ Modular, scoped CSS with ES6

CSJS logo

build status coverage status dependencies status npm version

CSJS allows you to write modular, scoped CSS with valid JavaScript.

Features

  • Extremely simple and lightweight
  • Leverages native ES6 and CSS features (1) rather than reinventing the wheel
    • Seamless modular, scoped styles with explicit dependencies powered by CommonJS/ES6 modules
    • Dead-simple variables/mixins using tagged ES6 template strings
    • Style composition with optimal reuse via natural class composition mechanics already in CSS/HTML(2)
  • Works tooling-free; no required transpilation/compilation/build steps (3)
  • Framework-agnostic (No React dependency; works with Web Components, etc.)
  • Fully supported native CSS media queries, pseudo-classes, keyframe animations, etc.
  • Server-rendered/universal JavaScript support

Quick Example

(Live editable codepen.io demo)

const csjs = require('csjs');
const {div, h1} = require('react').DOM;

const green = '#33aa22';

const styles = csjs`

  .panel {
    border: 1px solid black;
    background-color: ${green};
  }

  .title {
    padding: 4px;
    font-size: 15px;
  }

`;

const html = require('react-dom/server').renderToStaticMarkup(
  div({className: styles.panel}, [ 
    h1({className: styles.title}, 'Hello World!')
  ])
);
/*
<div class="panel_4Eda43">
  <h1 class="title_4Eda43">Hello World!</h1>
</div>
*/

const css = csjs.getCss(styles);
/*
.panel_4Eda43 {
  border: 1px solid black;
  background-color: #33aa22;
}

.title_4Eda43 {
  padding: 4px;
  font-size: 15px;
}
*/

Simple, tooling-free

CSJS runs in ES6 environments without transpilation, compilation, or build steps (including Node 4+ and latest stable Chrome/Firefox/Safari/Edge).

sauce labs test status

Of course, you can always transpile ES6 template strings using Babel, allowing you to use CSJS in any ES5 environment.

Framework-agnostic

CSJS works with any framework, be it React, native Web Components, or something else.

Full power of JavaScript in your CSS

  • Real, full-fledged JavaScript
  • Obviates the need for Sass/Less or other preprocessors
  • Proper imports/require
  • Real variables, functions, loops, etc.
  • As extensible as JavaScript itself

Class Composition Syntax

CSJS also features class composition that works like CSS Modules:

(Live editable codepen.io demo)

common-styles.js

const csjs = require('csjs');

module.exports = csjs`

  .border {
    border: 1px solid black;
  }

  .italic {
    font-family: serif;
    font-style: italic;
  }

`;

quote-styles.js

const csjs = require('csjs');

const common = require('./common-styles');

module.exports = csjs`

  .blockQuote extends ${common.italic} {
    background: #ccc;
    padding: 8px;
    border-radius: 4px;
  }

  .pullQuote extends .blockQuote, ${common.border} {
    background: #eee;
    font-weight: bold;
  }

`;

app.js

const getCss = require('csjs/get-css');
const commonStyles = require('./common-styles');
const quoteStyles = require('./quote-styles');

quoteStyles.blockQuote;
// => "blockQuote_2bVd7K italic_3YGtO7"

quoteStyles.pullQuote;
// => "pullQuote_2bVd7K blockQuote_2bVd7K italic_3YGtO7 border_3YGtO7"

getCss(quoteStyles);
/*
.blockQuote_2bVd7K {
  background: #ccc;
  padding: 8px;
  border-radius: 4px;
}

.pullQuote_2bVd7K {
  background: #eee;
  font-weight: bold;
}
*/

getCss(commonStyles);
/*
.border_3YGtO7 {
  border: 1px solid black;
}

.italic_3YGtO7 {
  font-family: serif;
  font-style: italic;
}
*/

Optional tooling

Extracted static CSS bundles

csjs-extractify is a browserify plugin that allows you to extract your application's CSJS into a static CSS file at build time.

Automatic CSS injection

csjs-injectify is a browserify transform that automatically replaces csjs with csjs-inject, which automatically injects your scoped CSS into the <head> at runtime. It is recommended to use this rather than the csjs-inject module directly.

PostCSS

babel-plugin-csjs-postcss is a Babel plugin that allows you to run PostCSS on the CSS contained within CSJS template string literals at build time. Works with plugins such as Autoprefixer.

Syntax highlighting

neurosnap has created an Atom plugin for syntax highlighting CSS within CSJS tagged template strings.

FAQ

Why the name CSJS?

CSJS is 100% valid JavaScript, hence the name Cascading Style JavaScripts.

Why not Sass?

Sass doesn't provide any way to scope CSS, thus encapsulation of styles in components isn't possible with Sass alone. Additionally, because Sass was designed for use in a global CSS namespace, many of its features just don't make sense when styles are scoped and encapsulated in components. @extend in Sass is extremely problematic, whereas CSJS has a proper mechanism for class composition that actually works like it should. Furthermore, with CSJS, you have the ability to use real JavaScript in CSS, which is significantly more powerful and extensible than the language features included in Sass, so there's not really any reason to use Sass at all.

Why not CSS Modules?

CSJS was inspired by CSS Modules and they are virtually identical in concept. However, unlike CSS Modules which attempts to reproduce an ES6-style module system into CSS itself, CSJS simply uses native JS modules. CSJS also uses normal JS variables whereas CSS Modules invents its own CSS variable syntax.

Consquently, CSJS is merely plain JavaScript and works without any extra tooling (CSS Modules is not valid CSS). Furthermore, because CSJS is essentially an amalgamation of plain JavaScript and plain CSS, there's no any new syntax or semantics to learn (besides the optional composition syntactic sugar, which closely mimicks ES6 classes).

Why not Radium?

Inline styles are cool, but there are limitations to using pure inline styles. For example, CSS pseudo-classes and media queries aren't possible with inline styles. This is the premise behind Radium, which works around this by re-implementing these CSS features using JavaScript.

Whereas Radium is wholly dependent on React and involves performance trade-offs in its JavaScript implementations of CSS features, CSJS works regardless of framework (or lack thereof) and allows for the use of all CSS features natively (including media queries and pseudo-classes).

See Also

License

MIT

More Repositories

1

babel-preset-es2015-node

🐠 Babel preset for the necessary es2015 plugins for your version of node
JavaScript
89
star
2

hokusai

🌊 Modern static site generator
JavaScript
64
star
3

create-universal-package

📦 A toolchain for developing universal (Node.js and browser) JavaScript packages
JavaScript
37
star
4

csjs-inject

CSJS with auto-injection of styles
JavaScript
34
star
5

babel-plugin-csjs-postcss

🔌 Babel plugin for running PostCSS on CSJS at build time
JavaScript
28
star
6

browser-unhandled-rejection

A ponyfill/polyfill for browser Promise unhandledrejection events
JavaScript
28
star
7

unitest

🌎 Seamless node and browser unit testing with code coverage
JavaScript
28
star
8

scope-styles

converts inline style objects into scoped css
JavaScript
27
star
9

react-stylematic

A stylematic wrapper for React
JavaScript
18
star
10

stylematic

💫 A comprehensive solution for inline styles
JavaScript
16
star
11

csjs-extractify

Browserify plugin to extract csjs into an external css bundle
JavaScript
14
star
12

postcss-rtlcss-combined

PostCSS plugin that creates a combined LTR/RTL stylesheet
JavaScript
13
star
13

www

😎 My personal website
JavaScript
12
star
14

postcss-match

PostCSS plugin for Rust-style pattern matching
JavaScript
12
star
15

lostyle

A JavaScript style utility library
JavaScript
11
star
16

polypackage

📦 npm packages done right
JavaScript
11
star
17

css-in-js-perf-tests

CSS-in-JS performance tests
JavaScript
10
star
18

gh-label-svg

🏷 A microservice for SVG GitHub label images
JavaScript
10
star
19

saison

A minimal templating language
JavaScript
9
star
20

css-to-js-sourcemap

Generates CSS w/ sourcemaps to lines in JS at runtime
JavaScript
8
star
21

csjs-injectify

Browserify transform to auto-inject csjs
JavaScript
8
star
22

react-fp

⚡ A functional JSX alternative for React
JavaScript
8
star
23

cssparse

A css parser powered by the style crate from Servo
Rust
8
star
24

GitHub-Raw-Preview-Chrome-Extension

Directly view GitHub raw HTML/CSS/JS in Chrome
JavaScript
7
star
25

react-svg-arrow

▶️ A React component for tooltip and popover arrows
JavaScript
6
star
26

zirconium

A picoframework for Web Components
JavaScript
5
star
27

styletron-legacy

Legacy v1.x styletron and styletron-server
JavaScript
5
star
28

babel-plugin-transform-prune-unused-imports

Babel plugin to prune unused/unreachable imports
JavaScript
5
star
29

npm-publish-prerelease

npm publish, but with dist-tag based on semver prerelease parts if present
JavaScript
4
star
30

safe-string

🔒 Escapes HTML characters and JavaScript line terminators
JavaScript
4
star
31

scc

Find strongly connected components of a directed graph using Tarjan's algorithm
JavaScript
3
star
32

aoc-2019

Learning Haskell via Advent of Code 2019
Haskell
3
star
33

epistyle

💄 Enhanced inline style objects
JavaScript
3
star
34

csjs-styletron-legacy

A legacy styletron wrapper for CSJS
JavaScript
3
star
35

Real-Time-Quote-Server

A node.js server to push real-time stock quotes over WebSockets
JavaScript
2
star
36

csjs-example-app

example app using csjs
JavaScript
2
star
37

postcss-composes-shorthand

PostCSS plugin to add shorthand for composing classes from modules
JavaScript
2
star
38

babel-plugin-styletron

Babel transform for Styletron
JavaScript
2
star
39

scope-styles-inject

scope-styles with auto-injection of styles
JavaScript
2
star
40

derive-pkg

📦 A helper utility for publishing transpiled code to npm
JavaScript
2
star
41

simple-menhir-bs-re

simple bug repro repo
OCaml
2
star
42

sudoku

A CSS3-animated sudoku app
JavaScript
2
star
43

strip-flow-types

Remove flow types with tree-sitter
JavaScript
2
star
44

ppx_deriving_comparison

ppx to generate a function that compares variant constructors
OCaml
2
star
45

now-replace

[Deprecated] Changes a now.sh alias to a new deployment, removing the old deployment
JavaScript
1
star
46

progressive-enhancement

Demos from Progressive enhancement: not just for websites
JavaScript
1
star
47

selector-has

A helper utility to check the presence of a class in a CSS selector
JavaScript
1
star
48

vite-bug-repro-cjs-workspaces

A reproduction of broken SSR with CommonJS workspace dependencies.
JavaScript
1
star
49

transform-require

A node require hook for running browserify transforms
JavaScript
1
star
50

scope-styles-injectify

Browserify transform to automatically inject the scoped css from scope-styles into the page
JavaScript
1
star
51

sson

Strict style object notation
JavaScript
1
star
52

polyhedral-symmetry

🌐 A WebGL visualization of polyhedral symmetry on the sphere
JavaScript
1
star