Skip to content

The open alternative to Salesforce, designed for AI.

11.4 KB
Raw Blame

Front Components

Use this reference for Twenty app front component source files, registration, data access, Twenty UI imports, runtime imports, and browser verification.

Use layout.md for placing a front component in a page layout. Use standalone-pages.md for full-page custom UI rendered through a front component widget. Use ../design/front-component-ui.md for visual design, spacing, states, and polish. Use app-structure.md for the develop-app validation checklist and ../manage-app/cli-and-sync.md for command behavior or troubleshooting.

Documentation

Use these instructions for essential app defaults and the official docs for detailed APIs. Start with the front component guide for app-specific setup and runtime limitations. Twenty UI getting started describes the library; its standalone React setup does not apply inside a front component.

Find the relevant component page through the components or primitives index, then read its current props and composition before implementation. Use theming for semantic tokens. When the app pins an older version, check its installed public types before using an API from the latest docs.

Source And Registration

By convention, keep front components under src/front-components/, often as <name>.front-component.tsx.

Register the component with defineFrontComponent:

import { defineFrontComponent } from 'twenty-sdk/define';

const MyFrontComponent = () => {
  return <div />;
};

export default defineFrontComponent({
  universalIdentifier: '<front-component-uuid>',
  name: '<front-component-name>',
  description: '<short description>',
  component: MyFrontComponent,
});

Set widgetAvailability to offer a non-headless component in a widget picker while a page layout is edited. It is a single object, because a component is built either for record pages or for dashboards: { pageLayoutType: PageLayoutType.RECORD_PAGE } offers it on every record page, adding objectUniversalIdentifiers (non-empty) narrows it to those objects' record pages, and { pageLayoutType: PageLayoutType.DASHBOARD } offers it on dashboards. Omitted, the component is offered in no widget picker, which is right for components opened only from a command, a settings menu item or a timeline row. widgetAvailability never limits where a component is displayed: commands, settings menu items and the app's own page layouts render it whatever its value.

Do not import or call react-dom/client in the component source. The SDK renderer mounts front components.

Runtime Imports

Keep imports narrow:

  • Use twenty-sdk/front-component for front component hooks and host APIs.
  • Use twenty-client-sdk/core or twenty-client-sdk/metadata for data access.
  • Use sendInboxMessage and the inbox mention helpers from twenty-sdk/front-component to post in members' inbox (see logic.md). Open the conversation with navigate(AppPath.AiChatInbox, { threadId }): the route is main-page only, and it opens only for the conversation's participants.
  • Install twenty-ui from npm at the same version as twenty-sdk and twenty-client-sdk and import Twenty UI components, icons, and theme tokens from its subpaths (twenty-ui/primitives/input, twenty-ui/primitives/data-display, twenty-ui/icon, twenty-ui/primitives/typography, twenty-ui/theme, and others) before adding external UI libraries.

Keep the SDK packages and twenty-ui in devDependencies. Import shared components from their family entry point, such as twenty-ui/components/input for MainButton and LightButton, twenty-ui/components/feedback for Callout, twenty-ui/components/layout for Section, and twenty-ui/components/navigation for Dropdown. Use twenty-ui/primitives/input for foundational controls such as Button, Field and Input.

Twenty loads the UI stylesheets and applies the workspace color scheme on the host. In the app, inherit the workspace color scheme without adding another root provider, forcing light or dark mode, or importing the standalone stylesheets. For isolated examples or local story-style verification, wrapping the component in ThemeProvider from twenty-ui/theme is also acceptable.

The canonical Twenty UI front component example is packages/twenty-front-component-renderer/src/__stories__/showcase/twenty-ui-example.front-component.tsx. It imports Button from twenty-ui/primitives/input; Chip, Status, and Tag from twenty-ui/primitives/data-display; Section from twenty-ui/components/layout; and ThemeProvider from twenty-ui/theme. Its explicit provider is for the isolated showcase.

Read theme tokens with the useTheme() hook from twenty-ui/theme inside the component body. The same tokens are also exported as the themeCssVariables constant, but the SDK mocks twenty-ui during manifest extraction, so a module-level constant that dereferences themeCssVariables can fail before the component renders — prefer useTheme().

Prefer Twenty UI icons from twenty-ui/icon when one exists. Use inline SVG only for app-specific marks or icons that are not available from twenty-ui/icon.

Import individual icons, not twenty-ui/icon/provider (IconsProvider, useIcons, by-name Icon), which bundles the dynamic icon catalog. Use themeCssVariables only for CSS values inside render-time code. In front components, useTheme() also returns CSS reference strings by default, including for tokens typed as numbers. Do not perform arithmetic on those references or pass them to APIs requiring resolved numbers.

