-
Notifications
You must be signed in to change notification settings - Fork 1
/
styled.ts
216 lines (181 loc) · 7.15 KB
/
styled.ts
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React, { useRef, useEffect, MutableRefObject, ReactElement, FunctionComponent } from 'react'
import * as ReactNative from 'react-native'
import { registerListener, removeListener, responsiveProperty, getBreakpoint } from './index'
import { NativeStyle, StyledSheet, Conditionals, ComponentInput, ComponentProps } from './types'
let autorun: Function
const importAutorunIfInstalled = async () => {
if (typeof jest !== 'undefined') {
// @ts-ignore
autorun = global.autorun
return
}
try {
const mobx = await import('mobx')
autorun = mobx.autorun
} catch (_) {}
}
const assignStyles = (styles: NativeStyle | NativeStyle[], ref: MutableRefObject<any>) => {
if (ref.current) {
if (ref.current.setNativeProps) {
ref.current.setNativeProps({
style: styles,
})
} else {
// react-native-web compatibility
Object.assign(ref.current.style, styles)
}
}
}
importAutorunIfInstalled()
const responsifyStyles = (styles: NativeStyle) => {
const properties = Object.keys(styles) as (keyof NativeStyle)[]
properties.forEach((property) => {
// @ts-ignore
styles[property] = responsiveProperty(property, styles[property], responsifyStyles)
})
return styles
}
const generateStyles = <T extends NativeStyle, R extends NativeStyle, S extends string>(
baseStyles: StyledSheet<T> | ((props: any) => StyledSheet<T>),
conditionalStyles:
| { [U in S & Conditionals]?: StyledSheet<R> }
| ((props: any) => { [U in S & Conditionals]?: StyledSheet<R> }) = {},
props: Record<string, any>,
ref: MutableRefObject<any>,
isUpdate = false,
) => {
if (typeof baseStyles === 'function' || typeof conditionalStyles === 'function') {
return autoRunStyles(
baseStyles as (props: any) => StyledSheet<T>,
conditionalStyles as (props: any) => Record<string, StyledSheet<R>>,
props,
ref,
isUpdate,
) as NativeStyle[]
}
const styles = {}
const breakpoint = getBreakpoint() as S & Conditionals
const platform = ReactNative.Platform.OS as S & Conditionals
Object.assign(styles, baseStyles)
if (typeof conditionalStyles[breakpoint] === 'object') {
Object.assign(styles, conditionalStyles[breakpoint])
}
if (typeof conditionalStyles[platform] === 'object') {
Object.assign(styles, conditionalStyles[platform])
}
const propsKeys = Object.keys(props) as (S & Conditionals)[]
propsKeys.forEach((property) => {
if (typeof conditionalStyles[property] === 'object' && props[property] === true) {
Object.assign(styles, conditionalStyles[property])
}
})
if (!isUpdate && props.style) {
if (Array.isArray(props.style)) {
return [...props.style, responsifyStyles(styles)] as NativeStyle[]
}
return [props.style, responsifyStyles(styles)] as NativeStyle[]
}
return responsifyStyles(styles)
}
const autoRunStyles = <T extends NativeStyle, R extends NativeStyle>(
baseStyles: (props: any) => StyledSheet<T>,
conditionalStyles: (props: any) => Record<string, StyledSheet<R>>,
props: Record<string, any>,
ref: MutableRefObject<any>,
isUpdate: boolean,
) => {
if (typeof autorun !== 'function') {
console.warn('responsive-react-native: failed to import MobX make sure to install it with "npm i mobx".')
}
let styles: NativeStyle | null = null
autorun(() => {
let currentStyles = baseStyles(props)
const currentConditionalStyles =
typeof conditionalStyles === 'function' ? conditionalStyles(props) : conditionalStyles
const breakpoint = getBreakpoint()
if (typeof currentConditionalStyles[breakpoint] === 'object') {
Object.assign(currentStyles, currentConditionalStyles[breakpoint])
}
if (typeof currentConditionalStyles[ReactNative.Platform.OS] === 'object') {
Object.assign(currentStyles, currentConditionalStyles[ReactNative.Platform.OS])
}
Object.keys(props).forEach((property) => {
if (typeof currentConditionalStyles[property] === 'object' && props[property] === true) {
Object.assign(currentStyles, currentConditionalStyles[property])
}
})
const currentStylesFlat = responsifyStyles(currentStyles as NativeStyle)
if (!isUpdate && styles) {
assignStyles(currentStylesFlat, ref)
} else {
styles = currentStylesFlat
}
})
if (props.style) {
if (Array.isArray(props.style)) {
return [...props.style, styles]
}
return [props.style, styles]
}
return styles
}
function resolveType(
type: ComponentInput,
Components: Record<string, any>,
): FunctionComponent<ComponentProps<ComponentInput>> {
if (typeof type !== 'string' && !Array.isArray(type)) {
return type as unknown as FunctionComponent<ComponentProps<ComponentInput>>
}
if (typeof type === 'string' && type.includes('.')) {
return type.split('.').reduce((items, property) => items[property], Components) as FunctionComponent<
ComponentProps<ComponentInput>
>
}
return Components[type as string]
}
// Overloads necessary, as case when conditionalStyles defaults to the initializer will not work.
// Overload for when conditionalStyles is explicitly provided
export function Styled<T extends NativeStyle, V extends Object, S extends string>(
type: ComponentInput,
baseStyles: StyledSheet<T> | ((props: V) => StyledSheet<T>),
conditionalStyles:
| { [U in S | Conditionals]?: StyledSheet<NativeStyle> }
| ((props: V) => { [U in S | Conditionals]?: StyledSheet<NativeStyle> }),
): (props: ComponentProps<ComponentInput> & { [K in Exclude<S, Conditionals>]?: boolean } & V) => ReactElement<any>
// Overload for when conditionalStyles uses the default value
export function Styled<T extends NativeStyle, V extends Object, S extends string = ''>(
type: ComponentInput,
baseStyles: StyledSheet<T> | ((props: V) => StyledSheet<T>),
): (props: ComponentProps<ComponentInput> & { [K in Exclude<S, Conditionals>]?: boolean } & V) => ReactElement<any>
// TODO extend existing styled
export function Styled<T extends NativeStyle, V extends Object, S extends string>(
type: ComponentInput,
baseStyles: StyledSheet<T> | ((props: V) => StyledSheet<T>),
conditionalStyles:
| { [U in S | Conditionals]?: StyledSheet<NativeStyle> }
| ((props: V) => { [U in S | Conditionals]?: StyledSheet<NativeStyle> }) = {} as {
[U in S | Conditionals]?: StyledSheet<NativeStyle>
},
) {
const ComponentType = resolveType(type, ReactNative)
if (typeof ComponentType !== 'object' && typeof ComponentType !== 'function') {
console.warn(`responsive-react-native: component ${type} passed to Styled isn't a valid RN component.`)
}
return ({ ...props }: ComponentProps<ComponentInput> & { [K in Exclude<S, Conditionals>]?: boolean } & V) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const ref = useRef<any>()
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
const listener = () => {
assignStyles(generateStyles(baseStyles, conditionalStyles, props, ref, true), ref)
}
registerListener(listener)
return () => removeListener(listener)
})
return React.createElement(ComponentType, {
...props,
style: generateStyles(baseStyles, conditionalStyles, props, ref),
ref,
})
}
}