Easy CSS Transform Builder
easy-css-transform-builder is a JavaScript/TypeScript utility library designed to simplify the creation of complex CSS `transform` values. It enables developers to define various transform properties like `translateX`, `scale`, `rotate3d`, and `skewY` within a simple JavaScript object, which the library then serializes into a valid CSS `transform` string. The current version is 0.1.0, indicating it is in its initial release phase without an established release cadence, and it ships with TypeScript types for enhanced developer experience. Its key differentiator is its straightforward, fluent API for programmatically generating CSS transform strings, making it particularly useful for dynamic styling and animation integrations within JavaScript-driven UI frameworks, as illustrated by its usage examples.
Common errors
-
TypeError: createCSSTransformBuilder is not a function
cause Attempting to use a CommonJS `require` statement for an ESM-first package or an incorrect named import.fixUse a named ESM import: `import { createCSSTransformBuilder } from 'easy-css-transform-builder';` -
ReferenceError: builder is not defined
cause The `builder` instance was not initialized or is out of scope when attempting to call it.fixEnsure `createCSSTransformBuilder()` is called to get a builder instance before using it: `const builder = createCSSTransformBuilder();` -
Argument of type 'number[]' is not assignable to parameter of type '[number, number, number]' for 'translate3d' (or similar TypeScript errors for 3D transforms with incorrect array lengths).
cause Providing an array with an incorrect number of elements for 3D transform functions like `translate3d`, `scale3d`, or `rotate3d` (e.g., 2 elements instead of 3 or 4).fixEnsure array inputs for `translate3d` and `scale3d` have exactly 3 elements, and `rotate3d` has exactly 4 elements (x, y, z, angle).
Warnings
- gotcha This package is currently in its very early stages (v0.1.0). While functional, expect potential API changes, limited community support, and unoptimized performance as it matures.
- gotcha Default units for length (`px`) and angle (`deg`) are applied to number values when not explicitly provided. Be mindful when mixing unit-less numbers with string-based unit values, as implicit units will be added to numbers.
- gotcha The library does not perform extensive validation on input values for transform properties. Providing invalid types (e.g., an object instead of a number for `translateX`) or out-of-range values may lead to unexpected CSS output or runtime errors in the browser.
Install
-
npm install easy-css-transform-builder -
yarn add easy-css-transform-builder -
pnpm add easy-css-transform-builder
Imports
- createCSSTransformBuilder
const createCSSTransformBuilder = require('easy-css-transform-builder');import { createCSSTransformBuilder } from 'easy-css-transform-builder'; - properties
import properties from 'easy-css-transform-builder/properties';
import { properties } from 'easy-css-transform-builder';
Quickstart
import { createCSSTransformBuilder } from 'easy-css-transform-builder';
// Initialize the builder, optionally setting default units.
const builder = createCSSTransformBuilder({
length: 'px', // Default for length values
angle: 'deg' // Default for angle values
});
// Define your transform properties as an object.
const transformProperties = {
translateX: 30,
scale: 2.8,
rotate3d: [1, 0, 0, 60],
skewY: '40rad' // Units can be explicitly provided as strings
};
// Generate the CSS transform string.
const cssTransformString = builder(transformProperties);
console.log(cssTransformString);
// Expected output: translateX(30px) scale(2.8) rotate3d(1, 0, 0, 60deg) skewY(40rad)
// Example of dynamic usage (e.g., in a React component's style prop)
const myElementStyle = {
transform: builder({
translateX: 100,
rotate: '45deg'
})
};
// In a real application, you'd apply myElementStyle to an element.
console.log('Dynamic style transform:', myElementStyle.transform);