Skip to content

Commit 40d45cf

Browse files
authored
improve withTheme types (#552)
1 parent 352cbc7 commit 40d45cf

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

src/theme/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { default as defaultTheme } from './src/default-theme'
2-
export { ThemeProvider, ThemeConsumer } from './src/ThemeContext'
3-
export { default as withTheme } from './src/withTheme'
2+
export { ThemeProvider, ThemeConsumer, Theme } from './src/ThemeContext'
3+
export { withTheme } from './src/withTheme'

src/theme/src/ThemeContext.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import React from 'react'
22
import defaultTheme from './default-theme'
33

4+
export interface Theme {
5+
getCodeProps: (appearance: 'default' | 'minimal') => any
6+
getFontFamily: (fontFamily?: string) => string
7+
getHeadingStyle: (size?: number) => any
8+
getLinkClassName: (color?: string) => string
9+
getParagraphStyle: (size?: number) => any
10+
getTextColor: (colorAlias?: string) => string
11+
getTextStyle: (size?: number) => any
12+
}
13+
414
/**
515
* Use React 16.3+ createContext API.
616
*/
717
const {
818
Provider: ThemeProvider,
919
Consumer: ThemeConsumer
10-
} = React.createContext(defaultTheme)
20+
} = React.createContext<Theme>(defaultTheme)
1121

1222
export { ThemeProvider, ThemeConsumer }

src/theme/src/withTheme.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import React from 'react'
2-
import { ThemeConsumer } from './ThemeContext'
2+
import { ThemeConsumer, Theme } from './ThemeContext'
33

4-
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
4+
type Difference<T, K> = Pick<T, Exclude<keyof T, keyof K>>
55

66
interface WithThemeProps {
7-
// TODO: type this
8-
theme: object
7+
/**
8+
* The theme context from the ThemeProvider
9+
*/
10+
theme: Theme
911
}
1012

1113
/**
1214
* HOC that injects the `theme` from the ThemeConsumer into the wrapper component.
1315
*/
14-
export default function withTheme<P extends WithThemeProps>(Component: React.ComponentType<P>) {
15-
const displayName =
16-
Component.displayName || Component.name || 'Component'
16+
export function withTheme<P extends WithThemeProps>(
17+
Component: React.ComponentType<P>
18+
): React.ComponentClass<Difference<P, WithThemeProps>, {}> {
19+
const displayName = Component.displayName || Component.name || 'Component'
1720

18-
return class WithTheme extends React.Component<Omit<P, keyof WithThemeProps>> {
21+
return class WithTheme extends React.Component<
22+
Difference<P, WithThemeProps>
23+
> {
1924
static displayName = `withTheme(${displayName})`
2025

2126
render() {

src/typography/src/Text.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import React, { PureComponent } from 'react'
22
import Box from 'ui-box'
3-
import { withTheme } from '../../theme'
4-
5-
interface Theme {
6-
getTextStyle: (size?: number) => any
7-
getTextColor: (colorAlias?: string) => string
8-
getFontFamily: (fontFamily?: string) => string
9-
}
3+
import { withTheme, Theme } from '../../theme'
104

115
interface TextProps {
12-
/** The color (alias or valid color) applied to the text */
6+
/**
7+
* The color (alias or valid color) applied to the text
8+
*/
139
color?: string
14-
/** The font family alias applied to the text */
10+
11+
/**
12+
* The font family alias applied to the text
13+
*/
1514
fontFamily?: 'ui' | 'display' | 'mono'
16-
/** The size of the text style */
15+
16+
/**
17+
* The size of the text style
18+
*/
1719
size?: 300 | 400 | 500 | 600
18-
/** Theme provided by ThemeProvider. */
20+
21+
/**
22+
* Theme provided by ThemeProvider.
23+
*/
1924
theme: Theme
2025
}
2126

0 commit comments

Comments
 (0)