Skip to content

Commit 159577a

Browse files
Merge branch 'main' into reduce-range-units
2 parents f9b12cc + ced5458 commit 159577a

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf

apps/builder/app/shared/tailwind/tailwind.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,65 @@ describe("extract breakpoints", () => {
511511
)
512512
);
513513
});
514+
515+
test("adapt max-* breakpoints", async () => {
516+
expect(
517+
await generateFragmentFromTailwind(
518+
renderTemplate(
519+
<ws.element
520+
ws:tag="div"
521+
class="max-sm:opacity-10 max-md:opacity-20 max-lg:opacity-30 max-xl:opacity-40 max-2xl:opacity-50 opacity-60"
522+
></ws.element>
523+
)
524+
)
525+
).toEqual(
526+
renderTemplate(
527+
<ws.element
528+
ws:tag="div"
529+
ws:style={css`
530+
@media (min-width: 1440px) {
531+
opacity: 0.6;
532+
}
533+
@media (min-width: 1280px) {
534+
opacity: 0.5;
535+
}
536+
opacity: 0.4;
537+
@media (max-width: 991px) {
538+
opacity: 0.3;
539+
}
540+
@media (max-width: 767px) {
541+
opacity: 0.2;
542+
}
543+
@media (max-width: 479px) {
544+
opacity: 0.1;
545+
}
546+
`}
547+
></ws.element>
548+
)
549+
);
550+
});
551+
552+
test("ignore composite breakpoints", async () => {
553+
expect(
554+
await generateFragmentFromTailwind(
555+
renderTemplate(
556+
<ws.element
557+
ws:tag="div"
558+
class="opacity-10 md:max-xl:flex"
559+
></ws.element>
560+
)
561+
)
562+
).toEqual(
563+
renderTemplate(
564+
<ws.element
565+
ws:tag="div"
566+
ws:style={css`
567+
opacity: 0.1;
568+
`}
569+
></ws.element>
570+
)
571+
);
572+
});
514573
});
515574

516575
test("generate space without display property", async () => {

apps/builder/app/shared/tailwind/tailwind.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ const availableBreakpoints = [
2727
];
2828

2929
const tailwindToWebstudioMappings: Record<number, undefined | number> = {
30-
639: 479,
30+
639.9: 479,
3131
640: 480,
32-
1023: 991,
32+
767.9: 767,
33+
1023.9: 991,
3334
1024: 992,
34-
1535: 1439,
35+
1279.9: 1279,
36+
1535.9: 1439,
3537
1536: 1440,
3638
};
3739

@@ -120,8 +122,10 @@ const rangesToBreakpoints = (ranges: Range[]) => {
120122
const adaptBreakpoints = (
121123
parsedStyles: Omit<ParsedStyleDecl, "selector">[]
122124
) => {
125+
const newStyles: typeof parsedStyles = [];
123126
const breakpointGroups = new Map<string, Breakpoint[]>();
124127
for (const styleDecl of parsedStyles) {
128+
newStyles.push(styleDecl);
125129
const mediaQuery = styleDecl.breakpoint
126130
? parseMediaQuery(styleDecl.breakpoint)
127131
: undefined;
@@ -155,13 +159,14 @@ const adaptBreakpoints = (
155159
breakpointsByKey.set(breakpoint.key, breakpoint);
156160
}
157161
}
158-
for (const styleDecl of parsedStyles) {
162+
for (const styleDecl of newStyles) {
159163
const styleDeclKey = `${styleDecl.breakpoint ?? ""}:${styleDecl.property}:${styleDecl.state ?? ""}`;
160164
const breakpoint = breakpointsByKey.get(styleDeclKey);
161165
if (breakpoint) {
162166
styleDecl.breakpoint = serializeBreakpoint(breakpoint);
163167
}
164168
}
169+
return newStyles;
165170
};
166171

167172
const createUnoGenerator = async () => {

packages/css-data/src/parse-css.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,30 @@ test("ignore unsupported media queries", () => {
714714
]);
715715
});
716716

717+
test("ignore nested media queries", () => {
718+
expect(
719+
parseCss(`
720+
@media (min-width: 768px) {
721+
a {
722+
color: green;
723+
}
724+
@media (max-width: 1024px) {
725+
a {
726+
color: red;
727+
}
728+
}
729+
}
730+
`)
731+
).toEqual([
732+
{
733+
breakpoint: "(min-width:768px)",
734+
selector: "a",
735+
property: "color",
736+
value: { type: "keyword", value: "green" },
737+
},
738+
]);
739+
});
740+
717741
test("ignore unsupported at rules", () => {
718742
expect(
719743
parseCss(`

packages/css-data/src/parse-css.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ export const parseCss = (css: string): ParsedStyleDecl[] => {
103103
}
104104

105105
csstree.walk(ast, function (node) {
106+
// forbid nested at rules
107+
if (node.type === "Atrule" && this.atrule) {
108+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
109+
// @ts-ignore https://github.com/csstree/csstree/blob/v2.3.1/docs/traversal.md
110+
return this.break;
111+
}
106112
if (node.type !== "Declaration" || this.rule?.prelude.type === undefined) {
107113
return;
108114
}

0 commit comments

Comments
 (0)