Skip to content

Instantly share code, notes, and snippets.

@ademilter
Last active April 11, 2021 01:12
Show Gist options
  • Save ademilter/4e77c2fcdd6d348436c56f4ee836469b to your computer and use it in GitHub Desktop.
Save ademilter/4e77c2fcdd6d348436c56f4ee836469b to your computer and use it in GitHub Desktop.
figma variant generator
const baseComponents: ComponentNode = selection()[0];
if (!baseComponents) return alert("Not selected!");
const w = baseComponents.width;
const h = baseComponents.height;
const fixedItem = "value";
const childNames = baseComponents.children.map((c) => c.name);
const combinations: [[String]] = getCombinations(childNames);
for (let comb of combinations) {
if (comb.length > 1 && comb.includes(fixedItem)) continue;
let newInstance: InstanceNode = baseComponents.createInstance();
let compName = ["Input", "Default"];
newInstance.children.forEach((c) => {
if (c.name === fixedItem) {
c.visible = true;
} else if (comb.includes(c.name)) {
c.visible = true;
compName.push("Yes");
} else {
c.visible = false;
compName.push("No");
}
});
const a = createComponent(_, newInstance);
a.name = compName.join("/");
a.clipsContent = true;
a.resize(w, h);
}
function getCombinations(valuesArray) {
var combi = [];
var temp = [];
var slent = Math.pow(2, valuesArray.length);
for (var i = 0; i < slent; i++) {
temp = [];
for (var j = 0; j < valuesArray.length; j++) {
if (i & Math.pow(2, j)) {
temp.push(valuesArray[j]);
}
}
if (temp.length > 0) {
combi.push(temp);
}
}
combi.sort((a, b) => a.length - b.length);
return combi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment