Code Component
Introduction
Momen allows you to extend your applications by developing custom React components. These code components enable dynamic forms, data-driven charts, advanced animations, interactive maps, and more, greatly expanding the interactive capabilities of your projects.
Prerequisites
- Familiarity with the Momen platform
- Basic front-end development experience
- Knowledge of TypeScript
- Ability to develop custom React components
If you are new to React, refer to the React official documentation .
Getting Started
Node.js 18 or 20 is recommended. Node.js 22 is not supported.
Install the global CLI tool:
# Install globally
npm i -g momen-cli
Verify installation:
momen --version
CLI Commands
create <project_name>
: Initialize a new code component projectpublish
: Publish the projectreinit
: Re-register the project ID and publish as a new project under your accountreset
: Reset the template project in the current directorysignin <username/email> <password>
: Sign in to the Momen platformsignout
: Sign out and remove current login info
Run zvm --help
to see all available commands and options.
Platform Login
Sign in from your working directory. Replace your_email
and your_password
with your actual credentials.
momen signin your_email your_password
Project Creation
Create a new code component project:
momen create project_name
This command creates a new folder with all project files and a template component. It also registers a new code component project on the platform. Multiple components can be added to a single project. No version is published at this stage.
Editing Code
Navigate to your project directory and install dependencies:
cd project_name
npm install
Project Structure
A typical code component library project has the following structure:
- codegen.ts
- index.html
- package-lock.json
- package.json
- App.scss
- App.tsx
- index.ts
- index.css
- main.tsx
- vite-env.d.ts
- tree.txt
- tsconfig.json
- tsconfig.node.json
- vite.config.ts
The src/components
directory is where you declare and edit your code components.
Conventions
- Each component must have its own directory under
src/components
, named after the component. The directory must contain at leastindex.ts
(for export) andComponentName.tsx
(for logic). - you must export four interfaces in
[ComponentName].tsx
:[ComponentName]PropData
: Data received from the host project (supported types: string, number, boolean)[ComponentName]StateData
: State exposed to the host, must useState<string | number | boolean>
fromzvm-code-context
[ComponentName]Event
: Events, each property must be of typeEventHandler
fromzvm-code-context
[ComponentName]Props
: Combines the above three aspropData
,propState
, andevent
Notes:
- The component name must follow JavaScript naming conventions, start with an uppercase letter, and match the directory and file name.
- Type names must be strictly
[ComponentName]PropData
,[ComponentName]StateData
,[ComponentName]Event
,[ComponentName]Props
, and all must be exported.- The component function must be
export function [ComponentName](props: [ComponentName]Props) {}
.- Do not use
import('zvm-code-context').EventHandler
; instead, useimport { EventHandler } from 'zvm-code-context'
.
Example:
import { EventHandler } from 'zvm-code-context'
export interface ConfirmModalPropData {
buttonTitle: string
modalTitle: string
modalContent?: string
}
export interface ConfirmModalStateData {}
export interface ConfirmModalEvent {
onConfirm?: EventHandler
onCancel?: EventHandler
}
export interface ConfirmModalProps {
propData: ConfirmModalPropData
propState: ConfirmModalStateData
event: ConfirmModalEvent
}
export function ConfirmModal({ propData, event }: ConfirmModalProps) {
// ...component logic...
}
- Each component must be exported in
src/components/index.ts
:
export { ConfirmModal } from './ConfirmModal/ConfirmModal'
This allows the Momen platform to discover and use your component.
AI-assisted Component Development
You can use AI tools (such as VS Code Copilot or Cursor) to help generate code components. Prompt the AI to generate components in the src/components
directory and ensure the output follows the above conventions.
Recommended prompt:
Read “https://docs.momen.app/account_community/code-component/” and strictly follow the Momen platform conventions to write a React component named ConfirmModal. The component should support:
- Properties: button text, modal title, modal content
- State: confirm and cancel event handlers
- Use Ant Design’s Modal and Button
- Export the required interfaces Use TypeScript and ensure the code meets the platform’s requirements.
Local Debugging
To debug locally, import your component in App.tsx
.
-
Start the local development server:
npm run dev
-
To preview in real time on the Momen platform, you must publish the component at least once, then run:
npm run preview
Enable automatic build (auto-compile on save):
npm run watch:build
After configuring input parameters and behaviors, you can select the corresponding port in the platform’s live preview for debugging.
Related APIs
The zvm-code-context
package provides APIs to interact with the host project:
import { useAppContext } from 'zvm-code-context'
const { component, discover, query, navigate, globalData, pageData } = useAppContext()
component
: Info about the current component instancediscover(id: string)
: Get a specific component instance by IDquery
: Make backend requestsnavigate
: Navigate to a specific page in the host projectglobalData
: Access global datapageData
: Access current page data
Publishing the Project
Update the version
field in package.json
to follow npm semantic versioning (e.g., 1.0.0
). Then, publish your project:
momen publish
After publishing, the new code component library will be available for configuration in any Momen project.
Importing Code Components
To add your custom code component to a no-code project:
- Click the code component icon on the left sidebar.
- Select your newly published component project in the pop-up window.
Once added, the component will appear at the bottom of the left sidebar.
Backend Synchronization
After configuring or modifying a code component project, you must sync the database or publish to use it at runtime.
Configuring Component Properties
Configure data and event properties in the right sidebar. The available properties are parsed from your code, so follow the conventions strictly.
Live Preview or Publish
You can preview your code component in real time or after publishing, ensuring it works as expected in the host project.