Hi, I am in a trouble of locating component on a different page than I am currently interacting with plugin.
In a documentation I found a way to select component on a current page with figma.currentPage.selection()
, but I need to select component from page UI Kit.
Also, I managed to select only whole main component:
But I need to select only one variant from there.
tank666
September 30, 2022, 4:44pm
2
Find the required PageNode using figma.root
, find the desired ComponentSetNode using, for example, findAllWithCriteria
, get the id
of one of the children and create an instance (figma.getNodeById(id) .createInstance() ) or get the defaultVariant
and create an instance.
Is there any way on finding required PageNode by its name and not id?
Yes, of course, just add a condition.
For example:
let page = figma.root.findChild(node => node.name === 'Your page name');
Thanks, that is exactly what I need, but how can work with that variable?
For example finding ComponentSetNode by name outputs me error, that says âObject is possibly nullâ and wonât let me compile.
I managed to locate ComponentSet on desired Page, but not sure how to work with that variable now and create instance:
const componentPage = figma.root.findChild(PageNode => PageNode.name === 'UI Kit')
const buttonsComponent = componentPage?.findChild(ComponentSetNode => ComponentSetNode.name === 'Buttons')
buttonsComponent.createInstance()
When I log output of buttonsComponent into console, I get:
You just need to specify which variant you want to create an instance for. For example: buttonsComponent.defaultVariant.createInstance();
I described the logic here:
And I would write this code like this:
let page = figma.root.findChild(node => node.name === 'UI Kit'),
allComponentSets = page.findAllWithCriteria({types: ['COMPONENT_SET']}),
componentSet = allComponentSets.find(node => node.name === 'Buttons');
componentSet.defaultVariant.createInstance();
Thank you very much When I try to use your code it wonât let me compile it, until I add question mark behind every variable, because I am getting this error: Object is possibly âundefinedâ.
Is there any other way around it, than this?
let page = figma.root.findChild(node => node.name === 'UI Kit'),
allComponentSets = page?.findAllWithCriteria({types: ['COMPONENT_SET']}),
componentSet = allComponentSets?.find(node => node.name === 'Buttons')
componentSet?.defaultVariant.createInstance()
@tank666 one more question:
When I successfully create instance and would like to move it under parent frame like this mainFrame.appendChild(actionButton)
I get error:
I canât find anything in documentation about âundefinedâ InstanceNode.
This is the declaration of actionButton
:
let actionButton = locateComponent("Buttons")?.defaultVariant.createInstance()
I donât use Typescript in my work, but you can try this:
let actionButton = (locateComponent("Buttons") as ComponentSetNode).defaultVariant.createInstance()
Or:
mainFrame.appendChild(actionButton as InstanceNode)
1 Like
Both solutions work like a charm, thank you.