Skip to content

Commit 3db5f61

Browse files
authored
Enhancements to Tailwind Class Generation and Figma Plugin Updates (#131)
* Added Tailwind class names for serif and mono fonts When the user select a specific font family in Figma and it matches with the default font family defined in Tailwind, the generated code in the plugin uses the Tailwind utility classes. https://tailwindcss.com/docs/font-family * Added Tailwind class for overflow hidden When the option "Clip content" in Figma on a frame, or component is enabled, the plugin prints the class "overflow-hidden" in the generated code. https://tailwindcss.com/docs/overflow https://www.delasign.com/blog/figma-frame-clip-content/ * Improvement to css class name generation for custom color names Up until now the plugin would generate class names for custom color names as "hello-hello world" if the Figma variable name is "hello/hello world". This created 2 css class names. With this code change spaces would be converted to dashes, so that the Figma variable name "hello/hello world" will generate "hello-hello-world". * Added Figma "documentonchange" listener Every time something is being updated in the Figma file, the generated plugin code updates.
1 parent 53025e9 commit 3db5f61

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

apps/plugin/plugin-src/code.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,17 @@ const safeRun = (settings: PluginSettings) => {
8383
const standardMode = async () => {
8484
figma.showUI(__html__, { width: 450, height: 550, themeColors: true });
8585
await initSettings();
86+
87+
// Listen for selection changes
8688
figma.on("selectionchange", () => {
8789
safeRun(userPluginSettings);
8890
});
91+
92+
// Listen for document changes
93+
figma.on("documentchange", () => {
94+
safeRun(userPluginSettings);
95+
});
96+
8997
figma.ui.onmessage = (msg) => {
9098
console.log("[node] figma.ui.onmessage", msg);
9199

packages/backend/src/tailwind/conversionTables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const nearestColorFromRgb = (color: RGB) => {
115115

116116
export const variableToColorName = (alias: VariableAlias) => {
117117
return (
118-
figma.variables.getVariableById(alias.id)?.name.replaceAll("/", "-") ||
118+
figma.variables.getVariableById(alias.id)?.name.replaceAll("/", "-").replaceAll(" ", "-") ||
119119
alias.id.toLowerCase().replaceAll(":", "-")
120120
);
121121
};

packages/backend/src/tailwind/tailwindMain.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,24 @@ const tailwindFrame = (
146146
isJsx,
147147
);
148148

149+
// Add overflow-hidden class if clipsContent is true
150+
const clipsContentClass = node.clipsContent ? " overflow-hidden" : "";
151+
149152
if (node.layoutMode !== "NONE") {
150153
const rowColumn = tailwindAutoLayoutProps(node, node);
151-
return tailwindContainer(node, childrenStr, rowColumn, isJsx);
154+
return tailwindContainer(node, childrenStr, rowColumn + clipsContentClass, isJsx);
152155
} else {
153156
if (
154157
localTailwindSettings.optimizeLayout &&
155158
node.inferredAutoLayout !== null
156159
) {
157160
const rowColumn = tailwindAutoLayoutProps(node, node.inferredAutoLayout);
158-
return tailwindContainer(node, childrenStr, rowColumn, isJsx);
161+
return tailwindContainer(node, childrenStr, rowColumn + clipsContentClass, isJsx);
159162
}
160163

161164
// node.layoutMode === "NONE" && node.children.length > 1
162165
// children needs to be absolute
163-
return tailwindContainer(node, childrenStr, "", isJsx);
166+
return tailwindContainer(node, childrenStr, clipsContentClass, isJsx);
164167
}
165168
};
166169

packages/backend/src/tailwind/tailwindTextBuilder.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,46 @@ export class TailwindTextBuilder extends TailwindDefaultBuilder {
9797
};
9898

9999
fontFamily = (fontName: FontName): string => {
100+
const sansSerif = [
101+
'ui-sans-serif',
102+
'system-ui',
103+
'sans-serif',
104+
'Apple Color Emoji',
105+
'Segoe UI Emoji',
106+
'Segoe UI Symbol',
107+
'Noto Color Emoji'
108+
];
109+
110+
const serif = [
111+
'ui-serif',
112+
'Georgia',
113+
'Cambria',
114+
'Times New Roman',
115+
'Times',
116+
'serif'
117+
];
118+
119+
const mono = [
120+
'ui-monospace',
121+
'SFMono-Regular',
122+
'Menlo',
123+
'Monaco',
124+
'Consolas',
125+
'Liberation Mono',
126+
'Courier New',
127+
'monospace'
128+
];
129+
130+
if (sansSerif.includes(fontName.family)) {
131+
return 'font-sans';
132+
}
133+
if (serif.includes(fontName.family)) {
134+
return 'font-serif';
135+
}
136+
if (mono.includes(fontName.family)) {
137+
return 'font-mono';
138+
}
139+
100140
return "font-['" + fontName.family + "']";
101141
};
102142

0 commit comments

Comments
 (0)