Efficient component search

What’s the most efficient way to search for a component? I want to first check the page, then if not found there check the file, and then if not there check if it exists in a team library.

I’ve tried to use ChatGPT to help me, but the code it’s given me really slows down the plugin (and currently it’s only searching page and file - if I add team library as well, it’s gonna be painful!).

Feels like this would be a common scenario for plugins, so wondering if anyone has any efficient or best practice code they’d be willing to share?

Apologies if I’ve missed an obvious snippet from this site.

Thanks in advance!

This might help: Figma layers tree traversal + estimating size

If you want to find one specific component: https://www.figma.com/plugin-docs/api/properties/nodes-findone/

Or if you want to find multiple components, using this function might be helpful: findAllWithCriteria | Plugin API

Also note that searching or accessing the team library isn’t possible through the plugins API. You can only save component keys while running the plugin in the library file and then import them in the file where you are working in. Or you can use REST API (by asking the user to authenticate, requires a server and websockets).

1 Like

Thanks @Gleb - this is really useful.

I was initially confused as to why you couldn’t search or access team libraries, but then it hit me that it makes sense!