Fela

State-Driven Styling in JavaScript

README

Fela

Fela is a small, high-performant and framework-agnostic toolbelt to handle state-driven styling in JavaScript.
It is dynamic by design and renders your styles depending on your application state.
It generates atomic CSS and supports all common CSS features such as media queries, pseudo classes, keyframes and font-faces. Fela ships with a powerful plugin API adding e.g. vendor prefixing or fallback value support.
Fela can be used with React or with any other view library. It even supports React Native.
Bundlephobia npm downloads Spectrum

Support Us

Support Robin Weser's work on Fela and its ecosystem directly via [GitHub Sponsors](https://github.com/sponsors/robinweser).

Benefits

- Predictable Styling
- Scoped Atomic CSS
- Minimal Bundle Size
- No Specificity Issues
- No Naming Conflicts
- Framework-Agnostic
- Huge Ecosystem
- RTL Support

The Gist

Fela's core principle is to consider [style as a function of state](http://fela.js.org/docs/latest/intro/principles).
The whole API and all plugins and bindings are built on that idea.
It is reactive and auto-updates once registered to the DOM.
The following example illustrates the key parts of Fela though it only shows the very basics.
  1. ``` js
  2. import { createRenderer } from 'fela'
  3. // a simple style rule is a pure function of state
  4. // that returns an object of style declarations
  5. const rule = (state) => ({
  6.   textAlign: 'center',
  7.   padding: '5px 10px',
  8.   // directly use the state to compute style values
  9.   fontSize: state.fontSize + 'pt',
  10.   borderRadius: 5,
  11.   // deeply nest media queries and pseudo classes
  12.   ':hover': {
  13.     fontSize: state.fontSize + 2 + 'pt',
  14.     boxShadow: '0 0 2px rgb(70, 70, 70)',
  15.   },
  16. })
  17. const renderer = createRenderer()
  18. // fela generates atomic CSS classes in order to achieve
  19. // maximal style reuse and minimal CSS output
  20. const className = renderer.renderRule(rule, {
  21.   fontSize: 14,
  22. }) // =>  a b c d e f
  23. ```
The generated CSS output would look like this:
  1. ```CSS
  2. .a { text-align: center }
  3. .b { padding: 5px 10px }
  4. .c { font-size: 14pt }
  5. .d { border-radius: 5px }
  6. .e:hover { font-size: 16pt }
  7. .f:hover { box-shadow: 0 0 2px rgb(70, 70, 70) }
  8. ```

Primitive Components

If you're using Fela, you're most likely also using React.
Using the [React bindings](packages/react-fela), you get powerful APIs to create primitive components.

Read: Usage with React for a full guide.

  1. ``` js
  2. import * as React from 'react'
  3. import { useFela } from 'react-fela'
  4. const rule = ({ fontSize }) => ({
  5.   textAlign: 'center',
  6.   padding: '5px 10px',
  7.   // directly use the props to compute style values
  8.   fontSize: fontSize + 'pt',
  9.   borderRadius: 5,
  10.   ':hover': {
  11.     fontSize: fontSize + 2 + 'pt',
  12.     boxShadow: '0 0 2px rgb(70, 70, 70)',
  13.   },
  14. })
  15. function Button({ fontSize, children }) {
  16.   const { css } = useFela({ fontSize })
  17.   return <button className={css(rule)}>{children}</button>
  18. }
  19. ```

Check this example on CodeSandbox

Examples

  - Next

Documentation

- FAQ

Workshop

If you are coming from CSS and want to learn JavaScript Styling with Fela, there is a full-feature fela-workshop which demonstrates typical Fela use cases. It teaches all important parts, step by step with simple examples. If you already know other CSS in JS solutions and are just switching to Fela, you might not need to do the whole workshop, but it still provides useful information to get started quickly.

Talks

- [**CSS in JS: The Good & Bad Parts**](https://www.youtube.com/watch?v=95M-2YzyTno) ([Slides](https://speakerdeck.com/robinweser/css-in-js-the-good-and-bad-parts))
- _by [Robin Weser](https://twitter.com/robinweser)_ - [**CSS in JS: Patterns**](https://www.webexpo.net/prague2018/talk/css-in-js-patterns)
- _by [Vojtech Miksu](https://twitter.com/vmiksu)_

Posts

- [**Style as a Function of State**](https://weser.io/blog/style-as-a-function-of-state)
- _by [Robin Weser](https://twitter.com/robinweser)_ - [**CSS in JS: The Argument Refined**](https://medium.com/@steida/css-in-js-the-argument-refined-471c7eb83955#.3otvkubq4)
- _by [Daniel Steigerwald](https://twitter.com/steida)_ - [**What is Fela?**](https://davidsinclair.io/thoughts/what-is-fela)
- _by [David Sinclair](https://davidsinclair.io)_ - [**Choosing a CSS in JS library**](https://gist.github.com/troch/c27c6a8cc47b76755d848c6d1204fdaf#file-choosing-a-css-in-js-library-md)
- _by [Thomas Roch](https://twitter.com/tcroch)_ - [**Introducing Fela 6**](https://weser.io/blog/introducing-fela-6)
- _by [Robin Weser](https://twitter.com/robinweser)_ - [**A journey into CSS and then into CSS-in-JS**](https://www.zeolearn.com/magazine/a-journey-into-css-and-then-into-css-in-js)
- _by [Prithvi Raju](https://twitter.com/aga5tya)_ - [**CSS In JS — Future of styling components**](https://we-are.bookmyshow.com/css-in-js-future-of-styling-components-ad315eb5448b)
- _by [Manjula Dube](https://twitter.com/manjula_dube)_ - [**Styling Components with React Fela**](https://alligator.io/react/styling-with-react-fela/)
- _by [Josh Sherman](https://twitter.com/joshtronic)_ - [**The Future of Fela**](https://weser.io/blog/the-future-of-fela)
- _by [Robin Weser](https://twitter.com/robinweser)_ - [**Introducing Fela 12**](https://weser.io/blog/introducing-fela-12)
- _by [Robin Weser](https://twitter.com/robinweser)_

Ecosystem

There are tons of useful packages maintained within this repository including plugins, enhancers, bindings and tools that can be used together with Fela. Check the Ecosystem documentation for a quick overview.

Community

Apart from all the packages managed within this repository, there are many community third-party projects that are worth mentioning:
- aesthetic - React style and theme layer with Fela support
- base-styling-components - Abstract Box and Text Components
- bs-react-fela - BuckleScript / ReasonReact bindings for Fela
- catstack - A modular mad science framework for teams working on production web apps
- css-in-js-playground - A simple playground for CSS in JS solutions
- cf-ui - Cloudflare UI Framework
- counter-component-with-react-mobx-fela - Counter Component using Fela
- cycle-fela - Cycle bindings for Fela
- dogstack - A popular-choice grab-bag framework for teams working on production web apps
- fela-components - Styling library for React and Fela
- fela-react-helpers - A set of useful helpers for Fela
- fela-react-prop - Generate class names for fela style rule and apply them as property on a wrapped component
- fela-styles-connector - Simplified react-felaconnect with auto-bound styles
- frejya: Pass styles as props to components
- gatsby-plugin-fela - Integrates fela with Gatsby
- hyper-fela - HyperScript bindings for Fela
- htz-frontend - Source for Haaretz frontend app(s)
- kilvin - Primitive React Layout Components with Fela
- olymp - Create and build a next gen app using node, react, cssInJS and other cool stuff
- preact-fela-simple - Super simple Preact bindings for Fela
- reason-react-starter - A ReasonReact starter kit using Fela
- storybook-addon-props-fela: Document the props of your Fela components in storybook.
- superslider - Slider Component using Fela
- telaviv - React Universal Rendering
- vashet - ClojureScript wrapper for Fela
- veel - Base react styling components using fela with a design system
- vue-fela - Vue bindings for Fela
- black-box - combines behavior, presentation, structure in one place & creates all-in-one components using only JS syntax

Support

Got a question? Come and join us on [Github Discussion](https://github.com/robinweser/fela/discussions)!
We'd love to help out. We also highly appreciate any feedback.
Don't want to miss any update? Follow us on Twitter.

Who's using Fela?

Check all the logos on the website.

Want to add yours? Please create a new issue with your logo attached and we will add it!

Contributing

This project exists thanks to all the people who contribute.
We highly appreciate any contribution.
For more information follow the [contribution guide](.github/CONTRIBUTING.md).
Also, please read our code of conduct.

License

Fela is licensed under the [MIT License](http://opensource.org/licenses/MIT).
Documentation is licensed under [Creative Commons License](http://creativecommons.org/licenses/by/4.0/).
Created with ♥ by @robinweser and all the great contributors.