
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-style-stringify
Advanced tools
A utility for converting React CSSProperties and style maps into CSS strings, designed to simplify the management of inline styles in HTML email templates and React projects.
A utility for converting React CSSProperties objects or Record<string, CSSProperties> into CSS strings.
This utility was originally created to simplify the process of adding inline CSS styles to HTML email templates in a React project. Previously, all styles were written as plain strings, which became unmanageable as the project grew. To make styles more maintainable and consistent, this utility was developed to convert React CSSProperties objects into CSS strings, streamlining the process of embedding styles in the final HTML before sending emails.
CSSProperties object to a CSS string.Record<string, CSSProperties> map to a CSS string.px by default) for numeric values.!important statement for each css declaration.npm install react-style-stringify
or
yarn add react-style-stringify
[!TIP] This package uses the
CSSPropertiestype from@types/react.If you're working with TypeScript and don't use React, install @types/react.
import {
stringifyCSSProperties,
stringifyStyleMap,
} from "react-style-stringify";
CSSProperties objectconst cssString = stringifyCSSProperties({
flex: 1,
padding: 20,
backgroundColor: "teal",
});
// Output: "flex:1;padding:20px;background-color:teal;"
const importantCssString = stringifyCSSProperties(
{
flex: 1,
padding: 20,
backgroundColor: "teal",
},
{ important: true } // `true` in versions <= 1.1.1
);
// Output: "flex:1!important;padding:20px!important;background-color:teal!important;"
const cssStringWtihDefinedUnit = stringifyCSSProperties(
{
padding: 10,
fontSize: 1.6,
},
{
unit: "em",
}
);
// Output: "padding:10em;font-size:1.6em;"
const cssStringWtihDefinedUnitMap = stringifyCSSProperties(
{
padding: 10,
fontSize: 1.6,
},
{
unit: { fontSize: "rem" },
}
);
// Output: "padding:10px;font-size:1.6rem;"
[!WARNING] In versions
<= 1.1.1, onlytruewas accepted as the second argument. As ofv1.2.0, the options object{ important: true }is recommended.
Record<string, CSSProperties> objectconst cssMapString = stringifyStyleMap({
p: {
margin: 0,
color: "teal",
},
"#root ul.my-list > li": {
padding: 10,
},
});
// Output: "p{margin:0;color:teal;}#root ul.my-list>li{padding:10px;}"
[!NOTE] The
optionsargument is forwarded internally tostringifyCSSProperties, so all options (likeimportantorunit) work the same way.
import {
stringifyStyleDeclaration,
stringifyStyleRule,
} from "react-style-stringify";
type MyStyle = {
padding: number;
fontSize: number;
};
stringifyStyleDeclaration<MyStyle>({
padding: 10,
fontSize: 16,
})
// Output: "padding:10px;font-size:16px;"
stringifyStyleRule<MyStyle>({
".container": {
padding: 10,
fontSize: 16,
},
});
// Output: ".container{"padding:10px;font-size:16px;"}"
[!NOTE] The
optionsargument works the same way as forstringifyCSSPropertiesandstringifyStyleMap.
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>;
function stringifyCSSProperties(
cssProperties: CSSProperties,
optionsOrImportant?: StringifyOptions<CSSProperties> | boolean
): string;
function stringifyStyleMap(
styleMap: StyleMap,
optionsOrImportant?: StringifyOptions<CSSProperties> | boolean
): string;
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;
line-height, z-index, etc.).CSSProperties type for defining style objects.Contributions are welcome! If you have ideas or improvements, feel free to open an issue or submit a pull request.
git checkout -b feature-name).git commit -am 'Add new feature').git push origin feature-name).Please make sure your code adheres to the project's coding standards and passes the existing tests.
FAQs
A utility for converting React CSSProperties and style maps into CSS strings, designed to simplify the management of inline styles in HTML email templates and React projects.
The npm package react-style-stringify receives a total of 47,479 weekly downloads. As such, react-style-stringify popularity was classified as popular.
We found that react-style-stringify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.