React with Styles CSS Interface

6.0.0 · maintenance · verified Tue Apr 21

This package serves as an interface for `react-with-styles`, enabling the compilation of CSS-in-JS styles into static CSS classes. It's designed to produce deterministic and human-friendly class names, which aids in easily overriding styles with standard CSS. At version 6.0.0, it integrates with `react-with-styles` (latest major version 4.x) and leverages a separate compiler (`react-with-styles-interface-css-compiler`) to generate physical CSS files during the build process, distinguishing it from runtime CSS-in-JS solutions. The library is part of the AirBnB open-source ecosystem, which typically implies a maintenance cadence, focusing on stability rather than rapid feature development. Its primary differentiation lies in offering a build-time static CSS solution within the `react-with-styles` abstraction, promoting performance and simplified debugging through predictable CSS output.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register the `CSSInterface` with `react-with-styles`, optionally namespace class names, and then apply styles to a React component using `withStyles` and `css` functions.

import ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet';
import CSSInterface, { registerCSSInterfaceNamespace } from 'react-with-styles-interface-css';
import { withStyles, css } from 'react-with-styles';

// 1. Register the CSS interface with react-with-styles
ThemedStyleSheet.registerInterface(CSSInterface);

// 2. Optional: Register a namespace to prefix generated class names
const namespace = 'MyAwesomeApp';
registerCSSInterfaceNamespace(namespace);

// 3. Define your styles using the withStyles HOC
const MyStyledComponent = withStyles(({ color, unit }) => ({
  container: {
    backgroundColor: color.primary,
    padding: 2 * unit,
    borderRadius: unit,
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    color: color.white,
    fontSize: 3 * unit,
    fontWeight: 'bold',
  },
}))(({ styles, css, message = "Hello, CSS Interface!" }) => (
  <div {...css(styles.container)}>
    <span {...css(styles.text)}>{message}</span>
  </div>
));

// Example usage (this part would typically be in your app's component tree)
// This example is for demonstration; a full app would also need a ThemeProvider
// (from react-with-styles) to provide `color` and `unit`.
// console.log(<MyStyledComponent />);

view raw JSON →