Skip to content

Commit 5f11bbc

Browse files
committed
fix: rules which set variables causing remounts
1 parent 2e4c95f commit 5f11bbc

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/runtime/native/__tests__/container-queries.test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@ import { registerCSS } from "react-native-css/jest";
55
const parentID = "parent";
66
const childID = "child";
77

8+
test("Unnamed containers", () => {
9+
registerCSS(`
10+
:root, :host {
11+
--color-white: #fff;
12+
}
13+
.\\@container {
14+
container-type: inline-size;
15+
}
16+
.\\@sm\\:text-white {
17+
@container (width >= 24rem) {
18+
color: var(--color-white);
19+
}
20+
}
21+
`);
22+
23+
render(
24+
<View testID={parentID} className="@container">
25+
<View testID={childID} className="@sm:text-white" />
26+
</View>,
27+
);
28+
29+
const parent = screen.getByTestId(parentID);
30+
const child = screen.getByTestId(childID);
31+
32+
expect(child).toHaveStyle(undefined);
33+
34+
// Jest does not fire layout events, so we need to manually
35+
fireEvent(parent, "layout", {
36+
nativeEvent: {
37+
layout: {
38+
width: 500,
39+
height: 200,
40+
},
41+
},
42+
});
43+
44+
expect(child).toHaveStyle({ color: "#fff" });
45+
});
46+
847
test("container query width", () => {
948
registerCSS(`
1049
.container {

src/runtime/native/react/rules.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
hoverFamily,
1313
VAR_SYMBOL,
1414
weakFamily,
15+
type ContainerContextValue,
1516
type VariableContextValue,
1617
} from "../reactivity";
1718
import { stylesFamily } from "../styles";
@@ -36,8 +37,8 @@ export function updateRules(
3637

3738
let usesVariables = false;
3839

39-
let variables = state.variables ? inheritedVariables : undefined;
40-
let containers = state.containers ? inheritedContainers : undefined;
40+
let variables: VariableContextValue | undefined;
41+
let containers: ContainerContextValue | undefined;
4142
const inlineVariables = new Set<InlineVariable>();
4243

4344
let animated = false;
@@ -101,6 +102,16 @@ export function updateRules(
101102

102103
usesVariables ||= Boolean(rule.dv);
103104

105+
// We do this even if the rule doesn't match so we can maintain a consistent render tree
106+
// We we need to inject React context
107+
if (rule.v) {
108+
variables ??= inheritedVariables;
109+
}
110+
111+
if (rule.c) {
112+
containers ??= inheritedContainers;
113+
}
114+
104115
if (
105116
!testRule(
106117
rule,
@@ -114,11 +125,8 @@ export function updateRules(
114125
}
115126

116127
if (rule.v) {
117-
// We're going to set a value, so we need to create a new object
118128
if (variables === inheritedVariables) {
119129
variables = { ...inheritedVariables };
120-
} else {
121-
variables ??= { ...inheritedVariables };
122130
}
123131

124132
for (const v of rule.v) {
@@ -199,6 +207,7 @@ export function updateRules(
199207
guards,
200208
animated,
201209
pressable,
210+
variables,
202211
};
203212
}
204213

0 commit comments

Comments
 (0)