Skip to content

Commit 2e82f66

Browse files
committed
Merge branch 'main' of github.com:ArtemKarlov/react-style-stringify into main
2 parents 0cedd34 + 55a07d0 commit 2e82f66

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Run tests
1+
name: Tests
22

33
on:
44
pull_request:

README.md

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
[![NPM Type Definitions](https://img.shields.io/npm/types/react-style-stringify?color=3178C6)](https://www.npmjs.com/package/react-style-stringify)
77
[![NPM Downloads](https://img.shields.io/npm/dm/react-style-stringify)](https://www.npmjs.com/package/react-style-stringify)
88

9+
[![GitHub Actions - Tests](https://github.com/arkarlov/react-style-stringify/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/arkarlov/react-style-stringify/actions/workflows/tests.yml)
10+
911
A utility for converting React `CSSProperties` objects or `Record<string, CSSProperties>` into CSS strings.
1012

1113
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.
@@ -31,38 +33,75 @@ yarn add react-style-stringify
3133
> [!NOTE]
3234
> This package uses the `CSSProperties` type from `@types/react`.
3335
>
34-
> If you're working with TypeScript and don't use React, install `@types/react`.
36+
> If you're working with TypeScript and don't use React, install [@types/react](https://www.npmjs.com/package/@types/react).
3537
3638
## Usage
3739

38-
Here’s a basic example of how to use the package:
40+
### Import utils
3941

4042
```tsx
4143
import {
4244
stringifyCSSProperties,
4345
stringifyStyleMap,
4446
} from "react-style-stringify";
47+
```
48+
49+
### Convert a single `CSSProperties` object
4550

46-
// Convert a single CSSProperties object
51+
```tsx
4752
const cssString = stringifyCSSProperties({
4853
flex: 1,
4954
padding: 20,
5055
backgroundColor: "teal",
5156
});
5257
// Output: "flex:1; padding:20px; background-color:teal;"
58+
```
5359

54-
// Convert a Record<string, CSSProperties> object
60+
**Inject `!important` into CSS string**
61+
62+
```tsx
63+
const importantCssString = stringifyCSSProperties(
64+
{
65+
flex: 1,
66+
padding: 20,
67+
backgroundColor: "teal",
68+
},
69+
true
70+
);
71+
// Output: "flex:1!important; padding:20px!important; background-color:teal!important;"
72+
```
73+
74+
### Convert a `Record<string, CSSProperties>` object
75+
76+
```tsx
5577
const cssMapString = stringifyStyleMap({
56-
div: {
57-
color: "blue",
58-
marginBottom: 20,
78+
p: {
79+
margin: 0,
80+
color: "teal",
5981
},
60-
".my-class": {
82+
"#root ul.my-list > li": {
6183
padding: 10,
62-
fontSize: 14,
6384
},
6485
});
65-
// Output: "div{color:blue; margin-bottom:20px;} .my-class{padding:10px; font-size:14px;}"
86+
// Output: "p{margin:0; color:teal;} #root ul.my-list > li{padding:10px;}"
87+
```
88+
89+
**Inject `!important` into CSS string**
90+
91+
```tsx
92+
const importantCssMapString = stringifyStyleMap(
93+
{
94+
p: {
95+
margin: 0,
96+
color: "teal",
97+
},
98+
"#root ul.my-list > li": {
99+
padding: 10,
100+
},
101+
},
102+
true
103+
);
104+
// Output: "p{margin:0!important; color:teal!important;} #root ul.my-list > li{padding:10px!important;}"
66105
```
67106

68107
## API

0 commit comments

Comments
 (0)