Skip to content

Commit 2a4f1bf

Browse files
authored
refactor: stylesheet types (#3099)
1 parent e8d72d7 commit 2a4f1bf

19 files changed

+222
-163
lines changed

.changeset/early-carrots-provide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-pdf/stylesheet": patch
3+
---
4+
5+
refactor: stylesheet types

packages/stylesheet/globals.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'hsl-to-hex';
2+
declare module 'color-string';
3+
declare module 'media-engine';
4+
declare module 'postcss-value-parser/lib/parse.js';
5+
declare module 'postcss-value-parser/lib/unit.js';

packages/stylesheet/src/resolve/borders.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,31 @@ const resolveBorderShorthand = <K extends BorderKey>(
113113
};
114114

115115
const handlers = {
116-
border: resolveBorderShorthand,
117-
borderBottom: resolveBorderShorthand,
118-
borderBottomColor: processColorValue,
119-
borderBottomLeftRadius: processUnitValue,
120-
borderBottomRightRadius: processUnitValue,
121-
borderBottomStyle: processNoopValue,
122-
borderBottomWidth: processUnitValue,
123-
borderColor: resolveBorderShorthand,
124-
borderLeft: resolveBorderShorthand,
125-
borderLeftColor: processColorValue,
126-
borderLeftStyle: processNoopValue,
127-
borderLeftWidth: processUnitValue,
128-
borderRadius: resolveBorderShorthand,
129-
borderRight: resolveBorderShorthand,
130-
borderRightColor: processColorValue,
131-
borderRightStyle: processNoopValue,
132-
borderRightWidth: processUnitValue,
133-
borderStyle: resolveBorderShorthand,
134-
borderTop: resolveBorderShorthand,
135-
borderTopColor: processColorValue,
136-
borderTopLeftRadius: processUnitValue,
137-
borderTopRightRadius: processUnitValue,
138-
borderTopStyle: processNoopValue,
139-
borderTopWidth: processUnitValue,
140-
borderWidth: resolveBorderShorthand,
116+
border: resolveBorderShorthand<'border'>,
117+
borderBottom: resolveBorderShorthand<'borderBottom'>,
118+
borderBottomColor: processColorValue<'borderBottomColor'>,
119+
borderBottomLeftRadius: processUnitValue<'borderBottomLeftRadius'>,
120+
borderBottomRightRadius: processUnitValue<'borderBottomRightRadius'>,
121+
borderBottomStyle: processNoopValue<'borderBottomStyle'>,
122+
borderBottomWidth: processUnitValue<'borderBottomWidth'>,
123+
borderColor: resolveBorderShorthand<'borderColor'>,
124+
borderLeft: resolveBorderShorthand<'borderLeft'>,
125+
borderLeftColor: processColorValue<'borderLeftColor'>,
126+
borderLeftStyle: processNoopValue<'borderLeftStyle'>,
127+
borderLeftWidth: processUnitValue<'borderLeftWidth'>,
128+
borderRadius: resolveBorderShorthand<'borderRadius'>,
129+
borderRight: resolveBorderShorthand<'borderRight'>,
130+
borderRightColor: processColorValue<'borderRightColor'>,
131+
borderRightStyle: processNoopValue<'borderRightStyle'>,
132+
borderRightWidth: processUnitValue<'borderRightWidth'>,
133+
borderStyle: resolveBorderShorthand<'borderStyle'>,
134+
borderTop: resolveBorderShorthand<'borderTop'>,
135+
borderTopColor: processColorValue<'borderTopColor'>,
136+
borderTopLeftRadius: processUnitValue<'borderTopLeftRadius'>,
137+
borderTopRightRadius: processUnitValue<'borderTopRightRadius'>,
138+
borderTopStyle: processNoopValue<'borderTopStyle'>,
139+
borderTopWidth: processUnitValue<'borderTopWidth'>,
140+
borderWidth: resolveBorderShorthand<'borderWidth'>,
141141
};
142142

143143
export default handlers;

packages/stylesheet/src/resolve/boxModel.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const logError = (style: any, value: any) => {
2424
};
2525

2626
/**
27-
* @param {Object} options
28-
* @param {Function} [options.expandsTo]
29-
* @param {number} [options.maxValues]
30-
* @param {boolean} [options.autoSupported]
27+
* @param options
28+
* @param [options.expandsTo]
29+
* @param [options.maxValues]
30+
* @param [options.autoSupported]
3131
*/
3232
const expandBoxModel =
3333
<S, E>({
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { processColorValue, processNumberValue } from './utils';
22

33
const handlers = {
4-
backgroundColor: processColorValue,
5-
color: processColorValue,
6-
opacity: processNumberValue,
4+
backgroundColor: processColorValue<'backgroundColor'>,
5+
color: processColorValue<'color'>,
6+
opacity: processNumberValue<'opacity'>,
77
};
88

99
export default handlers;
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { processUnitValue } from './utils';
22

33
const handlers = {
4-
height: processUnitValue,
5-
maxHeight: processUnitValue,
6-
maxWidth: processUnitValue,
7-
minHeight: processUnitValue,
8-
minWidth: processUnitValue,
9-
width: processUnitValue,
4+
height: processUnitValue<'height'>,
5+
maxHeight: processUnitValue<'maxHeight'>,
6+
maxWidth: processUnitValue<'maxWidth'>,
7+
minHeight: processUnitValue<'minHeight'>,
8+
minWidth: processUnitValue<'minWidth'>,
9+
width: processUnitValue<'width'>,
1010
};
1111

1212
export default handlers;
+26-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex#values
22

3+
import { parseFloat } from '@react-pdf/fns';
4+
import { Container, Style, StyleKey } from '../types';
35
import transformUnit from '../utils/units';
4-
import { processNoopValue, processUnitValue } from './utils';
6+
import {
7+
processNoopValue,
8+
processNumberValue,
9+
processUnitValue,
10+
} from './utils';
511

612
type FlexDefaults = (number | string | 'auto')[];
713

@@ -10,7 +16,11 @@ const flexDefaults: FlexDefaults = [1, 1, 0];
1016

1117
const flexAuto: FlexDefaults = [1, 1, 'auto'];
1218

13-
const processFlexShorthand = (key, value, container) => {
19+
const processFlexShorthand = <K extends StyleKey>(
20+
key: K,
21+
value: Style[K],
22+
container: Container,
23+
) => {
1424
let defaults = flexDefaults;
1525
let matches: FlexDefaults = [];
1626

@@ -20,26 +30,26 @@ const processFlexShorthand = (key, value, container) => {
2030
matches = `${value}`.split(' ');
2131
}
2232

23-
const flexGrow = transformUnit(container, matches[0] || defaults[0]);
24-
const flexShrink = transformUnit(container, matches[1] || defaults[1]);
33+
const flexGrow = parseFloat(matches[0] || defaults[0]);
34+
const flexShrink = parseFloat(matches[1] || defaults[1]);
2535
const flexBasis = transformUnit(container, matches[2] || defaults[2]);
2636

2737
return { flexGrow, flexShrink, flexBasis };
2838
};
2939

3040
const handlers = {
31-
alignContent: processNoopValue,
32-
alignItems: processNoopValue,
33-
alignSelf: processNoopValue,
34-
flex: processFlexShorthand,
35-
flexBasis: processUnitValue,
36-
flexDirection: processNoopValue,
37-
flexFlow: processNoopValue,
38-
flexGrow: processUnitValue,
39-
flexShrink: processUnitValue,
40-
flexWrap: processNoopValue,
41-
justifyContent: processNoopValue,
42-
justifySelf: processNoopValue,
41+
alignContent: processNoopValue<'alignContent'>,
42+
alignItems: processNoopValue<'alignItems'>,
43+
alignSelf: processNoopValue<'alignSelf'>,
44+
flex: processFlexShorthand<'flex'>,
45+
flexBasis: processUnitValue<'flexBasis'>,
46+
flexDirection: processNoopValue<'flexDirection'>,
47+
flexFlow: processNoopValue<'flexFlow'>,
48+
flexGrow: processNumberValue<'flexGrow'>,
49+
flexShrink: processNumberValue<'flexShrink'>,
50+
flexWrap: processNoopValue<'flexWrap'>,
51+
justifyContent: processNoopValue<'justifyContent'>,
52+
justifySelf: processNoopValue<'justifySelf'>,
4353
};
4454

4555
export default handlers;
+11-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
import { Container, Style, StyleKey } from '../types';
12
import transformUnit from '../utils/units';
23
import { processUnitValue } from './utils';
34

4-
const processGapShorthand = (key, value, container) => {
5+
const processGapShorthand = <K extends StyleKey>(
6+
key: K,
7+
value: Style[K],
8+
container: Container,
9+
) => {
510
const match = `${value}`.split(' ');
611

7-
const rowGap = transformUnit(container, match?.[0] || value);
8-
const columnGap = transformUnit(container, match?.[1] || value);
12+
const rowGap = transformUnit(container, match?.[0] || value) as any;
13+
const columnGap = transformUnit(container, match?.[1] || value) as any;
914

1015
return { rowGap, columnGap };
1116
};
1217

1318
const handlers = {
14-
gap: processGapShorthand,
15-
columnGap: processUnitValue,
16-
rowGap: processUnitValue,
19+
gap: processGapShorthand<'gap'>,
20+
columnGap: processUnitValue<'columnGap'>,
21+
rowGap: processUnitValue<'rowGap'>,
1722
};
1823

1924
export default handlers;

packages/stylesheet/src/resolve/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Handler = (
1919
style: Style,
2020
) => SafeStyle;
2121

22-
const shorthands = {
22+
const shorthands: Partial<Record<StyleKey, Handler>> = {
2323
...borderHandlers,
2424
...colorHandlers,
2525
...dimensionHandlers,
@@ -32,7 +32,7 @@ const shorthands = {
3232
...textHandlers,
3333
...transformHandlers,
3434
...svgHandlers,
35-
} as Record<StyleKey, Handler>;
35+
};
3636

3737
/**
3838
* Expand the shorthand properties.

packages/stylesheet/src/resolve/layout.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import {
55
} from './utils';
66

77
const handlers = {
8-
aspectRatio: processNumberValue,
9-
bottom: processUnitValue,
10-
display: processNoopValue,
11-
left: processUnitValue,
12-
position: processNoopValue,
13-
right: processUnitValue,
14-
top: processUnitValue,
15-
overflow: processNoopValue,
16-
zIndex: processNumberValue,
8+
aspectRatio: processNumberValue<'aspectRatio'>,
9+
bottom: processUnitValue<'bottom'>,
10+
display: processNoopValue<'display'>,
11+
left: processUnitValue<'left'>,
12+
position: processNoopValue<'position'>,
13+
right: processUnitValue<'right'>,
14+
top: processUnitValue<'top'>,
15+
overflow: processNoopValue<'overflow'>,
16+
zIndex: processNumberValue<'zIndex'>,
1717
};
1818

1919
export default handlers;

packages/stylesheet/src/resolve/margins.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ const processMarginSingle = processBoxModel<MarginStyle, MarginSafeStyle>({
3535
});
3636

3737
const handlers = {
38-
margin: processMargin,
39-
marginBottom: processMarginSingle,
40-
marginHorizontal: processMarginHorizontal,
41-
marginLeft: processMarginSingle,
42-
marginRight: processMarginSingle,
43-
marginTop: processMarginSingle,
44-
marginVertical: processMarginVertical,
38+
margin: processMargin<'margin'>,
39+
marginBottom: processMarginSingle<'marginBottom'>,
40+
marginHorizontal: processMarginHorizontal<'marginHorizontal'>,
41+
marginLeft: processMarginSingle<'marginLeft'>,
42+
marginRight: processMarginSingle<'marginRight'>,
43+
marginTop: processMarginSingle<'marginTop'>,
44+
marginVertical: processMarginVertical<'marginVertical'>,
4545
};
4646

4747
export default handlers;

packages/stylesheet/src/resolve/paddings.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ const processPaddingHorizontal = processBoxModel<
3333
const processPaddingSingle = processBoxModel<PaddingStyle, PaddingSafeStyle>();
3434

3535
const handlers = {
36-
padding: processPadding,
37-
paddingBottom: processPaddingSingle,
38-
paddingHorizontal: processPaddingHorizontal,
39-
paddingLeft: processPaddingSingle,
40-
paddingRight: processPaddingSingle,
41-
paddingTop: processPaddingSingle,
42-
paddingVertical: processPaddingVertical,
36+
padding: processPadding<'padding'>,
37+
paddingBottom: processPaddingSingle<'paddingBottom'>,
38+
paddingHorizontal: processPaddingHorizontal<'paddingHorizontal'>,
39+
paddingLeft: processPaddingSingle<'paddingLeft'>,
40+
paddingRight: processPaddingSingle<'paddingRight'>,
41+
paddingTop: processPaddingSingle<'paddingTop'>,
42+
paddingVertical: processPaddingVertical<'paddingVertical'>,
4343
};
4444

4545
export default handlers;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import transformUnit from '../utils/units';
22
import offsetKeyword from '../utils/offsetKeyword';
33
import { processNoopValue } from './utils';
4+
import { Container, Style, StyleKey } from '../types';
45

5-
const processObjectPosition = (key, value, container) => {
6+
const processObjectPosition = <K extends StyleKey>(
7+
key: K,
8+
value: Style[K],
9+
container: Container,
10+
) => {
611
const match = `${value}`.split(' ');
712

813
const objectPositionX = offsetKeyword(
@@ -11,19 +16,22 @@ const processObjectPosition = (key, value, container) => {
1116
const objectPositionY = offsetKeyword(
1217
transformUnit(container, match?.[1] || value),
1318
);
14-
1519
return { objectPositionX, objectPositionY };
1620
};
1721

18-
const processObjectPositionValue = (key, value, container) => ({
22+
const processObjectPositionValue = <K extends StyleKey>(
23+
key: K,
24+
value: Style[K],
25+
container: Container,
26+
) => ({
1927
[key]: offsetKeyword(transformUnit(container, value)),
2028
});
2129

2230
const handlers = {
23-
objectPosition: processObjectPosition,
24-
objectPositionX: processObjectPositionValue,
25-
objectPositionY: processObjectPositionValue,
26-
objectFit: processNoopValue,
31+
objectPosition: processObjectPosition<'objectPosition'>,
32+
objectPositionX: processObjectPositionValue<'objectPositionX'>,
33+
objectPositionY: processObjectPositionValue<'objectPositionY'>,
34+
objectFit: processNoopValue<'objectFit'>,
2735
};
2836

2937
export default handlers;

packages/stylesheet/src/resolve/svg.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import {
66
} from './utils';
77

88
const handlers = {
9-
fill: processColorValue,
10-
stroke: processColorValue,
11-
strokeDasharray: processNoopValue,
12-
strokeWidth: processUnitValue,
13-
fillOpacity: processNumberValue,
14-
strokeOpacity: processNumberValue,
15-
fillRule: processNoopValue,
16-
textAnchor: processNoopValue,
17-
strokeLinecap: processNoopValue,
18-
strokeLinejoin: processNoopValue,
19-
visibility: processNoopValue,
20-
clipPath: processNoopValue,
21-
dominantBaseline: processNoopValue,
9+
fill: processColorValue<'fill'>,
10+
stroke: processColorValue<'stroke'>,
11+
strokeDasharray: processNoopValue<'strokeDasharray'>,
12+
strokeWidth: processUnitValue<'strokeWidth'>,
13+
fillOpacity: processNumberValue<'fillOpacity'>,
14+
strokeOpacity: processNumberValue<'strokeOpacity'>,
15+
fillRule: processNoopValue<'fillRule'>,
16+
textAnchor: processNoopValue<'textAnchor'>,
17+
strokeLinecap: processNoopValue<'strokeLinecap'>,
18+
strokeLinejoin: processNoopValue<'strokeLinejoin'>,
19+
visibility: processNoopValue<'visibility'>,
20+
clipPath: processNoopValue<'clipPath'>,
21+
dominantBaseline: processNoopValue<'dominantBaseline'>,
2222
};
2323

2424
export default handlers;

0 commit comments

Comments
 (0)