Add swipe-back gesture navigation for mobile

This commit is contained in:
Chris Scott committed 2025-12-06 23:38:28 -05:00
1 parent 8aabb79328
commit 08e3414677
7 files changed
+234 -12

No files matched your search

@@ -1,9 +1,10 @@
import { useEffect, useState, memo, useCallback } from 'react'
import { useEffect, useState, memo, useCallback, useRef } from 'react'
import { FileBrowser } from './FileBrowser'
import { Button } from '@/components/ui/button'
import { PathDisplay } from '@/components/ui/path-display'
import { X } from 'lucide-react'
import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from '@/lib/utils'
import { useSwipeBack } from '@/hooks/useMobile'
interface FileBrowserSheetProps {
isOpen: boolean
@@ -18,6 +19,15 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
const [isEditing, setIsEditing] = useState(false)
const [displayPath, setDisplayPath] = useState<string>('/')
const [shouldRender, setShouldRender] = useState(false)
const containerRef = useRef<HTMLDivElement>(null)
const { bind, swipeStyles } = useSwipeBack(onClose, {
enabled: isOpen && !isEditing,
})
useEffect(() => {
return bind(containerRef.current)
}, [bind])
useEffect(() => {
if (isOpen) {
@@ -76,6 +86,7 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
return (
<div
ref={containerRef}
className="fixed inset-0 z-50"
style={{
opacity: isOpen ? 1 : 0,
@@ -85,7 +96,7 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
>
<div
className="absolute inset-0 bg-background flex flex-col"
style={GPU_ACCELERATED_STYLE}
style={{ ...GPU_ACCELERATED_STYLE, ...swipeStyles }}
>
<div className="flex-shrink-0 border-b border-border bg-background backdrop-blur-sm px-4 py-1">
<div className="flex items-center justify-between">
@@ -1,11 +1,11 @@
import { useState, useEffect } from 'react'
import { useState, useEffect, useRef } from 'react'
import { Dialog, DialogContent } from '@/components/ui/dialog'
import { GitChangesPanel } from './GitChangesPanel'
import { FileDiffView } from './FileDiffView'
import { FilePreviewDialog } from './FilePreviewDialog'
import { Button } from '@/components/ui/button'
import { X, GitBranch } from 'lucide-react'
import { useMobile } from '@/hooks/useMobile'
import { useMobile, useSwipeBack } from '@/hooks/useMobile'
import { useQueryClient } from '@tanstack/react-query'
interface GitChangesSheetProps {
@@ -22,6 +22,16 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch, repoLo
const [previewLineNumber, setPreviewLineNumber] = useState<number | undefined>()
const isMobile = useMobile()
const queryClient = useQueryClient()
const contentRef = useRef<HTMLDivElement>(null)
const { bind: bindSwipe, swipeStyles } = useSwipeBack(
selectedFile ? () => setSelectedFile(undefined) : onClose,
{ enabled: isOpen && !previewFilePath }
)
useEffect(() => {
return bindSwipe(contentRef.current)
}, [bindSwipe])
useEffect(() => {
if (!isOpen) {
@@ -62,8 +72,10 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch, repoLo
return (
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
<DialogContent
ref={contentRef}
className="w-screen h-screen max-w-none max-h-none p-0 gap-0 bg-background border-0 flex flex-col"
hideCloseButton
style={swipeStyles}
>
<div className="flex items-center justify-between px-4 sm:py-3 border-b border-border flex-shrink-0">
<div className="flex items-center gap-2">
@@ -2,6 +2,7 @@ import { memo, useCallback, useState, useEffect, useRef } from "react";
import { FilePreview } from "./FilePreview";
import type { FileInfo } from "@/types/files";
import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from "@/lib/utils";
import { useSwipeBack } from "@/hooks/useMobile";
interface MobileFilePreviewModalProps {
isOpen: boolean;
@@ -18,6 +19,15 @@ export const MobileFilePreviewModal = memo(function MobileFilePreviewModal({
}: MobileFilePreviewModalProps) {
const [localFile, setLocalFile] = useState<FileInfo | null>(null);
const isClosingRef = useRef(false);
const containerRef = useRef<HTMLDivElement>(null);
const { bind, swipeStyles } = useSwipeBack(onClose, {
enabled: isOpen,
});
useEffect(() => {
return bind(containerRef.current);
}, [bind]);
useEffect(() => {
if (isOpen && file && !file.isDirectory) {
@@ -42,8 +52,9 @@ export const MobileFilePreviewModal = memo(function MobileFilePreviewModal({
return (
<div
ref={containerRef}
className="fixed inset-0 z-50 bg-background"
style={{ isolation: 'isolate', ...GPU_ACCELERATED_STYLE }}
style={{ isolation: 'isolate', ...GPU_ACCELERATED_STYLE, ...swipeStyles }}
>
<div
className={`h-full overflow-hidden bg-background ${showFilePreviewHeader ? "" : "pb-8"}`}
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useState, useRef, useEffect, useCallback } from 'react'
import { GeneralSettings } from '@/components/settings/GeneralSettings'
import { KeyboardShortcuts } from '@/components/settings/KeyboardShortcuts'
import { OpenCodeConfigManager } from '@/components/settings/OpenCodeConfigManager'
@@ -7,6 +7,7 @@ import { Dialog, DialogContent } from '@/components/ui/dialog'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Settings2, Keyboard, Code, ChevronLeft, X, Key } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { useSwipeBack } from '@/hooks/useMobile'
interface SettingsDialogProps {
open: boolean
@@ -17,6 +18,24 @@ type SettingsView = 'menu' | 'general' | 'shortcuts' | 'opencode' | 'providers'
export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
const [mobileView, setMobileView] = useState<SettingsView>('menu')
const contentRef = useRef<HTMLDivElement>(null)
const handleSwipeBack = useCallback(() => {
if (mobileView === 'menu') {
setMobileView('menu')
onOpenChange(false)
} else {
setMobileView('menu')
}
}, [mobileView, onOpenChange])
const { bind: bindSwipe, swipeStyles } = useSwipeBack(handleSwipeBack, {
enabled: open,
})
useEffect(() => {
return bindSwipe(contentRef.current)
}, [bindSwipe])
const menuItems = [
{ id: 'general', icon: Settings2, label: 'General Settings', description: 'App preferences and behavior' },
@@ -32,7 +51,11 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className="max-w-4xl w-full h-[100vh] sm:h-auto sm:w-[95vw] sm:max-h-[90vh] bg-gradient-to-br from-background via-background to-background border-border p-0 sm:rounded-lg overflow-hidden !flex !flex-col left-0 top-0 translate-x-0 translate-y-0 max-h-[100vh] sm:left-[50%] sm:top-[50%] sm:translate-x-[-50%] sm:translate-y-[-50%] [&>button:last-child]:hidden">
<DialogContent
ref={contentRef}
className="max-w-4xl w-full h-[100vh] sm:h-auto sm:w-[95vw] sm:max-h-[90vh] bg-gradient-to-br from-background via-background to-background border-border p-0 sm:rounded-lg overflow-hidden !flex !flex-col left-0 top-0 translate-x-0 translate-y-0 max-h-[100vh] sm:left-[50%] sm:top-[50%] sm:translate-x-[-50%] sm:translate-y-[-50%] [&>button:last-child]:hidden"
style={swipeStyles}
>
<div className="hidden sm:flex sm:flex-col sm:h-[90vh]">
<div className="sticky top-0 z-10 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-6 py-4 flex-shrink-0">