diff --git a/.changeset/fix-mobile-drawer-offscreen-scroll.md b/.changeset/fix-mobile-drawer-offscreen-scroll.md new file mode 100644 index 000000000..ab47e3293 --- /dev/null +++ b/.changeset/fix-mobile-drawer-offscreen-scroll.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fixed the mobile nav drawer sliding permanently out of frame, and made the panel slide continue the speed of your swipe instead of always taking the same time. diff --git a/src/app/components/page/MobileNavDrawer.css.ts b/src/app/components/page/MobileNavDrawer.css.ts new file mode 100644 index 000000000..d9fb5404f --- /dev/null +++ b/src/app/components/page/MobileNavDrawer.css.ts @@ -0,0 +1,10 @@ +import { globalStyle, style } from '@vanilla-extract/css'; + +export const NoScrollbar = style({ + msOverflowStyle: 'none', + scrollbarWidth: 'none', +}); + +globalStyle(`${NoScrollbar}::-webkit-scrollbar`, { + display: 'none', +}); diff --git a/src/app/components/page/MobileNavDrawer.test.tsx b/src/app/components/page/MobileNavDrawer.test.tsx new file mode 100644 index 000000000..1f4c45167 --- /dev/null +++ b/src/app/components/page/MobileNavDrawer.test.tsx @@ -0,0 +1,51 @@ +import { render, screen } from '@testing-library/react'; +import { beforeAll, describe, expect, it, vi } from 'vitest'; +import { MemoryRouter } from 'react-router-dom'; +import { MobileNavDrawer } from './MobileNavDrawer'; + +vi.mock('$state/hooks/settings', () => ({ + useSetting: () => [true, vi.fn<() => void>()], +})); + +vi.mock('./PersistentRoomHost', () => ({ + PersistentRoomHost: () =>
, +})); + +beforeAll(() => { + window.matchMedia = + window.matchMedia ?? + ((query: string) => + ({ + matches: false, + media: query, + addEventListener: () => {}, + removeEventListener: () => {}, + addListener: () => {}, + removeListener: () => {}, + onchange: null, + dispatchEvent: () => false, + }) as unknown as MediaQueryList); +}); + +const renderDrawer = () => + render( + + nav
}> +
content
+ + + ); + +describe('MobileNavDrawer', () => { + // The panels sit side by side in a track twice the viewport wide, moved by transform. + // `hidden` leaves a scrollport that focus or scrollIntoView scrolls a full panel width, + // stacking on top of the transform and stranding the active panel off frame. + it('clips the viewport instead of hiding overflow, so it can never be scrolled', () => { + renderDrawer(); + + const viewport = screen.getByTestId('mobile-nav-drawer-viewport'); + + expect(viewport.style.overflow).toBe('clip'); + expect(viewport.style.overflow).not.toBe('hidden'); + }); +}); diff --git a/src/app/components/page/MobileNavDrawer.tsx b/src/app/components/page/MobileNavDrawer.tsx index 3cd0b84a3..c0a8c84b4 100644 --- a/src/app/components/page/MobileNavDrawer.tsx +++ b/src/app/components/page/MobileNavDrawer.tsx @@ -28,6 +28,7 @@ import { resolveSection } from '$pages/pathUtils'; import { isRoomAlias, isRoomId } from '$utils/matrix'; import { PersistentRoomHost } from './PersistentRoomHost'; import { MobileNavDrawerContext, type MobileSwipeTarget } from './MobileNavDrawerContext'; +import { NoScrollbar } from './MobileNavDrawer.css'; import { classifyMobileGesture, getDrawerSettlePosition, @@ -41,7 +42,17 @@ type MobileNavDrawerProps = { children: ReactNode; }; -const DRAWER_TRANSITION_MS = 220; +const DRAWER_EASE = 'cubic-bezier(0.32, 0.72, 0, 1)'; +// Initial slope of DRAWER_EASE; matched against release speed so the slide picks up the +// finger's motion instead of starting from a standstill. +const DRAWER_EASE_INITIAL_SLOPE = 2.25; +const DRAWER_FULL_SLIDE_MS = 300; +const DRAWER_MIN_SLIDE_MS = 160; +const DRAWER_MAX_SLIDE_MS = 420; +const DRAWER_FLICK_SPEED = 0.05; // px/ms + +const clamp = (value: number, min: number, max: number): number => + Math.min(Math.max(value, min), max); type ActiveTouchGesture = { startX: number; @@ -100,53 +111,94 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra const gestureRef = useRef(); const messageTargetsRef = useRef(new WeakMap()); const chatTargetsRef = useRef(new WeakMap()); - const settleAnimationRef = useRef(); + const settleTimeoutRef = useRef(); const programmaticTargetRef = useRef(); const setTrackPosition = useCallback((position: number) => { positionRef.current = position; - if (trackRef.current) { - trackRef.current.style.transform = `translate3d(${position}px, 0, 0)`; + const el = trackRef.current; + if (!el) return; + el.style.transition = 'none'; + el.style.transform = `translate3d(${position}px, 0, 0)`; + }, []); + + // Reading the computed transform also flushes pending style, without which a settle + // starting right after a drag frame would jump instead of interpolating. + const readTrackPosition = useCallback((): number => { + const el = trackRef.current; + if (!el) return positionRef.current; + const { transform } = getComputedStyle(el); + if (!transform || transform === 'none') return positionRef.current; + try { + return new DOMMatrix(transform).m41; + } catch { + return positionRef.current; } }, []); + // While a transition is in flight positionRef holds its target, not the visible position. + const currentPosition = useCallback( + () => (settleTimeoutRef.current === undefined ? positionRef.current : readTrackPosition()), + [readTrackPosition] + ); + + const setTrackBoosted = useCallback((boosted: boolean) => { + const el = trackRef.current; + if (el) el.style.willChange = boosted ? 'transform' : 'auto'; + }, []); + + const cancelSettle = useCallback(() => { + if (settleTimeoutRef.current === undefined) return; + window.clearTimeout(settleTimeoutRef.current); + settleTimeoutRef.current = undefined; + setTrackPosition(readTrackPosition()); + }, [readTrackPosition, setTrackPosition]); + const settleToPanel = useCallback( - (targetPosition: number, onComplete?: () => void) => { - if (!trackRef.current) return; + (targetPosition: number, velocityX: number, onComplete?: () => void) => { + const el = trackRef.current; + if (!el) return; - window.cancelAnimationFrame(settleAnimationRef.current ?? 0); - if (reduceMotion) { - setTrackPosition(targetPosition); - onComplete?.(); - return; - } + window.clearTimeout(settleTimeoutRef.current); + settleTimeoutRef.current = undefined; - const startPosition = positionRef.current; + const startPosition = readTrackPosition(); const distance = targetPosition - startPosition; - if (Math.abs(distance) <= 1) { + + if (reduceMotion || Math.abs(distance) <= 1) { setTrackPosition(targetPosition); + setTrackBoosted(false); onComplete?.(); return; } - const startTime = window.performance.now(); - const tick = (time: number) => { - const progress = Math.min(1, (time - startTime) / DRAWER_TRANSITION_MS); - const eased = 1 - (1 - progress) ** 3; - setTrackPosition(startPosition + distance * eased); + // A flick against the committed direction must not shorten the glide. + const speed = Math.sign(velocityX) === Math.sign(distance) ? Math.abs(velocityX) : 0; + const duration = + speed > DRAWER_FLICK_SPEED + ? clamp( + (DRAWER_EASE_INITIAL_SLOPE * Math.abs(distance)) / speed, + DRAWER_MIN_SLIDE_MS, + DRAWER_MAX_SLIDE_MS + ) + : clamp( + (Math.abs(distance) / Math.max(widthRef.current, 1)) * DRAWER_FULL_SLIDE_MS, + DRAWER_MIN_SLIDE_MS, + DRAWER_FULL_SLIDE_MS + ); - if (progress < 1) { - settleAnimationRef.current = window.requestAnimationFrame(tick); - } else { - setTrackPosition(targetPosition); - settleAnimationRef.current = undefined; - onComplete?.(); - } - }; + setTrackBoosted(true); + positionRef.current = targetPosition; + el.style.transition = `transform ${Math.round(duration)}ms ${DRAWER_EASE}`; + el.style.transform = `translate3d(${targetPosition}px, 0, 0)`; - settleAnimationRef.current = window.requestAnimationFrame(tick); + settleTimeoutRef.current = window.setTimeout(() => { + settleTimeoutRef.current = undefined; + setTrackBoosted(false); + onComplete?.(); + }, Math.round(duration)); }, - [reduceMotion, setTrackPosition] + [reduceMotion, readTrackPosition, setTrackBoosted, setTrackPosition] ); const registerMessageSwipe = useCallback((element: HTMLElement, target: MobileSwipeTarget) => { @@ -193,7 +245,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra // Start the exact same settle used after a swipe immediately, while the // selected room renders concurrently in the panel being revealed. - settleToPanel(-width, () => { + settleToPanel(-width, 0, () => { if (programmaticTargetRef.current === -width) { programmaticTargetRef.current = undefined; } @@ -239,7 +291,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra if (programmaticTargetRef.current === targetPosition) return; if (Math.abs(positionRef.current - targetPosition) > 5) { - settleToPanel(targetPosition, () => setPanelIntent(contentOpen ? 1 : 0)); + settleToPanel(targetPosition, 0, () => setPanelIntent(contentOpen ? 1 : 0)); } else { setPanelIntent(contentOpen ? 1 : 0); } @@ -263,7 +315,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra return; } } - settleToPanel(0); + settleToPanel(0, 0); setPanelIntent(0); return; } @@ -316,14 +368,16 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra cancelled, }); - settleToPanel(targetPosition, () => commitPanel(targetPosition === -width)); + settleToPanel(targetPosition, gesture.velocityX, () => + commitPanel(targetPosition === -width) + ); }, [commitPanel, settleToPanel, width] ); useEffect(() => { return () => { - window.cancelAnimationFrame(settleAnimationRef.current ?? 0); + window.clearTimeout(settleTimeoutRef.current); const gesture = gestureRef.current; gestureRef.current = undefined; gesture?.message?.cancel(); @@ -339,7 +393,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra const drawer = (
{ const viewport = viewportRef.current; const touch = event.touches[0]; @@ -360,7 +414,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra gestureRef.current = { startX: touch.clientX, startY: touch.clientY, - startPosition: positionRef.current, + startPosition: currentPosition(), lastX: touch.clientX, lastTime: event.timeStamp, velocityX: 0, @@ -389,18 +443,18 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra gesture.mode = classifyMobileGesture({ distanceX, distanceY, - startPosition: positionRef.current, + startPosition: currentPosition(), width, canOpenRoom, hasMessage: gesture.message !== undefined, hasChat: gesture.chat !== undefined, }); if (gesture.mode === 'drawer' || gesture.mode === 'message' || gesture.mode === 'chat') { - const animationActive = settleAnimationRef.current !== undefined; - window.cancelAnimationFrame(settleAnimationRef.current ?? 0); - settleAnimationRef.current = undefined; + const wasSettling = settleTimeoutRef.current !== undefined; + cancelSettle(); programmaticTargetRef.current = undefined; - if (animationActive) gesture.startPosition = positionRef.current - distanceX; + if (wasSettling) gesture.startPosition = positionRef.current - distanceX; + if (gesture.mode === 'drawer') setTrackBoosted(true); } } @@ -419,20 +473,12 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra flexGrow: 1, height: '100%', width: '100%', - overflow: 'hidden', - overscrollBehaviorX: 'none', + // `clip`, not `hidden`: hidden keeps a scrollport, so a focus or scrollIntoView on + // the revealed panel scrolls it a full panel width out of frame, permanently. + overflow: 'clip', touchAction: 'pan-y', }} > -
+ ({ + pointerId: 1, + pointerType: 'touch', + isPrimary: true, + button: 0, + clientX: 0, + clientY: 0, + timeStamp: 0, + preventDefault: vi.fn<() => void>(), + ...init, + }) as unknown as ReactPointerEvent; + +const clickEvent = () => ({}) as unknown as ReactMouseEvent; + +const setup = (enabled = true, withClickHandler = true) => { + const onActivate = vi.fn<() => void>(); + const onClick = vi.fn<() => void>(); + const { result } = renderHook(() => + useMobileTapActivation( + enabled, + onActivate, + withClickHandler ? onClick : undefined + ) + ); + return { onActivate, onClick, result }; +}; + +describe('useMobileTapActivation', () => { + it('activates a touch tap on pointerup, before any click arrives', () => { + const { onActivate, onClick, result } = setup(); + const up = pointerEvent(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerUp(up); + }); + + expect(onActivate).toHaveBeenCalledOnce(); + expect(onClick).not.toHaveBeenCalled(); + expect(up.preventDefault).toHaveBeenCalledOnce(); + }); + + // The double-tap bug: pointerup activated, then the synthetic click ran the handler again. + it('swallows the synthetic click that follows a touch tap', () => { + const { onActivate, onClick, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerUp(pointerEvent()); + result.current.onClick(clickEvent()); + }); + + expect(onActivate).toHaveBeenCalledOnce(); + expect(onClick).not.toHaveBeenCalled(); + }); + + it('leaves mouse clicks to the click handler', () => { + const { onActivate, onClick, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent({ pointerType: 'mouse' })); + result.current.onPointerUp(pointerEvent({ pointerType: 'mouse' })); + result.current.onClick(clickEvent()); + }); + + expect(onActivate).not.toHaveBeenCalled(); + expect(onClick).toHaveBeenCalledOnce(); + }); + + it('does not activate when the touch moved beyond the tap threshold', () => { + const { onActivate, onClick, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerMove(pointerEvent({ clientX: 20 })); + result.current.onPointerUp(pointerEvent({ clientX: 20 })); + }); + + expect(onActivate).not.toHaveBeenCalled(); + + // No touch activation happened, so a click that does arrive must still get through. + act(() => result.current.onClick(clickEvent())); + expect(onClick).toHaveBeenCalledOnce(); + }); + + // Guards `eligible`: the release-point check alone accepts this, the finger came back. + it('does not activate a touch that wandered and returned to its origin', () => { + const { onActivate, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerMove(pointerEvent({ clientX: 50 })); + result.current.onPointerUp(pointerEvent({ clientX: 0 })); + }); + + expect(onActivate).not.toHaveBeenCalled(); + }); + + it('does not activate when enabled turns off mid-gesture', () => { + const onActivate = vi.fn<() => void>(); + const { result, rerender } = renderHook( + ({ enabled }: { enabled: boolean }) => + useMobileTapActivation(enabled, onActivate), + { initialProps: { enabled: true } } + ); + + act(() => result.current.onPointerDown(pointerEvent())); + rerender({ enabled: false }); + act(() => result.current.onPointerUp(pointerEvent())); + + expect(onActivate).not.toHaveBeenCalled(); + }); + + it('does not activate a touch held past the tap duration', () => { + const { onActivate, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent({ timeStamp: 0 })); + result.current.onPointerUp(pointerEvent({ timeStamp: 500 })); + }); + + expect(onActivate).not.toHaveBeenCalled(); + }); + + it('does not activate when disabled, but still forwards the click', () => { + const { onActivate, onClick, result } = setup(false); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerUp(pointerEvent()); + }); + expect(onActivate).not.toHaveBeenCalled(); + + act(() => result.current.onClick(clickEvent())); + expect(onClick).toHaveBeenCalledOnce(); + }); + + // Android WebView drops the click after a swipe, so a tap can activate with none following. + it('resets the activation flag on pointerdown so it cannot swallow a later click', () => { + const { onActivate, onClick, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerUp(pointerEvent()); + }); + expect(onActivate).toHaveBeenCalledOnce(); + + act(() => { + result.current.onPointerDown(pointerEvent({ pointerType: 'mouse' })); + result.current.onPointerUp(pointerEvent({ pointerType: 'mouse' })); + result.current.onClick(clickEvent()); + }); + + expect(onClick).toHaveBeenCalledOnce(); + expect(onActivate).toHaveBeenCalledOnce(); + }); + + it('falls back to onActivate when no click handler is given', () => { + const { onActivate, result } = setup(true, false); + + act(() => result.current.onClick(clickEvent())); + + expect(onActivate).toHaveBeenCalledOnce(); + }); + + it('ignores a pointerup from a different pointer', () => { + const { onActivate, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent({ pointerId: 1 })); + result.current.onPointerUp(pointerEvent({ pointerId: 2 })); + }); + + expect(onActivate).not.toHaveBeenCalled(); + }); + + it('does not activate after the pointer is cancelled', () => { + const { onActivate, result } = setup(); + + act(() => { + result.current.onPointerDown(pointerEvent()); + result.current.onPointerCancel(pointerEvent()); + result.current.onPointerUp(pointerEvent()); + }); + + expect(onActivate).not.toHaveBeenCalled(); + }); +});