Skip to content

Commit 15ba740

Browse files
committed
Allowing for a single container override
fixes #67
1 parent 092daa3 commit 15ba740

File tree

9 files changed

+89
-28
lines changed

9 files changed

+89
-28
lines changed

packages/postcss-container-query/src/containerQuery.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const walkRules = (root, opts, ruleHandler) => {
1515
containerSelectors.indexOf(selector) !== -1;
1616

1717
const handleRule = (rule, parentCQAtRule) => {
18+
const definedContainer = hasContainerDefinition(rule);
1819
const isContainer =
19-
hasContainerDefinition(rule) ||
20+
definedContainer ||
2021
hasContainerSelector(rule.selector) ||
2122
rule.selector === ":self" ||
2223
(opts.singleContainer && containerSelectors.length === 0);
2324

24-
const data = { rule, isContainer };
25+
const data = { rule, isContainer, definedContainer };
2526

2627
if (isContainer && !hasContainerSelector(rule.selector)) {
2728
containerSelectors.push(rule.selector);
@@ -103,20 +104,33 @@ function containerQuery(options = {}) {
103104
if (!meta) {
104105
const containers = {};
105106
let currentContainerSelector = null;
107+
let containerDefinitionCount = 0;
106108

107109
walkRules(
108110
root,
109111
{ singleContainer },
110-
({ rule, isContainer, parentCQAtRule }) => {
112+
({ rule, isContainer, definedContainer, parentCQAtRule }) => {
111113
if (
112114
isContainer &&
113115
rule.selector !== ":self" &&
114116
!containers[rule.selector]
115117
) {
118+
if (definedContainer) {
119+
containerDefinitionCount++;
120+
}
121+
116122
const nextContainerSelector = rule.selector;
117-
if (singleContainer && currentContainerSelector) {
123+
if (
124+
// We allow for a single custom definition to be used in
125+
// singleContainer mode so that the user can select the container
126+
// selector themselves, instead that being picked up as the first
127+
// one automatically.
128+
containerDefinitionCount > 1 &&
129+
singleContainer &&
130+
currentContainerSelector
131+
) {
118132
throw rule.error(
119-
`define-container declaration detected in singleContainer mode. ` +
133+
`More than one @define-container declaration was detected in singleContainer mode. ` +
120134
`Tried to override "${currentContainerSelector}" with "${nextContainerSelector}".`
121135
);
122136
}

packages/postcss-container-query/src/containerQuery.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import postcss from "postcss";
22
import * as regularTest from "./test/regular";
33
import * as customPropertiesTest from "./test/custom-properties";
44
import * as exessContainerDeclarationTest from "./test/exess-container-declaration";
5+
import * as singleContainerOverrideTest from "./test/single-container-override";
56
import * as containerAutoDetectionTest from "./test/container-auto-detection";
67
import * as unrecognisedAtRulesTest from "./test/unrecognised-at-rules";
78
import * as missingContainerDelcarationTest from "./test/missing-container-declaration";
@@ -92,12 +93,15 @@ test("should properly process CSS", () =>
9293
test("should detect the first class as the container by default", () =>
9394
assertProcessingResult(containerAutoDetectionTest));
9495

95-
test("should throw in non singleContainer mode for defining a different container", () => {
96+
test("should allow for one @define-container declaration in singleContainer mode", () =>
97+
assertProcessingResult(singleContainerOverrideTest));
98+
99+
test("should throw in singleContainer mode for defining a different container more than one time", () => {
96100
expect.assertions(1);
97101

98102
return processCss(exessContainerDeclarationTest.cssInput).catch(e => {
99103
expect(e.reason).toBe(
100-
"define-container declaration detected in singleContainer mode. " +
104+
"More than one @define-container declaration was detected in singleContainer mode. " +
101105
'Tried to override ".Container" with ".AnotherContainer".'
102106
);
103107
});
@@ -121,7 +125,6 @@ test("should be able to disable the css meta export", () =>
121125
exportMetaInCss: false
122126
}));
123127

124-
// todo rename test to reexport
125128
// In parcel bundler postcss plugins run multiple times for some reason over the
126129
// same css file. If we respect the meta export, then subsequent runs become noop.
127130
test("should be able to reexport meta from a previously processed css file", () =>

packages/postcss-container-query/src/test/container-auto-detection.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import {
22
SELECTOR,
33
QUERIES,
44
ELEMENTS,
5-
VALUES,
6-
CONDITIONS
5+
VALUES
76
} from "@zeecoder/container-query-meta-builder";
87

8+
// These container definitions should be merged together, as they use the same
9+
// selector
910
export const cssInput = `
1011
.Container {
1112
line-height: 3rh;

packages/postcss-container-query/src/test/exess-container-declaration.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
export const cssInput = `
2+
body {
3+
/* This would be picked up as the container normally in singleContainer mode. */
4+
}
5+
26
.Container {
7+
/* One override is allowed */
8+
@define-container;
39
line-height: 3rh;
410
border: none;
511
}
612
713
.AnotherContainer {
14+
/* Two override is not allowed */
815
@define-container;
916
font-size: 2rh;
1017
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
SELECTOR,
3+
QUERIES,
4+
ELEMENTS,
5+
VALUES
6+
} from "@zeecoder/container-query-meta-builder";
7+
8+
export const cssInput = `
9+
body {
10+
background: red;
11+
}
12+
13+
.Container {
14+
@define-container;
15+
font-size: 1rh;
16+
}`;
17+
18+
export const meta = {
19+
[SELECTOR]: ".Container",
20+
[QUERIES]: [
21+
{
22+
[ELEMENTS]: [
23+
{
24+
[VALUES]: {
25+
"font-size": `1rh`
26+
}
27+
}
28+
]
29+
}
30+
]
31+
};
32+
33+
export const cssOutput = `
34+
body {
35+
background: red;
36+
}
37+
38+
.Container {
39+
}
40+
41+
:export { meta: '${JSON.stringify(meta)}' }`;

tests/react/hoc/Test/Test.pcss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
body {
2+
background: rgb(200, 200, 200);
3+
}
4+
15
.Test {
6+
@define-container;
27
position: absolute;
38
width: 100%;
49
height: 100%;

tests/react/hoc/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
clearDOM,
66
waitForTestComponentToHaveStyle,
77
changeRootSize,
8-
waitForTestComponentToHaveCustomProperties
8+
waitForTestComponentToHaveCustomProperties,
9+
waitForElementToHaveStyle
910
} from "../../utils";
1011

1112
// Features covered:
@@ -20,6 +21,12 @@ describe("withContainerQuery", () => {
2021
});
2122
});
2223

24+
it("should apply styles to the body", async () => {
25+
await waitForElementToHaveStyle(document.body, {
26+
backgroundColor: "rgb(200, 200, 200)"
27+
});
28+
});
29+
2330
it("should not have any of the container queries applied", async () => {
2431
await waitForTestComponentToHaveStyle({
2532
backgroundColor: "rgb(255, 0, 0)"

tests/react/manual/Test/Test.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/react/manual/Test/Test.pcss

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)