@@ -3,17 +3,17 @@ import { jsx } from 'theme-ui'
3
3
import { useTheme } from './context'
4
4
import TypeStyle from './TypeStyle'
5
5
6
- const getValue = ( fontSizes : Array < string | number > , key : number ) => {
7
- const raw = fontSizes [ key ]
8
- if ( typeof raw !== 'number' ) return raw
9
- return raw + 'px'
10
- }
6
+ const getValue = ( fontSize : string | number ) =>
7
+ typeof fontSize === 'number' ? `${ fontSize } px` : fontSize
11
8
12
9
export interface TypeScaleProps {
13
10
reverse ?: boolean
14
11
}
15
12
export const TypeScale = ( { reverse = true , ...props } ) => {
16
- const fontSizes = ( useTheme ( ) ! . fontSizes as Array < string | number > ) || [ ]
13
+ const theme = useTheme ( ) || { }
14
+ const fontSizeEntries = reverse
15
+ ? Object . entries ( theme . fontSizes || [ ] ) . reverse ( )
16
+ : Object . entries ( theme . fontSizes || [ ] )
17
17
18
18
return (
19
19
< div
@@ -22,16 +22,22 @@ export const TypeScale = ({ reverse = true, ...props }) => {
22
22
flexWrap : 'wrap' ,
23
23
alignItems : 'baseline' ,
24
24
} } >
25
- { fontSizes . map ( ( n , i ) => {
26
- const key = reverse ? fontSizes . length - 1 - i : i
27
- return (
25
+ { fontSizeEntries . map ( ( [ key , val ] ) => {
26
+ if ( typeof val === 'object' ) {
27
+ // TODO: can theme.fontSizes be a deeply nested object?
28
+ // This wasn't handled previously
29
+ // We should either update the types or recursively render here.
30
+ return null
31
+ }
32
+
33
+ return (
28
34
< TypeStyle
29
- key = { i }
35
+ key = { key }
30
36
fontSize = { key }
31
37
sx = { {
32
38
mr : 3 ,
33
39
} }
34
- children = { getValue ( fontSizes , key ) }
40
+ children = { getValue ( val ) }
35
41
{ ...props }
36
42
/>
37
43
)
0 commit comments