-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathstyled.tsx
More file actions
199 lines (180 loc) · 6.24 KB
/
styled.tsx
File metadata and controls
199 lines (180 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as React from 'react'
import {
type ImageStyle,
type StyleProp,
StyleSheet,
type TextStyle,
type ViewStyle
} from 'react-native'
import {
cacheStyles,
getTheme,
type Theme,
useTheme
} from '../services/ThemeContext'
interface StyleProps {
style?: StyleProp<any>
}
type ValidStyles = ImageStyle | TextStyle | ViewStyle
type Styler<Props> =
| ValidStyles
| ((
theme: Theme
) => ValidStyles | ((props: Props) => ValidStyles | ValidStyles[]))
/**
* Creates a styled component using a `styler` parameter. The `styler` can be the
* styles itself, a function with the current theme and component props as
* curried arguments (e.g. `theme => props => styles` or `theme => styles`).
* The styles are cached when no `props` parameter is required by the `styler`.
*
* **Example Usage**
*
* ```
* const Colorful = styled(Text)({ fontWeight: 'Red' })
* const Themed = styled(Text)(theme => ({ color: theme.color }))
* const Dynamic = styled(Text)(theme => props => ({ color: props.color ?? theme.color }))
* const DynamicSansTheme = styled(Text)(_theme => props => ({ color: props.color }))
* ```
*
* @deprecated Use `cacheStyles` for styles, or if there really is a needed
* abstraction, create a new full component (but think about it atleast three
* times before you do).
*/
export function styled<BaseProps extends StyleProps>(
Component: React.ComponentType<BaseProps>
) {
function makeStyledComponent<Props extends object>(
styler: Styler<Props>
): React.ComponentType<Omit<BaseProps, 'style'> & Props> {
function addName<P extends Omit<BaseProps, 'style'> & Props>(
StyledComponent: React.ComponentType<P>
) {
// Use optional chaining to handle circular dependencies where Component
// may be undefined during module loading.
StyledComponent.displayName =
Component?.displayName != null
? `StyledComponent(${Component.displayName})`
: `StyledComponent`
return StyledComponent
}
if (typeof styler === 'function') {
const rv = styler(getTheme())
if (typeof rv === 'function') {
const stylerNarrowed = styler as (
theme: Theme
) => (props: Props) => ValidStyles | ValidStyles[]
return addName(function StyledComponent(props) {
const theme = useTheme()
const style = stylerNarrowed(theme)(props)
const allProps: Omit<BaseProps, 'style'> & BaseProps['style'] = {
...props,
style
}
return <Component {...allProps} />
})
} else {
const stylerNarrowed = styler as (theme: Theme) => ValidStyles
const getStyles = cacheStyles((theme: Theme) => ({
style: stylerNarrowed(theme)
}))
return addName(function StyledComponent(props) {
const theme = useTheme()
const stylesheet = getStyles(theme)
const allProps: Omit<BaseProps, 'style'> & BaseProps['style'] = {
...props,
style: stylesheet.style
}
return <Component {...allProps} />
})
}
} else {
const stylesheet = StyleSheet.create({ style: styler })
return addName(function StyledComponent(props) {
const allProps: Omit<BaseProps, 'style'> & BaseProps['style'] = {
...props,
style: stylesheet.style
}
return <Component {...allProps} />
})
}
}
return makeStyledComponent
}
export function styledWithRef<Ref, BaseProps extends StyleProps>(
Component: React.ComponentType<BaseProps>
) {
type RefAttribute = React.RefAttributes<Ref>
type PropsWithoutStyle = Omit<BaseProps, 'style'>
function makeStyledComponent<Props extends object>(
styler: Styler<Props>
): React.ForwardRefExoticComponent<
React.PropsWithoutRef<PropsWithoutStyle & Props> & RefAttribute
> {
function addName<P extends PropsWithoutStyle & Props & RefAttribute>(
StyledComponentWithRef: React.ForwardRefExoticComponent<
React.PropsWithoutRef<P>
>
): React.ForwardRefExoticComponent<React.PropsWithoutRef<P>> {
// Use optional chaining to handle circular dependencies where Component
// may be undefined during module loading.
StyledComponentWithRef.displayName =
Component?.displayName != null
? `StyledComponentWithRef(${Component.displayName})`
: `StyledComponentWithRef`
return StyledComponentWithRef
}
if (typeof styler === 'function') {
const rv = styler(getTheme())
if (typeof rv === 'function') {
const stylerNarrowed = styler as (
theme: Theme
) => (props: Props) => ValidStyles | ValidStyles[]
return addName(
React.forwardRef<any, PropsWithoutStyle & Props>(
function StyledComponent(props, ref) {
const theme = useTheme()
const style = stylerNarrowed(theme)(props as any)
const allProps: PropsWithoutStyle & BaseProps['style'] = {
...props,
style
} as any
return <Component {...allProps} ref={ref} />
}
)
)
} else {
const stylerNarrowed = styler as (theme: Theme) => ValidStyles
const getStyles = cacheStyles((theme: Theme) => ({
style: stylerNarrowed(theme)
}))
return addName(
React.forwardRef<any, PropsWithoutStyle & Props>(
function StyledComponent(props, ref) {
const theme = useTheme()
const stylesheet = getStyles(theme)
const allProps: PropsWithoutStyle & BaseProps['style'] = {
...props,
style: stylesheet.style
} as any
return <Component {...allProps} ref={ref} />
}
)
)
}
} else {
const stylesheet = StyleSheet.create({ style: styler })
return addName(
React.forwardRef<any, PropsWithoutStyle & Props>(
function StyledComponent(props, ref) {
const allProps: PropsWithoutStyle & BaseProps['style'] = {
...props,
style: stylesheet.style
} as any
return <Component {...allProps} ref={ref} />
}
)
)
}
}
return makeStyledComponent
}