Template API: executeTemplate() fails on instances found via findLayers and findConnectedInstances

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

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
react, typescript
领域
tooling

调研方向

Compare how the Template API handles instances returned by findInstance, findLayers, and findConnectedInstances, focusing on why only the single result renders through executeTemplate().example. Reproduce with the reported nested component case using the React parser and Template API; done means instances found by the multi-result methods render their expected snippets. The issue does not name source files or tests, so locate the relevant implementation and test entry points first.

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

描述

API question
Description

When using the Figma Template API for nested component rendering (Code Connect), there is a significant inconsistency between discovery methods. While findInstance successfully allows for snippet generation of a child component, methods that support multiple results (findLayers, findConnectedInstances) fail to render the underlying templates.

Environment
  • @figma/code-connect CLI: v1.4.4

  • Parser: React

  • Template Format: Template API (.figma.ts with id, imports, example)


Test Case & Findings

Goal: Dynamically find and render all child components (Buttons, Selects, Inputs) within a parent component (Modal) using the Template API.

1. findInstance(layerName) — Working (Limited)
  • Result: Success, but limited to a single instance.

  • Behavior: Correctly finds the layer and executeTemplate().example returns the full, expected code snippet.

  • Limitation: It only returns the first match. If a Modal has two Buttons, the second one is inaccessible.

2. findLayers(selectorFn) — Broken Rendering
  • Result: Finds layers, but cannot render.

  • Behavior: It correctly identifies the number of children (e.g., layers.length = 6). However, calling executeTemplate().example on these handles returns empty/null.

  • Suspected Bug: The handles returned by findLayers seem to be "unresolved" compared to those from findInstance.

3. findConnectedInstances() — Fails to Locate
  • Result: Failed (0 results found).

  • Behavior: Despite documentation suggesting this for nested compositions, it returns an empty array even when traverseInstances: true is set and children have valid Code Connect files.

4. getInstanceSwap(propName) — Partial Failure
  • Result: Returns reference only.

  • Behavior: It finds the swap instance but fails to render the internal structure of the swapped component, returning only a placeholder name or layer name instead of the code snippet.


Comparison Table

Method: findInstance | Discovery: ✅ | executeTemplate Works?: ✅ | Multi-instance Support?: ❌ (Single only)

Method: findLayers | Discovery: ✅ | executeTemplate Works?: ❌ (Empty result) | Multi-instance Support?: ✅

Method: findConnectedInstances | Discovery: ❌ | executeTemplate Works?: — | Multi-instance Support?: ✅

Method: getInstanceSwap | Discovery: ✅ | executeTemplate Works?:⚠️ (Name only) | Multi-instance Support?:❌


Impact

This issue makes dynamic nested composition impossible. Currently, we cannot dynamically loop through and render a variable number of children (e.g., form elements in a Modal body or multiple buttons in a footer) because the methods that find multiple children do not support rendering.

The only "workaround" is manually calling findInstance for every known layer name, which is not scalable and fails when multiple instances share the same name.

Steps to Reproduce
  1. Create a parent component using the Template API format.

  2. Create multiple child components also using the Template API format (including id and metadata: { nestable: true }).

  3. Attempt to use findLayers to map through children and call executeTemplate().example on each.

  4. Observe that the returned value is empty.

Expected Behavior

Any component handle returned by findLayers or findConnectedInstances should behave identically to the handle returned by findInstance, allowing executeTemplate() to produce the correct code snippet.


Proposed Fix/Action
  • Investigate why handles from findLayers lose their template context.

  • Ensure findConnectedInstances correctly identifies children using the Template API format.

主要语言
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 摘要。