An object of CSS properties that don't accept values with units
npx @tessl/cli install tessl/npm-emotion--unitless@0.10.0@emotion/unitless is a utility library that provides a comprehensive object of CSS properties that don't accept unit values. This package is essential for CSS-in-JS libraries to automatically determine when to omit units from numeric style values, enabling proper styling for properties like flex, opacity, and zIndex that should remain unitless.
npm install @emotion/unitlessimport unitless from "@emotion/unitless";For CommonJS:
const unitless = require("@emotion/unitless");import unitless from "@emotion/unitless";
// Check if a CSS property should be unitless
if (unitless.flex) {
// flex property doesn't need units: flex: 1 (not flex: 1px)
}
if (!unitless.width) {
// width property needs units: width: 100px (not width: 100)
}
// Common usage in CSS-in-JS libraries
function addUnitsIfNeeded(property: string, value: number): string {
if (unitless[property]) {
return value.toString();
}
return value + 'px';
}
// Examples:
addUnitsIfNeeded('flex', 1); // Returns: "1"
addUnitsIfNeeded('opacity', 0.5); // Returns: "0.5"
addUnitsIfNeeded('width', 100); // Returns: "100px"
addUnitsIfNeeded('height', 50); // Returns: "50px"The main functionality is a single object that maps CSS property names to the value 1 for properties that should not have units when given numeric values.
/**
* Object containing CSS properties that don't accept unit values
* Each property maps to 1 if it's unitless, undefined otherwise
*/
declare const unitless: Record<string, 1>;
export default unitless;Properties included (47 total):
Animation Properties:
animationIterationCount: 1Aspect Ratio:
aspectRatio: 1Border Image Properties:
borderImageOutset: 1borderImageSlice: 1borderImageWidth: 1Legacy Box Model (deprecated but supported):
boxFlex: 1boxFlexGroup: 1boxOrdinalGroup: 1Column Properties:
columnCount: 1columns: 1Flexbox Properties:
flex: 1flexGrow: 1flexPositive: 1flexShrink: 1flexNegative: 1flexOrder: 1CSS Grid Properties:
gridRow: 1gridRowEnd: 1gridRowSpan: 1gridRowStart: 1gridColumn: 1gridColumnEnd: 1gridColumnSpan: 1gridColumnStart: 1Microsoft Grid Properties (legacy):
msGridRow: 1msGridRowSpan: 1msGridColumn: 1msGridColumnSpan: 1Typography Properties:
fontWeight: 1lineHeight: 1Layout Properties:
opacity: 1order: 1orphans: 1scale: 1tabSize: 1widows: 1zIndex: 1zoom: 1WebKit Properties:
WebkitLineClamp: 1SVG Properties:
fillOpacity: 1floodOpacity: 1stopOpacity: 1strokeDasharray: 1strokeDashoffset: 1strokeMiterlimit: 1strokeOpacity: 1strokeWidth: 1/**
* Type definition for the unitless object
* Properties that are unitless map to 1, others are undefined
*/
type UnitlessProperties = Record<string, 1 | undefined>;import unitless from "@emotion/unitless";
function applyStyles(element: HTMLElement, styles: Record<string, number | string>) {
Object.entries(styles).forEach(([property, value]) => {
if (typeof value === 'number') {
// Add units only if the property requires them
const finalValue = unitless[property] ? value.toString() : `${value}px`;
element.style.setProperty(property, finalValue);
} else {
element.style.setProperty(property, value);
}
});
}
// Usage:
applyStyles(myElement, {
flex: 1, // Applied as "1"
opacity: 0.8, // Applied as "0.8"
width: 200, // Applied as "200px"
height: 100, // Applied as "100px"
zIndex: 999 // Applied as "999"
});import unitless from "@emotion/unitless";
function isUnitlessProperty(property: string): boolean {
return property in unitless;
}
// Examples:
isUnitlessProperty('flex'); // true
isUnitlessProperty('opacity'); // true
isUnitlessProperty('width'); // false
isUnitlessProperty('margin'); // falseimport unitless from "@emotion/unitless";
function validateNumericStyle(property: string, value: number): void {
if (!unitless[property] && value !== 0) {
console.warn(`Property "${property}" with value ${value} may need units`);
}
}
validateNumericStyle('flex', 1); // No warning
validateNumericStyle('width', 100); // Warning: may need units