Skip to content

Commit 4443017

Browse files
committed
feat: platform functions
1 parent e9a97e3 commit 4443017

File tree

3 files changed

+51
-87
lines changed

3 files changed

+51
-87
lines changed

src/compiler/declarations.ts

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ function parseUnparsed(
17351735
tokenOrValue.value.name = `@${tokenOrValue.value.name}`;
17361736
return unparsedFunction(tokenOrValue, options);
17371737
case "platformColor":
1738-
case "getPixelSizeForLayoutSize":
1738+
case "pixelSizeForLayoutSize":
17391739
case "roundToNearestPixel":
17401740
case "pixelScale":
17411741
case "fontScale":
@@ -1747,14 +1747,6 @@ function parseUnparsed(
17471747
return unparsedFunction(tokenOrValue, options);
17481748
case "hairlineWidth":
17491749
return [{}, tokenOrValue.value.name, []];
1750-
case "platformSelect":
1751-
case "fontScaleSelect":
1752-
case "pixelScaleSelect":
1753-
return parseRNRuntimeSpecificsFunction(
1754-
tokenOrValue.value.name,
1755-
tokenOrValue.value.arguments,
1756-
options,
1757-
);
17581750
case "calc":
17591751
case "max":
17601752
case "min":
@@ -2580,82 +2572,6 @@ function parseGap(value: GapValue, options: ParserOptions) {
25802572
return parseLength(value.value, options);
25812573
}
25822574

2583-
function parseRNRuntimeSpecificsFunction(
2584-
name: string,
2585-
args: TokenOrValue[],
2586-
options: ParserOptions,
2587-
): StyleDescriptor {
2588-
let key: string | undefined;
2589-
const runtimeArgs: Record<string, StyleDescriptor> = {};
2590-
2591-
for (const token of args) {
2592-
if (!key) {
2593-
if (
2594-
token.type === "token" &&
2595-
(token.value.type === "ident" || token.value.type === "number")
2596-
) {
2597-
key = token.value.value.toString();
2598-
continue;
2599-
}
2600-
} else {
2601-
if (token.type !== "token") {
2602-
const value = parseUnparsed(token, options);
2603-
if (value === undefined) {
2604-
return;
2605-
}
2606-
runtimeArgs[key] = value;
2607-
key = undefined;
2608-
} else {
2609-
switch (token.value.type) {
2610-
case "string":
2611-
case "number":
2612-
case "ident": {
2613-
if (key) {
2614-
runtimeArgs[key] = parseUnparsed(token, options);
2615-
key = undefined;
2616-
continue;
2617-
} else {
2618-
return;
2619-
}
2620-
}
2621-
case "delim":
2622-
case "comma":
2623-
continue;
2624-
case "function":
2625-
case "at-keyword":
2626-
case "hash":
2627-
case "id-hash":
2628-
case "unquoted-url":
2629-
case "percentage":
2630-
case "dimension":
2631-
case "white-space":
2632-
case "comment":
2633-
case "colon":
2634-
case "semicolon":
2635-
case "include-match":
2636-
case "dash-match":
2637-
case "prefix-match":
2638-
case "suffix-match":
2639-
case "substring-match":
2640-
case "cdo":
2641-
case "cdc":
2642-
case "parenthesis-block":
2643-
case "square-bracket-block":
2644-
case "curly-bracket-block":
2645-
case "bad-url":
2646-
case "bad-string":
2647-
case "close-parenthesis":
2648-
case "close-square-bracket":
2649-
case "close-curly-bracket":
2650-
return undefined;
2651-
}
2652-
}
2653-
}
2654-
}
2655-
2656-
return [{}, name, Object.entries(runtimeArgs)];
2657-
}
2658-
26592575
function parseTextAlign(textAlign: TextAlign, options: ParserOptions) {
26602576
const allowed = new Set(["auto", "left", "right", "center", "justify"]);
26612577
if (allowed.has(textAlign)) {

src/runtime/native/styles/platform-functions.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PlatformColor } from "react-native";
1+
import { PixelRatio, PlatformColor, StyleSheet } from "react-native";
22
import type { StyleFunctionResolver } from "./resolve";
33

44
export const platformColor: StyleFunctionResolver = (resolveValue, value) => {
@@ -11,3 +11,39 @@ export const platformColor: StyleFunctionResolver = (resolveValue, value) => {
1111

1212
return;
1313
};
14+
15+
export const hairlineWidth: StyleFunctionResolver = () => {
16+
return StyleSheet.hairlineWidth;
17+
};
18+
19+
export const pixelRatio: StyleFunctionResolver = () => {
20+
return PixelRatio.get();
21+
};
22+
23+
export const fontScale: StyleFunctionResolver = () => {
24+
return PixelRatio.getFontScale();
25+
};
26+
27+
export const pixelSizeForLayoutSize: StyleFunctionResolver = (
28+
resolveValue,
29+
value,
30+
) => {
31+
const size: unknown = resolveValue(value[2]?.[0]);
32+
if (typeof size === "number") {
33+
return PixelRatio.getPixelSizeForLayoutSize(size);
34+
}
35+
36+
return;
37+
};
38+
39+
export const roundToNearestPixel: StyleFunctionResolver = (
40+
resolveValue,
41+
value,
42+
) => {
43+
const size: unknown = resolveValue(value[2]?.[0]);
44+
if (typeof size === "number") {
45+
return PixelRatio.roundToNearestPixel(size);
46+
}
47+
48+
return;
49+
};

src/runtime/native/styles/resolve.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import { type Getter, type VariableContextValue } from "../reactivity";
99
import { animation } from "./animation";
1010
import { calc } from "./calc";
1111
import { transformKeys } from "./defaults";
12-
import { platformColor } from "./platform-functions";
12+
import {
13+
fontScale,
14+
hairlineWidth,
15+
pixelRatio,
16+
pixelSizeForLayoutSize,
17+
platformColor,
18+
roundToNearestPixel,
19+
} from "./platform-functions";
1320
import { textShadow } from "./text-shadow";
1421
import { transform } from "./transform";
1522
import { em, rem, vh, vw } from "./units";
@@ -34,6 +41,11 @@ const functions: Record<string, StyleFunctionResolver> = {
3441
vh,
3542
rem,
3643
platformColor,
44+
hairlineWidth,
45+
pixelRatio,
46+
fontScale,
47+
pixelSizeForLayoutSize,
48+
roundToNearestPixel,
3749
"@textShadow": textShadow,
3850
"@transform": transform,
3951
animationName: animation,

0 commit comments

Comments
 (0)