[Swift] Support nested/compound property mappings (parity with React)

未关闭
#376 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
swift

调研方向

Start with the SwiftUI Code Connect documentation and the existing @FigmaInstance, @FigmaBoolean, and @FigmaEnum APIs; the issue provides no source files or tests to target. Compare their behavior with the React nested property helpers and determine how Swift should represent optional instances and compound mappings. Done means Swift can express both use cases and produces valid snippets; the issue leaves the API design open.

由索引模型根据 Issue 内容生成。

描述

feature request

Summary

React Code Connect supports nesting property helpers inside each other — figma.boolean wrapping figma.instance, figma.enum wrapping figma.enum, etc. This enables conditional rendering and compound property mappings. Swift Code Connect has no equivalent for any of these patterns, making it impossible to express common real-world component APIs.

Use Case 1: Conditionally omitting an optional instance

In React, you can conditionally include or omit an instance property (docs):

props: {
  icon: figma.boolean("Has Icon", {
    true: figma.instance("Icon"),
    false: undefined, // omitted from snippet
  }),
}

In Swift, @FigmaInstance with an optional type renders an empty value when no instance is selected, producing invalid code:

struct MyButton_CodeConnect: FigmaConnect {
  @FigmaInstance("Icon") var icon: Icon?

  var body: some View {
    MyButton("Submit", icon: icon) { }
  }
}

When no icon is selected:

MyButton("Submit", icon: ) { }
//                  ^^^^^ empty — not valid code

Expected: The parameter should either be omitted entirely (MyButton("Submit") { }) or render as nil.

This was discussed for React in #71 and #86, where figma.boolean wrapping was the solution.

Use Case 2: Combining multiple Figma properties into one code value

In React, you can nest figma.enum inside figma.enum to map combinations of Figma properties to a single code value (docs):

props: {
  type: figma.enum("Type", {
    "Secondary": figma.enum("Theme", {
      "Light": "secondary",
      "Dark": "secondary-dark",
    }),
    "Tertiary": figma.enum("Theme", {
      "Light": "tertiary",
      "Dark": "tertiary-dark",
    }),
  }),
}

In Swift, @FigmaEnum mappings are flat — each maps a single Figma property independently. There's no way to express "when Type=Secondary AND Theme=Dark, use .secondary(variation: .dark)":

// Forced to ignore Theme entirely:
@FigmaEnum("Type", mapping: [
  "Secondary": CookbookButton.Style.secondary(),
  "Tertiary": CookbookButton.Style.tertiary(),
])
var type: CookbookButton.Style = .primary()

Attempted Workaround: Variant-specific structs

The docs describe variant for mapping one Figma component to different code components (e.g. PrimaryButton vs DangerButton). We attempted using multiple FigmaConnect structs with the same component type but different variant restrictions to work around both use cases above.

This does not work — Figma only ever displays one of the snippets, regardless of which variant is active. The variant property appears to only function when each struct declares a different component type, as shown in the docs.

Proposal

Support nested/compound property mappings in Swift, bringing parity with React's composable helpers.

For optional instances (Use Case 1)

Option A: Automatically omit the parameter when a @FigmaInstance with an optional type resolves to nothing — consistent with how falsy props are omitted in React.

Option B: Add hideDefault to @FigmaInstance, consistent with the existing @FigmaEnum API:

@FigmaInstance("Icon", hideDefault: true) var icon: Icon?

Option C: Allow @FigmaBoolean to wrap @FigmaInstance:

@FigmaBoolean("Has Icon", mapping: [
  true: FigmaInstance("Icon"),
  false: nil,
])
var icon: Icon?
For compound property mappings (Use Case 2)

Allow nesting @FigmaEnum (or similar) inside @FigmaEnum mapping values:

@FigmaEnum("Type", mapping: [
  "Secondary": FigmaEnum("Theme", mapping: [
    "Light": CookbookButton.Style.secondary(),
    "Dark": CookbookButton.Style.secondary(variation: .dark),
  ]),
  "Tertiary": FigmaEnum("Theme", mapping: [
    "Light": CookbookButton.Style.tertiary(),
    "Dark": CookbookButton.Style.tertiary(variation: .dark),
  ]),
])
var type: CookbookButton.Style = .primary()

Environment

  • Code Connect version: 1.4.1
  • Platform: Swift (SwiftUI)
主要语言
TypeScript
星标
1.6k
派生
126
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

figma/code-connect 的其他 Issue

查看 figma/code-connect 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。