Swapping instances programmatically does not preserve overrides

I have a plugin that is interacting with the REST API to swap an instance’s mainComponent.

The process looks like this:

  1. Find all instances on the page.
  2. Enter file key into an input.
  3. Use REST API to get published components in the file (via the file key).
  4. For any given instance, search through the published components and if a match is found by name, replace the mainComponent property for that instance with the found component from the file.

The process works fine. However, when I assign the new component to the instance’s mainComponent field all of the overrides are lost.

BUT if I swap instances with the same exact component in the same exact file using Figma’s Swap Instance UI the overrides are preserved.

Screen Shot 2021-03-24 at 3.40.52 PM

Screen Shot 2021-03-24 at 3.40.46 PM

I’m assuming that Figma does some extra work behind the scenes to preserve overrides?

I can understand why the overrides are not being preserved when I run my plugin since I’m essentially just replacing an object property with a new one. But I’m wondering if there is something obvious I’m missing? Or if anyone has any suggestions on how to preserve overrides? Would it involve traversing through the instance and recording node settings, and then running those same checks on the new component when it’s assigned to instance.mainComponent?

That’s simply how it works, there is no explanation. Overrides preservation wasn’t implemented in the API. In order to save them you indeed need to copy/paste everything in the original instance. That’s how I do it in my plugin Master.

1 Like

@Gleb Thanks for the reply!

A question about your plugin: have you tried implementing a strategy to preserve variant options if an instance of a component set is swapped with another component set that has the same variant properties?

It’s technically complicated but I’m working on it.

1 Like