Skip to content

Commit f80b042

Browse files
authored
Fix: Skip explicit undefined values in stringifyCSSProperties (#12)
* fix: handle explicit undefined values in CSS props * docs: add publishing reminder
1 parent a152dc2 commit f80b042

7 files changed

Lines changed: 73 additions & 12 deletions

File tree

docs/publishing.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Manual Package Publishing Flow
2+
3+
1. ✅ Make code changes
4+
5+
Create a branch from `main`, commit and push your changes, then open a Pull Request.
6+
Merge the PR into `main` after it's reviewed and tests pass.
7+
8+
2. 🔄 Switch to the `main` branch
9+
10+
Ensure you're on the latest `main` branch:
11+
12+
```bash
13+
git checkout main
14+
git pull origin main
15+
```
16+
17+
3. 🔖 Bump the version using npm
18+
19+
Use [semver](https://semver.org/) to choose the correct bump type:
20+
21+
```bash
22+
npm version patch # or minor / major
23+
```
24+
25+
4. 🚀 Push changes and tags
26+
27+
```bash
28+
git push
29+
git push origin vX.Y.Z
30+
```
31+
32+
5. ✅ Run tests (before build)
33+
34+
```bash
35+
npm test
36+
```
37+
38+
6. 🧱 Build the package
39+
40+
```bush
41+
npm run build
42+
```
43+
44+
7. 📦 Publish to npm
45+
46+
```bush
47+
npm publish
48+
```
49+
50+
8. 📝 (Optional) Add release notes
51+
52+
Create or edit a release on GitHub for the new tag and describe the changes.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"scripts": {
3333
"build": "tsup",
34-
"test": "vitest --config vitest.config.ts"
34+
"test": "vitest run --config vitest.config.ts",
35+
"test:watch": "vitest --config vitest.config.ts"
3536
},
3637
"dependencies": {
3738
"@emotion/unitless": "^0.10.0"
@@ -45,4 +46,4 @@
4546
"engines": {
4647
"node": ">=14.0.0"
4748
}
48-
}
49+
}

src/__tests__/stringifyCSSProperties.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ describe("stringifyCSSProperties", () => {
2525
);
2626
});
2727

28-
it("throws error for wrong CSS-value", () => {
29-
expect(() =>
28+
it("skips CSS properties with wrong CSS value", () => {
29+
const expected = "padding:5px 10px;color:teal;";
30+
const actual = stringifyCSSProperties({
3031
//@ts-ignore
31-
stringifyCSSProperties({ color: { color: "teal" } })
32-
).toThrowError(
33-
"Invalid input: value of 'cssProperties' must be string or number."
34-
);
32+
margin: { top: 10 },
33+
padding: "5px 10px",
34+
background: undefined,
35+
color: "teal",
36+
//@ts-ignore
37+
border: null,
38+
});
39+
40+
expect(actual).toBe(expected);
3541
});
3642

3743
it("doesn't change string CSS-value", () => {

src/stringifyCSSProperties.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type CSSProperties } from "react";
2-
import { applyCssUnits, camelToKebab } from "./utils";
2+
import { applyCssUnits, camelToKebab, isCSSPropertyValue } from "./utils";
33

44
/**
55
* Converts a CSSProperties object into a CSS string.
@@ -20,6 +20,7 @@ export function stringifyCSSProperties(
2020
const important = isImportant ? "!important" : "";
2121

2222
return Object.entries(cssProperties)
23+
.filter(([_, value]) => isCSSPropertyValue(value))
2324
.map(
2425
([key, value]) =>
2526
`${camelToKebab(key)}:${applyCssUnits(key, value)}${important};`

src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import { type CSSProperties } from "react";
22

3-
export type CSSSelector = string;
4-
5-
export type StyleMap = Record<CSSSelector, CSSProperties>;
3+
export type StyleMap = Record<string, CSSProperties>;

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./camelToKebab";
22
export * from "./isUnitless";
33
export * from "./applyCssUnits";
44
export * from "./trimCssSelector";
5+
export * from "./isCSSPropertyValue";

src/utils/isCSSPropertyValue.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isCSSPropertyValue = (value: unknown): value is number | string =>
2+
typeof value === "number" || typeof value === "string";

0 commit comments

Comments
 (0)