Host UI And Runtime Constraints

Use openCommandConfirmationModal from twenty-sdk/front-component for confirmation dialogs, or CommandModal for a headless confirmation action. Use enqueueSnackbar for notifications. Do not render Twenty UI Dialog or AlertDialog, native dialog elements or native browser popovers directly. Use Twenty UI Dropdown for menus, pickers and nonmodal form panels.

The component runs in a Web Worker with a partial DOM. Prefer CSS flexbox, grid and width-based container queries for layout. Avoid height-based breakpoints, which can oscillate when content determines widget height. canvas, scrollIntoView and native top-layer APIs are unsupported. Check the current limitations in the front component guide before depending on other browser APIs.

Iframes stay sandboxed. An embedded phone or video tool receives microphone or camera access only through the embed flow in ../concepts/how-apps-work.md.

Record Context And Data

For record-page components, read selection from front component context:

  • Use useSelectedRecordIds() for single, bulk, or empty selection; derive one id only when the array length is 1.
  • Under Select all, useSelectedRecordIds() is empty. Use useSelectedRecordsFilter() for bulk commands: it returns a filter matching exactly the selection in every case; query records with it or pass it to a logic function.
  • Use useSelectedObjectMetadata() from twenty-sdk/front-component to identify the object for a record-page widget, a custom timeline row, an object-scoped command, or a front component opened in the side panel with an object (from a command, or through openSidePanelPage with an objectNameSingular). It returns { id, nameSingular, namePlural } or null. The id is the workspace's object-metadata UUID, and the names are metadata names such as person and people, not display labels.

Object metadata belongs to the front component's own context, so a side panel can expose a different object from the main page. It can be available even when no records are selected and updates when that context changes. The hook returns null when no object context is supplied, when the object cannot be resolved, or when an older host does not provide object metadata. Use it to select object-specific templates or configuration without searching every object for a record ID.

Use the generated or core Twenty client for reads and writes. Keep loading, empty, error, disabled, and saving states explicit so runtime failures are visible and recoverable.

Headless Actions And DRY Helpers

Headless front components should be thin action shells. The component file should mostly read SDK hooks, return the Command helper, and delegate reusable behavior to helpers.

Common headless action flow:

  1. Read selected record IDs.
  2. Load the selected records.
  3. Build a payload for one logic-function call.
  4. Execute the logic function.
  5. Parse the result summary.
  6. Show a snackbar.
  7. Let Command unmount the component.

Do not duplicate this orchestration across sibling front components. When two components share command execution flow, extract it before copying:

  • Pure helpers go in src/utils/<name>.util.ts.
  • Front-component runtime helpers go in src/front-components/utils/<name>.util.ts.
  • Payload builders, result parsers, selected-record validators, and summary formatters should be small testable functions with sibling specs.

Each extracted helper and type lives in its own file: one export per file. A local, non-exported type may stay with the util, but an exported type moves to its own file. See app-structure.md.

Prefer configuration-driven helpers when creating parallel actions for people, companies, tasks, or other objects. Each front component should provide only object-specific configuration, such as the front component universal identifier, logic function universal identifier, record query, payload builder, labels, and snackbar copy.

Bulk Logic Function Calls

When a front component triggers a logic function for selected records, always prefer a bulk payload shape unless the user explicitly says the logic function is only for one record. The front component should call the logic function once with all selected records, not loop and execute the same logic function once per record.

Default payload shape:

{
  records: Array<{
    id: string;
    // object-specific fields used by the logic function
  }>;
}

Inside records, prefer id for the Twenty record ID because the array name already establishes record context. Do not add flat recordId payloads unless the user explicitly asks for a single-record action.

Logic functions that return per-record outcomes should mirror the same naming:

{
  ok: boolean;
  enrichedCount: number;
  noMatchCount: number;
  failedCount: number;
  results: Array<{
    id: string;
    status: 'ENRICHED' | 'NO_MATCH' | 'FAILED';
    error?: string;
  }>;
}

If an existing logic function accepts only a flat record ID, prefer upgrading it to accept records and remove the old flat input unless the user explicitly asks to preserve it.

Runtime Verification

A clean typecheck and sync is not runtime verification. After the standard validation in app-structure.md, open the relevant Twenty surface and confirm:

  • The widget mounts without a FrontComponent error in the widget body or toast.
  • The component renders loading, empty, and error states correctly.
  • The UI works in both workspace color schemes, at narrow widget widths and with long text values.
  • Inputs have visible labels, icon-only actions have accessible names, and the main controls work with a keyboard.
  • The main user action works against a real record.
  • The page still renders after a hard refresh.