Skip to content

Commit 61b1495

Browse files
committed
Turn runtime error into a TODO comment
1 parent d093924 commit 61b1495

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

packages/style-guide/src/TypeScale.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { jsx } from 'theme-ui'
33
import { useTheme } from './context'
44
import TypeStyle from './TypeStyle'
55

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
118

129
export interface TypeScaleProps {
1310
reverse?: boolean
1411
}
1512
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 || [])
1717

1818
return (
1919
<div
@@ -22,16 +22,22 @@ export const TypeScale = ({ reverse = true, ...props }) => {
2222
flexWrap: 'wrap',
2323
alignItems: 'baseline',
2424
}}>
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 (
2834
<TypeStyle
29-
key={i}
35+
key={key}
3036
fontSize={key}
3137
sx={{
3238
mr: 3,
3339
}}
34-
children={getValue(fontSizes, key)}
40+
children={getValue(val)}
3541
{...props}
3642
/>
3743
)

packages/style-guide/src/TypeStyle.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { jsx } from 'theme-ui'
44
import Card from './Card'
55

66
export interface TypeStyleProps extends ComponentProps<typeof Card> {
7-
fontSize?: number
7+
fontSize?: number | string
88
fontFamily?: string
99
lineHeight?: string
1010
fontWeight?: string

0 commit comments

Comments
 (0)