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.

improvement: scroll table with keyboard navigation (#5595)

Myles Scolnick0031561b7941
diff --git a/frontend/src/components/data-table/range-focus/cell-selection-indicator.tsx b/frontend/src/components/data-table/range-focus/cell-selection-indicator.tsx
index 90a90d06ce..4015ad0503 100644
--- a/frontend/src/components/data-table/range-focus/cell-selection-indicator.tsx
+++ b/frontend/src/components/data-table/range-focus/cell-selection-indicator.tsx
@@ -22,6 +22,7 @@ export const CellRangeSelectionIndicator = memo<CellSelectionIndicatorProps>(
return (
<div
+ data-cell-id={cellId}
className={cn(
"absolute inset-0 pointer-events-none",
isSelected && "bg-[var(--green-3)]",
diff --git a/frontend/src/components/data-table/range-focus/use-scroll-into-view.ts b/frontend/src/components/data-table/range-focus/use-scroll-into-view.ts
new file mode 100644
index 0000000000..dc00d27f38
--- /dev/null
+++ b/frontend/src/components/data-table/range-focus/use-scroll-into-view.ts
@@ -0,0 +1,46 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { atom } from "jotai";
+import { useJotaiEffect } from "@/core/state/jotai";
+import { Logger } from "@/utils/Logger";
+import { cellSelectionStateAtom } from "./atoms";
+
+const focusedCellAtom = atom((get) => get(cellSelectionStateAtom).focusedCell);
+
+/**
+ * Hook that automatically scrolls the focused cell into view when it changes.
+ * This ensures that when users navigate with keyboard or select cells,
+ * the focused cell remains visible in scrollable tables.
+ */
+export function useScrollIntoViewOnFocus(
+ root: React.RefObject<HTMLElement | null>,
+) {
+ useJotaiEffect(focusedCellAtom, (focusedCell) => {
+ if (!focusedCell?.cellId) {
+ return;
+ }
+
+ const scroll = () => {
+ // Check if this cell contains the CellRangeSelectionIndicator with our cellId
+ const indicator = root.current?.querySelector(
+ `[data-cell-id="${focusedCell.cellId}"]`,
+ );
+ if (!indicator) {
+ Logger.warn(
+ "[ScrollIntoView] Could not find cell with ID:",
+ focusedCell.cellId,
+ );
+ return;
+ }
+
+ indicator.scrollIntoView({
+ behavior: "auto",
+ block: "nearest",
+ inline: "nearest",
+ });
+ };
+
+ // Small delay to ensure DOM updates have completed
+ setTimeout(scroll, 0);
+ });
+}
diff --git a/frontend/src/components/data-table/renderers.tsx b/frontend/src/components/data-table/renderers.tsx
index 4656d0a8ea..3484ff0d28 100644
--- a/frontend/src/components/data-table/renderers.tsx
+++ b/frontend/src/components/data-table/renderers.tsx
@@ -11,7 +11,7 @@ import {
type Table,
type Table as TanStackTable,
} from "@tanstack/react-table";
-import type { JSX } from "react";
+import { type JSX, useRef } from "react";
import {
TableBody,
TableCell,
@@ -22,6 +22,7 @@ import {
import { cn } from "@/utils/cn";
import { CellRangeSelectionIndicator } from "./range-focus/cell-selection-indicator";
import { useCellRangeSelection } from "./range-focus/use-cell-range-selection";
+import { useScrollIntoViewOnFocus } from "./range-focus/use-scroll-into-view";
export function renderTableHeader<TData>(
table: Table<TData>,
@@ -81,6 +82,10 @@ export const DataTableBody = <TData,>({
getRowIndex,
viewedRowIdx,
}: DataTableBodyProps<TData>) => {
+ // Automatically scroll focused cells into view
+ const tableRef = useRef<HTMLTableSectionElement>(null);
+ useScrollIntoViewOnFocus(tableRef);
+
const {
handleCellMouseDown,
handleCellMouseUp,
@@ -130,7 +135,7 @@ export const DataTableBody = <TData,>({
};
return (
- <TableBody onKeyDown={handleCellsKeyDown}>
+ <TableBody onKeyDown={handleCellsKeyDown} ref={tableRef}>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => {
// Only find the row index if the row viewer panel is open