Skip to content

Commit f689fe9

Browse files
committed
feat!: remove rem export. Allow rem to be set via :root selector
1 parent e5ed0bb commit f689fe9

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

src/__tests__/native/units.test.tsx

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { useNativeCss } from "react-native-css/runtime/native";
66

77
import {
88
dimensions,
9-
rem,
109
VAR_SYMBOL,
1110
VariableContext,
1211
vh,
@@ -112,7 +111,7 @@ test("rem - default", () => {
112111
});
113112
});
114113

115-
test("rem - override", () => {
114+
test("rem - inline override", () => {
116115
registerCSS(`.my-class { font-size: 10rem; }`, {
117116
inlineRem: 10,
118117
});
@@ -133,32 +132,21 @@ test("rem - override", () => {
133132
});
134133
});
135134

136-
test("rem - dynamic", () => {
137-
registerCSS(`.my-class { font-size: 10rem; }`, {
138-
inlineRem: false,
139-
});
135+
test("rem - css override", () => {
136+
registerCSS(
137+
`
138+
:root { font-size: 10px; }
139+
.my-class { font-size: 10rem; }
140+
`,
141+
{
142+
inlineRem: false,
143+
},
144+
);
140145

141146
const { result } = renderHook(() => {
142147
return useNativeCss(View, { className: "my-class" });
143148
});
144149

145-
expect(rem.get()).toEqual(14);
146-
expect(result.current.type).toBe(VariableContext.Provider);
147-
expect(result.current.props.value).toStrictEqual({
148-
[VAR_SYMBOL]: true,
149-
[emVariableName]: [{}, "rem", 10],
150-
});
151-
152-
expect(result.current.props.children.type).toBe(View);
153-
expect(result.current.props.children.props).toStrictEqual({
154-
style: { fontSize: 140 },
155-
});
156-
157-
act(() => {
158-
rem.set(10);
159-
});
160-
161-
expect(rem.get()).toEqual(10);
162150
expect(result.current.type).toBe(VariableContext.Provider);
163151
expect(result.current.props.value).toStrictEqual({
164152
[VAR_SYMBOL]: true,

src/compiler/stylesheet.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@ export class StylesheetBuilder {
568568
: [value];
569569
// Append extra media queries if they exist
570570
this.shared[type][name].push(variableValue);
571+
572+
if (type === "rootVariables" && name === "__rn-css-em") {
573+
const remName = "__rn-css-rem";
574+
this.shared[type][remName] ??= [];
575+
this.shared[type][remName].push(variableValue);
576+
}
571577
}
572578
}
573579
}

src/jest/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { inspect } from "node:util";
55
import { compile, type CompilerOptions } from "react-native-css/compiler";
66
import { StyleCollection } from "react-native-css/style-collection";
77

8-
import { colorScheme, dimensions, rem } from "../runtime/native/reactivity";
8+
import { colorScheme, dimensions } from "../runtime/native/reactivity";
99

1010
declare global {
1111
/* eslint-disable @typescript-eslint/no-namespace */
@@ -21,7 +21,6 @@ export const testID = "react-native-css";
2121
beforeEach(() => {
2222
StyleCollection.styles.clear();
2323
dimensions.set(Dimensions.get("window"));
24-
rem.set(14);
2524
Appearance.setColorScheme(null);
2625
colorScheme.set(null);
2726
});

src/runtime/native/reactivity.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ export const VariableContext = createContext<VariableContextValue>({
189189
[VAR_SYMBOL]: true,
190190
});
191191

192-
/** Units *********************************************************************/
193-
194-
export const rem = observable(14);
195-
196192
/** Pseudo Classes ************************************************************/
197193

198194
export const hoverFamily = weakFamily(() => observable(false));

src/runtime/native/styles/units.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/* eslint-disable */
2-
import { rem as remObs, vh as vhObs, vw as vwObs } from "../reactivity";
1+
import { vh as vhObs, vw as vwObs } from "../reactivity";
32
import type { StyleFunctionResolver } from "./resolve";
43

5-
export const em: StyleFunctionResolver = (resolve, func, get) => {
6-
let value = func[2];
4+
export const em: StyleFunctionResolver = (resolve, func) => {
5+
const value = func[2];
76

87
if (typeof value !== "number") {
98
return;
@@ -12,14 +11,14 @@ export const em: StyleFunctionResolver = (resolve, func, get) => {
1211
let emValue = resolve([{}, "var", ["__rn-css-em"]]);
1312

1413
if (typeof emValue !== "number") {
15-
emValue = get(remObs);
14+
emValue = resolve([{}, "var", ["__rn-css-rem"]]);
1615
}
1716

1817
if (typeof emValue !== "number") {
1918
return;
2019
}
2120

22-
return round(Number(value) * emValue);
21+
return round(value * emValue);
2322
};
2423

2524
export const vw: StyleFunctionResolver = (_, func, get) => {
@@ -42,14 +41,20 @@ export const vh: StyleFunctionResolver = (_, func, get) => {
4241
return round((value / 100) * get(vhObs));
4342
};
4443

45-
export const rem: StyleFunctionResolver = (_, func, get) => {
44+
export const rem: StyleFunctionResolver = (resolve, func) => {
4645
const value = func[2];
4746

4847
if (typeof value !== "number") {
4948
return;
5049
}
5150

52-
return round(value * get(remObs));
51+
const remValue = resolve([{}, "var", ["__rn-css-rem"]]);
52+
53+
if (typeof remValue === "number") {
54+
return round(value * remValue);
55+
}
56+
57+
return;
5358
};
5459

5560
function round(number: number) {

src/style-collection/root.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ const rootVariableFamily = () => {
3333

3434
export const rootVariables = rootVariableFamily();
3535
export const universalVariables = rootVariableFamily();
36+
37+
rootVariables("__rn-css-rem").set([[14]]);

0 commit comments

Comments
 (0)