AI-native design editor. Open-source Figma alternative.
feat(figma-api): add swapComponent() to instances (#780)
mrhard90909696e5d59ccb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0327d60d8..5d133b990 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,7 @@### Added+- Swap the component behind an instance with `instance.swapComponent(component)` in the plugin API, as in Figma.- Detach an instance from its component with `detachInstance()` in the plugin API, as in Figma, from scripts run through `eval`.- Run scripts written for Figma's dynamic-page mode that call `figma.getNodeByIdAsync()` or `getMainComponentAsync()`; both resolve to the same nodes as their synchronous forms.- Export components to Storybook with `openpencil export -f storybook`: one CSF3 story file per component set or component for React, Vue, or HTML, with a story and `select` controls per variant, a design image per variant, and an `openpencil://` link that opens the variant in OpenPencil. `--watch` re-exports on every save and removes stories of deleted components (#727).diff --git a/packages/core/src/figma-api/compatibility.ts b/packages/core/src/figma-api/compatibility.tsindex e4f639b78..c76d3630b 100644--- a/packages/core/src/figma-api/compatibility.ts+++ b/packages/core/src/figma-api/compatibility.ts@@ -72,6 +72,7 @@ type InstancePropertySurfaceMatch = Expect<| 'exposedInstances'| 'getMainComponentAsync'| 'detachInstance'+ | 'swapComponent'>,Pick<InstanceNode,@@ -82,6 +83,7 @@ type InstancePropertySurfaceMatch = Expect<| 'exposedInstances'| 'getMainComponentAsync'| 'detachInstance'+ | 'swapComponent'>>>diff --git a/packages/core/src/figma-api/proxy.ts b/packages/core/src/figma-api/proxy.tsindex e09f6d752..55208cd44 100644--- a/packages/core/src/figma-api/proxy.ts+++ b/packages/core/src/figma-api/proxy.ts@@ -253,6 +253,16 @@ export class FigmaNodeProxy {return this[INTERNAL_API].wrapNode(n.id)}+ /** Points this instance at another component, as Figma's swapComponent does. */+ swapComponent(component: FigmaNodeProxy): void {+ const n = this._raw()+ if (n.type !== 'INSTANCE') throw new Error('swapComponent() can only be called on instances')+ const target = this[INTERNAL_GRAPH].getNode(component[INTERNAL_ID])+ if (target?.type !== 'COMPONENT') throw new Error('swapComponent() needs a component')+ assertNodeEditable(this[INTERNAL_GRAPH], this[INTERNAL_ID])+ this[INTERNAL_GRAPH].swapInstanceComponent(n.id, target.id)+ }+// --- Tree ---get parent(): FigmaNodeProxy | null {diff --git a/packages/docs/programmable/cli/scripting.md b/packages/docs/programmable/cli/scripting.mdindex 564f4e037..e93887185 100644--- a/packages/docs/programmable/cli/scripting.md+++ b/packages/docs/programmable/cli/scripting.md@@ -164,6 +164,7 @@ Common node properties are readable/writable through the proxy, including:- `figma.listAvailableFontsAsync()` returns host-provided fonts when available- `figma.getNodeByIdAsync(id)` and `instance.getMainComponentAsync()` resolve to the same nodes as `figma.getNodeById(id)` and `instance.mainComponent`, for scripts written for Figma's dynamic-page mode- `figma.notify(message)` logs a warning in headless mode+- `instance.swapComponent(component)` points an instance at another component- `figma.viewport`## Not yet Figma-compatiblediff --git a/tests/engine/editor/components/library-capabilities.test.ts b/tests/engine/editor/components/library-capabilities.test.tsindex e51ab5ad7..60ebfd90f 100644--- a/tests/engine/editor/components/library-capabilities.test.ts+++ b/tests/engine/editor/components/library-capabilities.test.ts@@ -75,4 +75,28 @@ describe('library definition capabilities', () => {expect(() => nestedProxy.detachInstance()).toThrow(ReadOnlyLibraryDefinitionError)expect(graph.getNode(nested.id)?.type).toBe('INSTANCE')})++ test('does not swap an instance inside a read-only definition', () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ const icon = graph.createNode('COMPONENT', page.id, { name: 'Icon' })+ const otherIcon = graph.createNode('COMPONENT', page.id, { name: 'Other icon' })+ const component = graph.createNode('COMPONENT', page.id, {+ name: 'Remote button',+ librarySource: {+ identity: { libraryId: 'design-system', assetKey: 'button', revisionId: 'r1' },+ sourceNodeId: 'source',+ readOnly: true+ }+ })+ const nested = graph.createInstance(icon.id, component.id)+ if (!nested) throw new Error('Expected instance')++ const figma = new FigmaAPI(graph)+ const nestedProxy = figma.getNodeById(nested.id)+ const otherIconProxy = figma.getNodeById(otherIcon.id)+ if (!nestedProxy || !otherIconProxy) throw new Error('Expected proxies')+ expect(() => nestedProxy.swapComponent(otherIconProxy)).toThrow(ReadOnlyLibraryDefinitionError)+ expect(graph.getNode(nested.id)?.componentId).toBe(icon.id)+ })})diff --git a/tests/engine/figma/api/components.test.ts b/tests/engine/figma/api/components.test.tsindex 4a9f8970f..cb879fbd5 100644--- a/tests/engine/figma/api/components.test.ts+++ b/tests/engine/figma/api/components.test.ts@@ -97,4 +97,35 @@ describe('components', () => {expect((await instance.getMainComponentAsync())?.id).toBe(component.id)expect(await component.getMainComponentAsync()).toBeNull()})++ test('swapComponent points an instance at another component', () => {+ const api = createAPI()+ const primary = api.createComponent()+ primary.name = 'Primary'+ primary.appendChild(Object.assign(api.createText(), { name: 'Label', characters: 'Primary' }))+ const secondary = api.createComponent()+ secondary.name = 'Secondary'+ secondary.appendChild(+ Object.assign(api.createText(), { name: 'Icon', characters: 'Secondary' })+ )+ const instance = primary.createInstance()++ instance.swapComponent(secondary)++ expect(expectDefined(instance.mainComponent, 'swapped main component').id).toBe(secondary.id)+ expect(instance.name).toBe('Secondary')+ expect(instance.children.map((child) => child.name)).toEqual(['Icon'])+ })++ test('swapComponent rejects non-instances and non-components', () => {+ const api = createAPI()+ const component = api.createComponent()+ const instance = component.createInstance()+ expect(() => api.createFrame().swapComponent(component)).toThrow(+ 'swapComponent() can only be called on instances'+ )+ expect(() => instance.swapComponent(api.createFrame())).toThrow(+ 'swapComponent() needs a component'+ )+ })})