-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (62 loc) · 1.73 KB
/
index.ts
File metadata and controls
75 lines (62 loc) · 1.73 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
import { Appearance, Dimensions } from "react-native";
import { inspect } from "node:util";
import { compile, type CompilerOptions } from "react-native-css/compiler";
import { StyleCollection } from "react-native-css/style-collection";
import { colorScheme, dimensions, rem } from "../runtime/native/reactivity";
declare global {
/* eslint-disable @typescript-eslint/no-namespace */
namespace jest {
interface Matchers<R> {
toHaveAnimatedStyle(style?: unknown): R;
}
}
}
export const testID = "react-native-css";
beforeEach(() => {
StyleCollection.styles.clear();
dimensions.set(Dimensions.get("window"));
rem.set(14);
Appearance.setColorScheme(null);
colorScheme.set(null);
});
const debugDefault = Boolean(
process.env.REACT_NATIVE_CSS_TEST_DEBUG &&
process.env.NODE_OPTIONS?.includes("--inspect"),
);
export function registerCSS(
css: string,
options: CompilerOptions & { debug?: boolean } = {},
) {
const { debug = debugDefault } = options;
const compiled = compileWithAutoDebug(css, options);
if (debug) {
console.log(
`Compiled:\n---\n${inspect(
{
stylesheet: compiled.stylesheet(),
warnings: compiled.warnings(),
},
{ depth: null, colors: true, compact: false },
)}`,
);
}
StyleCollection.inject(compiled.stylesheet());
return compiled;
}
export function compileWithAutoDebug(
css: string,
{
debug = debugDefault,
...options
}: CompilerOptions & { debug?: boolean } = {},
) {
const logger = debug
? (text: string) => {
// Just log the rules
if (text.startsWith("[")) {
console.log(`Rules:\n---\n${text}`);
}
}
: undefined;
return compile(css, { ...options, logger });
}