[GH-ISSUE #2800] [Issue]: Mobile: Characters panel auto-scrolls on reopen (persisted scroll restoration re-fires on AnimatePresence remount) #1424

Closed
opened 2026-10-21 05:33:36 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 22, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2800

Summary

A mobile user reported that opening the agent / persona / character (and "other") side panels auto-scrolls the page, attributing it to autoFocus on the panel search/filter input pulling the focused field into view and popping the soft keyboard on mount.

That described cause is not present in v2.0.0. No panel search input has autoFocus, and no panel runs a focus or scroll effect on mount — so opening Agents, Personas, Connections, Lorebooks, or Presets cannot move the viewport from the code as written.

There is one real mount-time scroll, but it is Characters-only and is a different mechanism than reported: the Characters panel restores a persisted scroll offset on mount, and because the mobile right-panel is unmounted/remounted on every open (AnimatePresence), that restoration re-fires on every reopen. When the saved offset is non-zero, the list visibly jumps away from the top, which reads as an "auto-scroll on open."

This issue corrects the reported root cause, pins the real one, and gives a fix that actually removes the perceived jump (the obvious-looking guard does not).

Symptom & UX impact (mobile)

  • Reported: opening Agents / Personas / Characters panels on mobile auto-scrolls the page and/or opens the keyboard, blamed on search-input autoFocus.
  • Actually reproducible (Characters only): after you've scrolled the character list, reopening the Characters panel snaps the list to a previously saved offset instead of starting at the top. On mobile this looks like the panel "jumping" or auto-scrolling the moment it opens.
  • Not reproducible from code: Agents, Personas, Connections, Lorebooks, Presets — none auto-scroll or auto-focus on open. If the user genuinely saw a jump there, it is most likely a Create*Modal autoFocus name field (see Root cause / alternatives), which needs a recording to confirm.
  • Impact is cosmetic/annoyance-level (no data loss, no broken interaction), hence low severity — but the Characters jump is real and repeatable on mobile.

Steps to reproduce

Characters panel jump (real):

  1. On a mobile viewport, open the Characters panel.
  2. Scroll the character list down.
  3. Open a character's detail, switch panels, or close the panel.
  4. Reopen the Characters panel.
  • Expected: list at the top.
  • Actual: list jumps to the previously saved scroll offset.
  • Note: the very first open with no saved offset shows no jump (characterPanelScrollTop defaults to 0).

Reported "search autoFocus" cause (does NOT repro):

  1. Open Agents or Personas panel on mobile.
  2. Expected per report: page auto-scrolls / keyboard opens because the search input is autoFocus.
  3. Actual: nothing focuses or scrolls; only the framer-motion slide-in of the full-screen overlay plays.

Root cause (exact file:line trace)

1. The reported cause is absent. None of the panel search inputs use autoFocus:

  • packages/client/src/components/panels/AgentsPanel.tsx:847-852 ("Search agents")
  • packages/client/src/components/panels/PersonasPanel.tsx:514-519 ("Search personas")
  • packages/client/src/components/panels/CharactersPanel.tsx:689-694 ('Search characters or -tag:"..."')
  • packages/client/src/components/panels/ConnectionsPanel.tsx:1214-1220 ("Search connections...")
  • packages/client/src/components/panels/LorebooksPanel.tsx:729-735 ("Search lorebooks")
  • packages/client/src/components/panels/PresetsPanel.tsx:820-826 ("Search presets…")

The autoFocus instances that exist (AgentsPanel.tsx:935, PersonasPanel.tsx:695, CharactersPanel.tsx:871, ConnectionsPanel.tsx:804, etc.) are all inside conditionally-rendered rename inputs gated on isEditing/renaming, whose state initializes to null/false (e.g. editingGroupId PersonasPanel.tsx:128, CharactersPanel.tsx:139; editingFolderId AgentsPanel.tsx:247). They are not in the tree at mount and cannot fire on panel open.

2. The open path has no scroll/focus side effect. TopBar.tsx:334 toggleRightPanel(panel) → ui.store.ts:1201 sets { rightPanelOpen, rightPanel } only. On mobile, AppShell.tsx:907-926 renders the panel inside <AnimatePresence mode="wait"> → <motion.aside key="mobile" initial={{x:"100%"}} animate={{x:0}}> (full-screen fixed overlay; no autoFocus, no focus trap).

3. The real mechanism — Characters scroll restoration re-firing on every mobile remount:

  • RightPanel.tsx:60-61 declares a module-level const mountedPanels = new Set<string>() with the comment "Module-level set survives component remounts (e.g. mobile AnimatePresence unmount/remount)." Each panel renders inside its own absolute inset-0 overflow-y-auto container at RightPanel.tsx:113 — the only scroll container.
  • Because AppShell.tsx:908-909 gates the whole <motion.aside> on rightPanelOpen && inside AnimatePresence, the entire RightPanel section unmounts on close and remounts on reopen on mobile.
  • So CharactersPanel's restore effect at CharactersPanel.tsx:435-445 runs on every reopen. At :440 it sets node.scrollTop = Math.min(useUIStore.getState().characterPanelScrollTop, maxScrollTop). Its deps (CharactersPanel.tsx:445: isLoading, parsedGroups.length, sortedCharacters.length, visibleRootCharacters.length) also make it re-run after the async useCharacters fetch resolves.
  • characterPanelScrollTop is persisted across sessions: set on scroll at ui.store.ts:1223 (setCharacterPanelScrollTop), included in the persist partialize at ui.store.ts:2249, and rehydrated/normalized on load. A previously saved non-zero offset is therefore reapplied on every open → visible jump. Default is 0, so a first/clean open does not jump.

4. Agents/Personas cannot exhibit this. Neither panel has any useLayoutEffect/useEffect setting scrollTop, nor scrollIntoView/.focus() on mount (grep returns no matches). The reported cross-panel symptom is therefore not reproducible for them from the code.

Suggested fix (concrete)

Do NOT "remove autoFocus from the panel search inputs" — there is no such autoFocus; that change targets dead code and is a no-op.

Primary fix — stop the Characters panel from restoring scroll on a mobile reopen. The naive guard if (characterPanelScrollTop <= 0) return; is wrong: it only suppresses restoring-to-0-when-already-0 (a no-op) and lets the actual non-zero jump through. The fix must skip restoration when the panel is opened fresh on the mobile overlay, where users expect the list at the top.

In packages/client/src/components/panels/CharactersPanel.tsx:435-445, gate restoration off the mobile overlay so desktop session restoration is preserved:

const isMobileOverlay = useUIStore((s) => s.isMobile); // or the shellOverlayMode signal AppShell uses at :907

useLayoutEffect(() => {
  const node = panelScrollRef.current;
  if (!node || isLoading) return;
  // On the mobile slide-in overlay the panel remounts on every open
  // (RightPanel.tsx:60 + AppShell AnimatePresence), so restoring the
  // persisted offset reads as an unwanted auto-scroll. Start at top there.
  if (isMobileOverlay) return;
  const restoreScroll = () => {
    const maxScrollTop = Math.max(0, node.scrollHeight - node.clientHeight);
    node.scrollTop = Math.min(useUIStore.getState().characterPanelScrollTop, maxScrollTop);
  };
  restoreScroll();
  const frame = window.requestAnimationFrame(restoreScroll);
  return () => window.cancelAnimationFrame(frame);
}, [isLoading, parsedGroups.length, sortedCharacters.length, visibleRootCharacters.length, isMobileOverlay]);

Alternative to the primary fix (pick one):

  • Reset characterPanelScrollTop to 0 in closeRightPanel (ui.store.ts) when the closing panel is characters, so reopen always starts at the top on every platform. Simpler, but also drops desktop restoration.
  • Restore only once per panel-open session (track whether this is a true reopen vs. an in-session remount) rather than on every AnimatePresence remount.

Secondary hardening (only if a recording shows a focus-driven jump on a Create* modal or detail editor). The autoFocus name fields that actually mount over a panel are the create modals — CreateCharacterModal.tsx:113, CreatePersonaModal.tsx:59, CreateConnectionModal.tsx:69, CreateLorebookModal.tsx:74, CreatePresetModal.tsx:79. On iOS the soft keyboard can shift the visual viewport, which the user may perceive as the page scrolling. Replace autoFocus there with a focus that does not scroll and does not pop the keyboard on touch. This pattern is already established in the codebase (ChatMessage.tsx:295, ChatSettingsDrawer.tsx:1386 use focus({ preventScroll: true })):

const ref = useRef<HTMLInputElement>(null);
useEffect(() => {
  const isTouch = typeof window !== "undefined" && window.matchMedia("(pointer: coarse)").matches;
  if (isTouch) return; // don't pop the keyboard / scroll on mobile
  const id = requestAnimationFrame(() => ref.current?.focus({ preventScroll: true }));
  return () => cancelAnimationFrame(id);
}, []);

Before closing this issue: attach a fresh mobile screen recording and confirm which surface scrolls — the Characters list (primary fix covers it) or a Create* modal / detail editor (secondary hardening covers it). The static read can only explain the Characters case; the Agents/Personas symptom as reported is not reproducible from the code.

Affected files

  • packages/client/src/components/panels/CharactersPanel.tsx — primary fix (restore effect :435-445)
  • packages/client/src/stores/ui.store.ts — alternative fix (reset on closeRightPanel); persisted characterPanelScrollTop (:1223, :2249)
  • packages/client/src/components/layout/RightPanel.tsx — context: module-level mountedPanels (:60-61), scroll container (:113)
  • packages/client/src/components/layout/AppShell.tsx — context: mobile AnimatePresence remount (:907-926)
  • packages/client/src/components/modals/CreateCharacterModal.tsx, CreatePersonaModal.tsx, CreateConnectionModal.tsx, CreateLorebookModal.tsx, CreatePresetModal.tsx — only if recording implicates a create-modal autoFocus
  • Verified clean (no change needed): AgentsPanel.tsx, PersonasPanel.tsx, ConnectionsPanel.tsx

Validation

  • Mobile viewport: open the Characters panel for the first time (no saved offset) — list starts at the top, no jump.
  • Mobile viewport: scroll the character list, close the panel, reopen — list starts at the top (no auto-scroll) after the fix.
  • Mobile viewport: scroll the list, open a character detail, return — list does not jump on the panel return.
  • Mobile viewport: open Agents, Personas, Connections, Lorebooks, Presets panels — confirm none auto-scroll or open the keyboard on open (baseline; should already pass).
  • Desktop: confirm intended Characters scroll restoration still works (if the primary mobile-gate fix was chosen, not the global reset).
  • Reproduce the user's exact flow from a fresh mobile screen recording and identify the scrolling surface (panel list vs. Create* modal vs. detail editor).
  • iOS Safari: open a Create Character / Create Persona modal over a panel — confirm the soft keyboard / visual-viewport shift is not perceived as a page scroll (only if secondary hardening applied).
  • pnpm check passes (TypeScript + ESLint).

AI-investigated against released 2.0.0; verify before fixing.

Originally created by @kolacheee on GitHub (Jun 22, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2800 ## Summary A mobile user reported that opening the agent / persona / character (and "other") side panels auto-scrolls the page, attributing it to `autoFocus` on the panel search/filter input pulling the focused field into view and popping the soft keyboard on mount. **That described cause is not present in v2.0.0.** No panel search input has `autoFocus`, and no panel runs a focus or scroll effect on mount — so opening Agents, Personas, Connections, Lorebooks, or Presets cannot move the viewport from the code as written. There **is** one real mount-time scroll, but it is **Characters-only** and is a different mechanism than reported: the Characters panel restores a *persisted* scroll offset on mount, and because the mobile right-panel is unmounted/remounted on every open (`AnimatePresence`), that restoration **re-fires on every reopen**. When the saved offset is non-zero, the list visibly jumps away from the top, which reads as an "auto-scroll on open." This issue corrects the reported root cause, pins the real one, and gives a fix that actually removes the perceived jump (the obvious-looking guard does **not**). ## Symptom & UX impact (mobile) - **Reported:** opening Agents / Personas / Characters panels on mobile auto-scrolls the page and/or opens the keyboard, blamed on search-input `autoFocus`. - **Actually reproducible (Characters only):** after you've scrolled the character list, reopening the Characters panel snaps the list to a previously saved offset instead of starting at the top. On mobile this looks like the panel "jumping" or auto-scrolling the moment it opens. - **Not reproducible from code:** Agents, Personas, Connections, Lorebooks, Presets — none auto-scroll or auto-focus on open. If the user genuinely saw a jump there, it is most likely a `Create*Modal` autoFocus name field (see Root cause / alternatives), which needs a recording to confirm. - Impact is cosmetic/annoyance-level (no data loss, no broken interaction), hence **low** severity — but the Characters jump is real and repeatable on mobile. ## Steps to reproduce **Characters panel jump (real):** 1. On a mobile viewport, open the Characters panel. 2. Scroll the character list down. 3. Open a character's detail, switch panels, or close the panel. 4. Reopen the Characters panel. - **Expected:** list at the top. - **Actual:** list jumps to the previously saved scroll offset. - Note: the very first open with no saved offset shows no jump (`characterPanelScrollTop` defaults to `0`). **Reported "search autoFocus" cause (does NOT repro):** 1. Open Agents or Personas panel on mobile. 2. **Expected per report:** page auto-scrolls / keyboard opens because the search input is `autoFocus`. 3. **Actual:** nothing focuses or scrolls; only the framer-motion slide-in of the full-screen overlay plays. ## Root cause (exact file:line trace) **1. The reported cause is absent.** None of the panel search inputs use `autoFocus`: - `packages/client/src/components/panels/AgentsPanel.tsx:847-852` ("Search agents") - `packages/client/src/components/panels/PersonasPanel.tsx:514-519` ("Search personas") - `packages/client/src/components/panels/CharactersPanel.tsx:689-694` ('Search characters or -tag:"..."') - `packages/client/src/components/panels/ConnectionsPanel.tsx:1214-1220` ("Search connections...") - `packages/client/src/components/panels/LorebooksPanel.tsx:729-735` ("Search lorebooks") - `packages/client/src/components/panels/PresetsPanel.tsx:820-826` ("Search presets…") The `autoFocus` instances that exist (`AgentsPanel.tsx:935`, `PersonasPanel.tsx:695`, `CharactersPanel.tsx:871`, `ConnectionsPanel.tsx:804`, etc.) are all inside conditionally-rendered **rename** inputs gated on `isEditing`/`renaming`, whose state initializes to `null`/`false` (e.g. `editingGroupId` `PersonasPanel.tsx:128`, `CharactersPanel.tsx:139`; `editingFolderId` `AgentsPanel.tsx:247`). They are not in the tree at mount and cannot fire on panel open. **2. The open path has no scroll/focus side effect.** `TopBar.tsx:334` `toggleRightPanel(panel)` → `ui.store.ts:1201` sets `{ rightPanelOpen, rightPanel }` only. On mobile, `AppShell.tsx:907-926` renders the panel inside `<AnimatePresence mode="wait">` → `<motion.aside key="mobile" initial={{x:"100%"}} animate={{x:0}}>` (full-screen fixed overlay; no autoFocus, no focus trap). **3. The real mechanism — Characters scroll restoration re-firing on every mobile remount:** - `RightPanel.tsx:60-61` declares a **module-level** `const mountedPanels = new Set<string>()` with the comment *"Module-level set survives component remounts (e.g. mobile AnimatePresence unmount/remount)."* Each panel renders inside its own `absolute inset-0 overflow-y-auto` container at `RightPanel.tsx:113` — the only scroll container. - Because `AppShell.tsx:908-909` gates the whole `<motion.aside>` on `rightPanelOpen &&` inside `AnimatePresence`, the entire `RightPanel` section **unmounts on close and remounts on reopen** on mobile. - So `CharactersPanel`'s restore effect at `CharactersPanel.tsx:435-445` runs on **every** reopen. At `:440` it sets `node.scrollTop = Math.min(useUIStore.getState().characterPanelScrollTop, maxScrollTop)`. Its deps (`CharactersPanel.tsx:445`: `isLoading`, `parsedGroups.length`, `sortedCharacters.length`, `visibleRootCharacters.length`) also make it re-run after the async `useCharacters` fetch resolves. - `characterPanelScrollTop` is **persisted** across sessions: set on scroll at `ui.store.ts:1223` (`setCharacterPanelScrollTop`), included in the persist partialize at `ui.store.ts:2249`, and rehydrated/normalized on load. A previously saved non-zero offset is therefore reapplied on every open → visible jump. Default is `0`, so a first/clean open does not jump. **4. Agents/Personas cannot exhibit this.** Neither panel has any `useLayoutEffect`/`useEffect` setting `scrollTop`, nor `scrollIntoView`/`.focus()` on mount (grep returns no matches). The reported cross-panel symptom is therefore not reproducible for them from the code. ## Suggested fix (concrete) **Do NOT** "remove autoFocus from the panel search inputs" — there is no such autoFocus; that change targets dead code and is a no-op. **Primary fix — stop the Characters panel from restoring scroll on a mobile reopen.** The naive guard `if (characterPanelScrollTop <= 0) return;` is **wrong**: it only suppresses restoring-to-0-when-already-0 (a no-op) and lets the actual non-zero jump through. The fix must skip restoration when the panel is opened fresh on the mobile overlay, where users expect the list at the top. In `packages/client/src/components/panels/CharactersPanel.tsx:435-445`, gate restoration off the mobile overlay so desktop session restoration is preserved: ```ts const isMobileOverlay = useUIStore((s) => s.isMobile); // or the shellOverlayMode signal AppShell uses at :907 useLayoutEffect(() => { const node = panelScrollRef.current; if (!node || isLoading) return; // On the mobile slide-in overlay the panel remounts on every open // (RightPanel.tsx:60 + AppShell AnimatePresence), so restoring the // persisted offset reads as an unwanted auto-scroll. Start at top there. if (isMobileOverlay) return; const restoreScroll = () => { const maxScrollTop = Math.max(0, node.scrollHeight - node.clientHeight); node.scrollTop = Math.min(useUIStore.getState().characterPanelScrollTop, maxScrollTop); }; restoreScroll(); const frame = window.requestAnimationFrame(restoreScroll); return () => window.cancelAnimationFrame(frame); }, [isLoading, parsedGroups.length, sortedCharacters.length, visibleRootCharacters.length, isMobileOverlay]); ``` **Alternative to the primary fix** (pick one): - Reset `characterPanelScrollTop` to `0` in `closeRightPanel` (`ui.store.ts`) when the closing panel is `characters`, so reopen always starts at the top on every platform. Simpler, but also drops desktop restoration. - Restore only once per panel-open session (track whether this is a true reopen vs. an in-session remount) rather than on every `AnimatePresence` remount. **Secondary hardening (only if a recording shows a focus-driven jump on a `Create*` modal or detail editor).** The `autoFocus` name fields that actually mount over a panel are the create modals — `CreateCharacterModal.tsx:113`, `CreatePersonaModal.tsx:59`, `CreateConnectionModal.tsx:69`, `CreateLorebookModal.tsx:74`, `CreatePresetModal.tsx:79`. On iOS the soft keyboard can shift the visual viewport, which the user may perceive as the page scrolling. Replace `autoFocus` there with a focus that does not scroll and does not pop the keyboard on touch. This pattern is already established in the codebase (`ChatMessage.tsx:295`, `ChatSettingsDrawer.tsx:1386` use `focus({ preventScroll: true })`): ```ts const ref = useRef<HTMLInputElement>(null); useEffect(() => { const isTouch = typeof window !== "undefined" && window.matchMedia("(pointer: coarse)").matches; if (isTouch) return; // don't pop the keyboard / scroll on mobile const id = requestAnimationFrame(() => ref.current?.focus({ preventScroll: true })); return () => cancelAnimationFrame(id); }, []); ``` **Before closing this issue:** attach a fresh mobile screen recording and confirm which surface scrolls — the Characters list (primary fix covers it) or a Create* modal / detail editor (secondary hardening covers it). The static read can only explain the Characters case; the Agents/Personas symptom as reported is not reproducible from the code. ## Affected files - `packages/client/src/components/panels/CharactersPanel.tsx` — primary fix (restore effect `:435-445`) - `packages/client/src/stores/ui.store.ts` — alternative fix (reset on `closeRightPanel`); persisted `characterPanelScrollTop` (`:1223`, `:2249`) - `packages/client/src/components/layout/RightPanel.tsx` — context: module-level `mountedPanels` (`:60-61`), scroll container (`:113`) - `packages/client/src/components/layout/AppShell.tsx` — context: mobile `AnimatePresence` remount (`:907-926`) - `packages/client/src/components/modals/CreateCharacterModal.tsx`, `CreatePersonaModal.tsx`, `CreateConnectionModal.tsx`, `CreateLorebookModal.tsx`, `CreatePresetModal.tsx` — only if recording implicates a create-modal autoFocus - Verified clean (no change needed): `AgentsPanel.tsx`, `PersonasPanel.tsx`, `ConnectionsPanel.tsx` ## Validation - [ ] Mobile viewport: open the Characters panel for the first time (no saved offset) — list starts at the top, no jump. - [ ] Mobile viewport: scroll the character list, close the panel, reopen — list starts at the top (no auto-scroll) after the fix. - [ ] Mobile viewport: scroll the list, open a character detail, return — list does not jump on the panel return. - [ ] Mobile viewport: open Agents, Personas, Connections, Lorebooks, Presets panels — confirm none auto-scroll or open the keyboard on open (baseline; should already pass). - [ ] Desktop: confirm intended Characters scroll restoration still works (if the primary mobile-gate fix was chosen, not the global reset). - [ ] Reproduce the user's exact flow from a fresh mobile screen recording and identify the scrolling surface (panel list vs. Create* modal vs. detail editor). - [ ] iOS Safari: open a Create Character / Create Persona modal over a panel — confirm the soft keyboard / visual-viewport shift is not perceived as a page scroll (only if secondary hardening applied). - [ ] `pnpm check` passes (TypeScript + ESLint). --- *AI-investigated against released 2.0.0; verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 22, 2026):

Addressed in PR #2790 / commit acab1fba: CharactersPanel scroll restoration is disabled for the mobile overlay sheet so reopening the panel does not replay desktop-style persisted scroll jumps.

<!-- gh-comment-id:4772440949 --> @SpicyMarinara commented on GitHub (Jun 22, 2026): Addressed in PR #2790 / commit acab1fba: CharactersPanel scroll restoration is disabled for the mobile overlay sheet so reopening the panel does not replay desktop-style persisted scroll jumps.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1424
No description provided.