ref
createRef | useRef | callbackRef |
{ current: { readyPromise: resolvablePromise } }
You can pass a ref
when you want to access some excalidraw APIs. We expose the below APIs:
API | Signature | Usage |
---|---|---|
ready | boolean | This is set to true once Excalidraw is rendered |
readyPromise | function | This promise will be resolved with the api once excalidraw has rendered. This will be helpful when you want do some action on the host app once this promise resolves. For this to work you will have to pass ref as shown here |
updateScene | function | updates the scene with the sceneData |
updateLibrary | function | updates the scene with the sceneData |
addFiles | function | add files data to the appState |
resetScene | function | Resets the scene. If resetLoadingState is passed as true then it will also force set the loading state to false. |
getSceneElementsIncludingDeleted | function | Returns all the elements including the deleted in the scene |
getSceneElements | function | Returns all the elements excluding the deleted in the scene |
getAppState | function | Returns current appState |
history | object | This is the history API. history.clear() will clear the history |
scrollToContent | function | Scroll the nearest element out of the elements supplied to the center. Defaults to the elements on the scene. |
refresh | function | Updates the offsets for the Excalidraw component so that the coordinates are computed correctly (for example the cursor position). |
setToast | function | This API can be used to show the toast with custom message. |
id | string | Unique ID for the excalidraw component. |
getFiles | function | This API can be used to get the files present in the scene. |
setActiveTool | function | This API can be used to set the active tool |
setCursor | function | This API can be used to set customise the mouse cursor on the canvas |
resetCursor | function | This API can be used to reset to default mouse cursor on the canvas |
toggleMenu | function | Toggles specific menus on/off |
readyPromise
const excalidrawRef = { current:{ readyPromise: resolvablePromise } }
Since plain object is passed as a ref
, the readyPromise
is resolved as soon as the component is mounted. Most of the time you will not need this unless you have a specific use case where you can't pass the ref
in the react way and want to do some action on the host when this promise resolves.
const resolvablePromise = () => {
let resolve;
let reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
};
const App = () => {
const excalidrawRef = useMemo(
() => ({
current: {
readyPromise: resolvablePromise(),
},
}),
[],
);
useEffect(() => {
excalidrawRef.current.readyPromise.then((api) => {
console.log("loaded", api);
});
}, [excalidrawRef]);
return (
<div style={{ height: "500px" }}>
<Excalidraw ref={excalidrawRef} />
</div>
);
};
updateScene
(scene: sceneData) => void
You can use this function to update the scene with the sceneData. It accepts the below attributes.
Name | Type | Description |
---|---|---|
elements | ImportedDataState["elements"] | The elements to be updated in the scene |
appState | ImportedDataState["appState"] | The appState to be updated in the scene. |
collaborators | Map<string, Collaborator> | The list of collaborators to be updated in the scene. |
commitToHistory | boolean | Implies if the history (undo/redo) should be recorded. Defaults to false . |
updateLibrary
(opts: {
libraryItems: LibraryItemsSource;
merge?: boolean;
prompt?: boolean;
openLibraryMenu?: boolean;
defaultStatus?: "unpublished" | "published";
}) => Promise<LibraryItems>
You can use this function to update the library. It accepts the below attributes.
Name | Type | Default | Description |
---|---|---|---|
libraryItems | LibraryItemsSource | _ | The libraryItems to be replaced/merged with current library |
merge | boolean | false | Whether to merge with existing library items. |
prompt | boolean | false | Whether to prompt user for confirmation. |
openLibraryMenu | boolean | false | Keep the library menu open after library is updated. |
defaultStatus | "unpublished" | "published" | "unpublished" | Default library item's status if not present. |
addFiles
(files: BinaryFileData) => void
Adds supplied files data to the appState.files
cache on top of existing files present in the cache.
resetScene
(opts?: { resetLoadingState: boolean }) => void
Resets the scene. If resetLoadingState
is passed as true then it will also force set the loading state to false.
getSceneElementsIncludingDeleted
() => ExcalidrawElement[]
Returns all the elements including the deleted in the scene.
getSceneElements
() => NonDeleted<ExcalidrawElement[]>
Returns all the elements excluding the deleted in the scene
getAppState
() => AppState
Returns current appState.
history
{
clear: () => void
}
This is the history API. history.clear() will clear the history.
scrollToContent
(
target?: ExcalidrawElement | ExcalidrawElement[],
opts?:
| {
fitToContent?: boolean;
animate?: boolean;
duration?: number;
}
| {
fitToViewport?: boolean;
viewportZoomFactor?: number;
animate?: boolean;
duration?: number;
}
) => void
Scroll the nearest element out of the elements supplied to the center of the viewport. Defaults to the elements on the scene.
Attribute | type | default | Description |
---|---|---|---|
target | ExcalidrawElement | ExcalidrawElement[] | All scene elements | The element(s) to scroll to. |
opts.fitToContent | boolean | false | Whether to fit the elements to viewport by automatically changing zoom as needed. Note that the zoom range is between 10%-100%. |
opts.fitToViewport | boolean | false | Similar to fitToContent but the zoom range is not limited. If elements are smaller than the viewport, zoom will go above 100%. |
opts.viewportZoomFactor | number | 0.7 | when fitToViewport=true, how much screen should the content cover, between 0.1 (10%) and 1 (100%) |
opts.animate | boolean | false | Whether to animate between starting and ending position. Note that for larger scenes the animation may not be smooth due to performance issues. |
opts.duration | number | 500 | Duration of the animation if opts.animate is true . |
refresh
() => void
Updates the offsets
for the Excalidraw
component so that the coordinates are computed correctly (for example the cursor position).
You don't have to call this when the position is changed on page scroll or when the excalidraw container resizes (we handle that ourselves).
For any other cases if the position of excalidraw is updated (example due to scroll on parent container and not page scroll) you should call this API.
setToast
This API can be used to show the toast with custom message.
({ message: string, closable?:boolean,duration?:number
} | null) => void
Attribute | type | Description |
---|---|---|
message | string | The message to be shown on the toast. |
closable | boolean | Indicates whether to show the closable button on toast to dismiss the toast. |
duration | number | Determines the duration after which the toast should auto dismiss. To prevent autodimiss you can pass Infinity . |
To dismiss an existing toast you can simple pass null
setToast(null);
id
The unique id of the excalidraw component. This can be used to identify the excalidraw component, for example importing the library items to the excalidraw component from where it was initiated when you have multiple excalidraw components rendered on the same page as shown in multiple excalidraw demo.
getFiles
() => files
This API can be used to get the files present in the scene. It may contain files that aren't referenced by any element, so if you're persisting the files to a storage, you should compare them against stored elements.
setActiveTool
This API has the below signature. It sets the tool
passed in param as the active tool.
(tool:
{ type: SHAPES[number]["value"]| "eraser" } |
{ type: "custom"; customType: string }) => void
setCursor
This API can be used to customise the mouse cursor on the canvas and has the below signature. It sets the mouse cursor to the cursor passed in param.
(cursor: string) => void
toggleMenu
(type: "library" | "customSidebar", force?: boolean) => boolean;
This API can be used to toggle a specific menu (currently only the sidebars), and returns whether the menu was toggled on or off. If the force
flag passed, it will force the menu to be toggled either on/off based on the boolean
passed.
This API is especially useful when you render a custom sidebar using renderSidebar
prop, and you want to toggle it from your app based on a user action.
resetCursor
() => void
This API can be used to reset to default mouse cursor.