All files / frontend/src/components/Navigation BottomSheet.tsx

100% Statements 93/93
85% Branches 17/20
100% Functions 4/4
100% Lines 93/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x 1x                   1x 10x 10x 10x 10x 10x 10x 10x 10x 10x   10x 7x   6x 7x 1x 1x 7x 7x 7x 10x   10x 1x 1x 1x   10x 1x 1x 1x   10x 1x 1x     1x 1x 1x   1x 1x 1x   10x   10x 10x   10x 9x 9x 9x 9x 9x 9x 9x 9x 9x       10x 10x 10x 10x 10x 10x 10x 10x 10x 10x     10x 10x 10x 10x 10x   10x 10x 10x 10x 10x     10x 2x 2x 2x 2x 2x 2x   2x 2x 2x       10x 10x 10x 10x 10x   10x 10x 10x 10x   10x   1x  
import React, { useEffect, useState } from 'react';
import { X } from 'lucide-react';
 
interface BottomSheetProps {
    isOpen: boolean;
    onClose: () => void;
    title?: string;
    children: React.ReactNode;
    maxHeight?: string;
}
 
const BottomSheet: React.FC<BottomSheetProps> = ({ 
    isOpen, 
    onClose, 
    title, 
    children,
    maxHeight = '80vh'
}) => {
    const [startY, setStartY] = useState<number>(0);
    const [currentY, setCurrentY] = useState<number>(0);
    const [isDragging, setIsDragging] = useState(false);
 
    useEffect(() => {
        if (isOpen) {
            // Prevent body scroll when sheet is open
            document.body.style.overflow = 'hidden';
        } else {
            document.body.style.overflow = '';
        }
        return () => {
            document.body.style.overflow = '';
        };
    }, [isOpen]);
 
    const handleTouchStart = (e: React.TouchEvent) => {
        setStartY(e.touches[0].clientY);
        setIsDragging(true);
    };
 
    const handleTouchMove = (e: React.TouchEvent) => {
        if (!isDragging) return;
        setCurrentY(e.touches[0].clientY);
    };
 
    const handleTouchEnd = () => {
        if (!isDragging) return;
        setIsDragging(false);
        
        // If dragged down more than 100px, close the sheet
        if (currentY - startY > 100) {
            onClose();
        }
        
        setStartY(0);
        setCurrentY(0);
    };
 
    const translateY = isDragging && currentY > startY ? currentY - startY : 0;
 
    return (
        <>
            {/* Backdrop */}
            {isOpen && (
                <div
                    className="fixed inset-0 bg-black bg-opacity-50 z-40"
                    style={{ 
                        backdropFilter: 'blur(4px)',
                        transition: 'opacity 0.3s ease-in-out',
                        opacity: isOpen ? 1 : 0
                    }}
                    onClick={onClose}
                />
            )}
 
            {/* Bottom Sheet */}
            <div
                className={`fixed bottom-0 left-0 right-0 bg-oxford-blue rounded-t-2xl shadow-2xl z-50 transition-transform duration-300 ease-in-out ${
                    isOpen ? 'translate-y-0' : 'translate-y-full'
                }`}
                style={{
                    backgroundColor: 'var(--oxford-blue)',
                    maxHeight,
                    transform: isOpen ? `translateY(${translateY}px)` : 'translateY(100%)',
                    paddingBottom: 'env(safe-area-inset-bottom, 0px)', // iOS safe area
                }}
            >
                {/* Drag Handle */}
                <div
                    className="flex justify-center pt-3 pb-2 cursor-grab active:cursor-grabbing"
                    onTouchStart={handleTouchStart}
                    onTouchMove={handleTouchMove}
                    onTouchEnd={handleTouchEnd}
                >
                    <div
                        className="w-12 h-1 rounded-full"
                        style={{ backgroundColor: 'var(--french-gray)' }}
                    />
                </div>
 
                {/* Header */}
                {title && (
                    <div className="flex items-center justify-between px-4 py-3 border-b border-neon-blue border-opacity-20">
                        <h3 className="text-lg font-semibold text-white">{title}</h3>
                        <button
                            onClick={onClose}
                            className="p-2 hover:bg-sea-green hover:bg-opacity-20 rounded-lg transition-colors touch-target"
                            aria-label="Close"
                        >
                            <X size={20} className="text-neon-blue" />
                        </button>
                    </div>
                )}
 
                {/* Content */}
                <div 
                    className="overflow-y-auto p-4"
                    style={{
                        maxHeight: `calc(${maxHeight} - 120px)`
                    }}
                >
                    {children}
                </div>
            </div>
        </>
    );
};
 
export default BottomSheet;