Skip to main content
Question

How to create duplicate instance of a selected portion from figma canvas in plugin development?

  • September 26, 2022
  • 2 replies
  • 669 views

Prerana_Das
  //in first one I getting compNode from component. it's working fine. I'm able to create duplicate instance.
  const compNode = figma.root.findOne(node => node.type == "COMPONENT_SET" && node.name == "post");
  console.log(compNode,'compNode');
  const postComponentSet = compNode as ComponentSetNode;
  const defaultVarient= postComponentSet.defaultVariant as ComponentNode; 
  defaultVarient.createInstance();

//but 2nd option I getting selectedNode by selecting something on figma canvas
  const selectedNode = figma.currentPage.selection[0];
  console.log(selectedNode,'selected');
  const postComponentSet2=selectedNode as ComponentSetNode;
  const newNode = postComponentSet2.defaultVariant as ComponentNode;
  newNode.createInstance();

//log of these two


Here the first case we got a ComponentSetNode and 2nd time we got InstanceNode
So my question is how can I convert the 2nd InstanceNode to ComponentSetNode?

Or is there any other way to createInstance of a selected portion/duplicate?

2 replies

tank666
  • 4873 replies
  • September 26, 2022

You cannot create an InstanceNode from a ComponentSetNode, so use the clone() method. In addition, you don’t need to create an instance.

It’s not entirely clear if you want the InstanceNode to be nested within a variant in the component set, or do you want to detach the instance and create the variant?

If the first, then create a new ComponentNode, insert the selected instance into it, and then combine as variants.
If the second, then create a new ComponentNode, detach instance, insert the selected frame into the component, and then combine as variants.


Prerana_Das
  • Author
  • 9 replies
  • September 28, 2022

I see. Thanks


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings