• Stars
    star
    2,273
  • Rank 20,269 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

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

Repository Details

State-Driven Styling in JavaScript

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.

Benefits

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

Read more about the benefits!

The Gist

Fela's core principle is to consider style as a function of state.
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.

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:

.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) }

Primitive Components

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.

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 <button className={css(rule)}>{children}</button>
}

Check this example on CodeSandbox

Examples

Documentation

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

Posts

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:

Support

Got a question? Come and join us on Github Discussion!
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.
Also, please read our code of conduct.

License

Fela is licensed under the MIT License.
Documentation is licensed under Creative Commons License.
Created with ♥ by @robinweser and all the great contributors.

More Repositories

1

react-look

Advanced & Dynamic Component Styling for React and React Native. Ships with powerful Plugins, Mixins and Developer Tools. (Deprecated: use Fela)
JavaScript
583
star
2

inline-style-prefixer

Autoprefixer for JavaScript style objects
JavaScript
393
star
3

react-layout-components

Layout Components for React based on Flexbox
JavaScript
337
star
4

react-controlled-form

React Forms with Zod Validation
TypeScript
118
star
5

react-css-component

Injecting CSS via React Components
JavaScript
94
star
6

alveron

Tiny (0.8kb) Elm-inspired state management for React
JavaScript
74
star
7

css-in-js-utils

Useful utility functions for CSS in JS solutions
TypeScript
74
star
8

bredon

A modern CSS value compiler in JavaScript
JavaScript
40
star
9

elodin

The Elodin Styling Language
JavaScript
30
star
10

fast-loops

Tiny (0.6kb), performant & immutable iteration utilities for arrays and objects
JavaScript
25
star
11

kilvin

Primitive React Layout Components using Fela
JavaScript
22
star
12

alveron-old

Opinionated Elm-inspired Redux Component Architecture for React
JavaScript
17
star
13

inline-style-prefix-all

Static Autoprefixer for inline styles
JavaScript
16
star
14

elodin-old

Quality and Optimisation tools for CSS in JavaScript
JavaScript
16
star
15

inline-style-expand-shorthand

Expanding shorthand properties in JavaScript style objects
JavaScript
13
star
16

brandeur

Convenience tool belt for css-hooks
JavaScript
12
star
17

small-color

Tiny (0.8kb), opinionated & tree-shakable alternative to color
JavaScript
7
star
18

small-date

Tiny (0.8kb) date formatting library with built-in i18n
JavaScript
6
star
19

tehlu

Component System for React and Brandeur
JavaScript
5
star
20

Calculate.js

Calculate.js is kind of an extension to the JavaScript build-in Math-Library.
JavaScript
5
star
21

ambrose

Configurable Design System Skeleton for React
JavaScript
5
star
22

react-markdown-github-renderers

react-markdown renderers for GitHub-like appearance
JavaScript
5
star
23

pogolookup

Ads-free, Privacy-first Pokemon Go Data Lookup & Analysis Fanpage
JavaScript
5
star
24

inline-style-transformer

Transformation tools for inline styles
JavaScript
5
star
25

lorren

A PDF Framework for React based on react-pdf
JavaScript
4
star
26

radium-plugin-prefix-all

Radium plugin that adds all vendor prefixes
JavaScript
4
star
27

react-create-keyframe

Helper to create and render keyframes on-demand in React
TypeScript
3
star
28

Obscene-UI

[Sass] Obscene UI is a responsive, mobile-friendly UI-Template to build stable and beautiful app themes
CSS
3
star
29

assign-styles

ES6 Object.assign() polyfill especially for inline-styles respecting !important notation
JavaScript
3
star
30

radium-plugin-linter

[devTool] Radium plugin that enables style linting with inline-style-linter
JavaScript
2
star
31

read-transform-write

Transform files with ease
JavaScript
2
star
32

doc-badges

Useful badges for Code & API documentation
2
star
33

theme-centric-component-design

Example for theme-centric component design
JavaScript
2
star
34

tokenize-sync

Simple synchronous string tokenizer using Regex
JavaScript
1
star
35

react-onegraph

React Bindings for OneGraph's Authentication Client
JavaScript
1
star
36

unitless-css-property

Helper to check for unitless CSS property names
JavaScript
1
star
37

rofrischmann

My personal portfolio webpage
HTML
1
star
38

speaking

Speaker resources and talk slides
JavaScript
1
star
39

flow-semantic-versioning

Enforced Semantic Versioning for Flow
JavaScript
1
star