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 components as arguments to rendering functions. Register a component through the Facade API, and Univer can then find it at each mounting point.
If you do not have a Facade API instance yet, see Facade API.
Registering custom components
univerAPI.registerComponent(componentKey, CustomComponent, options)univerAPI.registerComponent accepts three parameters:
componentKey: unique identifier of the component, used to reference it inside Univer.CustomComponent: the component itself, as a React component, a Vue component, or a Web Component.options: optional settings, such as which framework the component depends on.
React components
React components require no additional configuration. The only requirement is that the component is a valid React component:
function ReactComponent(props: Record<string, any>) { return <div>Hello Univer!</div>}univerAPI.registerComponent('MyReactComponent', ReactComponent)Vue components
Vue 3.x
To register Vue components, install and register UniverVue3AdapterPlugin from the @univerjs/ui-adapter-vue3 package:
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)When registering a Vue component, set the framework option to 'vue3':
const Vue3Component = defineComponent({ setup(props) { return () => <div>Hello Univer!</div> },})univerAPI.registerComponent('MyVue3Component', Vue3Component, { framework: 'vue3',})Vue 2.x
Vue 2.x is EOL (End of Life) and is no longer maintained, so Univer does not provide a UI adapter plugin for it. You can implement one as a custom plugin. The example below builds a Vue 2.x adapter:
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 it:
import { UniverVue2AdapterPlugin } from './ui-adapter-vue2'univer.registerPlugin(UniverUIPlugin)univer.registerPlugin(UniverVue2AdapterPlugin)When registering a Vue 2.x component, set the framework option to 'vue2':
const Vue2Component = Vue.component('MyVue2Component', { template: '<div>Hello, Univer!</div>',})univerAPI.registerComponent('MyVue2Component', Vue2Component, { framework: 'vue2',})Web components
A Web Component must conform to the Web Components standard. Install and register UniverWebComponentAdapterPlugin from the @univerjs/ui-adapter-web-component package:
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)When registering a Web Component, set the framework option to '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',})Using custom components
The methods below mount custom components within Univer's own UI.
Caution
- Make sure that Univer has completed rendering before calling any of these methods. 2. Register a component before using it. 3. Call
dispose()to clean up and remove added components; otherwise, they cause memory leaks.
Adding custom menu items
The top menu bar (Ribbon) and the right-click menu (Context Menu) can both contain custom components. Provide these menu items from your own plugin, register their components before use, and dispose the registrations together with the plugin.
Replacing built-in components
Warning
Replacing built-in components may cause some features to stop working properly. Read the source code and documentation of the component you intend to replace, and proceed with caution.
univerAPI.registerComponent registers an override by default (override: true). The override takes precedence over the built-in component regardless of whether the plugin registers its default before or after your 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 is registered. Pass { override: false } to register a default instead.
Register an override before the corresponding view opens for the first time. Changing a registration does not rerender views that are already mounted.
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 in the Univer interface and renders a custom component inside 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 that renders a custom component.
// 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
How is this guide?