Skip to content

Commit c6e0748

Browse files
Jackardiosritz078
andauthored
Update "CSS to Tailwind" page (#355)
* update `css-to-tailwindcss` package * add `arbitraryPropertiesIsEnabled` configuration * update `css-to-tailwindcss` package * add note about using classes instead of `@apply` directive * update `css-to-tailwindcss` package * update `css-to-tailwindcss` package --------- Co-authored-by: Ritesh Kumar <[email protected]>
1 parent d893a2b commit c6e0748

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"babel-plugin-object-styles-to-template": "^0.2.2",
5555
"babel-standalone": "^6.26.0",
5656
"clipboard-copy": "^4.0.1",
57-
"css-to-tailwindcss": "^0.2.5",
57+
"css-to-tailwindcss": "^1.0.4",
5858
"evergreen-ui": "^4.28.0",
5959
"flowgen": "^1.14.1",
6060
"formik": "^2.2.9",

pages/css-to-tailwind.tsx

+65-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
toaster,
1212
TextInput,
1313
Heading,
14-
Text
14+
Text,
15+
Switch
1516
} from "evergreen-ui";
1617
import { TailwindConverter, TailwindConverterConfig } from "css-to-tailwindcss";
1718

@@ -25,13 +26,23 @@ const Monaco = dynamic(() => import("../components/Monaco"), {
2526
interface RawSettings {
2627
tailwindConfig?: string;
2728
remInPx?: string | null;
29+
arbitraryPropertiesIsEnabled?: boolean;
2830
}
2931

3032
const evalConfig = (configValue: string) =>
3133
eval(`const module = {}; ${configValue}; module.exports;`);
3234

3335
const DEFAULT_POSTCSS_PLUGINS = [require("postcss-nested")];
3436

37+
function decorateResult(result: string) {
38+
return `/*
39+
Based on TailwindCSS recommendations,
40+
consider using classes instead of the \`@apply\` directive
41+
@see https://tailwindcss.com/docs/reusing-styles#avoiding-premature-abstraction
42+
*/
43+
${result}`;
44+
}
45+
3546
function CssToTailwindSettings({
3647
open,
3748
toggle,
@@ -43,11 +54,16 @@ function CssToTailwindSettings({
4354
onConfirm: (props: {
4455
tailwindConfig: string;
4556
remInPx: string;
57+
arbitraryPropertiesIsEnabled: boolean;
4658
}) => boolean | Promise<boolean>;
4759
settings: RawSettings;
4860
}) {
4961
const [tailwindConfig, setTailwindConfig] = useState(settings.tailwindConfig);
5062
const [remInPx, setRemInPx] = useState(settings.remInPx);
63+
const [
64+
arbitraryPropertiesIsEnabled,
65+
setArbitraryPropertiesIsEnabled
66+
] = useState(settings.arbitraryPropertiesIsEnabled || false);
5167

5268
return (
5369
<Dialog
@@ -57,7 +73,8 @@ function CssToTailwindSettings({
5773
onConfirm={async close => {
5874
const isSuccess = await onConfirm({
5975
tailwindConfig,
60-
remInPx
76+
remInPx,
77+
arbitraryPropertiesIsEnabled
6178
});
6279
if (isSuccess) {
6380
close();
@@ -78,16 +95,38 @@ function CssToTailwindSettings({
7895
placeholder="Enter URL"
7996
onChange={e => setRemInPx(e.target.value)}
8097
value={remInPx || ""}
98+
marginTop="4px"
99+
/>
100+
101+
<Heading marginTop={24}>
102+
Enable arbitrary properties
103+
<a
104+
href="https://tailwindcss.com/docs/adding-custom-styles#arbitrary-properties"
105+
target="_blank"
106+
style={{ verticalAlign: "middle" }}
107+
>
108+
<Tooltip content="Open the TailwindCSS docs...">
109+
<Icon icon="help" color="info" marginLeft={8} size={16} />
110+
</Tooltip>
111+
</a>
112+
</Heading>
113+
<Switch
114+
checked={arbitraryPropertiesIsEnabled}
115+
onChange={e =>
116+
setArbitraryPropertiesIsEnabled((e.target as any).checked)
117+
}
118+
marginTop="4px"
81119
/>
82-
<Heading marginTop={30}>
120+
121+
<Heading marginTop={24}>
83122
Tailwind configuration
84123
<a
85124
href="https://tailwindcss.com/docs/configuration"
86125
target="_blank"
87126
style={{ verticalAlign: "middle" }}
88127
>
89128
<Tooltip content="Open the TailwindCSS docs...">
90-
<Icon icon="help" color="info" marginLeft={16} size={16} />
129+
<Icon icon="help" color="info" marginLeft={8} size={16} />
91130
</Tooltip>
92131
</a>
93132
</Heading>
@@ -124,7 +163,8 @@ export default function CssToTailwind3({ defaultSettings }) {
124163

125164
const converterConfig = useMemo(() => {
126165
const config: Partial<TailwindConverterConfig> = {
127-
remInPx: rawSettings.remInPx ? parseInt(rawSettings.remInPx, 10) : null
166+
remInPx: rawSettings.remInPx ? parseInt(rawSettings.remInPx, 10) : null,
167+
arbitraryPropertiesIsEnabled: !!rawSettings.arbitraryPropertiesIsEnabled
128168
};
129169

130170
if (isNaN(config["remInPx"])) {
@@ -150,18 +190,29 @@ export default function CssToTailwind3({ defaultSettings }) {
150190
}, [rawSettings]);
151191

152192
const tailwindConverter = useMemo(() => {
153-
return new TailwindConverter({
154-
postCSSPlugins: DEFAULT_POSTCSS_PLUGINS,
155-
...converterConfig
156-
});
193+
try {
194+
return new TailwindConverter({
195+
postCSSPlugins: DEFAULT_POSTCSS_PLUGINS,
196+
...converterConfig
197+
});
198+
} catch (e) {
199+
toaster.danger(
200+
"Unable to create TailwindConverter. Invalid configuration passed",
201+
{
202+
description: e.message
203+
}
204+
);
205+
206+
return new TailwindConverter({ postCSSPlugins: DEFAULT_POSTCSS_PLUGINS });
207+
}
157208
}, [converterConfig]);
158209

159210
const transformer = useCallback<Transformer>(
160211
async ({ value }) => {
161212
try {
162-
return (
163-
await tailwindConverter.convertCSS(value)
164-
).convertedRoot.toString();
213+
return decorateResult(
214+
(await tailwindConverter.convertCSS(value)).convertedRoot.toString()
215+
);
165216
} catch (e) {
166217
toaster.danger("Unable to convert CSS", {
167218
description: e.message
@@ -215,7 +266,8 @@ export async function getStaticProps() {
215266
props: {
216267
defaultSettings: {
217268
tailwindConfig: rawTailwindConfig,
218-
remInPx: "16"
269+
remInPx: "16",
270+
arbitraryPropertiesIsEnabled: false
219271
} as RawSettings
220272
}
221273
};

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -3431,10 +3431,10 @@ css-select@^2.0.0:
34313431
domutils "^1.7.0"
34323432
nth-check "^1.0.2"
34333433

3434-
css-to-tailwindcss@^0.2.5:
3435-
version "0.2.5"
3436-
resolved "https://registry.yarnpkg.com/css-to-tailwindcss/-/css-to-tailwindcss-0.2.5.tgz#eeaf61bfec563a6ed5c09cb6eb122a80fa13f699"
3437-
integrity sha512-XpaDq1hwehqrDqCA2Y2jtYoigNUnzskhJdxUk5WTtR/bDiy6z4vMQX30mDO+l9OVqw9IYjMWjQocdx9Wc3bZ8w==
3434+
css-to-tailwindcss@^1.0.4:
3435+
version "1.0.4"
3436+
resolved "https://registry.yarnpkg.com/css-to-tailwindcss/-/css-to-tailwindcss-1.0.4.tgz#81a06ae8e0544054bcba491b6a346705e256d547"
3437+
integrity sha512-u08nj9nDinKQT5PnoINjno2QqowWBhI+UY65wwccS51BYN1TyIJpO3ft7mV/9GUPnjFsJ0Rnd6zYtsji5zDyoA==
34383438
dependencies:
34393439
colord "^2.9.3"
34403440
css-what "^6.1.0"

0 commit comments

Comments
 (0)