Skip to content

Commit f36be18

Browse files
authored
Simplify Tailwind implementation (#110)
* Modify Tailwind color functions to support Figma variables * Refactor function to arrow function * Reorganise all config to separate file * Simplify Tailwind code * Clarify Tailwind color usage * Add Tailwind support for horizontal auto-layout hug (#111) Co-authored-by: Dave Stewart <[email protected]> * Add Tailwind support for Figma variables (#109) * Modify Tailwind color functions to support Figma variables * Refactor function to arrow function * Fix fill opacity code * Add new Tailwind preferences * Prefer Tailwind color when color matches exactly * Update UI with shorter labels and tooltips --------- Co-authored-by: Dave Stewart <[email protected]> --------- Co-authored-by: Dave Stewart <[email protected]>
1 parent d2c3f52 commit f36be18

File tree

6 files changed

+543
-502
lines changed

6 files changed

+543
-502
lines changed

apps/plugin/plugin-src/code.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,35 +144,13 @@ const codegenMode = async () => {
144144
},
145145
];
146146
case "tailwind":
147-
return [
148-
{
149-
title: `Code`,
150-
code: tailwindMain(convertedSelection, {
151-
...userPluginSettings,
152-
jsx: false,
153-
}),
154-
language: "HTML",
155-
},
156-
{
157-
title: `Colors`,
158-
code: retrieveGenericSolidUIColors("Tailwind")
159-
.map((d) => `#${d.hex} <- ${d.colorName}`)
160-
.join("\n"),
161-
language: "HTML",
162-
},
163-
{
164-
title: `Text Styles`,
165-
code: tailwindCodeGenTextStyles(),
166-
language: "HTML",
167-
},
168-
];
169147
case "tailwind_jsx":
170148
return [
171149
{
172150
title: `Code`,
173151
code: tailwindMain(convertedSelection, {
174152
...userPluginSettings,
175-
jsx: true,
153+
jsx: language === 'tailwind_jsx',
176154
}),
177155
language: "HTML",
178156
},
@@ -182,11 +160,20 @@ const codegenMode = async () => {
182160
// language: "HTML",
183161
// },
184162
{
185-
title: `Colors`,
163+
title: `Tailwind Colors`,
186164
code: retrieveGenericSolidUIColors("Tailwind")
187-
.map((d) => `#${d.hex} <- ${d.colorName}`)
165+
.map((d) => {
166+
let str = `${d.hex};`
167+
if (d.colorName !== d.hex) {
168+
str += ` // ${d.colorName}`
169+
}
170+
if (d.meta) {
171+
str += ` (${d.meta})`
172+
}
173+
return str;
174+
})
188175
.join("\n"),
189-
language: "HTML",
176+
language: "JAVASCRIPT",
190177
},
191178
{
192179
title: `Text Styles`,

packages/backend/src/common/retrieveUI/retrieveColors.ts

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
import { rgbTo6hex } from "../color";
2-
import {
3-
swiftuiColor,
4-
swiftuiGradient,
5-
} from "../../swiftui/builderImpl/swiftuiColor";
6-
import {
7-
getTailwindFromVariable,
8-
tailwindColors,
9-
tailwindGradient,
10-
tailwindNearestColor,
11-
tailwindSolidColor
12-
} from "../../tailwind/builderImpl/tailwindColor";
13-
import {
14-
flutterColor,
15-
flutterGradient,
16-
} from "../../flutter/builderImpl/flutterColor";
17-
import {
18-
htmlColor,
19-
htmlGradientFromFills,
20-
} from "../../html/builderImpl/htmlColor";
2+
import { swiftuiColor, swiftuiGradient } from "../../swiftui/builderImpl/swiftuiColor";
3+
import { tailwindColor, tailwindGradient } from "../../tailwind/builderImpl/tailwindColor";
4+
import { flutterColor, flutterGradient } from "../../flutter/builderImpl/flutterColor";
5+
import { htmlColor, htmlGradientFromFills } from "../../html/builderImpl/htmlColor";
216
import { calculateContrastRatio } from "./commonUI";
227
import { FrameworkTypes } from "../../code";
238

@@ -27,6 +12,7 @@ export type ExportSolidColor = {
2712
exportValue: string;
2813
contrastWhite: number;
2914
contrastBlack: number;
15+
meta?: string
3016
};
3117

3218
export const retrieveGenericSolidUIColors = (
@@ -35,15 +21,18 @@ export const retrieveGenericSolidUIColors = (
3521
const selectionColors = figma.getSelectionColors();
3622
if (!selectionColors || selectionColors.paints.length === 0) return [];
3723

38-
const colorStr: Array<ExportSolidColor> = [];
24+
const colors: Array<ExportSolidColor> = [];
3925
selectionColors.paints.forEach((paint) => {
4026
const fill = convertSolidColor(paint, framework);
4127
if (fill) {
42-
colorStr.push(fill);
28+
const exists = colors.find(col => col.colorName === fill.colorName)
29+
if (!exists) {
30+
colors.push(fill);
31+
}
4332
}
4433
});
4534

46-
return colorStr.sort((a, b) => a.hex.localeCompare(b.hex));
35+
return colors.sort((a, b) => a.hex.localeCompare(b.hex));
4736
};
4837

4938
const convertSolidColor = (
@@ -56,35 +45,25 @@ const convertSolidColor = (
5645
if (fill.type !== "SOLID") return null;
5746

5847
const opacity = fill.opacity ?? 1.0;
59-
let exported = "";
60-
let colorName = "";
61-
let contrastBlack = calculateContrastRatio(fill.color, black);
62-
let contrastWhite = calculateContrastRatio(fill.color, white);
48+
const output = {
49+
hex: rgbTo6hex(fill.color).toUpperCase(),
50+
colorName: "",
51+
exportValue: "",
52+
contrastBlack: calculateContrastRatio(fill.color, black),
53+
contrastWhite: calculateContrastRatio(fill.color, white),
54+
};
6355

6456
if (framework === "Flutter") {
65-
exported = flutterColor(fill.color, opacity);
57+
output.exportValue = flutterColor(fill.color, opacity);
6658
} else if (framework === "HTML") {
67-
exported = htmlColor(fill.color, opacity);
59+
output.exportValue = htmlColor(fill.color, opacity);
6860
} else if (framework === "Tailwind") {
69-
const kind = "solid";
70-
const hex = rgbTo6hex(fill.color);
71-
const hexNearestColor = tailwindNearestColor(hex);
72-
const colorVar = fill.boundVariables?.color
73-
exported = tailwindSolidColor(fill, kind);
74-
colorName = colorVar
75-
? getTailwindFromVariable(colorVar)
76-
: tailwindColors[hexNearestColor];
61+
Object.assign(output, tailwindColor(fill));
7762
} else if (framework === "SwiftUI") {
78-
exported = swiftuiColor(fill.color, opacity);
63+
output.exportValue = swiftuiColor(fill.color, opacity);
7964
}
8065

81-
return {
82-
hex: rgbTo6hex(fill.color).toUpperCase(),
83-
colorName,
84-
exportValue: exported,
85-
contrastBlack,
86-
contrastWhite,
87-
};
66+
return output;
8867
};
8968

9069
type ExportLinearGradient = { cssPreview: string; exportValue: string };
@@ -97,24 +76,24 @@ export const retrieveGenericLinearGradients = (
9776

9877
selectionColors?.paints.forEach((paint) => {
9978
if (paint.type === "GRADIENT_LINEAR") {
100-
let exported = "";
79+
let exportValue = "";
10180
switch (framework) {
10281
case "Flutter":
103-
exported = flutterGradient(paint);
82+
exportValue = flutterGradient(paint);
10483
break;
10584
case "HTML":
106-
exported = htmlGradientFromFills([paint]);
85+
exportValue = htmlGradientFromFills([paint]);
10786
break;
10887
case "Tailwind":
109-
exported = tailwindGradient(paint);
88+
exportValue = tailwindGradient(paint);
11089
break;
11190
case "SwiftUI":
112-
exported = swiftuiGradient(paint);
91+
exportValue = swiftuiGradient(paint);
11392
break;
11493
}
11594
colorStr.push({
11695
cssPreview: htmlGradientFromFills([paint]),
117-
exportValue: exported,
96+
exportValue,
11897
});
11998
}
12099
});

0 commit comments

Comments
 (0)