Skip to content

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

refactor(fig): split component prop assignments

Danila Poyarkov7992f8c0136c
diff --git a/packages/core/src/kiwi/instance-overrides/component-props/assignments.ts b/packages/core/src/kiwi/instance-overrides/component-props/assignments.ts
new file mode 100644
index 000000000..91147a9d1
--- /dev/null
+++ b/packages/core/src/kiwi/instance-overrides/component-props/assignments.ts
@@ -0,0 +1,105 @@
+import { applyComponentPropRef } from '#core/kiwi/instance-overrides/component-props/apply'
+import { fallbackRefsForChild, findPropRefs, valueForRef } from '#core/kiwi/instance-overrides/component-props/refs'
+import { assignmentsToValueMap } from '#core/kiwi/instance-overrides/component-props/values'
+import { resolveOverrideTarget } from '#core/kiwi/instance-overrides/resolve'
+import type {
+ ComponentPropAssignment,
+ ComponentPropRef,
+ ComponentPropValue,
+ OverrideContext
+} from '#core/kiwi/instance-overrides/types'
+
+function applyChildPropRefs(
+ ctx: OverrideContext,
+ childId: string,
+ refs: ComponentPropRef[] | undefined,
+ valueByDef: Map<string, ComponentPropValue>,
+ modified?: Set<string>
+): void {
+ if (!refs) return
+ for (const ref of refs) {
+ const val = valueForRef(ref, valueByDef)
+ if (val) applyComponentPropRef(ctx, childId, ref, val, modified)
+ }
+}
+
+function applyPropAssignments(
+ ctx: OverrideContext,
+ parentId: string,
+ valueByDef: Map<string, ComponentPropValue>,
+ propRefsMap: Map<string, ComponentPropRef[]>,
+ modified?: Set<string>
+): void {
+ const parent = ctx.graph.getNode(parentId)
+ if (!parent) return
+
+ for (const childId of parent.childIds) {
+ const child = ctx.graph.getNode(childId)
+ if (!child?.componentId) {
+ applyPropAssignments(ctx, childId, valueByDef, propRefsMap, modified)
+ continue
+ }
+
+ const refs =
+ findPropRefs(ctx, child.componentId, propRefsMap) ??
+ fallbackRefsForChild(ctx, child.name, valueByDef)
+ applyChildPropRefs(ctx, childId, refs, valueByDef, modified)
+ applyPropAssignments(ctx, childId, valueByDef, propRefsMap, modified)
+ }
+}
+
+export function applyInstanceDirectAssignments(
+ ctx: OverrideContext,
+ assignmentSources: Map<string, ComponentPropAssignment[]>,
+ propRefsMap: Map<string, ComponentPropRef[]>,
+ modified: Set<string>
+): void {
+ for (const node of ctx.graph.getAllNodes()) {
+ if (ctx.activeNodeIds && !ctx.activeNodeIds.has(node.id)) continue
+ if (node.type !== 'INSTANCE') continue
+ const ownFigmaId = ctx.nodeIdToGuid.get(node.id)
+ if (!ownFigmaId) continue
+ const ownAssignments = assignmentSources.get(ownFigmaId)
+ if (ownAssignments) {
+ applyPropAssignments(
+ ctx,
+ node.id,
+ assignmentsToValueMap(ctx, ownAssignments),
+ propRefsMap,
+ modified
+ )
+ }
+ }
+}
+
+export function applyOverrideAssignments(
+ ctx: OverrideContext,
+ propRefsMap: Map<string, ComponentPropRef[]>,
+ modified: Set<string>
+): void {
+ for (const [figmaId, nc] of ctx.changeMap) {
+ const instanceNodeId = ctx.guidToNodeId.get(figmaId)
+ if (!instanceNodeId || (ctx.activeNodeIds && !ctx.activeNodeIds.has(instanceNodeId))) continue
+ if (ctx.graph.getNode(instanceNodeId)?.type !== 'INSTANCE') continue
+
+ const overrides = nc.symbolData?.symbolOverrides
+ if (!overrides) continue
+ for (const ov of overrides) {
+ if (!ov.componentPropAssignments?.length) continue
+
+ const guids = ov.guidPath?.guids
+ if (!guids?.length) continue
+
+ const targetId = resolveOverrideTarget(ctx, instanceNodeId, guids)
+ if (!targetId) continue
+
+ applyPropAssignments(
+ ctx,
+ targetId,
+ assignmentsToValueMap(ctx, ov.componentPropAssignments, true),
+ propRefsMap,
+ modified
+ )
+ }
+ }
+}
diff --git a/packages/core/src/kiwi/instance-overrides/props.ts b/packages/core/src/kiwi/instance-overrides/props.ts
index 078b8eee4..191aaa7b5 100644
--- a/packages/core/src/kiwi/instance-overrides/props.ts
+++ b/packages/core/src/kiwi/instance-overrides/props.ts
@@ -1,121 +1,9 @@
-import { applyComponentPropRef } from './component-props/apply'
+import {
+ applyInstanceDirectAssignments,
+ applyOverrideAssignments
+} from './component-props/assignments'
import { collectAssignmentsMap, collectPropRefsMap } from './component-props/maps'
-import { fallbackRefsForChild, findPropRefs, valueForRef } from './component-props/refs'
-import { assignmentsToValueMap } from './component-props/values'
-import { resolveOverrideTarget } from './resolve'
-import type {
- OverrideContext,
- ComponentPropAssignment,
- ComponentPropRef,
- ComponentPropValue
-} from './types'
-
-function applyChildPropRefs(
- ctx: OverrideContext,
- childId: string,
- refs: ComponentPropRef[] | undefined,
- valueByDef: Map<string, ComponentPropValue>,
- modified?: Set<string>
-): void {
- if (!refs) return
- for (const ref of refs) {
- const val = valueForRef(ref, valueByDef)
- if (val) applyComponentPropRef(ctx, childId, ref, val, modified)
- }
-}
-
-function applyPropAssignments(
- ctx: OverrideContext,
- parentId: string,
- valueByDef: Map<string, ComponentPropValue>,
- propRefsMap: Map<string, ComponentPropRef[]>,
- modified?: Set<string>
-): void {
- const parent = ctx.graph.getNode(parentId)
- if (!parent) return
-
- for (const childId of parent.childIds) {
- const child = ctx.graph.getNode(childId)
- if (!child?.componentId) {
- applyPropAssignments(ctx, childId, valueByDef, propRefsMap, modified)
- continue
- }
-
- const refs =
- findPropRefs(ctx, child.componentId, propRefsMap) ??
- fallbackRefsForChild(ctx, child.name, valueByDef)
- applyChildPropRefs(ctx, childId, refs, valueByDef, modified)
- applyPropAssignments(ctx, childId, valueByDef, propRefsMap, modified)
- }
-}
-
-/**
- * Apply component property assignments from each instance's own kiwi data.
- *
- * Only processes nodes with their own kiwi NC — cloned instances inherit
- * correct values from their source via transitive sync.
- */
-function applyInstanceDirectAssignments(
- ctx: OverrideContext,
- assignmentSources: Map<string, ComponentPropAssignment[]>,
- propRefsMap: Map<string, ComponentPropRef[]>,
- modified: Set<string>
-): void {
- for (const node of ctx.graph.getAllNodes()) {
- if (ctx.activeNodeIds && !ctx.activeNodeIds.has(node.id)) continue
- if (node.type !== 'INSTANCE') continue
- const ownFigmaId = ctx.nodeIdToGuid.get(node.id)
- if (!ownFigmaId) continue
- const ownAssignments = assignmentSources.get(ownFigmaId)
- if (ownAssignments) {
- applyPropAssignments(
- ctx,
- node.id,
- assignmentsToValueMap(ctx, ownAssignments),
- propRefsMap,
- modified
- )
- }
- }
-}
-
-/**
- * Apply component property assignments from symbolOverrides.
- *
- * These target nested instances via guidPath and may reset values to
- * component defaults (empty kiwi value `{}`).
- */
-function applyOverrideAssignments(
- ctx: OverrideContext,
- propRefsMap: Map<string, ComponentPropRef[]>,
- modified: Set<string>
-): void {
- for (const [figmaId, nc] of ctx.changeMap) {
- const instanceNodeId = ctx.guidToNodeId.get(figmaId)
- if (!instanceNodeId || (ctx.activeNodeIds && !ctx.activeNodeIds.has(instanceNodeId))) continue
- if (ctx.graph.getNode(instanceNodeId)?.type !== 'INSTANCE') continue
-
- const overrides = nc.symbolData?.symbolOverrides
- if (!overrides) continue
- for (const ov of overrides) {
- if (!ov.componentPropAssignments?.length) continue
-
- const guids = ov.guidPath?.guids
- if (!guids?.length) continue
-
- const targetId = resolveOverrideTarget(ctx, instanceNodeId, guids)
- if (!targetId) continue
-
- applyPropAssignments(
- ctx,
- targetId,
- assignmentsToValueMap(ctx, ov.componentPropAssignments, true),
- propRefsMap,
- modified
- )
- }
- }
-}
+import type { OverrideContext } from './types'
/**
* Apply all component property assignments (visibility toggles, instance swaps).