☰
FAST 设计系统注册入口 provideFASTDesignSystem() 完全指南:签名、参数与 DesignSystem 链式配置
2026/9/27 10:29:06 网站建设 项目流程
  • 前端
  • UI组件

【免费下载链接】fast

The adaptive interface system for modern web experiences.

项目地址:https://gitcode.com/gh_mirrors/fa/fast
点击查看免费下载

provideFASTDesignSystem()是 FAST 组件库(@microsoft/fast-components)面向应用侧的统一设计系统入口函数:调用它即可为指定元素获取或创建 DesignSystem 实例,并通过返回的链式 API 完成组件注册与全局配置。读完本文,你将掌握该函数的完整签名语义、底层DesignSystem.getOrCreate实现原理、基于 element 参数的多作用域挂载方式,以及withPrefix、withShadowRootMode、withElementDisambiguation等全套配置实战方案。

函数签名与语义

provideFASTDesignSystem由@microsoft/fast-components导出,其 API 契约定义在仓库的 fast-components.providefastdesignsystem.md 中:

export declare function provideFASTDesignSystem(element?: HTMLElement): DesignSystem;

参数说明

参数类型说明
elementHTMLElement(可选)将设计系统挂载(root)于该元素上。不传时默认挂载在document.body上。

返回值

返回DesignSystem实例,即一个"FAST Design System"。它本质上是设计系统功能的 API 网关,在 fast-foundation.designsystem.md 中可以看到其完整形状:

DesignSystem: Readonly<{ tagFor(type: Constructable): string; responsibleFor(element: HTMLElement): DesignSystem; getOrCreate(node?: Node | undefined): DesignSystem; }>

这个返回实例支持链式调用register、withPrefix、withShadowRootMode、withElementDisambiguation、withDesignTokenRoot等方法(每个方法都返回DesignSystem自身以便继续链式调用),这也是下文所有实战示例能够写成.register(...)链的基础。

函数语义

该函数的核心语义是:为指定元素提供一个设计系统——如果该元素上已经存在设计系统,则直接返回已创建的实例;否则新建一个。这正是"提供者函数(provider function)"设计模式在 FAST 中的落地。用一句话概括:provideFASTDesignSystem()是 FAST Frame 官方对底层DesignSystem.getOrCreate()的封装。

它在 FAST 架构中的位置

要理解这个函数为何存在,需要先了解 FAST 的设计系统分层思想。仓库的 creating-a-component-library.md 给出了四层模型:

  1. foundation components:@microsoft/fast-foundation提供的可复用组件类与模板,与任何设计系统无关,追求最大灵活性;
  2. design system:在 foundation 之上叠加设计系统自身的独特主张(样式、元素前缀、design token、主题色等),导出组件注册函数供应用选用;
  3. application:应用只注册自己需要的组件;
  4. features:仅使用应用中已注册的组件,并通过 design token 定制功能。

FAST Frame(即@microsoft/fast-components)处于第二层。它的组件必须先注册进一个DesignSystem,才能在 HTML 中作为自定义元素使用——这正是provideFASTDesignSystem()的职责所在。按照 fast-frame.md 的说明,Web 组件在用于 HTML 之前必须与DesignSystem完成注册。

底层原理:DesignSystem.getOrCreate 与提供者函数

从源码与文档可以确认,provideFASTDesignSystem()的底层机制与自定义设计系统的"提供者函数"完全同构。creating-a-component-library.md 展示了社区如何为自定义设计系统编写等价的提供者函数:

export function provideSpecialDesignSystem(element?: HTMLElement): DesignSystem { return DesignSystem.getOrCreate(element).withPrefix("special"); }

该文档明确解释了底层DesignSystem.getOrCreate的行为:获取直接附着在指定元素上的设计系统,若不存在则创建;默认情况下(未提供 element)在document.body上创建设计系统。由此可以推断,provideFASTDesignSystem()的内部实现遵循同样的getOrCreate(element)模式,只是额外固化了 FAST Frame 的默认配置(如默认前缀fast)。提供者函数的意义在于:把元素查找与创建的逻辑抽象成更友好的 API,同时允许把默认配置(例如默认元素前缀)"烘焙"进函数内部。

基本用法:注册组件并投入页面

安装

npm install --save @microsoft/fast-components

或使用 yarn:

yarn add @microsoft/fast-components

注册单个组件

按 components/getting-started.md 的标准做法,导入组件注册函数与提供者函数,然后链式注册:

