Skip to content

v1.2.0

Latest
Compare
Choose a tag to compare
@arkarlov arkarlov released this 12 Jun 20:32
· 1 commit to main since this release

New options Object & Generic API

v1.2.0 (2025-06-12)

✨ New Features

Support for options object in stringifyCSSProperties and stringifyStyleMap:

stringifyCSSProperties(style, {
  important: true,
  unit: 'em',
});

✅ Fully backward compatible: you can still pass true for important statement as in previous versions.

⚠️ However, using an options object is now recommended for clarity and future extensibility.

Inject important statment

Before

stringifyCSSProperties(style, true); // still supported

After

stringifyCSSProperties(style, { important: true });

Set css units for numeric values

stringifyCSSProperties(
  {
    padding: 10,
    fontSize: 1.6,
  },
  {
    unit: { fontSize: "rem" }, // use string (e.g. { unit: 'rem' }) to set common unit for all numeric values
  }
);
// Output: "padding:10px;font-size:1.6rem;"

New API

Types

type StyleMap = Record<string, CSSProperties>;

type CSSUnit = "px" | "em" | "rem" | "vw" | "vh" | "%";

type CSSUnitMap<K extends PropertyKey = string> = {
    [P in K]?: CSSUnit;
};

type StringifyOptions<T extends object = Record<string, string | number>> = {
    important?: boolean;
    unit?: CSSUnit | CSSUnitMap<keyof T>;
};

type StyleDeclaration = Record<string, string | number>;

type StyleRule<T extends object = StyleDeclaration> = Record<string, T>;

Functions

function stringifyCSSProperties(
  cssProperties: CSSProperties,
  optionsOrImportant?: StringifyOptions<CSSProperties> | boolean
): string;

function stringifyStyleMap(
  styleMap: StyleMap,
  optionsOrImportant?: StringifyOptions<CSSProperties> | boolean
): string;

Generic

function stringifyStyleDeclaration<T extends object = StyleDeclaration>(
  styleDeclaration: T,
  options?: StringifyOptions<T>
): string;

function stringifyStyleRule<T extends object = StyleDeclaration>(
  styleRule: StyleRule<T>,
  options?: StringifyOptions<T>
): string;

🤖 What's Changed

Full Changelog: v1.1.1...v1.2.0