[GH-ISSUE #3068] [Issue]: Spotify floating mini-player can be stranded off-screen and untappable after rotating/resizing because its clamp style is memoized and never recomputed on viewport change #1635

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

On a phone, the floating Spotify mini-player clamps its on-screen position against window.innerWidth/window.innerHeight so it stays inside the visible viewport. But that clamp is computed inside a useMemo keyed only on [collapsed, mobilePosition], neither of which changes on a plain orientation/resize event. So when the viewport shrinks (rotate landscape → portrait, or the mobile browser address bar expanding/collapsing), the clamp never re-runs, and the widget keeps rendering at its old, now-out-of-bounds position. Because dragging the widget back requires a pointerdown on the widget itself, an off-screen widget is effectively unrecoverable — it stays stranded until some unrelated state change happens to flip collapsed or mobilePosition, at which point the clamp self-heals. The component re-renders every ~5s on the playback poll, but the memo just returns its cached, stale style on every one of those renders.

How to reproduce (try it yourself)

  1. On a phone (viewport <=767px), set the music source to Spotify so the floating mini-player bubble appears.
  2. Rotate the phone to landscape (wide viewport, e.g. ~800px) and drag the collapsed bubble to the far right edge of the screen.
  3. Rotate back to portrait (narrower viewport, e.g. ~360px).

You'll see: the bubble sits past the right edge (partially or fully off-screen) and you cannot tap or drag it back, because the touch target is now outside the screen. It only reappears if you trigger an unrelated re-render path that flips collapsed or mobilePosition.

Expected: the widget should re-clamp into the visible viewport immediately on orientation/resize and remain tappable and draggable.

Where it is

packages/client/src/components/spotify/SpotifyMiniPlayer.tsx:792

const mobileWidgetStyle = useMemo(() => getMobileWidgetStyle(mobilePosition, collapsed), [collapsed, mobilePosition]);

The dependency array contains no viewport dimension. The next line has the identical gap for the expanded panel:

SpotifyMiniPlayer.tsx:793

const mobileExpandedPanelStyle = useMemo(() => getMobileExpandedPanelStyle(mobilePosition), [mobilePosition]);

getMobileWidgetStyle (lines 230-250) is what reads the viewport and bakes the clamped left/top into the memoized output:

left: Math.max(
  MOBILE_WIDGET_VIEWPORT_PADDING,
  Math.min(window.innerWidth - MOBILE_WIDGET_COLLAPSED_SIZE - MOBILE_WIDGET_VIEWPORT_PADDING, position.x),
),

The memoized style is consumed verbatim on the fixed-positioned container at lines 1031-1032:

className={cn("fixed z-[60] touch-none select-none", mobile && "md:hidden")}
style={mobileWidgetStyle}

Sibling that does it right: useMediaQuery (lines 1120-1133) does attach a matchMedia('change') listener — but it only drives the SpotifyMobileWidget mount/unmount decision; nothing in the file listens for resize/orientationchange to re-run the position clamp. (A grep for resize/orientationchange across the file returns zero listeners.)

What's happening

useMemo caches getMobileWidgetStyle's output keyed on [collapsed, mobilePosition]. window.innerWidth/window.innerHeight are read once, at memo-compute time, and the resulting clamped left/top is frozen into the cached object. A pure orientation/resize event changes neither dependency, so even though React re-renders the widget every 5s via the playback poll (refetchInterval: 5_000, line 329), the memo returns its stale value. The fixed-position element therefore keeps its old left/top, which now lies outside the shrunken viewport.

Concretely: at landscape width 800, the drag-time clamp can store x up to 744. On rotating to portrait width 360, getMobileWidgetStyle would re-clamp left to Math.max(8, Math.min(304, 744)) = 304 (on-screen) — but the memo never re-runs, so the stale left: 744 renders the 48px collapsed bubble entirely off the 360px-wide screen. The horizontal axis is the load-bearing one here; the collapsed top is intentionally left unclamped (lines 243-244), which only widens the affected range. With the touch target off-screen, startDrag (pointerdown) can never fire, so the bubble is stuck until an unrelated state change re-runs the clamp.

Suggested fix

Make the display clamp reactive to viewport size without mutating the persisted store position:

  1. Add a reactive viewport state in SpotifyMiniPlayer:
    const [viewport, setViewport] = useState(() => ({ w: window.innerWidth, h: window.innerHeight }));
    
  2. In a useEffect, subscribe to window resize and orientationchange (throttled via requestAnimationFrame, with a leading call and proper removeEventListener cleanup) and setViewport({ w: innerWidth, h: innerHeight }). Guard with typeof window !== "undefined" to match the SSR guards already in getMobileWidgetStyle.
  3. Add viewport.w/viewport.h to the dependency arrays of both memos:
    • line 792: useMemo(..., [collapsed, mobilePosition, viewport.w, viewport.h])
    • line 793: useMemo(..., [mobilePosition, viewport.w, viewport.h])

This re-runs getMobileWidgetStyle/getMobileExpandedPanelStyle on resize, so the clamp (Math.min(innerWidth - 48 - 8, x)) pulls the bubble back into view and makes it tappable/draggable again.

Do not call setMobilePosition on resize. The position is persisted across sessions (ui.store.ts:2539) and its setter only lower-bounds (ui.store.ts:1766-1767, Math.max(8, ...)) — never upper-bounds against the viewport. Writing the shrunk-viewport clamp back would permanently overwrite the user's chosen spot (so rotating back to landscape would not restore it) and risks a resize→setState→persist feedback loop. Optionally harden the store setter to also upper-bound against the viewport, but keep that out of the resize path.

Regression avoided: mobileWidgetStyle/mobileExpandedPanelStyle are consumed only by the floating branch (lines 1031-1032, 1056), never by the desktop non-floating branch (line 1087), so recomputing them cannot reflow desktop, other chat modes, or any flex container. Positioning is purely inline JS clamp math with no CSS/theme/z-index/media-class involvement, so light/dark/sillytavern themes are untouched. The globally-attached resize listener still fires on desktop, but it only bumps internal state the desktop branch never renders (a harmless extra throttled render); and for a forceFloating bubble that renders at >=768px, re-clamping on resize correctly keeps that bubble on-screen too — desired behavior. The persistence regression is sidestepped entirely by clamping for display only and never writing back.

AI-investigated against v2.0.6 (packages/client); cited lines read and confirmed. Mobile layout verified by reading the computed Tailwind classes + globals.css, not on a device — please confirm on a phone before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3068 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** On a phone, the floating Spotify mini-player clamps its on-screen position against `window.innerWidth`/`window.innerHeight` so it stays inside the visible viewport. But that clamp is computed inside a `useMemo` keyed only on `[collapsed, mobilePosition]`, neither of which changes on a plain orientation/resize event. So when the viewport *shrinks* (rotate landscape → portrait, or the mobile browser address bar expanding/collapsing), the clamp never re-runs, and the widget keeps rendering at its old, now-out-of-bounds position. Because dragging the widget back requires a `pointerdown` on the widget itself, an off-screen widget is effectively unrecoverable — it stays stranded until some unrelated state change happens to flip `collapsed` or `mobilePosition`, at which point the clamp self-heals. The component re-renders every ~5s on the playback poll, but the memo just returns its cached, stale style on every one of those renders. **How to reproduce (try it yourself)** 1. On a phone (viewport <=767px), set the music source to Spotify so the floating mini-player bubble appears. 2. Rotate the phone to landscape (wide viewport, e.g. ~800px) and drag the collapsed bubble to the far right edge of the screen. 3. Rotate back to portrait (narrower viewport, e.g. ~360px). You'll see: the bubble sits past the right edge (partially or fully off-screen) and you cannot tap or drag it back, because the touch target is now outside the screen. It only reappears if you trigger an unrelated re-render path that flips `collapsed` or `mobilePosition`. Expected: the widget should re-clamp into the visible viewport immediately on orientation/resize and remain tappable and draggable. **Where it is** `packages/client/src/components/spotify/SpotifyMiniPlayer.tsx:792` ``` const mobileWidgetStyle = useMemo(() => getMobileWidgetStyle(mobilePosition, collapsed), [collapsed, mobilePosition]); ``` The dependency array contains **no viewport dimension**. The next line has the identical gap for the expanded panel: `SpotifyMiniPlayer.tsx:793` ``` const mobileExpandedPanelStyle = useMemo(() => getMobileExpandedPanelStyle(mobilePosition), [mobilePosition]); ``` `getMobileWidgetStyle` (lines 230-250) is what reads the viewport and bakes the clamped `left`/`top` into the memoized output: ``` left: Math.max( MOBILE_WIDGET_VIEWPORT_PADDING, Math.min(window.innerWidth - MOBILE_WIDGET_COLLAPSED_SIZE - MOBILE_WIDGET_VIEWPORT_PADDING, position.x), ), ``` The memoized style is consumed verbatim on the `fixed`-positioned container at lines 1031-1032: ``` className={cn("fixed z-[60] touch-none select-none", mobile && "md:hidden")} style={mobileWidgetStyle} ``` Sibling that does it right: `useMediaQuery` (lines 1120-1133) *does* attach a `matchMedia('change')` listener — but it only drives the SpotifyMobileWidget mount/unmount decision; nothing in the file listens for `resize`/`orientationchange` to re-run the position clamp. (A grep for `resize`/`orientationchange` across the file returns zero listeners.) **What's happening** `useMemo` caches `getMobileWidgetStyle`'s output keyed on `[collapsed, mobilePosition]`. `window.innerWidth`/`window.innerHeight` are read once, at memo-compute time, and the resulting clamped `left`/`top` is frozen into the cached object. A pure orientation/resize event changes neither dependency, so even though React re-renders the widget every 5s via the playback poll (`refetchInterval: 5_000`, line 329), the memo returns its stale value. The `fixed`-position element therefore keeps its old `left`/`top`, which now lies outside the shrunken viewport. Concretely: at landscape width 800, the drag-time clamp can store `x` up to 744. On rotating to portrait width 360, `getMobileWidgetStyle` *would* re-clamp `left` to `Math.max(8, Math.min(304, 744)) = 304` (on-screen) — but the memo never re-runs, so the stale `left: 744` renders the 48px collapsed bubble entirely off the 360px-wide screen. The horizontal axis is the load-bearing one here; the collapsed `top` is intentionally left unclamped (lines 243-244), which only widens the affected range. With the touch target off-screen, `startDrag` (`pointerdown`) can never fire, so the bubble is stuck until an unrelated state change re-runs the clamp. **Suggested fix** Make the *display* clamp reactive to viewport size **without** mutating the persisted store position: 1. Add a reactive viewport state in `SpotifyMiniPlayer`: ```ts const [viewport, setViewport] = useState(() => ({ w: window.innerWidth, h: window.innerHeight })); ``` 2. In a `useEffect`, subscribe to window `resize` and `orientationchange` (throttled via `requestAnimationFrame`, with a leading call and proper `removeEventListener` cleanup) and `setViewport({ w: innerWidth, h: innerHeight })`. Guard with `typeof window !== "undefined"` to match the SSR guards already in `getMobileWidgetStyle`. 3. Add `viewport.w`/`viewport.h` to the dependency arrays of **both** memos: - line 792: `useMemo(..., [collapsed, mobilePosition, viewport.w, viewport.h])` - line 793: `useMemo(..., [mobilePosition, viewport.w, viewport.h])` This re-runs `getMobileWidgetStyle`/`getMobileExpandedPanelStyle` on resize, so the clamp (`Math.min(innerWidth - 48 - 8, x)`) pulls the bubble back into view and makes it tappable/draggable again. Do **not** call `setMobilePosition` on resize. The position is persisted across sessions (`ui.store.ts:2539`) and its setter only lower-bounds (`ui.store.ts:1766-1767`, `Math.max(8, ...)`) — never upper-bounds against the viewport. Writing the shrunk-viewport clamp back would permanently overwrite the user's chosen spot (so rotating back to landscape would *not* restore it) and risks a resize→setState→persist feedback loop. Optionally harden the store setter to also upper-bound against the viewport, but keep that out of the resize path. Regression avoided: `mobileWidgetStyle`/`mobileExpandedPanelStyle` are consumed only by the floating branch (lines 1031-1032, 1056), never by the desktop non-floating branch (line 1087), so recomputing them cannot reflow desktop, other chat modes, or any flex container. Positioning is purely inline JS clamp math with no CSS/theme/z-index/media-class involvement, so light/dark/sillytavern themes are untouched. The globally-attached `resize` listener still fires on desktop, but it only bumps internal state the desktop branch never renders (a harmless extra throttled render); and for a `forceFloating` bubble that renders at >=768px, re-clamping on resize correctly keeps *that* bubble on-screen too — desired behavior. The persistence regression is sidestepped entirely by clamping for display only and never writing back. *AI-investigated against v2.0.6 (packages/client); cited lines read and confirmed. Mobile layout verified by reading the computed Tailwind classes + globals.css, not on a device — please confirm on a phone before fixing.*
Author
Owner

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

Addressed in PR #3072 by commit 4146a380. Validated locally with pnpm check and pnpm smoke:ui.

<!-- gh-comment-id:4836258042 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Addressed in PR #3072 by commit 4146a380. Validated locally with pnpm check and pnpm smoke:ui.
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#1635
No description provided.