Skip to content

Commit 86e66d8

Browse files
authored
fix(tailwind): media queries not getting sanitized sometimes (#1929)
1 parent 0ec527b commit 86e66d8

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

.changeset/calm-birds-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/tailwind": patch
3+
---
4+
5+
Fix color-scheme media queries sometimes not getting sanitized at the className

packages/tailwind/src/__snapshots__/tailwind.spec.tsx.snap

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ exports[`Tailwind component > should work with custom components with fragment a
4444
4545
exports[`non-inlinable styles > should add css to <head/> and keep class names 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en"><head><!--$--><style>@media(min-width:640px){.sm_bg-red-300{background-color:rgb(252,165,165) !important}.sm_hover_bg-red-200:hover{background-color:rgb(254,202,202) !important}}@media(min-width:768px){.md_bg-red-400{background-color:rgb(248,113,113) !important}}@media(min-width:1024px){.lg_bg-red-500{background-color:rgb(239,68,68) !important}}.hover_bg-red-600:hover{background-color:rgb(220,38,38) !important}.focus_bg-red-700:focus{background-color:rgb(185,28,28) !important}</style></head><body><div class="hover_bg-red-600 focus_bg-red-700 sm_bg-red-300 sm_hover_bg-red-200 md_bg-red-400 lg_bg-red-500" style="background-color:rgb(254,202,202)"></div><!--/$--></body></html>"`;
4646
47-
exports[`non-inlinable styles > should not have duplicate media queries 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><meta name="x-apple-disable-message-reformatting"/><!--$--><style>@media(min-width:768px){.md_px-64px{padding-left:64px !important;padding-right:64px !important}}.hover_underline:hover{text-decoration-line:underline !important}</style></head><body class="hover_underline md_px-64px" style="margin-left:auto;margin-right:auto;margin-top:auto;margin-bottom:auto;background-color:rgb(255,255,255);font-family:ui-sans-serif, system-ui, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;"><div class="hover_underline md_px-64px"></div><!--/$--></body>"`;
47+
exports[`non-inlinable styles > should not have duplicate media queries 1`] = `
48+
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
49+
<head>
50+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
51+
<meta name="x-apple-disable-message-reformatting" />
52+
<!--$-->
53+
<style>
54+
@media(min-width:768px){.md_px-64px{padding-left:64px !important;padding-right:64px !important}}@media(prefers-color-scheme:dark){.dark_bg-black{background-color:rgb(0,0,0) !important}.dark_text-green-500{color:rgb(34,197,94) !important}}
55+
</style>
56+
</head>
57+
<body class="md_px-64px dark_bg-black dark_text-green-500">
58+
<div class="md_px-64px dark_text-green-500"></div>
59+
<!--/$-->
60+
</body>
61+
"
62+
`;
4863
4964
exports[`non-inlinable styles > should persist existing <head/> elements 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en"><head><!--$--><style></style><link/><style>@media(min-width:640px){.sm_bg-red-500{background-color:rgb(239,68,68) !important}}</style></head><body><div class="sm_bg-red-500" style="background-color:rgb(254,202,202)"></div><!--/$--></body></html>"`;
5065

packages/tailwind/src/tailwind.spec.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,13 @@ describe('non-inlinable styles', () => {
387387
const output = await render(
388388
<Tailwind>
389389
<Head />
390-
<Body className="mx-auto my-auto bg-white font-sans hover:underline md:px-[64px]">
391-
<div className="hover:underline md:px-[64px]" />
390+
<Body className="md:px-[64px] dark:bg-black dark:text-green-500">
391+
<div className="md:px-[64px] dark:text-green-500" />
392392
</Body>
393393
</Tailwind>,
394+
{
395+
pretty: true,
396+
},
394397
);
395398

396399
expect(output).toMatchSnapshot();

packages/tailwind/src/utils/tailwindcss/setup-tailwind.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function setupTailwind(config: TailwindConfig) {
2424
const tailwindContext = setupTailwindContext(config);
2525
return {
2626
generateRootForClasses: (classes: string[]) => {
27+
tailwindContext.candidateRuleCache = new Map();
2728
const bigIntRuleTuples: [bigint, Rule][] = rawGenerateRules(
2829
new Set(classes),
2930
tailwindContext,
@@ -42,8 +43,8 @@ export function setupTailwind(config: TailwindConfig) {
4243
evaluateTailwindFunctions(tailwindContext)(root);
4344
substituteScreenAtRules(tailwindContext)(root);
4445
resolveDefaultsAtRules(tailwindContext)(root);
45-
collapseAdjacentRules(tailwindContext)(root);
46-
collapseDuplicateDeclarations(tailwindContext)(root);
46+
collapseAdjacentRules()(root);
47+
collapseDuplicateDeclarations()(root);
4748

4849
resolveAllCSSVariables(root);
4950

packages/tailwind/src/utils/tailwindcss/tailwind-internals.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,14 @@ declare module "tailwindcss/lib/lib/collapseAdjacentRules" {
6363
import type { JitContext } from "tailwindcss/lib/lib/setupContextUtils";
6464
import type { Root } from "postcss";
6565

66-
export default async function collapseAdjacentRules(
67-
context: JITContext,
68-
): (root: Root) => void;
66+
export default function collapseAdjacentRules(): (root: Root) => void;
6967
}
7068

7169
declare module "tailwindcss/lib/lib/collapseDuplicateDeclarations" {
7270
import type { JitContext } from "tailwindcss/lib/lib/setupContextUtils";
7371
import type { Root } from "postcss";
7472

75-
export default async function collapseDuplicateDeclarations(
76-
context: JITContext,
77-
): (root: Root) => void;
73+
export default function collapseDuplicateDeclarations(): (root: Root) => void;
7874
}
7975

8076
declare module "tailwindcss/lib/lib/generateRules" {

0 commit comments

Comments
 (0)