CSS-in-JS Utilities
css-in-js-utils is a specialized utility library offering foundational functions for authors building CSS-in-JS solutions. It provides a suite of primitives like `assignStyle` for deep merging style objects, `camelCaseProperty` and `hyphenateProperty` for property name transformations, and `cssifyObject` to convert style objects into CSS strings. The current stable version is 3.1.0. While new feature development might be infrequent given its utility nature, the package demonstrates ongoing maintenance, with recent activity on its GitHub repository. This library distinguishes itself by targeting core functionalities required by other CSS-in-JS libraries, abstracting away common boilerplate, rather than serving as an end-user styling tool. Its design emphasizes stability and reusability of these low-level operations within the broader CSS-in-JS ecosystem.
Common errors
-
TypeError: [functionName] is not a function
cause Attempting to use CommonJS `require()` syntax (e.g., `const { assignStyle } = require('css-in-js-utils')`) in an environment where the package's primary export is ESM, or if using a build setup that doesn't correctly transpile ESM imports.fixEnsure your project is configured for ESM and use `import { functionName } from 'css-in-js-utils'` syntax. If using CommonJS, verify your build configuration or check the `package.json` `exports` field for CJS compatibility. -
TS2305: Module '"css-in-js-utils"' has no exported member 'default'.
cause Attempting to use a default import (e.g., `import StyleUtils from 'css-in-js-utils'`) when the library exclusively provides named exports.fixAll utilities are named exports. Use named imports: `import { assignStyle, cssifyObject } from 'css-in-js-utils'`. -
TypeError: Cannot read properties of undefined (reading 'property') or 'value'
cause Passing `undefined`, `null`, or an incorrect data type to a utility function that expects a string or object, such as `cssifyDeclaration(property, value)` where `property` or `value` is not a string/number as expected.fixAlways ensure that the arguments passed to `css-in-js-utils` functions match the expected types (e.g., string property names, string or number values, plain objects for style maps). Refer to the specific function's documentation for its required input types.
Warnings
- gotcha This package is explicitly designed for CSS-in-JS *library authors* and not for direct use in application development. Using it directly in your application code might lead to unnecessary complexity or unexpected behavior, as its APIs are low-level and optimized for library integration.
- breaking While specific breaking changes for v3.0.0 are not extensively documented in public release notes for `robinweser/css-in-js-utils`, a major version increment from 1.x.x to 3.x.x typically indicates potentially incompatible API changes. Users upgrading from v1.x.x should thoroughly review their usage and test for regressions, especially concerning argument signatures, return types, or edge case handling in utility functions.
Install
-
npm install css-in-js-utils -
yarn add css-in-js-utils -
pnpm add css-in-js-utils
Imports
- assignStyle
const { assignStyle } = require('css-in-js-utils')import { assignStyle } from 'css-in-js-utils' - cssifyObject
import CssUtils from 'css-in-js-utils'; CssUtils.cssifyObject(...)
import { cssifyObject } from 'css-in-js-utils' - camelCaseProperty
import * as Utils from 'css-in-js-utils'; Utils.camelCaseProperty(...)
import { camelCaseProperty } from 'css-in-js-utils'
Quickstart
import { assignStyle, cssifyObject, hyphenateProperty, isUnitlessProperty } from 'css-in-js-utils';
// Define some base styles and overrides
const baseStyles = {
backgroundColor: 'red',
paddingTop: '10px',
':hover': {
color: 'white',
fontSize: '16px'
}
};
const overrideStyles = {
backgroundColor: 'blue',
margin: '8px',
':hover': {
fontSize: '18px',
fontWeight: 'bold'
}
};
console.log('--- Demonstrating css-in-js-utils ---');
// 1. Merge styles deeply using assignStyle
const mergedStyles = assignStyle(baseStyles, overrideStyles);
console.log('Merged Styles:', mergedStyles);
/*
Output:
Merged Styles: {
backgroundColor: 'blue',
paddingTop: '10px',
margin: '8px',
':hover': {
color: 'white',
fontSize: '18px',
fontWeight: 'bold'
}
}
*/
// 2. Convert the top-level merged style object into a CSS string
// Note: cssifyObject only processes direct properties, not nested ones like ':hover'
const cssString = cssifyObject(mergedStyles);
console.log('CSS String (top-level):', cssString);
// Output: CSS String (top-level): background-color:blue;padding-top:10px;margin:8px
// 3. Demonstrate hyphenateProperty
const camelCaseProp = 'WebkitAppearance';
const hyphenatedProp = hyphenateProperty(camelCaseProp);
console.log(`Hyphenated property for "${camelCaseProp}":`, hyphenatedProp);
// Output: Hyphenated property for "WebkitAppearance": -webkit-appearance
// 4. Demonstrate isUnitlessProperty (e.g., for 'zIndex')
console.log('Is "zIndex" unitless?', isUnitlessProperty('zIndex')); // true
console.log('Is "width" unitless?', isUnitlessProperty('width')); // false