-
Notifications
You must be signed in to change notification settings - Fork 111
/
stylesheet.ts
100 lines (84 loc) · 2.34 KB
/
stylesheet.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
import { Dimensions, StyleSheet as RNStyleSheet } from "react-native";
import { globalStyles, rem, styleMetaMap, vh, vw } from "./globals";
import {
StyleSheetRegisterOptions,
ExtractedStyle,
StyleProp,
StyleMeta,
} from "../../types";
const subscriptions = new Set<() => void>();
const parialStyleSheet = {
rem,
__subscribe(subscription: () => void) {
subscriptions.add(subscription);
return () => {
subscriptions.delete(subscription);
};
},
__reset() {
globalStyles.clear();
rem.reset();
vw.reset(Dimensions);
vh.reset(Dimensions);
},
register: (options: StyleSheetRegisterOptions) => {
if (options.declarations) {
for (const [name, styles] of Object.entries(options.declarations)) {
globalStyles.set(name, tagStyles(styles));
}
}
for (const subscription of subscriptions) {
subscription();
}
},
create: (styles: Record<string, ExtractedStyle>) => {
const namedStyles: Record<string, StyleProp> = {};
for (const [name, style] of Object.entries(styles)) {
namedStyles[name] = tagStyles(style);
}
for (const subscription of subscriptions) {
subscription();
}
return namedStyles;
},
};
export const StyleSheet = Object.assign({}, RNStyleSheet, parialStyleSheet);
function tagStyles(styles: ExtractedStyle | ExtractedStyle[]): StyleProp {
if (Array.isArray(styles)) {
let didTag = false;
const taggedStyles = styles.map((s) => {
const taggedStyle = tagStyles(s);
didTag ||= styleMetaMap.has(s.style);
return taggedStyle;
});
if (didTag) {
styleMetaMap.set(taggedStyles, {});
}
return taggedStyles;
} else {
const meta: StyleMeta = {};
let hasMeta = false;
if (
Array.isArray(styles.runtimeStyleProps) &&
styles.runtimeStyleProps.length > 0
) {
meta.runtimeStyleProps = new Set<string>(styles.runtimeStyleProps);
hasMeta = true;
}
if (
Array.isArray(styles.variableProps) &&
styles.variableProps.length > 0
) {
meta.variableProps = new Set<string>(styles.variableProps);
hasMeta = true;
}
if (Array.isArray(styles.media) && styles.media.length > 0) {
meta.media = styles.media;
hasMeta = true;
}
if (hasMeta) {
styleMetaMap.set(styles.style, meta);
}
return styles.style;
}
}