前端组件可用位置
在 Twenty 中,前端组件可在两个位置进行渲染:- 侧边栏 — 非无头的前端组件会在右侧侧边栏中打开。 当前端组件从命令菜单触发时,这是默认行为。
- 小部件(仪表盘和记录页面) — 前端组件可以作为小部件嵌入页面布局中。 在配置仪表盘或记录页面布局时,用户可以添加前端组件小部件。
基础示例
最快体验前端组件运行方式的方法是将其注册为一个命令。 添加一个command 字段并设置 isPinned: true,即可让它以快速操作按钮的形式出现在页面右上角——无需页面布局:
src/front-components/hello-world.tsx
yarn twenty dev 同步后(或单次运行 yarn twenty dev --once),快速操作会出现在页面右上角:

配置字段
| 字段 | 必填 | 描述 |
|---|---|---|
universalIdentifier | 是 | 该组件的稳定唯一 ID |
component | 是 | 一个 React 组件函数 |
name | 否 | 显示名称 |
描述 | 否 | 组件的功能描述 |
isHeadless | 否 | Set to true if the component has no visible UI (see below) |
命令 | 否 | Register the component as a command (see command options below) |
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。 从命令菜单触发时,它会在侧边栏中打开。 当 isHeadless 为 false 或被省略时,这是默认行为。
无头 (isHeadless: true) — 该组件会在后台以不可见的方式挂载。 它不会打开侧边栏。 无头组件旨在用于执行逻辑后自行卸载的操作——例如运行异步任务、导航到某个页面或显示确认模态框。 它们与下文介绍的 SDK Command 组件天然契合。
src/front-components/sync-tracker.tsx
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— 导航到某个应用路径。 属性:to、params、queryParams、options。CommandModal— 打开一个确认模态框。 如果用户确认,则执行execute回调。 属性:title、subtitle、execute、confirmButtonText、confirmButtonAccent。CommandOpenSidePanelPage— 打开特定的侧边栏页面。 属性:page、pageTitle、pageIcon。
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
| 钩子 | Returns | 描述 |
|---|---|---|
useUserId() | string or null | The current user’s ID |
useRecordId() | string or null | The current record’s ID (when placed on a record page) |
useFrontComponentId() | string | This component instance’s ID |
useFrontComponentExecutionContext(selector) | 因情况而异 | Access the full execution context with a selector function |
Host communication API
Front components can trigger navigation, modals, and notifications using functions fromtwenty-sdk:
| 函数 | 描述 |
|---|---|
navigate(to, params?, queryParams?, options?) | Navigate to a page in the app |
openSidePanelPage(params) | Open a side panel |
closeSidePanel() | 关闭侧边栏 |
openCommandConfirmationModal(params) | Show a confirmation dialog |
enqueueSnackbar(params) | Show a toast notification |
unmountFrontComponent() | Unmount the component |
updateProgress(progress) | Update a progress indicator |
src/front-components/archive-record.tsx
Command options
Adding acommand 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.
| 字段 | 必填 | 描述 |
|---|---|---|
universalIdentifier | 是 | Stable unique ID for the command |
标签 | 是 | Full label shown in the command menu (Cmd+K) |
shortLabel | 否 | Shorter label displayed on the pinned quick-action button |
图标 | 否 | Icon name displayed next to the label (e.g. 'IconBolt', 'IconSend') |
isPinned | 否 | When true, shows the command as a quick-action button in the top-right corner of the page |
availabilityType | 否 | Controls where the command appears: 'GLOBAL' (always available), 'RECORD_SELECTION' (only when records are selected), or 'FALLBACK' (shown when no other commands match) |
availabilityObjectUniversalIdentifier | 否 | Restrict the command to pages of a specific object type (e.g. only on Company records) |
conditionalAvailabilityExpression | 否 | A boolean expression to dynamically control whether the command is visible (see below) |
Conditional availability expressions
TheconditionalAvailabilityExpression 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:
| 变量 | 类型 | 描述 |
|---|---|---|
pageType | string | Current page type (e.g. 'RecordIndexPage', 'RecordShowPage') |
isInSidePanel | boolean | Whether the component is rendered in a side panel |
numberOfSelectedRecords | 数字 | Number of currently selected records |
isSelectAll | boolean | Whether “select all” is active |
selectedRecords | array | The selected record objects |
favoriteRecordIds | array | IDs of favorited records |
objectPermissions | 对象 | Permissions for the current object type |
targetObjectReadPermissions | 对象 | Read permissions for the target object |
targetObjectWritePermissions | 对象 | Write permissions for the target object |
featureFlags | 对象 | Active feature flags |
objectMetadataItem | 对象 | Metadata of the current object type |
hasAnySoftDeleteFilterOnView | boolean | Whether the current view has a soft-delete filter |
| Operator | 描述 |
|---|---|
isDefined(value) | true if the value is not null/undefined |
isNonEmptyString(value) | true if the value is a non-empty string |
includes(array, value) | true if the array contains the value |
includesEvery(array, prop, value) | true if every item’s property includes the value |
every(array, prop) | true if the property is truthy on every item |
everyDefined(array, prop) | true if the property is defined on every item |
everyEquals(array, prop, value) | true if the property equals the value on every item |
some(array, prop) | true if the property is truthy on at least one item |
someDefined(array, prop) | true if the property is defined on at least one item |
someEquals(array, prop, value) | true if the property equals the value on at least one item |
someNonEmptyString(array, prop) | true if the property is a non-empty string on at least one item |
none(array, prop) | true if the property is falsy on every item |
noneDefined(array, prop) | true if the property is undefined on every item |
noneEquals(array, prop, value) | true if the property does not equal the value on any item |
Public assets
Front components can access files from the app’spublic/ directory using getPublicAssetUrl:
样式
Front components support multiple styling approaches. You can use:- Inline styles —
style={{ 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-components —
styled.divpatterns - Tailwind CSS — utility classes
- Any CSS-in-JS library compatible with React