import { fastButton, provideFASTDesignSystem } from "@microsoft/fast-components"; provideFASTDesignSystem() .register( fastButton() );

register()方法会把组件与服务注册到设计系统及其底层的依赖注入容器中(见 designsystem.register.md),从而向平台注册自定义元素。注册完成后,组件即可像原生 HTML 元素一样使用:

<fast-button>Hello world</fast-button>

一键注册全部组件

cheat-sheet.md 提供了快捷方式——通过allComponents一次性注册全部组件:

import { allComponents, provideFASTDesignSystem } from "@microsoft/fast-components"; provideFASTDesignSystem().register(allComponents);

按需注册与 tree-shaking

官方文档特别提醒:当使用 Webpack、Rollup 等支持 tree-shaking 的打包器时,应逐个导入并注册组件,确保未使用的组件被摇树优化剔除出产物(见 components/getting-started.md)。这是provideFASTDesignSystem().register(...)按参数逐个接收注册项(register(...params: any[]))的重要原因。

防 FOUC(未定义样式闪动)

自定义元素在升级完成前可能没有样式,浏览器渲染它们会造成"未样式内容闪动"(FOUC)。fast-frame.md 建议由消费应用自行添加如下 CSS:

:not(:defined) { visibility: hidden; }

注意:组件自身不会做这件事,必须由应用层应用这段样式。

CDN 方式

若不想走 npm 构建流程,可以使用 CDN 上预打包的模块脚本(type="module"),例如:

<script type="module" src="https://cdn.jsdelivr.net/npm/@microsoft/fast-components/dist/fast-components.min.js"></script>

从 CDN 使用时无需手动调用provideFASTDesignSystem()——CDN 脚本内部已经自动完成设计系统初始化与全部组件注册(见 components/getting-started.md)。生产环境建议锁定具体版本号,避免始终指向最新发布。

element 参数:设计系统的作用域控制

element参数是provideFASTDesignSystem()最具价值的可选项。它决定了设计系统被"扎根"(root)在哪棵 DOM 子树之上:

  • 不传参:设计系统默认建立在document.body,注册的组件在整个文档中可用,这是绝大多数单页应用的用法;
  • 传元素:设计系统只作用于以该元素为根的子树。这为多设计系统共存、分区定制主题提供了可能——例如页面的不同区域可以各自调用provideFASTDesignSystem(sectionEl)并注册不同的组件配置。

设计系统"根"的概念还延伸到 design token 的发射位置:链式方法withDesignTokenRoot(root)可覆盖默认 CSS 自定义属性(design token 默认值)的发射根节点,其签名见 designsystem.withdesigntokenroot.md:

withDesignTokenRoot(root: HTMLElement | Document | null): DesignSystem;

其中传入null会禁用 DesignToken 的自动注册。这一方法让provideFASTDesignSystem()返回的实例在"多根"应用场景下拥有精确控制 token 作用域的能力。

链式配置:五个常用 DesignSystem 配置方法

fast-frame.md 指出:DesignSystem 是绝大多数组件配置的入口,可以控制自定义元素在浏览器中的定义方式、使用的模板与样式表、以及同名自定义元素的消歧策略。以下配置作用于注册到该设计系统的所有组件,除非该选项在组件注册时被显式指定(组件级配置优先级更高)。

withPrefix:自定义元素前缀

Web 组件(Custom Elements)必须遵循 HTML 命名规范——元素名必须包含-。FAST 默认给所有注册元素加上fast前缀,因此fastButton注册后对应<fast-button>。如需整体更换前缀,使用withPrefix(签名见 designsystem.withprefix.md):

import { fastButton, provideFASTDesignSystem } from "@microsoft/fast-components"; provideFASTDesignSystem() .withPrefix("faster") .register(fastButton())

此后元素在 HTML 中写作:

<faster-button>Click me!</faster-button>

withShadowRootMode:统一关闭 Shadow DOM

组件开发者通常以"open"模式(推荐且默认)创建 Shadow Root,但可通过withShadowRootMode全局改为"closed"(签名见 designsystem.withshadowrootmode.md):

provideFASTDesignSystem() .withShadowRootMode("closed") .register(/* ... */)

withElementDisambiguation:同名元素消歧

默认行为下,已注册过的元素名不会向平台重复注册,但其元素定义回调仍会被调用,从而允许在所属 DOM 子树内定义备选外观(样式与模板)。fast-frame.md 强调:最佳实践是避免重复注册同一组件;若架构上难以避免,可提供自定义回调处理。

