-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmedia-query.ts
More file actions
92 lines (84 loc) · 2.24 KB
/
media-query.ts
File metadata and controls
92 lines (84 loc) · 2.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
/* eslint-disable */
import { PixelRatio, Platform } from "react-native";
import type { MediaCondition } from "../../../compiler";
import { colorScheme, vh, vw, type Getter } from "../reactivity";
export function testMediaQuery(mediaQueries: MediaCondition[], get: Getter) {
return mediaQueries.every((query) => test(query, get));
}
function test(mediaQuery: MediaCondition, get: Getter): Boolean {
switch (mediaQuery[0]) {
case "[]":
case "!!":
return false;
case "!":
return !test(mediaQuery[1], get);
case "&":
return mediaQuery[1].every((query) => {
return test(query, get);
});
case "|":
return mediaQuery[1].some((query) => {
return test(query, get);
});
case ">":
case ">=":
case "<":
case "<=":
case "=": {
return testComparison(mediaQuery, get);
}
}
}
function testComparison(mediaQuery: MediaCondition, get: Getter): Boolean {
let left: number | undefined;
const right = mediaQuery[2];
switch (mediaQuery[1]) {
case "platform":
return right === "native" || right === Platform.OS;
case "prefers-color-scheme": {
return right === get(colorScheme);
}
case "display-mode":
return right === "native" || Platform.OS === right;
case "min-width":
return typeof right === "number" && get(vw) >= right;
case "max-width":
return typeof right === "number" && get(vw) <= right;
case "min-height":
return typeof right === "number" && get(vh) >= right;
case "max-height":
return typeof right === "number" && get(vh) <= right;
case "orientation":
return right === "landscape" ? get(vh) < get(vw) : get(vh) >= get(vw);
}
if (typeof right !== "number") {
return false;
}
switch (mediaQuery[1]) {
case "width":
left = get(vw);
break;
case "height":
left = get(vh);
break;
case "resolution":
left = PixelRatio.get();
break;
default:
return false;
}
switch (mediaQuery[0]) {
case "=":
return left === right;
case ">":
return left > right;
case ">=":
return left >= right;
case "<":
return left < right;
case "<=":
return left <= right;
default:
return false;
}
}