Skip to content

Commit 473b1f2

Browse files
authored
feat: add a sample styled components migration workflow (#174)
1 parent 534d757 commit 473b1f2

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

.grit/workflows/styled.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as grit from '@getgrit/api';
2+
3+
export async function execute(options: grit.WorkflowOptions) {
4+
const transformResult = await grit.stdlib.transform(
5+
{
6+
objective: `You are an expert software engineer working on a migration from styled-components to Tailwind CSS.
7+
Given a styled component, you must migrate it a simple component with appropriate Tailwind classes.`,
8+
principles: ['Use the twMerge library to conditionally combine classes.'],
9+
model: 'slow',
10+
query: 'or { js"styled.$_`$_`", js"styled($_)`$_`" }',
11+
examples: [
12+
{
13+
input: `const StyledComponent = styled(({ backgroundColor, ...otherProps }) => (
14+
<MyContainer {...otherProps} />
15+
))\`
16+
position: relative;
17+
background-color: \${({ backgroundColor }) => backgroundColor};
18+
\`;`,
19+
replacements: [
20+
`({backgroundColor, ...otherProps}) => {
21+
const bgColorClass = backgroundColor ? \`bg-\${backgroundColor}\` : '';
22+
const className = twMerge(\`relative \${bgColorClass}\`);
23+
return (
24+
<MyContainer className={className} {...otherProps} />
25+
);
26+
}`,
27+
],
28+
},
29+
],
30+
},
31+
{},
32+
);
33+
if (!transformResult.success) return transformResult;
34+
await grit.stdlib.apply(
35+
{
36+
query: `js"twMerge" as $mg where { $mg <: ensure_import_from(source=js"'tailwind-merge'") }`,
37+
},
38+
{ paths: transformResult.paths },
39+
);
40+
await grit.stdlib.apply(
41+
{
42+
query: `js"import $_ from 'styled-components'" => .`,
43+
},
44+
{ paths: transformResult.paths },
45+
);
46+
return {
47+
success: true,
48+
message: `Successfully migrated ${transformResult.paths.length} files.`,
49+
};
50+
}

samples/styled.in.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled, { css } from 'styled-components';
2+
3+
export const Button = styled.a<{ $primary?: boolean }>`
4+
background: transparent;
5+
border-radius: 3px;
6+
7+
&:hover {
8+
filter: brightness(0.85);
9+
}
10+
11+
${(props) =>
12+
props.$primary &&
13+
css`
14+
background: red;
15+
color: black;
16+
`}
17+
`;

0 commit comments

Comments
 (0)