React Fela Bindings

12.2.1 · active · verified Sun Apr 19

react-fela provides the official React bindings for the Fela dynamic styling library. It facilitates CSS-in-JS styling within React applications, leveraging Fela's core capabilities for atomic CSS generation, performance optimization, and server-side rendering. The current stable version is 12.2.1, with a consistent release cadence that includes minor feature additions, TypeScript improvements, and bug fixes across patch versions. Key differentiators include its focus on a functional, atomic CSS approach, robust plugin system (enhanced with context-free plugins in v12), and efficient rehydration mechanisms. It assumes familiarity with the core Fela library for its effective use within a React ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic setup of Fela with React using the `useFela` hook, `RendererProvider`, and `ThemeProvider` to apply dynamic styles and theme values in a functional component.

import { createRenderer } from 'fela';
import { RendererProvider, ThemeProvider, useFela } from 'react-fela';
import { useState } from 'react';

const renderer = createRenderer();

const theme = {
  primary: 'blue',
  secondary: 'green',
  spacing: 16,
};

type ButtonProps = {
  onClick: () => void;
  children: React.ReactNode;
};

const Button: React.FC<ButtonProps> = ({ onClick, children }) => {
  const { css } = useFela((props: { primaryColor: string }) => ({
    button: {
      backgroundColor: props.primaryColor,
      color: 'white',
      padding: `${theme.spacing / 2}px ${theme.spacing}px`,
      borderRadius: '4px',
      border: 'none',
      cursor: 'pointer',
      fontSize: '16px',
      '&:hover': {
        opacity: 0.8,
      },
    },
  }));

  return (
    <button className={css('button', { primaryColor: theme.primary })} onClick={onClick}>
      {children}
    </button>
  );
};

const App: React.FC = () => {
  const [count, setCount] = useState(0);

  return (
    <RendererProvider renderer={renderer}>
      <ThemeProvider theme={theme}>
        <div style={{ fontFamily: 'sans-serif', padding: theme.spacing }}>
          <h1>Fela with React Example</h1>
          <p>Count: {count}</p>
          <Button onClick={() => setCount(prev => prev + 1)}>Increment</Button>
          <div className={useFela(() => ({
            marginTop: theme.spacing,
            color: theme.secondary,
            fontSize: '14px'
          }))().css({})}>
            Styled with `useFela` hook directly on a div.
          </div>
        </div>
      </ThemeProvider>
    </RendererProvider>
  );
};

export default App;

view raw JSON →