Skip to content

Commit 6aab68b

Browse files
authored
fix: treat fontSizeAdjust, initialLetter, mathDepth, shapeImageThreshold as unitless (#5872) (#5890)
* fix: treat fontSizeAdjust, initialLetter, mathDepth, shapeImageThreshold as unitless (#5872) * fix: use sorted Set in addUnitIfNeeded so Object.prototype keys take units
1 parent 4eaf66b commit 6aab68b

3 files changed

Lines changed: 62 additions & 41 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'styled-components': patch
3+
---
4+
5+
Numbers for `fontSizeAdjust`, `hyphenateLimitChars`, `initialLetter`, `mathDepth`, `shapeImageThreshold`, `maskBorderOutset`, `maskBorderSlice`, and `maskBorderWidth` in object styles render without a unit:
6+
7+
```ts
8+
const Heading = styled.h1({
9+
fontSizeAdjust: 0.5,
10+
hyphenateLimitChars: 6,
11+
initialLetter: 3,
12+
mathDepth: 1,
13+
shapeImageThreshold: 0.7,
14+
});
15+
// font-size-adjust: 0.5; hyphenate-limit-chars: 6; initial-letter: 3; math-depth: 1; shape-image-threshold: 0.7;
16+
```
17+
18+
Components that set any of these properties to a number get a new class name on upgrade, since the emitted CSS changes.

‎packages/styled-components/src/utils/addUnitIfNeeded.ts‎

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,15 @@
22
* CSS properties that accept unitless numeric values.
33
* Inlined from @emotion/unitless with IE-only entries removed
44
* (boxFlex, boxFlexGroup, boxOrdinalGroup, flexPositive, flexNegative,
5-
* flexOrder, msGridRow, msGridRowSpan, msGridColumn, msGridColumnSpan).
5+
* flexOrder, msGridRow, msGridRowSpan, msGridColumn, msGridColumnSpan),
6+
* plus fontSizeAdjust, hyphenateLimitChars, initialLetter, mathDepth,
7+
* shapeImageThreshold, and the maskBorder* aliases of the borderImage* entries.
68
*/
7-
const unitless: Record<string, 1> = {
8-
animationIterationCount: 1,
9-
aspectRatio: 1,
10-
borderImageOutset: 1,
11-
borderImageSlice: 1,
12-
borderImageWidth: 1,
13-
columnCount: 1,
14-
columns: 1,
15-
flex: 1,
16-
flexGrow: 1,
17-
flexShrink: 1,
18-
gridRow: 1,
19-
gridRowEnd: 1,
20-
gridRowSpan: 1,
21-
gridRowStart: 1,
22-
gridColumn: 1,
23-
gridColumnEnd: 1,
24-
gridColumnSpan: 1,
25-
gridColumnStart: 1,
26-
fontWeight: 1,
27-
lineHeight: 1,
28-
opacity: 1,
29-
order: 1,
30-
orphans: 1,
31-
scale: 1,
32-
tabSize: 1,
33-
widows: 1,
34-
zIndex: 1,
35-
zoom: 1,
36-
WebkitLineClamp: 1,
37-
fillOpacity: 1,
38-
floodOpacity: 1,
39-
stopOpacity: 1,
40-
strokeDasharray: 1,
41-
strokeDashoffset: 1,
42-
strokeMiterlimit: 1,
43-
strokeOpacity: 1,
44-
strokeWidth: 1,
45-
};
9+
const unitless = new Set(
10+
'WebkitLineClamp animationIterationCount aspectRatio borderImageOutset borderImageSlice borderImageWidth columnCount columns fillOpacity flex flexGrow flexShrink floodOpacity fontSizeAdjust fontWeight gridColumn gridColumnEnd gridColumnSpan gridColumnStart gridRow gridRowEnd gridRowSpan gridRowStart hyphenateLimitChars initialLetter lineHeight maskBorderOutset maskBorderSlice maskBorderWidth mathDepth opacity order orphans scale shapeImageThreshold stopOpacity strokeDasharray strokeDashoffset strokeMiterlimit strokeOpacity strokeWidth tabSize widows zIndex zoom'.split(
11+
' '
12+
)
13+
);
4614

4715
// Taken from https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/shared/dangerousStyleValue.js
4816
export default function addUnitIfNeeded(name: string, value: any) {
@@ -51,7 +19,7 @@ export default function addUnitIfNeeded(name: string, value: any) {
5119
return '';
5220
}
5321

54-
if (typeof value === 'number' && value !== 0 && !(name in unitless) && !name.startsWith('--')) {
22+
if (typeof value === 'number' && value !== 0 && !unitless.has(name) && !name.startsWith('--')) {
5523
return value + 'px'; // Presumes implicit 'px' suffix for unitless numbers except for CSS variables
5624
}
5725

‎packages/styled-components/src/utils/test/addUnitIfNeeded.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,38 @@ it('does not add a px prefix for unitless properties', () => {
2020
];
2121
pairs.forEach(([key, value]) => expect(addUnitIfNeeded(key, value)).toEqual(String(value)));
2222
});
23+
24+
it('does not add a px prefix to properties whose grammar takes a bare number', () => {
25+
// https://drafts.csswg.org/css-fonts-5/#propdef-font-size-adjust
26+
// "font-size-adjust Value: none | [ ex-height | cap-height | ch-width | ic-width | ic-height ]? [ from-font | <number [0,∞]> ]"
27+
expect(addUnitIfNeeded('fontSizeAdjust', 0.5)).toEqual('0.5');
28+
29+
// https://drafts.csswg.org/css-inline-3/#propdef-initial-letter
30+
// "initial-letter Value: normal | <number [1,∞]> <integer [1,∞]> | <number [1,∞]> && [ drop | raise ]?"
31+
expect(addUnitIfNeeded('initialLetter', 3)).toEqual('3');
32+
33+
// https://w3c.github.io/mathml-core/#propdef-math-depth
34+
// "math-depth Value: auto-add | add(<integer>) | <integer>"
35+
expect(addUnitIfNeeded('mathDepth', 2)).toEqual('2');
36+
37+
// https://drafts.csswg.org/css-shapes-1/#propdef-shape-image-threshold
38+
// "shape-image-threshold Value: <opacity-value>"
39+
expect(addUnitIfNeeded('shapeImageThreshold', 0.5)).toEqual('0.5');
40+
41+
// https://drafts.csswg.org/css-text-4/#propdef-hyphenate-limit-chars
42+
// "hyphenate-limit-chars Value: [ auto | <integer [0,∞]> ]{1,3}"
43+
expect(addUnitIfNeeded('hyphenateLimitChars', 3)).toEqual('3');
44+
45+
// https://drafts.csswg.org/css-masking-1/#propdef-mask-border-slice
46+
// "mask-border-slice Value: <'border-image-slice'>"
47+
// "mask-border-width Value: <'border-image-width'>"
48+
// "mask-border-outset Value: <'border-image-outset'>"
49+
expect(addUnitIfNeeded('maskBorderSlice', 30)).toEqual('30');
50+
expect(addUnitIfNeeded('maskBorderWidth', 2)).toEqual('2');
51+
expect(addUnitIfNeeded('maskBorderOutset', 1)).toEqual('1');
52+
});
53+
54+
it('treats an Object.prototype key as a property that takes a unit', () => {
55+
// No prototype key is a CSS property, so only the listed names are unitless.
56+
expect(addUnitIfNeeded('constructor', 5)).toEqual('5px');
57+
});

0 commit comments

Comments
 (0)