Skip to main content
Question

How to work with imported component


Tomas_Bana

Hello,
I need help with my code. I am importing components from the team library. When I call the import function, I get back a JS promise. I don’t know how to work with this promise. I need to change the text and variants of components in this imported component.

My code:

async function importNode() {

let importComponent = await Figma.importComponentByKeyAsync(“5cad1f5ace96bf648”);

const favC = importComponent.findOne(node => node.type == “COMPONENT” && node.name == “Favorite”) as ComponentNode;

return favC;
}

How can I access the imported component and browse it and use the embedded components?

Thanks

This topic has been closed for comments

9 replies

tank666
  • 4868 replies
  • January 24, 2024

Tomas_Bana
  • Author
  • 7 replies
  • January 29, 2024

Sorry for my dumb questions, but i don’t know what you mean “variable already stores a ComponentNode”. Maybe I asked the wrong question.

Now I have an instance, but when I try set the property I get a error

let importComponent = await Figma.importComponentByKeyAsync(“5cad03bxxxxx”);

importComponent.createInstance()

importComponent.setProperties({‘Layout’: ‘Mobile’});

Error: Property ‘setProperties’ does not exist on type ‘ComponentNode’.

I don’t need to change only properties. My component includes a more component and Ii need access to all children’s components. And don’t know how 😦

How i get access to the component (name = favorite) which is inside the “ImportComponent” ??


tank666
  • 4868 replies
  • January 29, 2024
figma.com

Find the desired node in the children’s tree.


Tomas_Bana
  • Author
  • 7 replies
  • February 12, 2024

Thanks for your advice. I use the function children, and get back the children instance component. But when i trying to change property with .setProperties. But I’m getting an error does not exist on type ‘SceneNode’.

We are a bit confused, I don’t know how to change the text or property for embedded component instances {label} or instance favorite. Attached is an example of the structure of a component

Thanks


tank666
  • 4868 replies
  • February 12, 2024

On which node was the setProperties method called?

Call the setProperties method on the “Participant” instance or change the characters property value of the “{Label}” text node.

// Set a value for a component's text property
pacticipantInstance.setProperties({'textPropertyName#id': 'Something'});

// Or change the value of the characters property
label.characters = 'Something';

Call the above method on the “Favourite” instance.


Tomas_Bana
  • Author
  • 7 replies
  • February 13, 2024

On which node was the setProperties method called?

this is my code:

let importComponent = await figma.importComponentByKeyAsync("5cad03b3xxx");
importComponent.createInstance()
importComponent.children[0].setProperties({'State': 'Like'})

tank666
  • 4868 replies
  • February 13, 2024

I don’t know why you use this way, but I would do it like this:

let importComponent = await figma.importComponentByKeyAsync(key);
let instance = importComponent.createInstance();
instance.children[0].setProperties(obj);

Also make sure that children[0] is an instance.

And where do you see the error: in the Figma app console or in the IDE?


Tomas_Bana
  • Author
  • 7 replies
  • February 13, 2024

My mistake, I see the error in my IDE. But when I run the plugin in Figma it works.

Thanks.

I have a last thing. When I am using setProperities and choosing from options everything is good. But I don’t no how a set value for properties (set text).

My code:

instance.children[1].children[0].setProperties({‘Time’: ‘11:00’});

I want a set text for properties Time, but my code doesn’t work.

Thx.


tank666
  • 4868 replies
  • February 13, 2024

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