Excalidraw React component

The Excalidraw React component is the editor published as the @excalidraw/excalidraw npm package, version 0.18.0, for developers who embed it in their own React apps. You set how it starts with props such as initialData, langCode and viewModeEnabled, read and change the drawing through onChange and the imperative API, and turn off interaction or the default UI with interaction and ui.

On this page

Install the component

The Excalidraw editor is published as the @excalidraw/excalidraw npm package, version 0.18.0, and it works in React apps. Its React and React DOM peer versions are ^17.0.2, ^18.2.0 or ^19.0.0.

Render the editor with the component's props. Pass interaction={{ enabled: { links: true } }} to keep links clickable while the editor is otherwise non-interactive.

Props that set up the editor

Each prop below is passed to the component. Unless a row says otherwise, the editor uses the value it has when the prop is not set.

Prop

What it does

Source

initialData

What it does

Sets the first scene. It takes a value or a function, and the value can be a promise.

initialState

What it does

Sets the starting app state.

langCode

What it does

Sets the language. It defaults to the default language's code.

theme

What it does

Sets the theme. When it is not set, the editor shows a toggle for it.

viewModeEnabled

What it does

Turns on view mode.

activeTool

What it does

Forces the active tool, as a controlled value. The tool stays active until the prop is unset.

zenModeEnabled, gridModeEnabled

What it does

Set zen mode and grid mode.

isCollaborating

What it does

Tells the editor whether a collaboration session is running. It defaults to off.

ownerDocument

What it does

Sets the document the editor mounts into. It defaults to the global document.

renderEnvironment

What it does

Supplies the canvas and image factories the renderer uses. Keep the object stable between renders, because the caches are keyed by its identity.

detectScroll

What it does

Turns on scroll detection. It defaults to on.

handleKeyboardGlobally

What it does

Passes keyboard events to the editor from anywhere on the page. It defaults to off.

autoFocus

What it does

Focuses the editor when it mounts. It defaults to off.

aiEnabled

What it does

Turns the AI features on. They stay on unless this is set to false.

imageOptions

What it does

Sets limits for inserted images. Missing values fall back to the defaults.

libraryReturnUrl

What it does

Sets the address the library returns to.

Read and change the drawing from code

Two props receive the drawing as it changes. onChange receives the elements, the app state and the files each time they change. The imperative API has an onChange method that takes a callback and returns a function that removes it.

The API is handed out through onExcalidrawAPI, which runs as soon as the API exists. The editor is not mounted at that point, and its state is not set yet. Its methods include updateScene, getSceneElements, getAppState, getFiles, resetScene, setActiveTool and the selection group.

Lifecycle props run in this order: onMount runs when the editor root mounts and gets the API and the container. onInitialize runs once the initial scene is loaded. onUnmount runs when the root unmounts.

Export from code

exportScene makes a canvas, a PNG or JPEG blob, or an SVG element, depending on its type. For PNG, embedScene writes the scene into the file's metadata, so the file can be opened again in Excalidraw. JPEG takes a quality between 0 and 1, which defaults to 0.92. JPEG has no transparency, so canvasBackgroundColor: false is ignored for it and the default background is used.

The onExport prop runs before a JSON export is saved, and it can wait for async work. It is called only for JSON exports, and it gets an abort signal.

Turn off interaction or the default UI

Set interaction={false} to make the editor non-interactive. It still draws the scene and reacts to changes made through the API, but a user cannot change it. Setting it implies view mode. Pass a config object instead to keep some interactions on.

Set ui={false} to hide the default UI, such as the toolbar, menus and sidebars. Host UI passed as children keeps rendering. Canvas content still renders, and the editor stays interactive unless interaction is off. Pass ui={{ enabled: { zoom: true } }} or ui={{ disabled: { minimap: true } }} to keep or remove single controls.

UIOptions sets which canvas actions and tools show. The image tool is on unless UIOptions.tools.image is set to false.

Sources

Related pages