CSS-in-JS Utilities

3.1.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how several `css-in-js-utils` functions can be combined to merge style objects, convert them to CSS strings, and inspect CSS property characteristics.

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

view raw JSON →