Skip to main content
前端组件是直接在 Twenty 的 UI 内渲染的 React 组件。 它们在使用 Remote DOM 的隔离 Web Worker中运行——你的代码在沙盒中执行,但会原生渲染到页面中,而非在 iframe 里。

前端组件可用位置

在 Twenty 中,前端组件可在两个位置进行渲染:
  • 侧边栏 — 非无头的前端组件会在右侧侧边栏中打开。 当前端组件从命令菜单触发时,这是默认行为。
  • 小部件(仪表盘和记录页面) — 前端组件可以作为小部件嵌入页面布局中。 在配置仪表盘或记录页面布局时,用户可以添加前端组件小部件。

基础示例

最快体验前端组件运行方式的方法是将其注册为一个命令。 添加一个 command 字段并设置 isPinned: true,即可让它以快速操作按钮的形式出现在页面右上角——无需页面布局:
src/front-components/hello-world.tsx
使用 yarn twenty dev 同步后(或单次运行 yarn twenty dev --once),快速操作会出现在页面右上角:
右上角的快速操作按钮
点击它以内联方式渲染该组件。

配置字段

Placing a front component on a page

Beyond commands, you can embed a front component directly into a record page by adding it as a widget in a page layout. See the definePageLayout section for details.

无头与非无头

前端组件有两种由 isHeadless 选项控制的渲染模式: 非无头(默认) — 该组件会渲染可见的 UI。 从命令菜单触发时,它会在侧边栏中打开。 当 isHeadlessfalse 或被省略时,这是默认行为。 无头 (isHeadless: true) — 该组件会在后台以不可见的方式挂载。 它不会打开侧边栏。 无头组件旨在用于执行逻辑后自行卸载的操作——例如运行异步任务、导航到某个页面或显示确认模态框。 它们与下文介绍的 SDK Command 组件天然契合。
src/front-components/sync-tracker.tsx
Because the component returns null, Twenty skips rendering a container for it — no empty space appears in the layout. The component still has access to all hooks and the host communication API.

SDK Command 组件

twenty-sdk 包提供了四个为无头前端组件设计的 Command 辅助组件。 每个组件都会在挂载时执行一个操作,通过显示 snackbar 通知来处理错误,并在完成后自动卸载该前端组件。 twenty-sdk/command 导入它们:
  • Command — 通过 execute 属性运行异步回调。
  • CommandLink — 导航到某个应用路径。 属性:toparamsqueryParamsoptions
  • CommandModal — 打开一个确认模态框。 如果用户确认,则执行 execute 回调。 属性:titlesubtitleexecuteconfirmButtonTextconfirmButtonAccent
  • CommandOpenSidePanelPage — 打开特定的侧边栏页面。 属性:pagepageTitlepageIcon
下面是一个完整示例:无头前端组件使用 Command 从命令菜单运行一个操作:
src/front-components/run-action.tsx
另一个示例:使用 CommandModal 在执行前请求确认:
src/front-components/delete-draft.tsx

Accessing runtime context

Inside your component, use SDK hooks to access the current user, record, and component instance:
src/front-components/record-info.tsx
Available hooks:

Host communication API

Front components can trigger navigation, modals, and notifications using functions from twenty-sdk: 下面是一个示例,使用宿主 API 在操作完成后显示一条 snackbar 并关闭侧边栏:
src/front-components/archive-record.tsx

Command options

Adding a command field to defineFrontComponent registers the component in the command menu (Cmd+K). If isPinned is true, it also appears as a quick-action button in the top-right corner of the page.

Conditional availability expressions

The conditionalAvailabilityExpression field lets you control when a command is visible based on the current page context. Import typed variables and operators from twenty-sdk to build expressions:
Context variables — these represent the current state of the page: Operators — combine variables into boolean expressions:

Public assets

Front components can access files from the app’s public/ directory using getPublicAssetUrl:
See the public assets section for details.

样式

Front components support multiple styling approaches. You can use:
  • Inline stylesstyle={{ color: 'red' }}
  • Twenty UI components — import from twenty-sdk/ui (Button, Tag, Status, Chip, Avatar, and more)
  • Emotion — CSS-in-JS with @emotion/react
  • Styled-componentsstyled.div patterns
  • Tailwind CSS — utility classes
  • Any CSS-in-JS library compatible with React