From 094f20749b3d1778ce608fd76b3cd11202c4bb69 Mon Sep 17 00:00:00 2001 From: Jane Chu <7559015+janechu@users.noreply.github.com> Date: Wed, 3 Jun 2026 10:47:16 -0700 Subject: [PATCH] feat: support async declarative template strings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...-78e81f3e-f6be-4ebe-be05-ec6b62829ace.json | 7 + packages/fast-element/DESIGN.md | 12 +- packages/fast-element/README.md | 34 +++ packages/fast-element/SIZES.md | 2 +- .../fast-element/docs/declarative-html.md | 45 +++- .../docs/declarative/api-report.api.md | 20 ++ .../fast-element/src/declarative/debug.ts | 4 + .../fast-element/src/declarative/index.ts | 8 +- .../src/declarative/interfaces.ts | 2 + .../declarative/template-bridge.pw.spec.ts | 64 +++++ .../fast-element/src/declarative/template.ts | 237 +++++++++++++++++- .../declarative/fixtures/ecosystem/README.md | 1 + .../async-template-string.spec.ts | 57 +++++ .../async-template-string/entry.html | 14 ++ .../fast-build.config.json | 6 + .../async-template-string/index.html | 13 + .../ecosystem/async-template-string/main.ts | 60 +++++ .../async-template-string/state.json | 4 + .../async-template-string/template-string.ts | 12 + .../async-template-string/templates.html | 0 .../fast-element.declarativetemplate_1.md | 64 +++++ ...ast-element.declarativetemplatecallback.md | 27 ++ ...ativetemplatecallbackcontext.definition.md | 24 ++ ...ment.declarativetemplatecallbackcontext.md | 91 +++++++ ...ecallbackcontext.templatestringresolver.md | 24 ++ ...ent.declarativetemplateoptions.callback.md | 24 ++ ...fast-element.declarativetemplateoptions.md | 71 ++++++ ...ement.declarativetemplatestringresolver.md | 26 ++ .../fast-element/declarative/fast-element.md | 59 +++++ .../defining-elements.md | 39 ++- .../src/docs/3.x/resources/export-sizes.md | 2 +- 31 files changed, 1037 insertions(+), 16 deletions(-) create mode 100644 change/@microsoft-fast-element-78e81f3e-f6be-4ebe-be05-ec6b62829ace.json create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/async-template-string.spec.ts create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/entry.html create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/fast-build.config.json create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/index.html create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/main.ts create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/state.json create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/template-string.ts create mode 100644 packages/fast-element/test/declarative/fixtures/ecosystem/async-template-string/templates.html create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplate_1.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplatecallback.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplatecallbackcontext.definition.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplatecallbackcontext.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplatecallbackcontext.templatestringresolver.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplateoptions.callback.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplateoptions.md create mode 100644 sites/website/src/docs/3.x/api/fast-element/declarative/fast-element.declarativetemplatestringresolver.md diff --git a/change/@microsoft-fast-element-78e81f3e-f6be-4ebe-be05-ec6b62829ace.json b/change/@microsoft-fast-element-78e81f3e-f6be-4ebe-be05-ec6b62829ace.json new file mode 100644 index 00000000000..535d3c70ac7 --- /dev/null +++ b/change/@microsoft-fast-element-78e81f3e-f6be-4ebe-be05-ec6b62829ace.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "feat: allow declarativeTemplate callbacks to resolve templates from f-template strings", + "packageName": "@microsoft/fast-element", + "email": "7559015+janechu@users.noreply.github.com", + "dependentChangeType": "none" +} diff --git a/packages/fast-element/DESIGN.md b/packages/fast-element/DESIGN.md index 752c5f110e5..d23cb16483d 100644 --- a/packages/fast-element/DESIGN.md +++ b/packages/fast-element/DESIGN.md @@ -351,6 +351,16 @@ the imperative `html` API: - The internal `` publisher parses HTML and returns concrete `ViewTemplate` instances through the registry-aware declarative template bridge. +- `declarativeTemplate({ callback })` registers a transient string publisher for + the definition's name. The callback receives the element definition and a + `templateStringResolver()` function, so it can load template markup with + async flows such as `import()` or `fetch()`. The resolver accepts either a + string or `Promise`, validates that the resolved string contains + exactly one ``, preserves attributes on that element, and then + routes it through the same publisher path as a connected ``. The + callback must return or await the resolver promise; if it completes + successfully without calling `templateStringResolver()`, template resolution + rejects with a diagnostic error instead of waiting indefinitely. - `TemplateParser` lowers declarative syntax to the same `strings` / `values` shape used by `ViewTemplate.create()`. - `attributeMap()` and `observerMap()` are `FASTElementExtension` factories @@ -577,7 +587,7 @@ src/ │ └── di.ts # DI container, decorators, resolvers, Registration ├── context.ts # Context, FASTContext, Context protocol ├── declarative/ -│ ├── template.ts # declarativeTemplate() and internal f-template publisher +│ ├── template.ts # declarativeTemplate(), string callback, internal f-template publisher │ ├── template-parser.ts # Declarative HTML parser → ViewTemplate strings/values │ ├── schema.ts # Compatibility re-export for Schema │ ├── definition-options.ts # Compatibility re-export for schema transforms diff --git a/packages/fast-element/README.md b/packages/fast-element/README.md index 6d4a720f3ee..66fe8524099 100644 --- a/packages/fast-element/README.md +++ b/packages/fast-element/README.md @@ -139,6 +139,40 @@ MyElement.define({ before `define()` resolves. Consumers should not import or define the `` implementation directly. +When template markup needs to be loaded or generated by JavaScript, pass a +`callback` to `declarativeTemplate()`. The callback receives +`templateStringResolver`, can perform async work such as dynamic imports or +`fetch()`, and must return or await the resolver promise. The resolver accepts a +string or `Promise` that contains exactly one `` element. If +the callback completes successfully without calling the resolver, template +resolution rejects with a diagnostic error. The `` can include +attributes such as `name`, and it must contain exactly one child `