@@ -16,8 +16,8 @@ This utility was originally created to simplify the process of adding inline CSS
1616
1717- Converts a single ` CSSProperties ` object to a CSS string.
1818- Converts a ` Record<string, CSSProperties> ` map to a CSS string.
19- - Automatically adds units (e.g., ` px ` ) where necessary .
20- - Optionally injects the ` !important ` statement for each style property .
19+ - Automatically adds units (` px ` by default) for numeric values .
20+ - Optionally injects the ` !important ` statement for each css declaration .
2121
2222## Installation
2323
3131yarn add react-style-stringify
3232```
3333
34- > [ !NOTE ]
34+ > [ !TIP ]
3535> This package uses the ` CSSProperties ` type from ` @types/react ` .
3636>
3737> If you're working with TypeScript and don't use React, install [ @types/react ] ( https://www.npmjs.com/package/@types/react ) .
@@ -56,22 +56,44 @@ const cssString = stringifyCSSProperties({
5656 backgroundColor: " teal" ,
5757});
5858// Output: "flex:1;padding:20px;background-color:teal;"
59- ```
60-
61- ** Inject ` !important ` into CSS string**
6259
63- ``` tsx
6460const importantCssString = stringifyCSSProperties (
6561 {
6662 flex: 1 ,
6763 padding: 20 ,
6864 backgroundColor: " teal" ,
6965 },
70- true
66+ { important: true } // `true` in versions <= 1.1.1
7167);
7268// Output: "flex:1!important;padding:20px!important;background-color:teal!important;"
69+
70+ const cssStringWtihDefinedUnit = stringifyCSSProperties (
71+ {
72+ padding: 10 ,
73+ fontSize: 1.6 ,
74+ },
75+ {
76+ unit: " em" ,
77+ }
78+ );
79+ // Output: "padding:10em;font-size:1.6em;"
80+
81+ const cssStringWtihDefinedUnitMap = stringifyCSSProperties (
82+ {
83+ padding: 10 ,
84+ fontSize: 1.6 ,
85+ },
86+ {
87+ unit: { fontSize: " rem" },
88+ }
89+ );
90+ // Output: "padding:10px;font-size:1.6rem;"
7391```
7492
93+ > [ !WARNING]
94+ > In versions ` <= 1.1.1 ` , only ` true ` was accepted as the second argument.
95+ > As of ` v1.2.0 ` , the options object ` { important: true } ` is recommended.
96+
7597### Convert a ` Record<string, CSSProperties> ` object
7698
7799``` tsx
@@ -87,45 +109,90 @@ const cssMapString = stringifyStyleMap({
87109// Output: "p{margin:0;color:teal;}#root ul.my-list>li{padding:10px;}"
88110```
89111
90- ** Inject ` !important ` into CSS string**
112+ > [ !NOTE]
113+ > The ` options ` argument is forwarded internally to ` stringifyCSSProperties ` , so all options (like ` important ` or ` unit ` ) work the same way.
114+
115+ ### Generic
91116
92- ``` tsx
93- const importantCssMapString = stringifyStyleMap (
94- {
95- p: {
96- margin: 0 ,
97- color: " teal" ,
98- },
99- " #root ul.my-list > li" : {
117+ ``` ts
118+ import {
119+ stringifyStyleDeclaration ,
120+ stringifyStyleRule ,
121+ } from " react-style-stringify" ;
122+
123+ type MyStyle = {
124+ padding: number ;
125+ fontSize: number ;
126+ };
127+
128+ stringifyStyleDeclaration <MyStyle >({
129+ padding: 10 ,
130+ fontSize: 16 ,
131+ })
132+ // Output: "padding:10px;font-size:16px;"
133+
134+ stringifyStyleRule <MyStyle >({
135+ " .container" : {
100136 padding: 10 ,
101- } ,
137+ fontSize: 16 ,
102138 },
103- true
104- );
105- // Output: "p{margin:0!important;color:teal!important;}#root ul.my-list>li{padding:10px!important;}"
139+ });
140+ // Output: ".container{"padding:10px;font-size:16px;"}"
106141```
107142
143+ > [ !NOTE]
144+ > The ` options ` argument works the same way as for ` stringifyCSSProperties ` and ` stringifyStyleMap ` .
145+
108146## API
109147
110- ### Exported Types
148+ ### Types
111149
112- #### ` StyleMap: Record<string, CSSProperties> `
150+ ``` ts
151+ type StyleMap = Record <string , CSSProperties >;
113152
114- Defines a map where keys are CSS selectors ( ` string ` ) and values are ` CSSProperties ` objects, which represent inline CSS styles in React.
153+ type CSSUnit = " px " | " em " | " rem " | " vw " | " vh " | " % " ;
115154
116- ### Exported Functions
155+ type CSSUnitMap <K extends PropertyKey = string > = {
156+ [P in K ]? : CSSUnit ;
157+ };
117158
118- #### ` stringifyCSSProperties(style: CSSProperties, important?: boolean): string `
159+ type StringifyOptions <T extends object = Record <string , string | number >> = {
160+ important? : boolean ;
161+ unit? : CSSUnit | CSSUnitMap <keyof T >;
162+ };
119163
120- Converts a single ` CSSProperties ` object to a CSS string. Automatically adds units (e.g., ` px ` ) where required.
164+ type StyleDeclaration = Record < string , string | number >;
121165
122- When set ` important ` argument to ` true ` , appends ` !important ` to each CSS property in the resulting string. Default is ` false ` .
166+ type StyleRule <T extends object = StyleDeclaration > = Record <string , T >;
167+ ```
123168
124- #### ` stringifyStyleMap(styles: StyleMap, important?: boolean): string `
169+ ### Functions
125170
126- Converts a ` StyleMap ` object to a string.
171+ ``` ts
172+ function stringifyCSSProperties(
173+ cssProperties : CSSProperties ,
174+ optionsOrImportant ? : StringifyOptions <CSSProperties > | boolean
175+ ): string ;
127176
128- When set ` important ` argument to ` true ` , appends ` !important ` to each CSS property in the resulting string. Default is ` false ` .
177+ function stringifyStyleMap(
178+ styleMap : StyleMap ,
179+ optionsOrImportant ? : StringifyOptions <CSSProperties > | boolean
180+ ): string ;
181+ ```
182+
183+ ### Generic
184+
185+ ``` ts
186+ function stringifyStyleDeclaration<T extends object = StyleDeclaration >(
187+ styleDeclaration : T ,
188+ options ? : StringifyOptions <T >
189+ ): string ;
190+
191+ function stringifyStyleRule<T extends object = StyleDeclaration >(
192+ styleRule : StyleRule <T >,
193+ options ? : StringifyOptions <T >
194+ ): string ;
195+ ```
129196
130197## Dependencies
131198
0 commit comments