Custom Components
Web SDK provides multiple ways to integrate custom components, allowing you to extend and customize its functionality. This guide will introduce several common methods.
Univer does not accept a component as an argument to a render call. A component must be registered through the Facade API before it can be referenced wherever Univer mounts it.
If a Facade API instance is not available yet, see Facade API.
Registering custom components
univerAPI.registerComponent(componentKey, CustomComponent, options)univerAPI.registerComponent registers a component and takes three arguments:
componentKey: the unique identifier of the component, used to reference it inside Univer.CustomComponent: the component itself, which can be a React component, a Vue component, or a Web Component.options: optional settings, such as the framework the component depends on or other related configuration.
React components
React components require no extra configuration. The component only needs to be a valid React component, as in the following example:
function ReactComponent(props: Record<string, any>) { return <div>Hello Univer!</div>}univerAPI.registerComponent('MyReactComponent', ReactComponent)Vue components
Vue 3.x
For Vue components, install the @univerjs/ui-adapter-vue3 package and register UniverVue3AdapterPlugin from it:
pnpm add @univerjs/ui-adapter-vue3npm install @univerjs/ui-adapter-vue3yarn add @univerjs/ui-adapter-vue3bun add @univerjs/ui-adapter-vue3import { UniverVue3AdapterPlugin } from '@univerjs/ui-adapter-vue3'univer.registerPlugin(UniverVue3AdapterPlugin)Pass framework as 'vue3' when you register the component:
const Vue3Component = defineComponent({ setup(props) { return () => <div>Hello Univer!</div> },})univerAPI.registerComponent('MyVue3Component', Vue3Component, { framework: 'vue3',})Vue 2.x
Vue 2.x has reached end-of-life and is no longer maintained, so Univer does not currently provide a UI adapter plugin for it. The adapter can be implemented as a custom plugin. The code below is a Vue 2.x adapter plugin that can serve as a starting point:
import { DependentOn, Inject, Injector, Plugin } from '@univerjs/core'import { ComponentManager, UniverUIPlugin } from '@univerjs/ui'import Vue from 'vue'/** * The plugin that allows Univer to use Vue 2 components as UI components. */@DependentOn(UniverUIPlugin)export class UniverVue2AdapterPlugin extends Plugin { static override pluginName = 'UNIVER_UI_VUE2_ADAPTER_PLUGIN' constructor( private readonly _config = {}, @Inject(Injector) protected readonly _injector: Injector, @Inject(ComponentManager) protected readonly _componentManager: ComponentManager, ) { super() } override onStarting(): void { const { createElement, useEffect, useRef } = this._componentManager.reactUtils this._componentManager.setHandler('vue2', (component: any) => { return (props: Record<string, any>) => createElement(VueComponentWrapper, { component, props: Object.keys(props).reduce<Record<string, any>>((acc, key) => { if (key !== 'key') { acc[key] = props[key] } return acc }, {}), reactUtils: { createElement, useEffect, useRef }, }) }) }}export function VueComponentWrapper(options: { component: any props: Record<string, any> reactUtils: typeof ComponentManager.prototype.reactUtils}) { const { component, props, reactUtils } = options const { createElement, useEffect, useRef } = reactUtils const domRef = useRef<HTMLDivElement>(null) useEffect(() => { if (!domRef.current) return const Constructor = Vue.extend(component) const instance = new Constructor({ data: props, }) instance.$mount() domRef.current.appendChild(instance.$el) return () => { instance.$destroy() } }, [props]) return createElement('div', { ref: domRef })}Then register the plugin:
import { UniverVue2AdapterPlugin } from './ui-adapter-vue2'univer.registerPlugin(UniverUIPlugin)univer.registerPlugin(UniverVue2AdapterPlugin)Vue 2.x components need framework set to 'vue2':
const Vue2Component = Vue.component('MyVue2Component', { template: '<div>Hello, Univer!</div>',})univerAPI.registerComponent('MyVue2Component', Vue2Component, { framework: 'vue2',})Web components
To register Web Components, the component must follow the Web Components standard. Install the @univerjs/ui-adapter-web-component package and register UniverWebComponentAdapterPlugin from it:
pnpm add @univerjs/ui-adapter-web-componentnpm install @univerjs/ui-adapter-web-componentyarn add @univerjs/ui-adapter-web-componentbun add @univerjs/ui-adapter-web-componentimport { UniverWebComponentAdapterPlugin } from '@univerjs/ui-adapter-web-component'univer.registerPlugin(UniverWebComponentAdapterPlugin)Pass framework as 'web-component':
class WebComponent extends HTMLElement { constructor() { super() const shadow = this.attachShadow({ mode: 'open' }) const div = document.createElement('div') div.textContent = 'Hello Univer!' shadow.appendChild(div) }}univerAPI.registerComponent('my-web-component', WebComponent, { framework: 'web-component',})Angular
createCustomElement from @angular/elements converts an Angular component into a custom element. After the element is registered with Univer's Web Component adapter, the Angular component can appear in a Univer sidebar or dialog.
Add @angular/elements at the same version as @angular/core, and @univerjs/ui-adapter-web-component at the same version as your other Univer packages.
Call this function after Univer is initialized. The Angular injector is supplied by the host component: obtain it with private readonly injector = inject(Injector) and then call registerAngularPanel(univer, univerAPI, this.injector). The host component imports inject and Injector from @angular/core.
import type { Injector } from '@angular/core'import type { FUniver, Univer } from '@univerjs/presets'import { Component } from '@angular/core'import { createCustomElement } from '@angular/elements'import { UniverWebComponentAdapterPlugin } from '@univerjs/ui-adapter-web-component'@Component({ selector: 'app-angular-panel', standalone: true, template: '<p>Hello from Angular!</p>',})export class AngularPanelComponent {}export function registerAngularPanel(univer: Univer, univerAPI: FUniver, injector: Injector) { const element = createCustomElement(AngularPanelComponent, { injector }) univer.registerPlugin(UniverWebComponentAdapterPlugin) return univerAPI.registerComponent('univer-angular-panel', element, { framework: 'web-component', })}Once the editor has rendered, pass the registered key as the sidebar content:
const sidebar = univerAPI.openSidebar({ header: { title: 'Angular' }, children: { label: 'univer-angular-panel' }, width: 360,})Retain the registration handle and the sidebar handle, and call dispose() on each when it is no longer needed. Register the element once per application. Its name must contain a hyphen and must differ from the Angular component selector.
Angular Elements maps inputs to element properties and outputs to DOM CustomEvent events. Univer's adapter sets the properties but does not subscribe to Angular outputs on its own, so the application must bridge those events when they are required.
Using custom components
The methods below place custom components at the locations Univer renders, extending and customizing the UI.
Caution
- Call these methods only after Univer has finished rendering.
- Register a component before you use it.
- Call
dispose()to clean up what you added and avoid memory leaks.
Adding custom menu items
The Ribbon at the top and the Context Menu on right-click can both host custom components. Menu items are registered by a plugin, so adding one requires a custom plugin. A best practice guide for custom menu items is available as a starting point.
Replacing built-in components
Warning
Replacing a built-in component can break features that depend on it. Read the source code and the documentation before you attempt a replacement, and proceed with caution.
univerAPI.registerComponent registers an override by default (override: true). The override takes priority over the built-in component regardless of whether the plugin registers its default before or after the call. Only one override can be active for each componentKey, so dispose the previous registration before registering another. Calling dispose() on the returned registration restores the built-in default, if one was registered. Use { override: false } to register a default instead.
Register the override before the view it covers opens for the first time. Changing a registration does not rerender views that are already mounted.
Sheets exports the following keys from its public package entries; use them to override the matching built-in components:
| Component | Exported key | Package |
|---|---|---|
| Defined names | DEFINED_NAME_CONTAINER | @univerjs/sheets-ui |
| More functions | MORE_FUNCTIONS_COMPONENT | @univerjs/sheets-formula-ui |
| Data validation | DATA_VALIDATION_PANEL | @univerjs/sheets-data-validation-ui |
| Conditional formatting | CF_PANEL_KEY | @univerjs/sheets-conditional-formatting-ui |
For example, the following code replaces the built-in ColorPicker component:
// Following code only replaces the UI of the built-in ColorPicker component, not its functionalityuniverAPI.registerComponent('UI_COLOR_PICKER_COMPONENT', () => <input type="color" />)Using as content components
In the sidebar
univerAPI.openSidebar opens a sidebar inside the Univer UI and renders your registered component in it.
// You should register the component at an appropriate time (e.g., after Univer has loaded)univerAPI.registerComponent('MyCustomSidebarComponent', () => <div>Hello Univer!</div>)const sidebar = univerAPI.openSidebar({ header: { title: 'My Sidebar' }, children: { label: 'MyCustomSidebarComponent' }, onClose: () => { console.log('close') }, width: 360,})// Later close the sidebarsidebar.dispose()Reference: univerAPI.openSidebar
In a dialog
univerAPI.openDialog opens a dialog and renders your registered component inside it.
// You should register the component at an appropriate time (e.g., after Univer has loaded)univerAPI.registerComponent('MyCustomDialogComponent', () => <div>Hello Univer!</div>)const dialog = univerAPI.openDialog({ id: 'unique-dialog-id', // The unique identifier for the dialog draggable: true, width: 300, title: { title: 'My Dialog' }, children: { label: 'MyCustomDialogComponent', }, destroyOnClose: true, preservePositionOnDestroy: true, onClose: () => {},})// Later close the dialogdialog.dispose()Reference: univerAPI.openDialog
Attach popup to cells
FRange.attachPopup attaches a custom popup to a specific cell range.
- A popup is a temporary DOM element attached to a cell. It usually displays transient status information and does not support persistent storage.
- The method signature is
FRange.attachPopup(popup: IFCanvasPopup). - The popup attaches to the edges of the cell and adjusts its position and orientation when something obstructs it.
// You should register the component at an appropriate time (e.g., after Univer has loaded)univerAPI.registerComponent( 'MyCustomPopupComponent', () => <div>Hello Univer!</div>,)const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:J10')// Attach the popup to the first cell of the range// If `disposable` is null, it indicates that the popup was not added successfullyconst disposable = fRange.attachPopup({ componentKey: 'MyCustomPopupComponent',})// Remove the popupdisposable.dispose()Attach alert popup
FRange.attachAlertPopup attaches an alert popup to the starting cell of a specific range.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:B2')const disposable = fRange.attachAlertPopup({ key: 'unique-alert-key', // Unique identifier title: '这是一个警告!', message: '这是一个警告!', width: 300, height: 200, // 0: Information // 1: Warning // 2: Error type: 0,})// Remove the alert laterdisposable.dispose()Add floating DOM to worksheet
FWorksheet.addFloatDomToPosition adds a floating DOM element to the worksheet.
- Before you use this method, install the
@univerjs/sheets-drawing-uiplugin or the@univerjs/preset-sheets-drawingpreset. - A floating DOM element is a draggable component that hovers over the worksheet and supports persistent storage.
- Call this method after Univer has finished rendering.
componentKey: the unique identifier for the component, used to reference it inside Univer.- Full parameter definition
// You should register the component at an appropriate time (e.g., after Univer has loaded)univerAPI.registerComponent( 'MyCustomFloatDOMComponent', ({ data }) => ( <div> Hello {data?.label} ! </div> ),)const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Add a floating DOM to the worksheet// If `disposable` is null, it indicates that the floating DOM was not added successfullyconst disposable = fWorksheet.addFloatDomToPosition({ componentKey: 'MyCustomFloatDOMComponent', initPosition: { startX: 100, endX: 200, startY: 100, endY: 200, }, // The data for the component data: { label: 'Univer', },})// Remove the floating DOMdisposable.dispose()How is this guide?