StyleQ Atomic CSS Runtime
styleQ is a compact and efficient JavaScript runtime designed for merging HTML class names generated by Atomic CSS compilers. It provides high-performance merging capabilities for initial renders, incorporates built-in memoization to optimize updates, and seamlessly handles both static compiled styles and dynamic inline styles. The library is currently at version 0.2.1 and is actively maintained, though specific release cadence (e.g., weekly, monthly) is not explicitly stated but typically follows an as-needed pattern for utility libraries. Its key differentiators include its small gzipped size (0.7 KB), robust support for various CSS compiler designs, and its focus on performance for critical render paths. It contrasts with traditional CSS-in-JS solutions by operating on pre-compiled class names, acting as a final merging step.
Common errors
-
TypeError: styleq is not a function
cause Attempting to use `require()` for importing `styleq` in an environment that expects ESM imports, or trying to destructure a CommonJS module incorrectly.fixUse `import { styleq } from 'styleq';` instead of `const styleq = require('styleq');` for modern JavaScript modules. -
Property '$$css' is missing in type '{ ... }' but required in type 'CompiledStyle'.cause Passing an object to `styleq` that is intended to be a compiled style but lacks the mandatory `$$css` property.fixEnsure all compiled style objects explicitly include `$$css: true` (for production) or `$$css: 'debug-info'` (for development). -
The 'values' argument must be of type string. Received type object
cause This error (or similar type mismatches) can occur if a compiled style object's value is not a class string but another type (e.g., a raw CSS property value like `{ color: 'red' }` instead of `{ color: 'text-red-500' }`).fixVerify that compiled style objects passed to `styleq` have values that are valid HTML class strings, as `styleq` is designed to concatenate these strings.
Warnings
- gotcha Compiled style objects *must* include the `$$css` property. For production, `$$css: true` is typical. For development, a string value like `$$css: 'path/to/file:line'` can be used for debugging. Objects without this property are treated as inline styles.
- gotcha Values within compiled style objects (those with `$$css`) must be HTML class strings, not raw CSS property values. `styleq` merges class strings, it does not process CSS properties directly for compiled styles.
- gotcha Memoization is enabled by default. While generally beneficial for updates, disabling it via `styleq.factory({ disableCache: true })` might offer faster *initial* computations if your use case involves very few repeat merges.
- gotcha Inline styles are merged with static styles by default. If you need to manage these merges independently for specific performance characteristics or compiler designs, use `styleq.factory({ disableMix: true })`.
Install
-
npm install styleq -
yarn add styleq -
pnpm add styleq
Imports
- styleq
const styleq = require('styleq');import { styleq } from 'styleq'; - styleq.factory
import { styleq } from 'styleq'; const customStyleq = styleq.factory({ disableCache: true }); - Options (type)
import { Options } from 'styleq';import type { Options } from 'styleq';
Quickstart
import { styleq } from 'styleq';
// Example compiled styles from an Atomic CSS compiler
const compiledStyles = {
root: {
$$css: true, // Required for production compiled styles
display: 'display-flex-class',
alignItems: 'alignItems-center-class',
justifyContent: 'justifyContent-spaceBetween-class',
},
button: {
$$css: 'button.ts:15', // String for dev debugging
backgroundColor: 'bg-blue-500-class',
padding: 'p-4-class',
borderRadius: 'rounded-md-class',
},
};
// Example inline styles (without $$css)
const dynamicStyles = {
opacity: 0.8,
color: 'red',
};
// Merge compiled and inline styles
const [className, inlineStyle] = styleq(
compiledStyles.root,
compiledStyles.button,
dynamicStyles,
// Conditional styles
true && { cursor: 'pointer' },
false && { display: 'none' }
);
console.log('Generated className:', className);
console.log('Generated inlineStyle:', inlineStyle);
// Using styleq.factory for custom configurations
const styleqNoCache = styleq.factory({ disableCache: true });
const [classNameNoCache, inlineStyleNoCache] = styleqNoCache(compiledStyles.root);
console.log('Generated className (no cache):', classNameNoCache);