Home

Awesome

Rockey <sup>Component Based CSS in JS</sup>

Stressless CSS for components using JS. Write Component Based CSS with functional mixins.

npm install --save rockey

# For React applications:
npm install --save rockey-react

Rockey tests


Documentation

Why do we need CSS in JS?

Firstly, CSS in JS approach is the vanilla JS. 

CSS in JS approach — is native JS. You don’t need additional tools to use or build it.

More details explained at Medium Post - <img src="http://i.imgur.com/ULoeOL4.png" height="16"/> CSS in JS. Rockey

Features and Advantages

Framework Agnostic

Rockey could be used in any application.

Small Size

npm run minify output:

rockey and rockey-react size

Uniq Class Names

Each time generate uniq class names with randomly generated hash. Same as css-modules.

Component based selectors

Write CSS according your components structure. Use real components names for CSS rules instead of classes. Means that if you have component Card  — use its name as CSS selector. If you have component PrimaryCard — use its name as CSS selector. Use nested selectors according to components structure.

Live demo: Card example

Readable CSS Class Names

Each generated classname is clear and readable. The same components renders with same class names. It is very useful and сompatible with browser dev tools — change styles for one component will always apply changes for the rest of the same components.

Generated class names

~100% CSS Syntax

Card {
  CardHeader, CardFooter {
    padding: 15px;
  }

  CardBody {
    + Button {
      padding-left: 15px;
    }
  }

  :hover {
    border: 1px solid #000;

    CardHeader {
      background: yellow;
    }
  }  

  CardFooter {
    background: purple;

    @media (max-width: 600px) {
      display: none
    }
  }  
}

There is no needs to import specific function to render @media, keyframes, font-faces or pseudo classes like :hover or ::after. Support nested and multiple selectors.

Live demo with complex selectors: Material TextField

Fast. And Will be More Faster!

Rendering CSS string, generating CSS rules and inserting them into DOM is really fast. There is example React application with implemented different approaches: fela / jss / glamor / styled-components / rockey.

Benchmark: parsing and generating CSS

npm run best-results -- --size 10000

Note that rockey and postcss were developed for different tasks. Rockey parser configured for specific syntax and will never be able to replace postcss

Benchmark: A-gambit/CSS-IN-JS-Benchmarks

Results could be found here.

Class Names

rockey uses separated CSS classes for each rule and for each mixin. That is why it is very сompatible with devtools. When change CSS values of parent component via devtools — it will be applied for all children.

rockey-react example (works same as rockey.addParent):

import rockey from 'rockey-react';

const Button = rockey.button('Button')`
  color: black;

  ${rockey.when('LargeButton', props => props.large)`
    font-size: 20px;
  `}
`;

const PrimaryButton = Button('PrimaryButton')`
  color: blue;
`;

const SuperButton = PrimaryButton('SuperButton')`
  color: red;
`;

Inserted CSS (after component is rendered):

.Button-{{ hash }} {
  color: black;
}

.PrimaryButton-{{ hash }} {
  color: blue;
}

.SuperButton-{{ hash }} {
  color: red;
}

.Mixin-LargeButton-{{ hash }}.Button-{{ hash }} {
  font-size: 20px;
}

And for <PrimaryButton large={true}/> className prop will equal .PrimaryButton-{{ hash }} .Button-{{ hash }} .Mixin-LargeButton-{{ hash }}.

That is why it is very сompatible with devtools. When change CSS values of parent component via devtools — it will be applied for all children.

If prop large is changed to false - only mixin class will be removed instead of all styles re-calculating. This is another reason why rockey is fast.

git how extends works

Dynamic CSS

Button {
  color: black;

  ${rockey.when('isPrimary', props => props.primary)`
    color: blue;
  `}

  Icon {
    margin: 5px;
  }
}

Inserted CSS:

.Button-{{ hash }} {
  color: black;
}

.isPrimary-{{ hash }}.Button-{{ hash }} {
  color: blue;
}

.Button-{{ hash }} .Icon-{{ hash }} {
  font-size: 12px;
}

Rockey React

Rockey was integrated with React. There are much more feature and advanteags.


Current Disadvantages

This is a very new and young library and not all features are implemented yet. But with each new release this list will be much and much shorter until there are no disadvantages :)

Examples

Contribute

After clone:

If you want to run rockey inside another applicaiton via npm link - run npm run dev at rockey to start watchers and transpile code.

benchmarks:

For nested CSS syntax:

npm run bench:nested -- --size {{ SIZE }}

For native CSS syntax:

npm run bench:native -- --size {{ SIZE }}

There is precommit hook (via husky) to run prettier for all staged files.

Feedback wanted

This is a very new approach and library and not all features are implemented yet. Feel free to file issue or suggest feature to help me to make rockey better. Or ping me on twitter @tuchk4.

🎉

Upcoming plans: