Skip to content

Instantly share code, notes, and snippets.

@JamesIves
Last active March 22, 2022 14:43
Show Gist options
  • Save JamesIves/2a2e92f94df6ad890d4205c277888e53 to your computer and use it in GitHub Desktop.
Save JamesIves/2a2e92f94df6ad890d4205c277888e53 to your computer and use it in GitHub Desktop.
Convert Figma CSS Properties to TailwindCSS utilities. This is only partially functional, feel free to take this code and use it to build your own plugin.
/** Converts Figma CSS Properties to TailwindCSS utilities.
* This plugin is unfinished, feel free to take this code and extend it to build your own plugin!
* @author James Ives - https://jamesiv.es
*
*/
const convertComponentToHex = (c: number) => {
const hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
};
const rgbToHex = (r: number, g: number, b: number) =>
`#${convertComponentToHex(r)}${convertComponentToHex(
g
)}${convertComponentToHex(b)}`;
const getRGBValues = ({ r, g, b }) =>
[r, g, b].map((channel) => Math.round(channel * 255));
figma.showUI(__html__);
figma.ui.onmessage = (msg, callback) => {
if (msg.type === "convert-element") {
if (
figma.currentPage.selection.length &&
figma.currentPage.selection.length === 1
) {
const selection = figma.currentPage.selection[0];
if (selection.type === "RECTANGLE") {
const height = selection.height;
const width = selection.width;
const radius = selection.cornerRadius;
const opacity = selection.opacity;
const fill = selection.fills[0];
const sheet = {
height: `h-[${height}px]`,
width: `w-[${width}px]`,
radius: `rounded-[${String(radius)}px]`,
opacity: `opacity-[${opacity}]`,
background: ``,
effects: ``,
};
// Applies effects for things such as drop shadows.
selection.effects.forEach((effect) => {
if (effect.type === "DROP_SHADOW") {
const [r, g, b] = getRGBValues(effect.color);
sheet.effects += `shadow-[${effect.offset.x}px_${effect.offset.y}px_${effect.radius}px_${effect.spread}px_rgba(${r}, ${g}, ${b}, ${effect.color.a})]`;
}
});
//
if (fill.type === "SOLID") {
const [r, g, b] = getRGBValues(fill.color);
const hex = rgbToHex(r, g, b);
sheet.background = `bg-[${hex}]`;
}
if (fill.type === "GRADIENT_LINEAR") {
fill.gradientStops.forEach((stop, index, array) => {
const [r, g, b] = getRGBValues(stop.color);
const hex = rgbToHex(r, g, b);
if (index === array.length - 1) {
sheet.background += `to-[${hex}] `;
} else if (index === 0) {
sheet.background += `bg-gradient-to-b from-[${hex}] `;
} else {
sheet.background += `via-[${hex}] `;
}
});
}
// Alerts the converted properties to Figma.
alert(`${[...Object.values(sheet)].join(" ")}`);
} else {
// If the user attempts to run the plugin on an unsupported selection type, inform them.
alert("Selection type not supported");
}
} else {
// If the user attempts to run the plugin on multiple nodes at once, inform them.
alert("Please select a single node");
}
}
figma.closePlugin();
};
<h2>Convert to TailwindCSS Classes</h2>
<button id="create">Convert</button>
<script>
document.getElementById('create').onclick = () => {
parent.postMessage({ pluginMessage: { type: 'convert-element' } }, '*')
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment