perf: prevent expensive excalidraw re-renders on keystroke

Wraps `onExcalidrawAPI` in `useCallback` to prevent the heavy Excalidraw canvas component from re-rendering on every keystroke when typing in the code block editor.
Wraps `DrawingSidebar` in `memo` and utilizes stable callbacks passed from `App.tsx` instead of inline functions to prevent unnecessary list re-renders.
This commit is contained in:
ruinivist
2026-06-05 08:11:35 +00:00
parent 01300c7061
commit b8ea606f92
3 changed files with 28 additions and 21 deletions
+4
View File
@@ -0,0 +1,4 @@
## 2024-06-25 - Excalidraw Memoization
**Learning:** The `@excalidraw/excalidraw` package's `Excalidraw` component is exceptionally expensive to re-render. Even though `EditorCanvas` was wrapped in `React.memo`, passing an inline arrow function to `onExcalidrawAPI` in the parent `App` broke memoization, causing severe input lag when typing in completely independent UI elements like the codeblock editor sidebar due to the entire canvas re-rendering.
**Action:** When passing callbacks to heavy third-party components like Excalidraw, always wrap them in `useCallback` hook to preserve their prop stability and maintain `React.memo` benefits, preventing disastrous performance regressions on typing/input.
+9 -7
View File
@@ -307,6 +307,10 @@ function PrivateApp() {
[], [],
); );
const handleExcalidrawAPI = useCallback((api: ExcalidrawImperativeAPI) => {
excalidrawApiRef.current = api;
}, []);
return ( return (
<div className="app-shell" ref={appShellRef}> <div className="app-shell" ref={appShellRef}>
{toastMessage && ( {toastMessage && (
@@ -332,9 +336,7 @@ function PrivateApp() {
onSceneChange={handleSceneChange} onSceneChange={handleSceneChange}
onSelectionStateChange={handleCodeBlockSelectionChange} onSelectionStateChange={handleCodeBlockSelectionChange}
onEditorActivity={scheduleThemeTokenSync} onEditorActivity={scheduleThemeTokenSync}
onExcalidrawAPI={(api) => { onExcalidrawAPI={handleExcalidrawAPI}
excalidrawApiRef.current = api;
}}
renderEmbeddable={renderCodeBlockEmbeddable} renderEmbeddable={renderCodeBlockEmbeddable}
/> />
<CodeBlockSidebar <CodeBlockSidebar
@@ -357,12 +359,12 @@ function PrivateApp() {
onClose={closeSidebar} onClose={closeSidebar}
onCreate={handleCreateDrawing} onCreate={handleCreateDrawing}
onSelect={handleSelectDrawing} onSelect={handleSelectDrawing}
onDelete={(drawingId) => void deleteDrawing(drawingId)} onDelete={deleteDrawing}
onTitleChange={setActiveTitle} onTitleChange={setActiveTitle}
onTitleSubmit={() => void submitTitle()} onTitleSubmit={submitTitle}
onPublicationSlugChange={setPublicationSlug} onPublicationSlugChange={setPublicationSlug}
onPublish={() => void publishPublication()} onPublish={publishPublication}
onDisablePublication={() => void disablePublication()} onDisablePublication={disablePublication}
/> />
</div> </div>
); );
+15 -14
View File
@@ -1,3 +1,4 @@
import { memo } from "react";
import { type DrawingMeta, type DrawingPublication } from "../../core/shared"; import { type DrawingMeta, type DrawingPublication } from "../../core/shared";
type DrawingSidebarProps = { type DrawingSidebarProps = {
@@ -9,14 +10,14 @@ type DrawingSidebarProps = {
publicationSlug: string; publicationSlug: string;
publicationBusy: boolean; publicationBusy: boolean;
onClose: () => void; onClose: () => void;
onCreate: () => void; onCreate: () => void | Promise<void>;
onSelect: (drawingId: string) => void; onSelect: (drawingId: string) => void | Promise<void>;
onDelete: (drawingId: string) => void; onDelete: (drawingId: string) => void | Promise<void>;
onTitleChange: (title: string) => void; onTitleChange: (title: string) => void;
onTitleSubmit: () => void; onTitleSubmit: () => void | Promise<void>;
onPublicationSlugChange: (slug: string) => void; onPublicationSlugChange: (slug: string) => void;
onPublish: () => void; onPublish: () => void | Promise<void>;
onDisablePublication: () => void; onDisablePublication: () => void | Promise<void>;
}; };
function DrawerIcon() { function DrawerIcon() {
@@ -64,7 +65,7 @@ export function DrawingsToggle({ onClick }: { onClick: () => void }) {
); );
} }
export function DrawingSidebar({ export const DrawingSidebar = memo(function DrawingSidebar({
open, open,
drawings, drawings,
activeId, activeId,
@@ -133,7 +134,7 @@ export function DrawingSidebar({
className="title-input" className="title-input"
value={activeTitle} value={activeTitle}
onChange={(event) => onTitleChange(event.target.value)} onChange={(event) => onTitleChange(event.target.value)}
onBlur={onTitleSubmit} onBlur={() => void onTitleSubmit()}
onKeyDown={(event) => { onKeyDown={(event) => {
if (event.key === "Enter") { if (event.key === "Enter") {
event.currentTarget.blur(); event.currentTarget.blur();
@@ -143,7 +144,7 @@ export function DrawingSidebar({
<button <button
type="button" type="button"
className="icon-button drawing-delete-button" className="icon-button drawing-delete-button"
onClick={() => onDelete(drawing.id)} onClick={() => void onDelete(drawing.id)}
aria-label={`Delete ${drawing.title}`} aria-label={`Delete ${drawing.title}`}
> >
× ×
@@ -171,7 +172,7 @@ export function DrawingSidebar({
<button <button
type="button" type="button"
className="secondary-button" className="secondary-button"
onClick={onPublish} onClick={() => void onPublish()}
disabled={publicationBusy} disabled={publicationBusy}
> >
{publication.enabled ? "Update link" : "Publish"} {publication.enabled ? "Update link" : "Publish"}
@@ -179,7 +180,7 @@ export function DrawingSidebar({
<button <button
type="button" type="button"
className="secondary-button" className="secondary-button"
onClick={onDisablePublication} onClick={() => void onDisablePublication()}
disabled={!publication.enabled || publicationBusy} disabled={!publication.enabled || publicationBusy}
> >
Unpublish Unpublish
@@ -205,7 +206,7 @@ export function DrawingSidebar({
<button <button
type="button" type="button"
className="drawing-link" className="drawing-link"
onClick={() => onSelect(drawing.id)} onClick={() => void onSelect(drawing.id)}
> >
<span className="drawing-title">{drawing.title}</span> <span className="drawing-title">{drawing.title}</span>
</button> </button>
@@ -214,7 +215,7 @@ export function DrawingSidebar({
<button <button
type="button" type="button"
className="icon-button" className="icon-button"
onClick={() => onDelete(drawing.id)} onClick={() => void onDelete(drawing.id)}
aria-label={`Delete ${drawing.title}`} aria-label={`Delete ${drawing.title}`}
> >
× ×
@@ -226,4 +227,4 @@ export function DrawingSidebar({
</aside> </aside>
</> </>
); );
} });