Skip to content

Commit f5bc1e9

Browse files
authored
Merge pull request #42 from OnedocLabs/ffo-173-improve-support-for-dark-selectors-in-react-print
Improves handling of pseudo selectors with Tailwind
2 parents fe334d2 + 3c4204d commit f5bc1e9

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

package-lock.json

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"webdriverio": "^8.34.1"
8585
},
8686
"dependencies": {
87+
"@csstools/postcss-is-pseudo-class": "^4.0.8",
8788
"@emotion/cache": "^11.11.0",
8889
"@emotion/react": "^11.11.3",
8990
"@emotion/server": "^11.11.0",

src/compile/compile.tsx

+17-9
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,20 @@ export const compile = async (
6262

6363
const { default: postcss } = await import("postcss");
6464
const { default: cssvariables } = await import("postcss-css-variables");
65+
const { default: isPseudoClass } = await import(
66+
// @ts-ignore
67+
"@csstools/postcss-is-pseudo-class"
68+
);
6569
// @ts-ignore
6670
const { default: logical } = await import("postcss-logical");
6771

68-
const result = await postcss([cssvariables(), logical()]).process(
69-
mergedStylesheet,
70-
{
71-
from: undefined,
72-
}
73-
);
72+
const result = await postcss([
73+
cssvariables(),
74+
logical(),
75+
isPseudoClass(),
76+
]).process(mergedStylesheet, {
77+
from: undefined,
78+
});
7479

7580
return `<style>${result.css}</style>${html}`;
7681
};
@@ -85,13 +90,16 @@ export const __docConfig: DocConfig = {
8590
server: true,
8691
client: true,
8792
examples: {
88-
default:{
93+
default: {
8994
description: `A simple function to compile a React component to an HTML string with the Onedoc print styles.
9095
\`\`\`jsx
9196
const html = await compile(<Component />);
9297
\`\`\``,
93-
template: <Tailwind><div className="bg-red-400">Hello World!</div></Tailwind>,
94-
98+
template: (
99+
<Tailwind>
100+
<div className="bg-red-400">Hello World!</div>
101+
</Tailwind>
102+
),
95103
},
96104
emotion: {
97105
description: `Pass \`{ emotion: true }\` as the second compile option to merge and extract critical CSS using Emotion. Some libraries such as Chakra UI require this option to work correctly.

src/tailwind/tailwind.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import { CSS } from "../css/css";
1919
import preflightCss from "../../node_modules/tailwindcss/lib/css/preflight.css?raw";
2020
import { createTailwindcssPlugin } from "@mhsdesign/jit-browser-tailwindcss";
2121

22+
// @ts-ignore
23+
import isPseudoClass from "@csstools/postcss-is-pseudo-class";
24+
2225
export const Tailwind = ({
2326
children,
2427
config,
@@ -53,6 +56,7 @@ export const Tailwind = ({
5356
content: [{ content: markup, extension: "html" }],
5457
}),
5558
// postcssCssVariables,
59+
isPseudoClass(),
5660
postcssColorFunctionalNotation,
5761
]).process(
5862
String.raw`
@@ -89,7 +93,8 @@ The supported Tailwind version is 3.3.2 due to changes in the PostCSS plugin syn
8993
server: true,
9094
examples: {
9195
default: {
92-
description: "Use a simple Tailwind tag to support Tailwind in your document.",
96+
description:
97+
"Use a simple Tailwind tag to support Tailwind in your document.",
9398
template: (
9499
<Tailwind>
95100
<div className="bg-gradient-to-tr from-blue-500 to-blue-700 rounded-2xl p-12"></div>

tests/compile.test.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ test("works with tailwind", async () => {
2525

2626
expect(html).toContain("rgba(239, 68, 68");
2727
});
28+
29+
test("works with tailwind dark", async () => {
30+
const TestComponent = () => (
31+
<Tailwind
32+
config={{
33+
darkMode: "class",
34+
}}
35+
>
36+
<div className="dark:bg-red-500">Test</div>
37+
</Tailwind>
38+
);
39+
40+
const html = await compile(<TestComponent />);
41+
42+
console.log(html);
43+
44+
expect(html).toContain("rgba(239, 68, 68");
45+
});

0 commit comments

Comments
 (0)