You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Figma Code Connect图标映射异常:仅默认图标显示字符串值

Figma Code Connect 图标映射字符串值异常问题

我用Figma Code Connect把图标组件映射到React组件的字符串值,IconButton组件需要接收字符串类型的iconName属性,但只有默认图标(PlaceholderWireframe)能正确显示字符串值,其他图标都显示资产引用,而非预期的字符串。

Figma组件属性

  • Size="Large"
  • State="Pressed"
  • Type="Square Container"
  • Icon="PlaceholderWireframe"(实例替换属性)

IconButton组件代码

//icon for placeholder
figma.connect("https://www.figma.com/design/url-to-placeholderwireframe", {
  example: () => "Wireframe" //originally PlaceholderWireframe which changes to Wireframe
})

//icon for car
figma.connect("https://www.figma.com/design/url-to-car", {
  example: () => "car" // still shows asset reference instead
})

const sharedProps = {
  iconName: figma.instance<string>('Icon'),
  ariaLabel: 'aria text',
  size: figma.enum('Size', {
    Small: 'small',
    Medium: 'medium',
    Large: 'large',
  }),
  square: figma.enum('Type', {
    'Circle Container': false,
    'Square Container': true,
  }),
  disabled: figma.enum('State', {
    Disabled: true,
    Default: false,
  }),
};

Figma JSON结构

[
  {
    "id": "69520:27135",
    "name": "Size=Medium, State=Default, Type=Circle Container",
    "type": "COMPONENT",
    "visible": true,
    "locked": false,
    "children": [
      {
        "id": "69520:27136",
        "name": "PlaceholderWireframe",
        "type": "INSTANCE",
        "visible": true,
        "locked": false,
        "children": [
          {
            "id": "I69520:27136;1772:1165",
            "name": "Fill",
            "type": "VECTOR",
            "visible": true,
            "locked": false
          }
        ]
      }
    ]
  }
]

解决步骤

  1. 替换figma.connect的目标为组件节点ID
    把当前使用的设计文件URL换成对应图标组件的具体节点ID(比如JSON里的1772:1165),Code Connect需要精准关联到单个图标组件实例,而非整个设计文件。

  2. 调整iconName的读取逻辑
    放弃依赖figma.instance<string>('Icon')读取实例名称,改为给每个图标组件添加自定义属性(比如iconValue),然后用figma.string('iconValue')来获取指定的字符串值,这样所有替换的图标都能返回预期的字符串。

  3. 校验关联配置
    在Figma的Code Connect面板中,确认每个图标组件都成功关联到对应的映射配置,没有关联失败的提示。

  4. 统一example返回值与自定义属性匹配
    确保figma.connect里的example返回值和图标组件的自定义属性值一致,避免映射逻辑冲突。

内容的提问来源于stack exchange,提问作者parth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:13:08