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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: improve scroll position handling when toggling between edit and app mode (#7406)
Myles Scolnicka84a51ace8cf
diff --git a/frontend/src/components/editor/notebook-cell.tsx b/frontend/src/components/editor/notebook-cell.tsxindex de8cf361a3..da2a276787 100644--- a/frontend/src/components/editor/notebook-cell.tsx+++ b/frontend/src/components/editor/notebook-cell.tsx@@ -29,6 +29,7 @@ import { outputIsLoading, outputIsStale } from "@/core/cells/cell";import { isOutputEmpty } from "@/core/cells/outputs";import { autocompletionKeymap } from "@/core/codemirror/cm";import type { LanguageAdapterType } from "@/core/codemirror/language/types";+import { CSSClasses } from "@/core/constants";import { canCollapseOutline } from "@/core/dom/outline";import { isErrorMime } from "@/core/mime";import type { AppMode } from "@/core/mode";@@ -349,7 +350,7 @@ const ReadonlyCellComponent = forwardRef(<OutputAreaallowExpand={false}forceExpand={true}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}output={cellRuntime.output}stale={outputIsStale(cellRuntime, cellData.edited)}@@ -508,7 +509,7 @@ const EditableCellComponent = ({allowExpand={true}// Force expand when markdown is hiddenforceExpand={isMarkdownCodeHidden}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}output={cellRuntime.output}stale={isStaleCell}@@ -1168,7 +1169,7 @@ const SetupCellComponent = ({<OutputAreaallowExpand={true}forceExpand={true}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}output={cellRuntime.output}stale={false}diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsxindex 69680673e0..a27e083e67 100644--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx@@ -29,7 +29,7 @@ import { isOutputEmpty } from "@/core/cells/outputs";import type { CellData, CellRuntimeState } from "@/core/cells/types";import { MarkdownLanguageAdapter } from "@/core/codemirror/language/languages/markdown";import { useResolvedMarimoConfig } from "@/core/config/config";-import { KnownQueryParams } from "@/core/constants";+import { CSSClasses, KnownQueryParams } from "@/core/constants";import type { OutputMessage } from "@/core/kernel/messages";import { showCodeInRunModeAtom } from "@/core/meta/state";import { isErrorMime } from "@/core/mime";@@ -339,7 +339,7 @@ const VerticalCell = memo(<OutputAreaallowExpand={true}output={output}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}stale={outputStale}loading={loading}@@ -394,7 +394,7 @@ const VerticalCell = memo(<OutputAreaallowExpand={mode === "edit"}output={output}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}stale={outputStale}loading={loading}diff --git a/frontend/src/components/scratchpad/scratchpad.tsx b/frontend/src/components/scratchpad/scratchpad.tsxindex 56b8d185e4..b39a2803d2 100644--- a/frontend/src/components/scratchpad/scratchpad.tsx+++ b/frontend/src/components/scratchpad/scratchpad.tsx@@ -17,6 +17,7 @@ import { HTMLCellId, SCRATCH_CELL_ID } from "@/core/cells/ids";import { DEFAULT_CELL_NAME } from "@/core/cells/names";import type { LanguageAdapterType } from "@/core/codemirror/language/types";import { useResolvedMarimoConfig } from "@/core/config/config";+import { CSSClasses } from "@/core/constants";import { useRequestClient } from "@/core/network/requests";import type { CellConfig } from "@/core/network/types";import { LazyAnyLanguageCodeMirror } from "@/plugins/impl/code/LazyAnyLanguageCodeMirror";@@ -149,7 +150,7 @@ export const ScratchPad: React.FC = () => {<OutputAreaallowExpand={false}output={output}- className="output-area"+ className={CSSClasses.outputArea}cellId={cellId}stale={false}loading={false}diff --git a/frontend/src/core/constants.ts b/frontend/src/core/constants.tsindex e1728c4a17..d9696e1f5d 100644--- a/frontend/src/core/constants.ts+++ b/frontend/src/core/constants.ts@@ -50,3 +50,13 @@ export const KnownQueryParams = {*/showChrome: "show-chrome",};++/**+ * CSS class names used throughout the application+ */+export const CSSClasses = {+ /**+ * Class name for cell output areas+ */+ outputArea: "output-area",+};diff --git a/frontend/src/core/layout/useTogglePresenting.ts b/frontend/src/core/layout/useTogglePresenting.tsindex cfc5ed2546..fba130962b 100644--- a/frontend/src/core/layout/useTogglePresenting.ts+++ b/frontend/src/core/layout/useTogglePresenting.ts@@ -2,47 +2,91 @@import { useSetAtom } from "jotai";import { useCallback } from "react";+import { Logger } from "@/utils/Logger";import { type CellId, HTMLCellId } from "../cells/ids";+import { CSSClasses } from "../constants";import { toggleAppMode, viewStateAtom } from "../mode";+interface ScrollAnchor {+ cellId: CellId;+}++function findScrollAnchor(): ScrollAnchor | null {+ const outputAreas = document.getElementsByClassName(CSSClasses.outputArea);++ for (const elem of Array.from(outputAreas)) {+ const rect = elem.getBoundingClientRect();++ // Find first visible output area+ if (rect.bottom > 0 && rect.top < window.innerHeight) {+ const cellEl = HTMLCellId.findElement(elem);+ if (!cellEl) {+ Logger.warn("Could not find HTMLCellId for visible output area", elem);+ continue;+ }+ return {+ cellId: HTMLCellId.parse(cellEl.id),+ };+ }+ }++ Logger.warn("No visible output area found for scroll anchor");+ return null;+}++function restoreScrollPosition(anchor: ScrollAnchor | null): void {+ if (!anchor) {+ Logger.warn("No scroll anchor provided to restore scroll position");+ return;+ }++ // Find the cell element+ const cellElement = document.getElementById(HTMLCellId.create(anchor.cellId));+ if (!cellElement) {+ Logger.warn(+ "Could not find cell element to restore scroll position",+ anchor.cellId,+ );+ return;+ }++ // Find its output area+ const outputArea = cellElement.querySelector(`.${CSSClasses.outputArea}`);+ if (!outputArea) {+ Logger.warn(+ "Could not find output area to restore scroll position",+ anchor.cellId,+ );+ return;+ }++ // Adjust scroll to restore visual position+ cellElement.scrollIntoView({ block: "start", behavior: "auto" });+}+/*** Toggle the notebook's presentation state and scroll to current visible cell*/export function useTogglePresenting() {const setViewState = useSetAtom(viewStateAtom);- // Toggle the array's presenting state, and sets a cell to scroll to+ // Toggle the array's presenting state and preserve scroll positionconst togglePresenting = useCallback(() => {- const outputAreas = document.getElementsByClassName("output-area");- const viewportEnd =- window.innerHeight || document.documentElement.clientHeight;- let cellAnchor: CellId | null = null;-- // Find the first output area that is visible- // eslint-disable-next-line unicorn/prefer-spread- for (const elem of Array.from(outputAreas)) {- const rect = elem.getBoundingClientRect();- if (- (rect.top >= 0 && rect.top <= viewportEnd) ||- (rect.bottom >= 0 && rect.bottom <= viewportEnd)- ) {- cellAnchor = HTMLCellId.parse(- (elem.parentNode as HTMLElement).id as HTMLCellId,- );- break;- }- }+ // Capture scroll anchor BEFORE toggle+ const scrollAnchor = findScrollAnchor();+ // Toggle the modesetViewState((prev) => ({mode: toggleAppMode(prev.mode),- cellAnchor: cellAnchor,+ cellAnchor: scrollAnchor?.cellId ?? null,}));+ // Restore scroll position AFTER DOM updates+ // Double RAF ensures React commits changes and browser completes layoutrequestAnimationFrame(() => {- if (cellAnchor === null) {- return;- }- document.getElementById(HTMLCellId.create(cellAnchor))?.scrollIntoView();+ requestAnimationFrame(() => {+ restoreScrollPosition(scrollAnchor);+ });});}, [setViewState]);