回调会收到三个参数(被注册的标签名、当前注册的类型、该标签已注册的类型),并可返回三种值:

  • string:返回一个字符串作为元素的新注册名;
  • ElementDisambiguation.definitionCallbackOnly(默认回调的返回值):不重新注册元素,但允许其回调运行并为该元素定义备选外观。注意:同一元素存在多种外观会触发较慢的渲染路径,非必要不要使用;
  • ElementDisambiguation.ignoreDuplicate:完全忽略重复元素,注册时不采取任何动作。

示例(签名见 designsystem.withelementdisambiguation.md):

provideFASTDesignSystem() .withElementDisambiguation((nameAttempt, typeAttempt, existingType) => { if (nameAttempt === "foo") { return "bar"; } return ElementDisambiguation.ignoreDuplicate; }) .register(/* ... */)

组件级配置:在 register 时覆盖

以上 DesignSystem 级配置作用于全部组件,但每个选项也可以在组件注册时单独配置或覆盖,组件级配置优先于设计系统级配置(见 fast-frame.md):

// 组件级前缀 provideFASTDesignSystem() .register( fastButton({ prefix: "faster" }) ); // 组件级模板 provideFASTDesignSystem() .register( fastButton({ template: html` <p>A completely new template</p> ` }) ); // 组件级样式(完全替换) provideFASTDesignSystem() .register( fastButton({ styles: css` /* completely replace the original styles */ ` }) ); // 组件级样式(在原始样式上扩展) provideFASTDesignSystem() .register( fastButton({ styles: (ctx, def) => css` ${buttonStyles(ctx, def)} /* add your style augmentations here */ ` }) ); // 组件级 Shadow 配置(mode + delegatesFocus) provideFASTDesignSystem() .register( fastButton({ shadowOptions: { mode: "closed", delegatesFocus: true } }) );

官方文档同时提示:当前样式与模板函数存在一处轻微的类型缺陷,扩展原始样式时可能需要将第二个参数强转,例如${buttonStyles(ctx, def as any)}。

在框架集成中使用:React 示例

provideFASTDesignSystem()返回的 DesignSystem 也是框架集成(如 React)的接入点。integrations/react.md 展示了将其直接传给provideReactWrapper的用法:

import { provideFASTDesignSystem, fastCard, fastButton } from '@microsoft/fast-components'; import { provideReactWrapper } from '@microsoft/fast-react-wrapper'; import React from 'react'; const { wrap } = provideReactWrapper( React, provideFASTDesignSystem() ); export const FastCard = wrap(fastCard()); export const FastButton = wrap(fastButton())

随后即可在 JSX 中使用封装后的 React 组件:

<FastCard> <h2>FAST React</h2> <FastButton appearance="accent" onClick={() => console.log("clicked")}>Click Me</FastButton> </FastCard>

这一模式说明:提供者函数的返回值不只服务于原生注册,也是各类集成层(React/Angular/Svelte/Vue 等,详见 cheat-sheet.md 的集成列表)复用同一个 DesignSystem 实例的桥梁。

自定义设计系统:照搬提供者函数模式

如果要在 FAST Frame 之外构建自己的设计系统,可完全复刻provideFASTDesignSystem()的模式:creating-a-component-library.md 建议先用@microsoft/fast-foundation的基础组件类与模板组合出自己的组件:

import { Button, buttonTemplate as template, } from "@microsoft/fast-foundation"; import { buttonStyles as styles } from "./special-button.styles"; export const specialButton = Button.compose({ baseName: "button", template, styles, shadowOptions: { delegatesFocus: true, }, }); export const buttonStyles = styles;

然后导出提供者函数并注册组件:

provideSpecialDesignSystem() .register( specialButton() );

至此,provideFASTDesignSystem()所代表的设计系统提供者模式在自定义场景下得到了完整闭环:入口函数负责获取/创建设计系统并固化默认配置,链式注册负责登记组件与覆盖配置,element 参数负责划定作用域——三者共同构成了 FAST 自适应界面系统对外暴露的最主要编程接口。

  • 前端
  • UI组件

【免费下载链接】fast

The adaptive interface system for modern web experiences.

项目地址:https://gitcode.com/gh_mirrors/fa/fast
点击查看免费下载
上一篇:Podman `--volumes-from` 选项完全指南:容器间共享卷的原理、用法与源码实现
下一篇:League Akari:英雄联盟玩家的智能本地化效率工具完全指南

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询