React with Styles CSS Interface
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
-
ThemedStyleSheet.registerInterface is not a function
cause `ThemedStyleSheet` was not imported correctly or `react-with-styles` is not installed/incorrectly configured.fixEnsure `import ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet';` is used and `react-with-styles` is installed (peer dependency). Check the `react-with-styles` documentation for proper setup. -
Styles are not applying or are being overridden unexpectedly.
cause Class name collisions due to non-unique style names or improper handling of specificity.fixReview all style definitions for duplicate class names. Utilize `registerCSSInterfaceNamespace('YourAppName')` to prefix all generated class names. Avoid attempting to override the internal specificity selectors and stick to overriding via the original class names. Use browser developer tools to inspect the generated class names and their applied styles to identify conflicts. -
Module not found: Can't resolve 'react-with-styles-interface-css'
cause The package is not installed or the import path is incorrect.fixRun `npm install react-with-styles-interface-css` or `yarn add react-with-styles-interface-css`. Verify the import statement `import CSSInterface from 'react-with-styles-interface-css';`.
Warnings
- gotcha Class name collisions can occur if styles across your project are not given unique names, leading to unintended style overrides. The generated static CSS relies on these names being distinct.
- gotcha The CSS output by this interface uses specifiers with varying levels of specificity to maintain compatibility with `aphrodite`'s style resolution logic. Directly overriding these generated specifiers can lead to unpredictable behavior and should be avoided.
- breaking The `react-with-styles` ecosystem, including this interface, is considered to be in maintenance by Airbnb, with its last major `react-with-styles` update in 2021. While stable, this implies less active development and potential for slower adoption of new React features or changes in the broader CSS-in-JS landscape.
- gotcha Actual CSS file generation requires the separate `react-with-styles-interface-css-compiler` package. Forgetting to integrate this compiler into your build pipeline will result in no CSS output despite defining styles correctly in your components.
Install
-
npm install react-with-styles-interface-css -
yarn add react-with-styles-interface-css -
pnpm add react-with-styles-interface-css
Imports
- CSSInterface
import { CSSInterface } from 'react-with-styles-interface-css'; // Not a named export const CSSInterface = require('react-with-styles-interface-css'); // CommonJS is deprecated for modern React libsimport CSSInterface from 'react-with-styles-interface-css';
- registerCSSInterfaceNamespace
import registerCSSInterfaceNamespace from 'react-with-styles-interface-css'; // Not a default export
import { registerCSSInterfaceNamespace } from 'react-with-styles-interface-css'; - ThemedStyleSheet
import { ThemedStyleSheet } from 'react-with-styles'; // Incorrect path, ThemedStyleSheet is not a root exportimport ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet';
Quickstart
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 />);