Skip to content

AI-native design editor. Open-source Figma alternative.

fix(scene-graph): scope component property cleanup

Danila Poyarkov55305b4f0223
diff --git a/packages/scene-graph/src/components/properties.ts b/packages/scene-graph/src/components/properties.ts
index 0e0b67736..807a3d887 100644
--- a/packages/scene-graph/src/components/properties.ts
+++ b/packages/scene-graph/src/components/properties.ts
@@ -140,6 +140,7 @@ export function applyComponentPropertyValue(
): SceneNode | null {
const instance = graph.getNode(instanceId)
if (instance?.type !== 'INSTANCE') return null
+ if (definition.type === 'VARIANT') return null
const targets = findComponentPropertyTargets(graph, instance, definition.id)
const target =
definition.type === 'INSTANCE_SWAP' ? resolveComponentPropertyValue(graph, value) : null
@@ -162,6 +163,43 @@ export function applyComponentPropertyValue(
return definition.type === 'INSTANCE_SWAP' ? target : instance
}
+function componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {
+ const component = graph.getNode(componentId)
+ if (!component) return []
+ const nodes = [component]
+ const visit = (node: SceneNode): void => {
+ for (const child of graph.getChildren(node.id)) {
+ nodes.push(child)
+ visit(child)
+ }
+ }
+ visit(component)
+ return nodes
+}
+
+function componentInstances(graph: SceneGraph, componentId: string): SceneNode[] {
+ return [...graph.getAllNodes()].filter(
+ (node) => node.type === 'INSTANCE' && node.componentId === componentId
+ )
+}
+
+function removePropertyFromNode(graph: SceneGraph, node: SceneNode, propertyId: string): void {
+ if (node.componentPropertyReferences.some((reference) => reference.propertyId === propertyId)) {
+ graph.updateNode(node.id, {
+ componentPropertyReferences: node.componentPropertyReferences.filter(
+ (reference) => reference.propertyId !== propertyId
+ )
+ })
+ }
+ if (node.type === 'INSTANCE' && propertyId in node.componentPropertyAssignments) {
+ graph.updateNode(node.id, {
+ componentPropertyAssignments: Object.fromEntries(
+ Object.entries(node.componentPropertyAssignments).filter(([id]) => id !== propertyId)
+ )
+ })
+ }
+}
+
export function removeComponentProperty(
graph: SceneGraph,
ownerId: string,
@@ -174,21 +212,16 @@ export function removeComponentProperty(
(definition) => definition.id !== propertyId
)
})
- for (const node of graph.getAllNodes()) {
- if (node.componentPropertyReferences.some((reference) => reference.propertyId === propertyId)) {
- graph.updateNode(node.id, {
- componentPropertyReferences: node.componentPropertyReferences.filter(
- (reference) => reference.propertyId !== propertyId
- )
- })
- }
- if (node.type === 'INSTANCE' && propertyId in node.componentPropertyAssignments) {
- graph.updateNode(node.id, {
- componentPropertyAssignments: Object.fromEntries(
- Object.entries(node.componentPropertyAssignments).filter(([id]) => id !== propertyId)
- )
- })
- }
- }
+ const nodes = [
+ ...componentSubtree(graph, owner.id),
+ ...componentInstances(graph, owner.id),
+ ...(owner.type === 'COMPONENT_SET'
+ ? owner.childIds.flatMap((id) => [
+ ...componentSubtree(graph, id),
+ ...componentInstances(graph, id)
+ ])
+ : [])
+ ]
+ for (const node of nodes) removePropertyFromNode(graph, node, propertyId)
return true
}
diff --git a/tests/engine/scene-graph/components/properties.test.ts b/tests/engine/scene-graph/components/properties.test.ts
index be57af5ce..ce9af412d 100644
--- a/tests/engine/scene-graph/components/properties.test.ts
+++ b/tests/engine/scene-graph/components/properties.test.ts
@@ -111,6 +111,23 @@ describe('component properties', () => {
expect(target?.componentId).toBe(alternate.id)
})
+ test('rejects variant assignment', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const host = component(graph, 'Host', page.id)
+ graph.updateNode(host.id, {
+ componentPropertyDefinitions: [
+ { id: 'prop:variant', name: 'State', type: 'VARIANT', defaultValue: 'Default' }
+ ]
+ })
+ const instance = graph.createInstance(host.id, page.id)
+ if (!instance) throw new Error('instance creation failed')
+
+ expect(
+ applyComponentPropertyValue(graph, instance.id, host.componentPropertyDefinitions[0], 'Hover')
+ ).toBeNull()
+ expect(graph.getNode(instance.id)?.componentPropertyAssignments).toEqual({})
+ })
test('removes definitions, references, and assignments', () => {
const graph = new SceneGraph()
const page = graph.getPages()[0]