Skip to content

Commit 0a8e25e

Browse files
committed
Refactor box-shadow to handle 0 values and throw on unitless values
1 parent e0376a8 commit 0a8e25e

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

src/index.spec.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ describe("misc", () => {
230230
color: #656656;
231231
box-shadow: 10px 20px 30px #fff;
232232
}
233-
233+
234234
.container {
235235
padding: 30px;
236236
margin-top: 65px;
@@ -1198,6 +1198,64 @@ describe("box-shadow", () => {
11981198
});
11991199
});
12001200

1201+
it("trims values", () => {
1202+
expect(
1203+
transform(`
1204+
.test {
1205+
box-shadow: 10px 20px 30px #f00 ;
1206+
}
1207+
`),
1208+
).toEqual({
1209+
test: {
1210+
shadowOffset: { width: 10, height: 20 },
1211+
shadowRadius: 30,
1212+
shadowColor: "#f00",
1213+
},
1214+
});
1215+
});
1216+
1217+
it("transforms box-shadow with 0 values", () => {
1218+
expect(
1219+
transform(`
1220+
.test {
1221+
box-shadow: 0 0 1px red;
1222+
}
1223+
`),
1224+
).toEqual({
1225+
test: {
1226+
shadowOffset: { width: 0, height: 0 },
1227+
shadowRadius: 1,
1228+
shadowColor: "red",
1229+
},
1230+
});
1231+
expect(
1232+
transform(`
1233+
.test {
1234+
box-shadow: 0 0 0 red;
1235+
}
1236+
`),
1237+
).toEqual({
1238+
test: {
1239+
shadowOffset: { width: 0, height: 0 },
1240+
shadowRadius: 0,
1241+
shadowColor: "red",
1242+
},
1243+
});
1244+
expect(
1245+
transform(`
1246+
.test {
1247+
box-shadow: 1px 1px 0 #00f;
1248+
}
1249+
`),
1250+
).toEqual({
1251+
test: {
1252+
shadowOffset: { width: 1, height: 1 },
1253+
shadowRadius: 0,
1254+
shadowColor: "#00f",
1255+
},
1256+
});
1257+
});
1258+
12011259
it("transforms box-shadow without blur-radius", () => {
12021260
expect(
12031261
transform(`
@@ -1265,6 +1323,32 @@ describe("box-shadow", () => {
12651323
`);
12661324
}).toThrowError('Failed to parse declaration "boxShadow: 10px"');
12671325
});
1326+
1327+
it("transforms box-shadow and enforces units for non 0 values", () => {
1328+
expect(() => {
1329+
transform(`
1330+
.test {
1331+
box-shadow: 10 20px 30px #f00;
1332+
}
1333+
`);
1334+
}).toThrowError(
1335+
'Failed to parse declaration "boxShadow: 10 20px 30px #f00"',
1336+
);
1337+
expect(() => {
1338+
transform(`
1339+
.test {
1340+
box-shadow: 10px 20;
1341+
}
1342+
`);
1343+
}).toThrowError('Failed to parse declaration "boxShadow: 10px 20"');
1344+
expect(() => {
1345+
transform(`
1346+
.test {
1347+
box-shadow: 20;
1348+
}
1349+
`);
1350+
}).toThrowError('Failed to parse declaration "boxShadow: 20"');
1351+
});
12681352
});
12691353

12701354
describe("rem unit", () => {

src/transforms/boxShadow.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
const removePx = val => val.replace(/(\d+)px/g, "$1");
2+
3+
const formatNumber = val => Number(removePx(val));
4+
5+
const matchNumbers = val => val.match(/(^|\s+)\d+(px)?/g);
6+
7+
const isZeroOrPxValue = val => val === "0" || /\d+px/.test(val);
8+
9+
const filterNonNumbers = val =>
10+
val.split(" ").filter(val => isNaN(formatNumber(val)));
11+
12+
const filterNumbers = nums => {
13+
if (!nums) {
14+
return [];
15+
}
16+
return nums.map(val => val.trim()).map(val => {
17+
if (!isZeroOrPxValue(val)) {
18+
return undefined;
19+
}
20+
return formatNumber(val);
21+
});
22+
};
23+
124
export const boxShadowToShadowProps = value => {
2-
const pxs = value.match(/(\d*\.?\d+)px/g);
3-
const nums = pxs
4-
? pxs.map(val => val.replace("px", "")).map(val => Number(val))
5-
: [];
25+
const nums = filterNumbers(matchNumbers(value));
26+
const nonNums = filterNonNumbers(value);
627
const offsetX = nums[0];
728
const offsetY = nums[1];
829
const blurRadius = nums[2];
9-
const filtered = pxs
10-
? value.split(" ").filter(val => pxs.indexOf(val) === -1)
11-
: [];
12-
const color = filtered[0];
30+
const color = nonNums[0];
1331

1432
if (offsetX === undefined || offsetY === undefined) {
1533
throw new Error(`Failed to parse declaration "boxShadow: ${value}"`);

0 commit comments

Comments
 (0)