Skip to main content
May 29, 2023 9:17 AM UTC
Question

combineAsVariant

I want to Create buttons with the variants, and try to use combineAsVariant to show all the variants in it like handyComponents plugin.

How can I use the combineAsVariant and pass the argument as the node are the readOnly field so how can i pass the argument in it?

Any help would be appreciated.

This topic has been closed for replies.

5 replies

tank666
May 29, 2023 10:16 AM UTC

You don’t know how to add an array of ComponentNodes?

figma.combineAsVariants([ComponentNode, ComponentNode, …], parent)
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
May 29, 2023 11:02 AM UTC

yes don’t know, I have created a buttons with frame nodes and also how can I do something like

Following is my code

function createCombinedButtonComponent() {
  let variantComponents = [];
  
  // Create instances of the button variants within the frame
  const buttonInstance = figma.createComponent();
  for (const variant of buttonVariants) {
    buttonInstance.name='component'
    const variantComponent = figma.createFrame();
  
    variantComponent.resizeWithoutConstraints(75, 40); // Set the size of the button
    variantComponent.name = variant.name;
    variantComponent.fills = [{type: 'SOLID', color: variant.style.backgroundColor}]
    variantComponent.cornerRadius = 5; // Set the border radius
    variantComponent.primaryAxisAlignItems = 'CENTER';
    variantComponent.counterAxisAlignItems = 'CENTER';

    // Create the text node
    const buttonText = figma.createText();
    buttonText.characters = 'Label'; // Set the text content
    buttonText.fontSize = 16; // Set the font size
    buttonText.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
    buttonText.textAlignHorizontal = 'CENTER'; // Align the text horizontally
    buttonText.textAlignVertical = 'CENTER'; // Align the text 
    buttonText.resize(variantComponent.width, variantComponent.height);

    figma.currentPage.appendChild(variantComponent);
    variantComponent.appendChild(buttonText);
    buttonInstance.appendChild(variantComponent);
    
  }
  return buttonInstance;
}

// Get the current page in Figma
const currentPage = figma.currentPage;

// Create the combined button component with variant options on the current page
const combinedButtonComponent = createCombinedButtonComponent();
// console.log(combinedButtonComponent)
currentPage.appendChild(combinedButtonComponent);

May 29, 2023 11:04 AM UTC

What will be a parent component?

tank666
May 29, 2023 11:34 AM UTC

From the code you provided, it only creates one component, while you should have created components for each object and then use the combineAsVariants method to combine them into a component set.

I also want to note that before changing the font properties, you will need to load it first.

figma.com
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
May 29, 2023 11:47 AM UTC

Can you provide some any example code ? Also, I want to arrange the buttons and like image I given in screenshot. also how can I add the + icon to add the duplicate the button and show variant count?

The varintcombine do such thing or there is different way for it?