diff --git a/src/app/components/page/MobileNavDrawer.tsx b/src/app/components/page/MobileNavDrawer.tsx index 477fb014e..a3a2ef2d6 100644 --- a/src/app/components/page/MobileNavDrawer.tsx +++ b/src/app/components/page/MobileNavDrawer.tsx @@ -1,6 +1,6 @@ import type { ReactNode } from 'react'; import { useCallback, useLayoutEffect, useRef, useState } from 'react'; -import { animate, motion, useMotionValue } from 'framer-motion'; +import { motion, useMotionValue, useReducedMotion } from 'framer-motion'; import { useDrag } from '@use-gesture/react'; import { useAtomValue, useSetAtom } from 'jotai'; import { matchPath, useLocation, useNavigate } from 'react-router-dom'; @@ -19,13 +19,16 @@ import { } from '$pages/paths'; import { resolveSection } from '$pages/pathUtils'; -const SPRING = { type: 'spring', stiffness: 400, damping: 40 } as const; +const SLIDE_MS = 300; +const SLIDE_EASE = 'cubic-bezier(0.32, 0.72, 0, 1)'; const OPEN_FRACTION = 0.35; const VELOCITY_THRESHOLD = 0.4; const DIRECTION_DEADZONE = 10; type MobileNavDrawerProps = { nav: ReactNode; + rail?: ReactNode; + bottomNav?: ReactNode; children: ReactNode; }; @@ -34,8 +37,9 @@ const clamp = (value: number, min: number, max: number): number => /** Sliding mobile drawer: the list and active room are adjacent panels; dragging * reveals the list and commits the route on release. */ -export function MobileNavDrawer({ nav, children }: MobileNavDrawerProps) { +export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDrawerProps) { const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures'); + const reduceMotion = useReducedMotion(); const location = useLocation(); const navigate = useNavigate(); const setLastRoom = useSetAtom(lastVisitedRoomAtom); @@ -56,12 +60,51 @@ export function MobileNavDrawer({ nav, children }: MobileNavDrawerProps) { const contentOpen = !listView; const viewportRef = useRef(null); + const sliderRef = useRef(null); const navPanelRef = useRef(null); const contentPanelRef = useRef(null); const [width, setWidth] = useState(0); const x = useMotionValue(0); const draggingRef = useRef(false); const prevContentOpenRef = useRef(contentOpen); + const openRafRef = useRef(0); + const [deferContent, setDeferContent] = useState(false); + const [prevOpen, setPrevOpen] = useState(contentOpen); + + // Detect the open transition during render so the room isn't rendered in the slide's commit. + if (contentOpen !== prevOpen) { + setPrevOpen(contentOpen); + setDeferContent(contentOpen && !reduceMotion); + } + + // Compositor transition for the settle animation; off = finger-1:1 during drag. + const applyTransition = useCallback((animated: boolean) => { + const el = sliderRef.current; + if (el) el.style.transition = animated ? `transform ${SLIDE_MS}ms ${SLIDE_EASE}` : 'none'; + }, []); + + const settle = useCallback( + (target: number) => { + if (reduceMotion) { + applyTransition(false); + x.jump(target); + return; + } + applyTransition(true); + x.set(target); + }, + [reduceMotion, x, applyTransition] + ); + + // Release the deferred room after the slide is committed to the compositor. + useLayoutEffect(() => { + if (!deferContent) return undefined; + cancelAnimationFrame(openRafRef.current); + openRafRef.current = requestAnimationFrame(() => + requestAnimationFrame(() => setDeferContent(false)) + ); + return () => cancelAnimationFrame(openRafRef.current); + }, [deferContent]); useLayoutEffect(() => { const el = viewportRef.current; @@ -85,12 +128,13 @@ export function MobileNavDrawer({ nav, children }: MobileNavDrawerProps) { prevContentOpenRef.current = contentOpen; if (draggingRef.current) return; const target = contentOpen ? -width : 0; - if (routeChanged && width > 0) { - animate(x, target, SPRING); + if (routeChanged && width > 0 && !reduceMotion) { + settle(target); } else { + applyTransition(false); x.jump(target); } - }, [contentOpen, width, x]); + }, [contentOpen, width, x, settle, applyTransition, reduceMotion]); const goToList = useCallback(() => { const section = resolveSection(location.pathname); @@ -139,21 +183,24 @@ export function MobileNavDrawer({ nav, children }: MobileNavDrawerProps) { if (mx < -DIRECTION_DEADZONE) { if (draggingRef.current) { draggingRef.current = false; - animate(x, -width, SPRING); + settle(-width); } cancel(); return; } if (active) { - // Take over any settling spring; offset is seeded from the live position. - if (first) x.stop(); + // Take over any settling animation; offset is seeded from the live position. + if (first) { + x.stop(); + applyTransition(false); + } draggingRef.current = true; x.set(clamp(ox, -width, 0)); return; } draggingRef.current = false; const opened = width + ox > width * OPEN_FRACTION || (vx > VELOCITY_THRESHOLD && dx > 0); - animate(x, opened ? 0 : -width, SPRING); + settle(opened ? 0 : -width); if (opened) goToList(); return; } @@ -191,7 +238,10 @@ export function MobileNavDrawer({ nav, children }: MobileNavDrawerProps) { touchAction: 'pan-y', }} > - +
- {nav} +
+ {rail &&
{rail}
} +
+ {nav} +
+
+ {bottomNav}
- {children} + {deferContent ? null : children}
diff --git a/src/app/components/page/Page.tsx b/src/app/components/page/Page.tsx index 67193b0e4..8c5879cba 100644 --- a/src/app/components/page/Page.tsx +++ b/src/app/components/page/Page.tsx @@ -10,10 +10,12 @@ import * as css from './style.css'; type PageRootProps = { nav: ReactNode; + rail?: ReactNode; + bottomNav?: ReactNode; children: ReactNode; }; -export function PageRoot({ nav, children }: PageRootProps) { +export function PageRoot({ nav, rail, bottomNav, children }: PageRootProps) { const screenSize = useScreenSizeContext(); const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures'); const isMobile = screenSize === ScreenSize.Mobile; @@ -21,7 +23,9 @@ export function PageRoot({ nav, children }: PageRootProps) { if (isMobile && mobileGestures) { return ( - {children} + + {children} + ); } diff --git a/src/app/pages/MobileFriendly.tsx b/src/app/pages/MobileFriendly.tsx index fe48f3320..25e99a52c 100644 --- a/src/app/pages/MobileFriendly.tsx +++ b/src/app/pages/MobileFriendly.tsx @@ -40,12 +40,14 @@ export function MobileFriendlySidebarNav({ children }: MobileFriendlyClientNavPr export function MobileFriendlyBottomNav({ children }: MobileFriendlyClientNavProps) { const screenSize = useScreenSizeContext(); + const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures'); const homeMatch = useMatch({ path: HOME_PATH, caseSensitive: true, end: true }); const directMatch = useMatch({ path: DIRECT_PATH, caseSensitive: true, end: true }); const spaceMatch = useMatch({ path: SPACE_PATH, caseSensitive: true, end: true }); const settingsMatch = useMatch({ path: '/settings/', caseSensitive: true, end: true }); if ( screenSize !== ScreenSize.Mobile || + mobileGestures || (!homeMatch && !directMatch && !spaceMatch) || settingsMatch ) { diff --git a/src/app/pages/Router.tsx b/src/app/pages/Router.tsx index 872cd3427..4686929ca 100644 --- a/src/app/pages/Router.tsx +++ b/src/app/pages/Router.tsx @@ -249,6 +249,8 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) path={HOME_PATH} element={ } + bottomNav={} nav={ @@ -276,6 +278,8 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) path={DIRECT_PATH} element={ } + bottomNav={} nav={ @@ -302,6 +306,8 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) element={ } + bottomNav={} nav={ @@ -344,6 +350,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) path={EXPLORE_PATH} element={ } nav={ diff --git a/src/app/pages/client/ClientLayout.tsx b/src/app/pages/client/ClientLayout.tsx index 7c0db4677..b2b0fc660 100644 --- a/src/app/pages/client/ClientLayout.tsx +++ b/src/app/pages/client/ClientLayout.tsx @@ -1,7 +1,9 @@ import type { ReactNode } from 'react'; import { Box } from 'folds'; import { matchPath, useLocation } from 'react-router-dom'; -import { useScreenSizeContext } from '$hooks/useScreenSize'; +import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize'; +import { useSetting } from '$state/hooks/settings'; +import { settingsAtom } from '$state/settings'; import { SETTINGS_PATH } from '../paths'; import { isShallowSettingsRoute } from './ClientRouteOutlet'; @@ -12,13 +14,16 @@ type ClientLayoutProps = { export function ClientLayout({ nav, children }: ClientLayoutProps) { const location = useLocation(); const screenSize = useScreenSizeContext(); + const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures'); const fullPageSettings = Boolean(matchPath(SETTINGS_PATH, location.pathname)) && !isShallowSettingsRoute(location.pathname, location.state, screenSize); + const railInDrawer = screenSize === ScreenSize.Mobile && mobileGestures; + return ( - {!fullPageSettings && {nav}} + {!fullPageSettings && !railInDrawer && {nav}} {children} );