Skip to main content
Solved

How to create an INSTANCE_SWAP prop on a component?

  • July 27, 2023
  • 1 reply
  • 422 views

RisingTide

Hi,

I am trying to add an INSTANCE_SWAP property to a component and I’ve tried with both InstanceNode and ComponentSetNode and I get the same error, listed below.

I am passing the ID of the an InstanceNode that I appended to a frame inside the component to which I attach the property.

What kind of node does it need to be?
Can/must the node be a child of the component?

InstanceNode

Many thanks!

Best answer by RisingTide

I figured it out. The INSTANCE_SWAP property required the id of a ComponentNode or a ComponentSetNode of the instance you are attaching it to:

const newComponent = figma.createComponent();
const instance = componentSetNode.defaultVariant.createInstance();

newComponent.appendChild(instance);
instance.componentPropertyReferences = {
  mainComponent: newComponent.addComponentProperty(
    "PropertyName",
    "INSTANCE_SWAP",
    componentSetNode.defaultVariant.id
  ),
};

// if you start from an instance and do not have access to its original ComponentNode:
newComponent.appendChild(instance);
instance.componentPropertyReferences = {
  mainComponent: newComponent.addComponentProperty(
    "PropertyName",
    "INSTANCE_SWAP",
    instance.mainComponent.id
  ),
};
View original
This topic has been closed for comments

1 reply

RisingTide
  • Author
  • 1 reply
  • Answer
  • July 27, 2023

I figured it out. The INSTANCE_SWAP property required the id of a ComponentNode or a ComponentSetNode of the instance you are attaching it to:

const newComponent = figma.createComponent();
const instance = componentSetNode.defaultVariant.createInstance();

newComponent.appendChild(instance);
instance.componentPropertyReferences = {
  mainComponent: newComponent.addComponentProperty(
    "PropertyName",
    "INSTANCE_SWAP",
    componentSetNode.defaultVariant.id
  ),
};

// if you start from an instance and do not have access to its original ComponentNode:
newComponent.appendChild(instance);
instance.componentPropertyReferences = {
  mainComponent: newComponent.addComponentProperty(
    "PropertyName",
    "INSTANCE_SWAP",
    instance.mainComponent.id
  ),
};

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