State-Driven Styling in JavaScript
npm install fela
javascript
import { createRenderer } from 'fela'
// a simple style rule is a pure function of state
// that returns an object of style declarations
const rule = (state) => ({
textAlign: 'center',
padding: '5px 10px',
// directly use the state to compute style values
fontSize: state.fontSize + 'pt',
borderRadius: 5,
// deeply nest media queries and pseudo classes
':hover': {
fontSize: state.fontSize + 2 + 'pt',
boxShadow: '0 0 2px rgb(70, 70, 70)',
},
})
const renderer = createRenderer()
// fela generates atomic CSS classes in order to achieve
// maximal style reuse and minimal CSS output
const className = renderer.renderRule(rule, {
fontSize: 14,
}) // => a b c d e f
`
The generated CSS output would look like this:
`CSS
.a { text-align: center }
.b { padding: 5px 10px }
.c { font-size: 14pt }
.d { border-radius: 5px }
.e:hover { font-size: 16pt }
.f:hover { box-shadow: 0 0 2px rgb(70, 70, 70) }
`
$3
If you're using Fela, you're most likely also using React.
Using the React bindings, you get powerful APIs to create primitive components.
> Read: Usage with React for a full guide.
`jsx
import * as React from 'react'
import { useFela } from 'react-fela'
const rule = ({ fontSize }) => ({
textAlign: 'center',
padding: '5px 10px',
// directly use the props to compute style values
fontSize: fontSize + 'pt',
borderRadius: 5,
':hover': {
fontSize: fontSize + 2 + 'pt',
boxShadow: '0 0 2px rgb(70, 70, 70)',
},
})
function Button({ fontSize, children }) {
const { css } = useFela({ fontSize })
return
}
`
> Check this example on CodeSandbox
Examples
- Fela + React (source)
- React Styleguidist (source)
- React Native (source)
- ReasonReact
- Next
- Fela + Preact (source)
- Fela + Inferno (source)
- Fela + HyperScript
- Fela + Cycle
- Fela + Next.js
Documentation
- Getting Started
- Ecosystem
- Migration Guide
- Changelog
- 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 (Slides)
- _by Robin Weser_
- CSS in JS: Patterns
- _by Vojtech Miksu_
Posts
- Style as a Function of State
- _by Robin Weser_
- CSS in JS: The Argument Refined
- _by Daniel Steigerwald_
- What is Fela?
- _by David Sinclair_
- Choosing a CSS in JS library
- _by Thomas Roch_
- Introducing Fela 6.0
- _by Robin Weser_
- A journey into CSS and then into CSS-in-JS
- _by Prithvi Raju_
- CSS In JS — Future of styling components
- _by Manjula Dube_
- Styling Components with React Fela
- _by Josh Sherman_
- The Future of Fela
- _by Robin Weser_
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-fela connect` with auto-bound styles