Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: add ref handling for useEventListener (#2939)

Shahmir Varqha9768c2693935
diff --git a/frontend/src/components/debugger/debugger-code.tsx b/frontend/src/components/debugger/debugger-code.tsx
index 7c48743dfe..319eb25458 100644
--- a/frontend/src/components/debugger/debugger-code.tsx
+++ b/frontend/src/components/debugger/debugger-code.tsx
@@ -79,7 +79,7 @@ const DebuggerInput: React.FC<{
const ref = React.useRef<HTMLDivElement>(null);
// Capture some events to prevent default behavior
- useKeydownOnElement(ref.current, {
+ useKeydownOnElement(ref, {
ArrowUp: Functions.NOOP,
ArrowDown: Functions.NOOP,
});
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 50fb4752d5..c2a16185ae 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -352,7 +352,7 @@ const CellComponent = (
// Register hotkeys on the cell instead of the code editor
// This is in case the code editor is hidden
- useHotkeysOnElement(editing ? cellRef.current : null, {
+ useHotkeysOnElement(editing ? cellRef : null, {
"cell.run": handleRun,
"cell.runAndNewBelow": () => {
handleRun();
@@ -405,7 +405,7 @@ const CellComponent = (
},
});
- useKeydownOnElement(editing ? cellRef.current : null, {
+ useKeydownOnElement(editing ? cellRef : null, {
ArrowDown: () => {
moveToNextCell({ cellId, before: false, noCreate: true });
return true;
diff --git a/frontend/src/hooks/__tests__/useEventListener.test.tsx b/frontend/src/hooks/__tests__/useEventListener.test.tsx
new file mode 100644
index 0000000000..a69296466c
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useEventListener.test.tsx
@@ -0,0 +1,24 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { expect, describe, it } from "vitest";
+import { renderHook } from "@testing-library/react-hooks";
+import { useRef } from "react";
+import { isRefObject } from "../useEventListener";
+
+describe("isRefObject", () => {
+ it("should return true for React ref objects", () => {
+ const { result } = renderHook(() => useRef(null));
+ expect(isRefObject(result)).toBe(true);
+ });
+
+ it("should return false for non-ref values", () => {
+ expect(isRefObject(null)).toBe(false);
+ expect(isRefObject(123)).toBe(false);
+ expect(isRefObject(document.createElement("div"))).toBe(false);
+ expect(isRefObject(document)).toBe(false);
+ expect(isRefObject(window)).toBe(false);
+ });
+
+ it("should return true for objects with 'current' property", () => {
+ expect(isRefObject({ current: document.createElement("div") })).toBe(true);
+ });
+});
diff --git a/frontend/src/hooks/useEventListener.ts b/frontend/src/hooks/useEventListener.ts
index 97738aaf8b..96736b7cc3 100644
--- a/frontend/src/hooks/useEventListener.ts
+++ b/frontend/src/hooks/useEventListener.ts
@@ -1,7 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { useRef, useEffect } from "react";
+import { useRef, useEffect, type RefObject } from "react";
type Target = Document | HTMLElement | Window | null;
+type TargetRef = RefObject<Target>;
+
type EventMap<T extends Target> = T extends Document
? DocumentEventMap
: T extends HTMLElement
@@ -10,8 +12,14 @@ type EventMap<T extends Target> = T extends Document
? WindowEventMap
: never;
+export function isRefObject<T>(
+ target: T | RefObject<T>,
+): target is RefObject<T> {
+ return target !== null && typeof target === "object" && "current" in target;
+}
+
export function useEventListener<T extends Target, K extends keyof EventMap<T>>(
- target: T,
+ targetValue: T | TargetRef,
type: K & string,
listener: (ev: EventMap<T>[K]) => unknown,
options?: boolean | AddEventListenerOptions,
@@ -23,6 +31,9 @@ export function useEventListener<T extends Target, K extends keyof EventMap<T>>(
}, [listener]);
useEffect(() => {
+ // Get the actual target, whether it's from a ref or direct value
+ // We get ref.current inside the effect instead of during render because changes to ref.current will not trigger a re-render
+ const target = isRefObject(targetValue) ? targetValue.current : targetValue;
if (!target) {
return;
}
@@ -34,5 +45,5 @@ export function useEventListener<T extends Target, K extends keyof EventMap<T>>(
return () => {
target.removeEventListener(type, eventListener, options);
};
- }, [type, target, options]);
+ }, [type, targetValue, options]);
}
diff --git a/frontend/src/hooks/useHotkey.ts b/frontend/src/hooks/useHotkey.ts
index 8c9452ad6e..42ab5bf50b 100644
--- a/frontend/src/hooks/useHotkey.ts
+++ b/frontend/src/hooks/useHotkey.ts
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { useEffect } from "react";
+import { type RefObject, useEffect } from "react";
import { parseShortcut } from "../core/hotkeys/shortcuts";
import { useEventListener } from "./useEventListener";
@@ -54,10 +54,10 @@ export function useHotkey(shortcut: HotkeyAction, callback: HotkeyHandler) {
}
/**
- * Registers a hotkey listener on a given element.
+ * Registers a hotkey listener on a given element or ref to an element.
*/
export function useHotkeysOnElement<T extends HotkeyAction>(
- element: HTMLElement | null,
+ element: HTMLElement | RefObject<HTMLElement> | null,
handlers: Record<T, HotkeyHandler>,
) {
const hotkeys = useAtomValue(hotkeysAtom);
@@ -78,10 +78,10 @@ export function useHotkeysOnElement<T extends HotkeyAction>(
}
/**
- * Registers a hotkey listener on a given element.
+ * Registers a hotkey listener on a given element or ref to an element.
*/
export function useKeydownOnElement(
- element: HTMLElement | null,
+ element: HTMLElement | RefObject<HTMLElement> | null,
handlers: Record<string, HotkeyHandler>,
) {
useEventListener(element, "keydown", (e) => {