/
ConfigHelper.ts
171 lines (160 loc) · 4.12 KB
/
ConfigHelper.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
import ReanimatedModule from './ReanimatedModule';
/**
* Styles allowed to be direcly updated in UI thread
*/
let UI_THREAD_PROPS_WHITELIST: Record<string, boolean> = {
opacity: true,
transform: true,
/* colors */
backgroundColor: true,
borderRightColor: true,
borderBottomColor: true,
borderColor: true,
borderEndColor: true,
borderLeftColor: true,
borderStartColor: true,
borderTopColor: true,
/* ios styles */
shadowOpacity: true,
shadowRadius: true,
/* legacy android transform properties */
scaleX: true,
scaleY: true,
translateX: true,
translateY: true,
};
/**
* Whitelist of view props that can be updated in native thread via UIManagerModule
*/
let NATIVE_THREAD_PROPS_WHITELIST: Record<string, boolean> = {
borderBottomWidth: true,
borderEndWidth: true,
borderLeftWidth: true,
borderRightWidth: true,
borderStartWidth: true,
borderTopWidth: true,
borderWidth: true,
bottom: true,
flex: true,
flexGrow: true,
flexShrink: true,
height: true,
left: true,
margin: true,
marginBottom: true,
marginEnd: true,
marginHorizontal: true,
marginLeft: true,
marginRight: true,
marginStart: true,
marginTop: true,
marginVertical: true,
maxHeight: true,
maxWidth: true,
minHeight: true,
minWidth: true,
padding: true,
paddingBottom: true,
paddingEnd: true,
paddingHorizontal: true,
paddingLeft: true,
paddingRight: true,
paddingStart: true,
paddingTop: true,
paddingVertical: true,
right: true,
start: true,
top: true,
width: true,
zIndex: true,
borderBottomEndRadius: true,
borderBottomLeftRadius: true,
borderBottomRightRadius: true,
borderBottomStartRadius: true,
borderRadius: true,
borderTopEndRadius: true,
borderTopLeftRadius: true,
borderTopRightRadius: true,
borderTopStartRadius: true,
opacity: true,
elevation: true,
fontSize: true,
lineHeight: true,
textShadowRadius: true,
letterSpacing: true,
/* strings */
display: true,
backfaceVisibility: true,
overflow: true,
resizeMode: true,
fontStyle: true,
fontWeight: true,
textAlign: true,
textDecorationLine: true,
fontFamily: true,
textAlignVertical: true,
fontVariant: true,
textDecorationStyle: true,
textTransform: true,
writingDirection: true,
/* text color */
color: true,
tintColor: true,
shadowColor: true,
placeholderTextColor: true,
};
function configureProps(): void {
ReanimatedModule.configureProps(
Object.keys(NATIVE_THREAD_PROPS_WHITELIST),
Object.keys(UI_THREAD_PROPS_WHITELIST)
);
}
export function addWhitelistedNativeProps(
props: Record<string, boolean>
): void {
const oldSize = Object.keys(NATIVE_THREAD_PROPS_WHITELIST).length;
NATIVE_THREAD_PROPS_WHITELIST = {
...NATIVE_THREAD_PROPS_WHITELIST,
...props,
};
if (oldSize !== Object.keys(NATIVE_THREAD_PROPS_WHITELIST).length) {
configureProps();
}
}
export function addWhitelistedUIProps(props: Record<string, boolean>): void {
const oldSize = Object.keys(UI_THREAD_PROPS_WHITELIST).length;
UI_THREAD_PROPS_WHITELIST = { ...UI_THREAD_PROPS_WHITELIST, ...props };
if (oldSize !== Object.keys(UI_THREAD_PROPS_WHITELIST).length) {
configureProps();
}
}
const PROCESSED_VIEW_NAMES = new Set();
interface ViewConfig {
uiViewClassName: string;
validAttributes: Record<string, unknown>;
}
/**
* updates UI props whitelist for given view host instance
* this will work just once for every view name
*/
export function adaptViewConfig(viewConfig: ViewConfig): void {
const viewName = viewConfig.uiViewClassName;
const props = viewConfig.validAttributes;
// update whitelist of UI props for this view name only once
if (!PROCESSED_VIEW_NAMES.has(viewName)) {
const propsToAdd: Record<string, boolean> = {};
Object.keys(props).forEach((key) => {
// we don't want to add native props as they affect layout
// we also skip props which repeat here
if (
!(key in NATIVE_THREAD_PROPS_WHITELIST) &&
!(key in UI_THREAD_PROPS_WHITELIST)
) {
propsToAdd[key] = true;
}
});
addWhitelistedUIProps(propsToAdd);
PROCESSED_VIEW_NAMES.add(viewName);
}
}
configureProps();