-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringifyCSSProperties.ts
28 lines (25 loc) · 1.14 KB
/
stringifyCSSProperties.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { type CSSProperties } from "react";
import { applyCssUnits, camelToKebab } from "./utils";
/**
* Converts a CSSProperties object into a CSS string.
*
* @param {CSSProperties} cssProperties - An object representing the CSS properties, where the keys are camelCased property names and the values are the corresponding CSS values.
* @param {boolean} [isImportant=false] - A flag indicating whether to append the `!important` statement to each CSS property. Defaults to `false`.
*
* @returns {string} - A formatted CSS string, with properties converted to kebab-case and units added where necessary. If `isImportant` is true, each property will be suffixed with `!important`.
*/
export function stringifyCSSProperties(
cssProperties: CSSProperties,
isImportant: boolean = false
) {
if (typeof cssProperties !== "object" || cssProperties === null) {
throw new Error("Invalid input: 'cssProperties' must be an object.");
}
const important = isImportant ? "!important" : "";
return Object.entries(cssProperties)
.map(
([key, value]) =>
`${camelToKebab(key)}:${applyCssUnits(key, value)}${important};`
)
.join("");
}