Skip to content

Commit 4eb039c

Browse files
authored
fix: container queries and groups from overlapping (#104)
1 parent 64d6a52 commit 4eb039c

File tree

6 files changed

+73
-15
lines changed

6 files changed

+73
-15
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { fireEvent, screen } from "@testing-library/react-native";
2+
import { View } from "react-native-css/components";
3+
4+
import { render } from "./_tailwind";
5+
6+
const parentID = "parent";
7+
const childID = "child";
8+
9+
test("Unnamed containers", async () => {
10+
await render(
11+
<View testID={parentID} className="@container">
12+
<View testID={childID} className="@sm:text-white" />
13+
</View>,
14+
);
15+
16+
const parent = screen.getByTestId(parentID);
17+
const child = screen.getByTestId(childID);
18+
19+
expect(child).toHaveStyle(undefined);
20+
21+
// Jest does not fire layout events, so we need to manually
22+
fireEvent(parent, "layout", {
23+
nativeEvent: {
24+
layout: {
25+
width: 500,
26+
height: 200,
27+
},
28+
},
29+
});
30+
31+
expect(child).toHaveStyle({ color: "#fff" });
32+
});
33+
34+
test("Named containers", async () => {
35+
await render(
36+
<View testID={parentID} className="@container/main">
37+
<View testID={childID} className="@sm/main:text-white" />
38+
</View>,
39+
);
40+
41+
const parent = screen.getByTestId(parentID);
42+
const child = screen.getByTestId(childID);
43+
44+
expect(child).toHaveStyle(undefined);
45+
46+
fireEvent(parent, "layout", {
47+
nativeEvent: {
48+
layout: {
49+
width: 500,
50+
height: 200,
51+
},
52+
},
53+
});
54+
55+
expect(child).toHaveStyle({ color: "#fff" });
56+
});

src/compiler/__tests__/selectors.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test(".my-class { &:is(:where(.my-parent, .my-second-parent):hover *) {} }", ()
6060
className: "my-class",
6161
containerQuery: [
6262
{
63-
n: "my-parent",
63+
n: "g:my-parent",
6464
p: {
6565
h: 1,
6666
},
@@ -73,7 +73,7 @@ test(".my-class { &:is(:where(.my-parent, .my-second-parent):hover *) {} }", ()
7373
className: "my-class",
7474
containerQuery: [
7575
{
76-
n: "my-second-parent",
76+
n: "g:my-second-parent",
7777
p: {
7878
h: 1,
7979
},

src/compiler/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function extractContainer(
294294
};
295295

296296
if (containerRule.name) {
297-
query.n = containerRule.name;
297+
query.n = `c:${containerRule.name}`;
298298
}
299299

300300
builder.addContainerQuery(query);

src/compiler/inheritance.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test("nested classes", () => {
1212
"my-class",
1313
[
1414
{
15-
c: ["my-class"],
15+
c: ["g:my-class"],
1616
s: [0],
1717
},
1818
],
@@ -21,7 +21,7 @@ test("nested classes", () => {
2121
"test",
2222
[
2323
{
24-
cq: [{ n: "my-class" }],
24+
cq: [{ n: "g:my-class" }],
2525
d: [{ color: "#f00" }],
2626
v: [["__rn-css-color", "#f00"]],
2727
s: [1, 2],
@@ -44,7 +44,7 @@ test("multiple tiers classes", () => {
4444
"one",
4545
[
4646
{
47-
c: ["one"],
47+
c: ["g:one"],
4848
s: [0],
4949
},
5050
],
@@ -53,7 +53,7 @@ test("multiple tiers classes", () => {
5353
"two",
5454
[
5555
{
56-
c: ["two"],
56+
c: ["g:two"],
5757
s: [0],
5858
},
5959
],
@@ -62,7 +62,7 @@ test("multiple tiers classes", () => {
6262
"test",
6363
[
6464
{
65-
cq: [{ n: "one" }, { n: "two" }],
65+
cq: [{ n: "g:one" }, { n: "g:two" }],
6666
d: [{ color: "#f00" }],
6767
v: [["__rn-css-color", "#f00"]],
6868
s: [1, 3],
@@ -85,7 +85,7 @@ test("tiers with multiple classes", () => {
8585
"one",
8686
[
8787
{
88-
c: ["one"],
88+
c: ["g:one"],
8989
s: [0],
9090
},
9191
],
@@ -94,7 +94,7 @@ test("tiers with multiple classes", () => {
9494
"three",
9595
[
9696
{
97-
c: ["three.two"],
97+
c: ["g:three.two"],
9898
s: [0],
9999
aq: [["a", "className", "*=", "two"]],
100100
},
@@ -105,9 +105,9 @@ test("tiers with multiple classes", () => {
105105
[
106106
{
107107
cq: [
108-
{ n: "one" },
108+
{ n: "g:one" },
109109
{
110-
n: "three.two",
110+
n: "g:three.two",
111111
},
112112
],
113113
d: [{ color: "#f00" }],

src/compiler/selector-builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function parseComponents(
323323
containerQueries.unshift(ref);
324324
}
325325

326-
ref.n = ref.n ? `${ref.n}.${component.name}` : component.name;
326+
ref.n = ref.n ? `${ref.n}.${component.name}` : `g:${component.name}`;
327327
}
328328

329329
specificity[Specificity.ClassName] =
@@ -492,7 +492,9 @@ function parseIsWhereComponents(
492492
(query.specificity[Specificity.ClassName] ?? 0) + 1;
493493
}
494494

495-
query.n = query.n ? `${query.n}.${component.name}` : component.name;
495+
query.n = query.n
496+
? `${query.n}.${component.name}`
497+
: `g:${component.name}`;
496498
}
497499

498500
return parseIsWhereComponents(type, selector, index + 1, queries);

src/compiler/stylesheet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export class StylesheetBuilder {
507507
continue;
508508
}
509509

510-
const [first, ...rest] = name.split(".");
510+
const [first, ...rest] = name.slice(2).split(".");
511511

512512
if (typeof first !== "string") {
513513
continue;

0 commit comments

Comments
 (0)