diff options
| author | kj_sh604 | 2026-03-15 16:19:35 -0400 |
|---|---|---|
| committer | kj_sh604 | 2026-03-15 16:19:35 -0400 |
| commit | bc297e5e496d9f48ef77581b7fb41fdf328a62cf (patch) | |
| tree | 66192466eef76ee4c5cf71a9788ae9fe947514d4 /dev-docs/docs/@excalidraw/excalidraw/api/props | |
| parent | f6538b8f1a78a7d72a41916ac79376f8c2d30193 (diff) | |
refactor: dev-docs/
Diffstat (limited to 'dev-docs/docs/@excalidraw/excalidraw/api/props')
5 files changed, 931 insertions, 0 deletions
diff --git a/dev-docs/docs/@excalidraw/excalidraw/api/props/excalidraw-api.mdx b/dev-docs/docs/@excalidraw/excalidraw/api/props/excalidraw-api.mdx new file mode 100644 index 0000000..b7a3bab --- /dev/null +++ b/dev-docs/docs/@excalidraw/excalidraw/api/props/excalidraw-api.mdx @@ -0,0 +1,455 @@ +# excalidrawAPI + +<pre> + (api:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L616"> + ExcalidrawAPI + </a> + ) => void; +</pre> + +Once the callback is triggered, you will need to store the api in state to access it later. + +```jsx showLineNumbers +export default function App() { + const [excalidrawAPI, setExcalidrawAPI] = useState(null); + return <Excalidraw excalidrawAPI={(api)=> setExcalidrawAPI(api)} />; +} +``` + +You can use this prop when you want to access some [Excalidraw APIs](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L616). We expose the below APIs :point_down: + +| API | Signature | Usage | +| --- | --- | --- | +| [updateScene](#updatescene) | `function` | updates the scene with the sceneData | +| [updateLibrary](#updatelibrary) | `function` | updates the library | +| [addFiles](#addfiles) | `function` | add files data to the appState | +| [resetScene](#resetscene) | `function` | Resets the scene. If `resetLoadingState` is passed as true then it will also force set the loading state to false. | +| [getSceneElementsIncludingDeleted](#getsceneelementsincludingdeleted) | `function` | Returns all the elements including the deleted in the scene | +| [getSceneElements](#getsceneelements) | `function` | Returns all the elements excluding the deleted in the scene | +| [getAppState](#getappstate) | `function` | Returns current appState | +| [history](#history) | `object` | This is the history API. `history.clear()` will clear the history | +| [scrollToContent](#scrolltocontent) | `function` | Scroll the nearest element out of the elements supplied to the center. Defaults to the elements on the scene. | +| [refresh](#refresh) | `function` | Updates the offsets for the Excalidraw component so that the coordinates are computed correctly (for example the cursor position). | +| [setToast](#settoast) | `function` | This API can be used to show the toast with custom message. | +| [id](#id) | `string` | Unique ID for the excalidraw component. | +| [getFiles](#getfiles) | `function` | This API can be used to get the files present in the scene. | +| [setActiveTool](#setactivetool) | `function` | This API can be used to set the active tool | +| [setCursor](#setcursor) | `function` | This API can be used to set customise the mouse cursor on the canvas | +| [resetCursor](#resetcursor) | `function` | This API can be used to reset to default mouse cursor on the canvas | +| [toggleSidebar](#toggleSidebar) | `function` | Toggles specific sidebar on/off | +| [onChange](#onChange) | `function` | Subscribes to change events | +| [onPointerDown](#onPointerDown) | `function` | Subscribes to `pointerdown` events | +| [onPointerUp](#onPointerUp) | `function` | Subscribes to `pointerup` events | + +:::info The `Ref` support has been removed in v0.17.0 so if you are using refs, please update the integration to use the `excalidrawAPI`. + +Additionally `ready` and `readyPromise` from the API have been discontinued. These APIs were found to be superfluous, and as part of the effort to streamline the APIs and maintain simplicity, they were removed in version v0.17.0. + +::: + +## updateScene + +<pre> + (scene:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L339"> + sceneData + </a> + ) => void +</pre> + +You can use this function to update the scene with the sceneData. It accepts the below attributes. + +| Name | Type | Description | +| --- | --- | --- | +| `elements` | [`ImportedDataState["elements"]`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/data/types.ts#L38) | The `elements` to be updated in the scene | +| `appState` | [`ImportedDataState["appState"]`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/data/types.ts#L39) | The `appState` to be updated in the scene. | +| `collaborators` | <code>Map<string, <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L37">Collaborator></a></code> | The list of collaborators to be updated in the scene. | +| `captureUpdate` | [`CaptureUpdateAction`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/store.ts#L40) | Controls which updates should be captured by the `Store`. Captured updates are emmitted and listened to by other components, such as `History` for undo / redo purposes. | + +```jsx live +function App() { + const updateScene = () => { + const sceneData = { + elements: [ + { + type: "rectangle", + version: 141, + versionNonce: 361174001, + isDeleted: false, + id: "oDVXy8D6rom3H1-LLH2-f", + fillStyle: "hachure", + strokeWidth: 1, + strokeStyle: "solid", + roughness: 1, + opacity: 100, + angle: 0, + x: 100.50390625, + y: 93.67578125, + strokeColor: "#c92a2a", + backgroundColor: "transparent", + width: 186.47265625, + height: 141.9765625, + seed: 1968410350, + groupIds: [], + boundElements: null, + locked: false, + link: null, + updated: 1, + roundness: { + type: 3, + value: 32, + }, + }, + ], + appState: { + viewBackgroundColor: "#edf2ff", + }, + captureUpdate: CaptureUpdateAction.IMMEDIATELY, + }; + excalidrawAPI.updateScene(sceneData); + }; + const [excalidrawAPI, setExcalidrawAPI] = useState(null); + return ( + <div style={{ height: "500px" }}> + <p style={{ fontSize: "16px" }}> Click to update the scene</p> + <button className="custom-button" onClick={updateScene}> + Update Scene + </button> + <Excalidraw excalidrawAPI={(api) => setExcalidrawAPI(api)} /> + </div> + ); +} +``` + +#### captureUpdate + +You can use the `captureUpdate` to influence undo / redo behaviour. + +> **NOTE**: Some updates are not observed by the store / history - i.e. updates to `collaborators` object or parts of `AppState` which are not observed (not `ObservedAppState`). Such updates will never make it to the undo / redo stacks, regardless of the passed `captureUpdate` value. + +| | `captureUpdate` value | Notes | +| --- | --- | --- | +| _Immediately undoable_ | `CaptureUpdateAction.IMMEDIATELY` | Use for updates which should be captured. Should be used for most of the local updates. These updates will _immediately_ make it to the local undo / redo stacks. | +| _Eventually undoable_ | `CaptureUpdateAction.EVENTUALLY` | Use for updates which should not be captured immediately - likely exceptions which are part of some async multi-step process. Otherwise, all such updates would end up being captured with the next `CaptureUpdateAction.IMMEDIATELY` - triggered either by the next `updateScene` or internally by the editor. These updates will _eventually_ make it to the local undo / redo stacks. | +| _Never undoable_ | `CaptureUpdateAction.NEVER` | Use for updates which should never be recorded, such as remote updates or scene initialization. These updates will _never_ make it to the local undo / redo stacks. | + +### updateLibrary + +<pre> + (opts: { <br /> libraryItems:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L249"> + LibraryItemsSource + </a> + ;<br /> merge?: boolean; <br /> prompt?: boolean; + <br /> openLibraryMenu?: boolean; + <br /> defaultStatus?: "unpublished" | "published"; <br /> }) => Promise< + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L246"> + LibraryItems + </a> + > +</pre> + +You can use this function to update the library. It accepts the below attributes. + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| `libraryItems` | [LibraryItemsSource](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L249) | \_ | 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` | <code>"unpublished" | "published"</code> | `"unpublished"` | Default library item's `status` if not present. | + +```tsx live +function App() { + const [excalidrawAPI, setExcalidrawAPI] = useState(null); + + useEffect(() => { + if (!excalidrawAPI) { + return; + } + // to open the library sidebar + excalidrawAPI.updateScene({ appState: { openSidebar: "library" } }); + }, [excalidrawAPI]); + + return ( + <div style={{ height: "500px" }}> + <p style={{ fontSize: "16px" }}> Click to update the library items</p> + <button + className="custom-button" + onClick={() => { + const libraryItems = [ + { + status: "published", + id: "1", + created: 1, + elements: initialData.libraryItems[1], + }, + { + status: "unpublished", + id: "2", + created: 2, + elements: initialData.libraryItems[1], + }, + ]; + excalidrawAPI.updateLibrary({ + libraryItems, + openLibraryMenu: true, + }); + }} + > + Update Library + </button> + <Excalidraw + excalidrawAPI={(api) => setExcalidrawAPI(api)} + // initial data retrieved from https://github.com/excalidraw/excalidraw/blob/master/dev-docs/packages/excalidraw/initialData.js + initialData={{ + libraryItems: initialData.libraryItems, + appState: { openSidebar: "library" }, + }} + /> + </div> + ); +} +``` + +### addFiles + +<pre> + (files:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L59"> + BinaryFileData + </a> + ) => void +</pre> + +Adds supplied files data to the `appState.files` cache on top of existing files present in the cache. + +## resetScene + +```tsx +(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 + +<pre> + () =>{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L115"> + ExcalidrawElement[] + </a> +</pre> + +Returns all the elements including the deleted in the scene. + +## getSceneElements + +<pre> + () => NonDeleted< + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L115"> + ExcalidrawElement + </a> + []> +</pre> + +Returns all the elements excluding the deleted in the scene + +## getAppState + +<pre> + () =>{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95"> + AppState + </a> +</pre> + +Returns current appState. + +## history + +```tsx +{ + clear: () => void +} +``` + +This is the history API. history.clear() will clear the history. + +## scrollToContent + +```tsx +( + 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](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L115) | [ExcalidrawElement[]](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L115) | 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 + +```tsx +() => 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. + +```tsx +({ 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` + +```js +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](https://codesandbox.io/s/multiple-excalidraw-k1xx5). + +## getFiles + +<pre> + () =>{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L82"> + files + </a> +</pre> + +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. + +```ts +( + tool: ( + | ( + | { type: Exclude<ToolType, "image"> } + | { + type: Extract<ToolType, "image">; + insertOnCanvasDirectly?: boolean; + } + ) + | { type: "custom"; customType: string } + ) & { locked?: boolean }, +) => {}; +``` + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| `type` | [ToolType](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L91) | `selection` | The tool type which should be set as active tool. When setting `image` as active tool, the insertion onto canvas when using image tool is disabled by default, so you can enable it by setting `insertOnCanvasDirectly` to `true` | +| `locked` | `boolean` | `false` | Indicates whether the the active tool should be locked. It behaves the same way when using the `lock` tool in the editor interface | + +## 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. + +```tsx +(cursor: string) => void +``` + +## toggleSidebar + +```tsx +(opts: { name: string; tab?: string; force?: boolean }) => boolean; +``` + +This API can be used to toggle sidebar, optionally opening a specific sidebar tab. It returns whether the sidebar was toggled on or off. If the `force` flag passed, it will force the sidebar to be toggled either on/off. + +This API is especially useful when you render a custom [`<Sidebar/>`](/docs/@excalidraw/excalidraw/api/children-components/sidebar), and you want to toggle it from your app based on a user action. + +## resetCursor + +```tsx +() => void +``` + +This API can be used to reset to default mouse cursor. + +## onChange + +```tsx +( + callback: ( + elements: readonly ExcalidrawElement[], + appState: AppState, + files: BinaryFiles, + ) => void +) => () => void +``` + +Subscribes to change events, similar to [`props.onChange`](/docs/@excalidraw/excalidraw/api/props#onchange). + +Returns an unsubscribe function. + +## onPointerDown + +```tsx +( + callback: ( + activeTool: AppState["activeTool"], + pointerDownState: PointerDownState, + event: React.PointerEvent<HTMLElement>, + ) => void, +) => () => void +``` + +Subscribes to canvas `pointerdown` events. + +Returns an unsubscribe function. + +## onPointerUp + +```tsx +( + callback: ( + activeTool: AppState["activeTool"], + pointerDownState: PointerDownState, + event: PointerEvent, + ) => void, +) => () => void +``` + +Subscribes to canvas `pointerup` events. + +Returns an unsubscribe function. diff --git a/dev-docs/docs/@excalidraw/excalidraw/api/props/initialdata.mdx b/dev-docs/docs/@excalidraw/excalidraw/api/props/initialdata.mdx new file mode 100644 index 0000000..0fec6ea --- /dev/null +++ b/dev-docs/docs/@excalidraw/excalidraw/api/props/initialdata.mdx @@ -0,0 +1,55 @@ +# initialData + +<pre> +{ elements?: <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L114">ExcalidrawElement[]</a>, appState?: <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95">AppState</a> } +</pre> + +This helps to load Excalidraw with `initialData`. It must be an object or a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) which resolves to an object containing the below optional fields. + +| Name | Type | Description | +| --- | --- | --- | +| `elements` | [ExcalidrawElement[]](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L114) | The `elements` with which `Excalidraw` should be mounted. | +| `appState` | [AppState](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95) | The `AppState` with which `Excalidraw` should be mounted. | +| `scrollToContent` | `boolean` | This attribute indicates whether to `scroll` to the nearest element to center once `Excalidraw` is mounted. By default, it will not scroll the nearest element to the center. Make sure you pass `initialData.appState.scrollX` and `initialData.appState.scrollY` when `scrollToContent` is false so that scroll positions are retained | +| `libraryItems` | [LibraryItems](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L247) | Promise<[LibraryItems](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L200)> | This library items with which `Excalidraw` should be mounted. | +| `files` | [BinaryFiles](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L82) | The `files` added to the scene. | + +You might want to use this when you want to load excalidraw with some initial elements and app state. + +```jsx live +function App() { + return ( + <div style={{ height: "500px" }}> + <Excalidraw + initialData={{ + elements: [ + { + type: "rectangle", + version: 141, + versionNonce: 361174001, + isDeleted: false, + id: "oDVXy8D6rom3H1-LLH2-f", + fillStyle: "hachure", + strokeWidth: 1, + strokeStyle: "solid", + roughness: 1, + opacity: 100, + angle: 0, + x: 100.50390625, + y: 93.67578125, + strokeColor: "#000000", + backgroundColor: "transparent", + width: 186.47265625, + height: 141.9765625, + seed: 1968410350, + groupIds: [], + }, + ], + appState: { zenModeEnabled: true, viewBackgroundColor: "#a5d8ff" }, + scrollToContent: true + }} + /> + </div> + ); +} +``` diff --git a/dev-docs/docs/@excalidraw/excalidraw/api/props/props.mdx b/dev-docs/docs/@excalidraw/excalidraw/api/props/props.mdx new file mode 100644 index 0000000..e25aedc --- /dev/null +++ b/dev-docs/docs/@excalidraw/excalidraw/api/props/props.mdx @@ -0,0 +1,260 @@ +# Props + +All `props` are _optional_. + +| Name | Type | Default | Description | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | +| [`initialData`](/docs/@excalidraw/excalidraw/api/props/initialdata) | `object` | `null` | <code>Promise<object | null></code> | `null` | The initial data with which app loads. | +| [`excalidrawAPI`](/docs/@excalidraw/excalidraw/api/props/excalidraw-api) | `function` | \_ | Callback triggered with the excalidraw api once rendered | +| [`isCollaborating`](#iscollaborating) | `boolean` | \_ | This indicates if the app is in `collaboration` mode | +| [`onChange`](#onchange) | `function` | \_ | This callback is triggered whenever the component updates due to any change. This callback will receive the excalidraw `elements` and the current `app state`. | +| [`onPointerUpdate`](#onpointerupdate) | `function` | \_ | Callback triggered when mouse pointer is updated. | +| [`onPointerDown`](#onpointerdown) | `function` | \_ | This prop if passed gets triggered on pointer down events | +| [`onScrollChange`](#onscrollchange) | `function` | \_ | This prop if passed gets triggered when scrolling the canvas. | +| [`onPaste`](#onpaste) | `function` | \_ | Callback to be triggered if passed when something is pasted into the scene | +| [`onLibraryChange`](#onlibrarychange) | `function` | \_ | The callback if supplied is triggered when the library is updated and receives the library items. | +| [`generateLinkForSelection`](#generateLinkForSelection) | `function` | \_ | Allows you to override `url` generation when linking to Excalidraw elements. | +| [`onLinkOpen`](#onlinkopen) | `function` | \_ | The callback if supplied is triggered when any link is opened. | +| [`langCode`](#langcode) | `string` | `en` | Language code string to be used in Excalidraw | +| [`renderTopRightUI`](/docs/@excalidraw/excalidraw/api/props/render-props#rendertoprightui) | `function` | \_ | Render function that renders custom UI in top right corner | +| [`renderCustomStats`](/docs/@excalidraw/excalidraw/api/props/render-props#rendercustomstats) | `function` | \_ | Render function that can be used to render custom stats on the stats dialog. | +| [`viewModeEnabled`](#viewmodeenabled) | `boolean` | \_ | This indicates if the app is in `view` mode. | +| [`zenModeEnabled`](#zenmodeenabled) | `boolean` | \_ | This indicates if the `zen` mode is enabled | +| [`gridModeEnabled`](#gridmodeenabled) | `boolean` | \_ | This indicates if the `grid` mode is enabled | +| [`libraryReturnUrl`](#libraryreturnurl) | `string` | \_ | What URL should [libraries.excalidraw.com](https://libraries.excalidraw.com) be installed to | +| [`theme`](#theme) | `"light"` | `"dark"` | `"light"` | The theme of the Excalidraw component | +| [`name`](#name) | `string` | | Name of the drawing | +| [`UIOptions`](/docs/@excalidraw/excalidraw/api/props/ui-options) | `object` | [DEFAULT UI OPTIONS](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/constants.ts#L151) | To customise UI options. Currently we support customising [`canvas actions`](/docs/@excalidraw/excalidraw/api/props/ui-options#canvasactions) | +| [`detectScroll`](#detectscroll) | `boolean` | `true` | Indicates whether to update the offsets when nearest ancestor is scrolled. | +| [`handleKeyboardGlobally`](#handlekeyboardglobally) | `boolean` | `false` | Indicates whether to bind the keyboard events to document. | +| [`autoFocus`](#autofocus) | `boolean` | `false` | Indicates whether to focus the Excalidraw component on page load | +| [`generateIdForFile`](#generateidforfile) | `function` | \_ | Allows you to override `id` generation for files added on canvas | +| [`validateEmbeddable`](#validateEmbeddable) | string[] | `boolean | RegExp | RegExp[] | ((link: string) => boolean | undefined)` | \_ | use for custom src url validation | +| [`renderEmbeddable`](/docs/@excalidraw/excalidraw/api/props/render-props#renderEmbeddable) | `function` | \_ | Render function that can override the built-in `<iframe>` | + +### Storing custom data on Excalidraw elements + +Beyond attributes that Excalidraw elements already support, you can store `custom` data on each `element` in a `customData` object. The type of the attribute is [`Record<string, any>`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L66) and is optional. + +You can use this to add any extra information you need to keep track of. + +You can add `customData` to elements when passing them as [`initialData`](/docs/@excalidraw/excalidraw/api/props/initialdata), or using [`updateScene`](/docs/@excalidraw/excalidraw/api/props/excalidraw-api#updatescene) / [`updateLibrary`](/docs/@excalidraw/excalidraw/api/props/excalidraw-api#updatelibrary) afterwards. + +```js showLineNumbers +{ + type: "rectangle", + id: "oDVXy8D6rom3H1-LLH2-f", + customData: {customId: '162'}, +} +``` + +### isCollaborating + +This prop indicates if the app is in `collaboration` mode. + +### onChange + +Every time component updates, this callback if passed will get triggered and has the below signature. + +```js +(excalidrawElements, appState, files) => void; +``` + +1. `excalidrawElements`: Array of [excalidrawElements](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L114) in the scene. + +2. `appState`: [AppState](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95) of the scene. + +3. `files`: The [BinaryFiles](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L64) which are added to the scene. + +Here you can try saving the data to your backend or local storage for example. + +### onPointerUpdate + +This callback is triggered when mouse pointer is updated. + +```js +({ x, y }, button, pointersMap}) => void; +``` + +1.`{x, y}`: Pointer coordinates + +2.`button`: The position of the button. This will be one of `["down", "up"]` + +3.`pointersMap`: [`pointers`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L131) map of the scene + +```js +(exportedElements, appState, canvas) => void +``` + +1. `exportedElements`: An array of [non deleted elements](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L87) which needs to be exported. +2. `appState`: [AppState](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95) of the scene. +3. `canvas`: The `HTMLCanvasElement` of the scene. + +### onPointerDown + +This prop if passed will be triggered on pointer down events and has the below signature. + +<pre> + (activeTool:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L115"> + {" "} + AppState["activeTool"] + </a> + , pointerDownState: <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L424"> + PointerDownState + </a>) => void +</pre> + +### onScrollChange + +This prop if passed will be triggered when canvas is scrolled and has the below signature. + +```ts +(scrollX: number, scrollY: number) => void +``` + +### onPaste + +This callback is triggered if passed when something is pasted into the scene. You can use this callback in case you want to do something additional when the paste event occurs. + +<pre> + (data:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/clipboard.ts#L18"> + ClipboardData + </a> + , event: ClipboardEvent | null) => boolean +</pre> + +This callback must return a `boolean` value or a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) which resolves to a boolean value. + +In case you want to prevent the excalidraw paste action you must return `false`, it will stop the native excalidraw clipboard management flow (nothing will be pasted into the scene). + +### onLibraryChange + +This callback if supplied will get triggered when the library is updated and has the below signature. + +<pre> + (items:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L200"> + LibraryItems + </a> + ) => void | Promise<any> +</pre> + +It is invoked with empty items when user clears the library. You can use this callback when you want to do something additional when library is updated for example persisting it to local storage. + +### generateLinkForSelection + +This prop if passed will be used to replace the default link generation function. The idea is that the host app can take over the creation of element links, which can be used to navigate to a particular element or a group. If the host app chooses a different key for element link id, then the host app should also take care of the handling and the navigation in `onLinkOpen`. + +```tsx +(id: string, type: "element" | "group") => string; +``` + +### onLinkOpen + +This prop if passed will be triggered when clicked on `link`. To handle the redirect yourself (such as when using your own router for internal links), you must call `event.preventDefault()`. + +<pre> + (element:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/element/types.ts#L114"> + ExcalidrawElement + </a> + , event: CustomEvent<{ nativeEvent: MouseEvent }>) => void +</pre> + +Example: + +```js showLineNumbers +const history = useHistory(); + +// open internal links using the app's router, but opens external links in +// a new tab/window +const onLinkOpen: ExcalidrawProps["onLinkOpen"] = useCallback( + (element, event) => { + const link = element.link; + const { nativeEvent } = event.detail; + const isNewTab = nativeEvent.ctrlKey || nativeEvent.metaKey; + const isNewWindow = nativeEvent.shiftKey; + const isInternalLink = + link.startsWith("/") || link.includes(window.location.origin); + if (isInternalLink && !isNewTab && !isNewWindow) { + history.push(link.replace(window.location.origin, "")); + // signal that we're handling the redirect ourselves + event.preventDefault(); + } + }, + [history], +); +``` + +### langCode + +Determines the `language` of the UI. It should be one of the [available language codes](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/i18n.ts#L14). Defaults to `en` (English). We also export default language and supported languages which you can import as shown below. + +```js +import { defaultLang, languages } from "@excalidraw/excalidraw"; +``` + +| name | type | +| --- | --- | +| `defaultLang` | `string` | +| `languages` | [`Language[]`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/i18n.ts#L15) | + +### viewModeEnabled + +This prop indicates whether the app is in `view mode`. When supplied, the value takes precedence over _intialData.appState.viewModeEnabled_, the `view mode` will be fully controlled by the host app, and users won't be able to toggle it from within the app. + +### zenModeEnabled + +This prop indicates whether the app is in `zen mode`. When supplied, the value takes precedence over _intialData.appState.zenModeEnabled_, the `zen mode` will be fully controlled by the host app, and users won't be able to toggle it from within the app. + +### gridModeEnabled + +This prop indicates whether the shows the grid. When supplied, the value takes precedence over _intialData.appState.gridModeEnabled_, the grid will be fully controlled by the host app, and users won't be able to toggle it from within the app. + +### libraryReturnUrl + +If supplied, this URL will be used when user tries to install a library from [libraries.excalidraw.com](https://libraries.excalidraw.com). Defaults to _window.location.origin + window.location.pathname_. To install the libraries in the same tab from which it was opened, you need to set `window.name` (to any alphanumeric string) — if it's not set it will open in a new tab. + +### theme + +This prop controls Excalidraw's theme. When supplied, the value takes precedence over _intialData.appState.theme_, the theme will be fully controlled by the host app, and users won't be able to toggle it from within the app unless _UIOptions.canvasActions.toggleTheme_ is set to `true`, in which case the `theme` prop will control Excalidraw's default theme with ability to allow theme switching (you must take care of updating the `theme` prop when you detect a change to `appState.theme` from the [onChange](#onchange) callback). + +You can use [`THEME`](/docs/@excalidraw/excalidraw/api/utils#theme) to specify the theme. + +### name + +This prop sets the `name` of the drawing which will be used when exporting the drawing. When supplied, the value takes precedence over _intialData.appState.name_, the `name` will be fully controlled by host app and the users won't be able to edit from within Excalidraw. + +### detectScroll + +Indicates whether Excalidraw should listen for `scroll` event on the nearest scrollable container in the DOM tree and recompute the coordinates (e.g. to correctly handle the cursor) when the component's position changes. You can disable this when you either know this doesn't affect your app or you want to take care of it yourself (calling the [`refresh()`](#ref) method). + +### handleKeyboardGlobally + +Indicates whether to bind keyboard events to `document`. Disabled by default, meaning the keyboard events are bound to the Excalidraw component. This allows for multiple Excalidraw components to live on the same page, and ensures that Excalidraw keyboard handling doesn't collide with your app's (or the browser) when the component isn't focused. + +Enable this if you want Excalidraw to handle keyboard even if the component isn't focused (e.g. a user is interacting with the navbar, sidebar, or similar). + +### autoFocus + +This prop indicates whether to `focus` the Excalidraw component on page load. Defaults to false. + +### generateIdForFile + +Allows you to override `id` generation for files added on canvas (images). By default, an SHA-1 digest of the file is used. + +```tsx +(file: File) => string | Promise<string> +``` + +### validateEmbeddable + +```tsx +validateEmbeddable?: boolean | string[] | RegExp | RegExp[] | ((link: string) => boolean | undefined) +``` + +This is an optional property. By default we support a handful of well-known sites. You may allow additional sites or disallow the default ones by supplying a custom validator. If you pass `true`, all URLs will be allowed. You can also supply a list of hostnames, RegExp (or list of RegExp objects), or a function. If the function returns `undefined`, the built-in validator will be used. + +Supplying a list of hostnames (with or without `www.`) is the preferred way to allow a specific list of domains. diff --git a/dev-docs/docs/@excalidraw/excalidraw/api/props/render-props.mdx b/dev-docs/docs/@excalidraw/excalidraw/api/props/render-props.mdx new file mode 100644 index 0000000..0df7634 --- /dev/null +++ b/dev-docs/docs/@excalidraw/excalidraw/api/props/render-props.mdx @@ -0,0 +1,80 @@ +# Render Props + +## renderTopRightUI + +<pre> + (isMobile: boolean, appState: + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95"> + AppState + </a>) => JSX | null +</pre> + +A function returning `JSX` to render `custom` UI in the top right corner of the app. + +```jsx live +function App() { + return ( + <div style={{ height: "500px" }}> + <Excalidraw + renderTopRightUI={() => { + return ( + <button + style={{ + background: "#70b1ec", + border: "none", + color: "#fff", + width: "max-content", + fontWeight: "bold", + }} + onClick={() => window.alert("This is dummy top right UI")} + > + Click me + </button> + ); + }} + /> + </div> + ); +} +``` + +## renderCustomStats + +A function that can be used to render custom stats (returns JSX) in the `nerd stats` dialog. + + + +For example you can use this prop to render the size of the elements in the storage as do in [excalidraw.com](https://excalidraw.com). + +```jsx live +function App() { + return ( + <div style={{ height: "500px" }}> + <Excalidraw + renderCustomStats={() => ( + <p style={{ color: "#70b1ec", fontWeight: "bold" }}> + Dummy stats will be shown here + </p> + )} + /> + </div> + ); +} +``` + +## renderEmbeddable + +<pre> + (element: NonDeleted<ExcalidrawEmbeddableElement>, appState:{" "} + <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L95"> + AppState + </a> + ) => JSX.Element | null +</pre> + +Allows you to replace the renderer for embeddable elements (which renders `<iframe>` elements). + +| Parameter | Type | Description | +| --- | --- | --- | +| `element` | `NonDeleted<ExcalidrawEmbeddableElement>` | The embeddable element to be rendered. | +| `appState` | `AppState` | The current state of the UI. | diff --git a/dev-docs/docs/@excalidraw/excalidraw/api/props/ui-options.mdx b/dev-docs/docs/@excalidraw/excalidraw/api/props/ui-options.mdx new file mode 100644 index 0000000..9d77e39 --- /dev/null +++ b/dev-docs/docs/@excalidraw/excalidraw/api/props/ui-options.mdx @@ -0,0 +1,81 @@ +# UIOptions + +This prop can be used to customise UI of Excalidraw. Currently we support customising [`canvasActions`](#canvasactions), [`dockedSidebarBreakpoint`](#dockedsidebarbreakpoint) [`welcomeScreen`](#welcmescreen) and [`tools`](#tools). + +<pre> + { + <br /> canvasActions?: <a href="https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/types.ts#L372"> + CanvasActions + </a>, <br /> dockedSidebarBreakpoint?: number, <br /> welcomeScreen?: boolean <br /> + + } +</pre> + +## canvasActions + +This `prop` controls the visibility of the canvas actions inside the `menu`. + +| Prop | Type | Default | Description | +| --- | --- | --- | --- | +| `changeViewBackgroundColor` | `boolean` | `true` | Indicates whether to show `Background color picker`. | +| `clearCanvas` | `boolean` | `true` | Indicates whether to show `Clear canvas` button. | +| `export` | `false` | [`exportOpts`](#exportopts) | `object` | This prop allows to customize the UI inside the export dialog. By default it shows the `save file to disk`. For more details visit [`exportOpts`](#exportopts). | +| `loadScene` | `boolean` | `true` | Indicates whether to show `Load` button. | +| `saveToActiveFile` | `boolean` | `true` | Indicates whether to show `Save` button to save to current file. | +| `toggleTheme` | `boolean` | `null` | `null` | Indicates whether to show `Theme toggle`. When defined as `boolean`, takes precedence over [`props.theme`](/docs/@excalidraw/excalidraw/api/props#theme) to show `Theme toggle`. | +| `saveAsImage` | `boolean` | `true` | Indicates whether to show `Save as image` button. | + +```tsx live +function App() { + const UIOptions = { + canvasActions: { + changeViewBackgroundColor: false, + clearCanvas: false, + loadScene: false, + }, + }; + return ( + <div style={{ height: "500px" }}> + <Excalidraw UIOptions={UIOptions} /> + </div> + ); +} +``` + +### exportOpts + +The below attributes can be set in `UIOptions.canvasActions.export` to customize the export dialog. +If `UIOptions.canvasActions.export` is `false` the export button will not be rendered. + +| Prop | Type | Default | Description | +| --- | --- | --- | --- | +| `saveFileToDisk` | `boolean` | `true` | Indicates whether `save file to disk` button should be shown | +| `onExportToBackend` | `object` | \_ | This callback is triggered when the shareable-link button is clicked in the export dialog. The link button will only be shown if this callback is passed. | +| `renderCustomUI` | `object` | \_ | This callback should be supplied if you want to render custom UI in the export dialog. | + +## dockedSidebarBreakpoint + +This prop indicates at what point should we break to a docked, permanent sidebar. If not passed it defaults to [`MQ_RIGHT_SIDEBAR_MAX_WIDTH_PORTRAIT`](https://github.com/excalidraw/excalidraw/blob/master/packages/excalidraw/constants.ts#L161). +If the _width_ of the _excalidraw_ container exceeds _dockedSidebarBreakpoint_, the sidebar will be `dockable` and the button to `dock` the sidebar will be shown +If user choses to `dock` the sidebar, it will push the right part of the UI towards the left, making space for the sidebar as shown below. + + + +```tsx live +function App() { + return ( + <div style={{ height: "500px" }}> + <Excalidraw UIOptions={{dockedSidebarBreakpoint: 200}}/> + </div> + ); +} +``` + +## tools + +This `prop` controls the visibility of the tools in the editor. +Currently you can control the visibility of `image` tool via this prop. + +| Prop | Type | Default | Description | +| --- | --- | --- | --- | +| image | boolean | true | Decides whether `image` tool should be visible. |
