All files / web/src/app/create/worksheets/components MobileDrawer.tsx

0% Statements 0/181
0% Branches 0/1
0% Functions 0/1
0% Lines 0/181

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import { css } from '@styled/css'
import { useEffect, useRef, useState } from 'react'
import { useTheme } from '@/contexts/ThemeContext'

interface MobileDrawerProps {
  isOpen: boolean
  onClose: () => void
  children: React.ReactNode
}

export function MobileDrawer({ isOpen, onClose, children }: MobileDrawerProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'
  const drawerRef = useRef<HTMLDivElement>(null)
  const [touchStart, setTouchStart] = useState<number | null>(null)
  const [touchCurrent, setTouchCurrent] = useState<number | null>(null)

  // Handle escape key
  useEffect(() => {
    if (!isOpen) return

    const handleEscape = (e: KeyboardEvent) => {
      if (e.key === 'Escape') {
        onClose()
      }
    }

    window.addEventListener('keydown', handleEscape)
    return () => window.removeEventListener('keydown', handleEscape)
  }, [isOpen, onClose])

  // Prevent body scroll when drawer is open
  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = 'hidden'
    } else {
      document.body.style.overflow = ''
    }

    return () => {
      document.body.style.overflow = ''
    }
  }, [isOpen])

  // Touch event handlers for swipe-to-close
  const handleTouchStart = (e: React.TouchEvent) => {
    setTouchStart(e.touches[0].clientX)
    setTouchCurrent(e.touches[0].clientX)
  }

  const handleTouchMove = (e: React.TouchEvent) => {
    if (touchStart === null) return
    setTouchCurrent(e.touches[0].clientX)
  }

  const handleTouchEnd = () => {
    if (touchStart === null || touchCurrent === null) {
      setTouchStart(null)
      setTouchCurrent(null)
      return
    }

    const distance = touchCurrent - touchStart
    const threshold = 100 // pixels

    // Swipe left to close
    if (distance < -threshold) {
      onClose()
    }

    setTouchStart(null)
    setTouchCurrent(null)
  }

  // Calculate transform for swipe animation
  const getTransform = () => {
    if (touchStart === null || touchCurrent === null) {
      return isOpen ? 'translateX(0)' : 'translateX(-100%)'
    }

    const distance = touchCurrent - touchStart
    // Only allow leftward swipes
    if (distance < 0) {
      return `translateX(${distance}px)`
    }
    return 'translateX(0)'
  }

  return (
    <>
      {/* Backdrop */}
      <div
        data-component="mobile-drawer-backdrop"
        className={css({
          position: 'fixed',
          inset: 0,
          bg: 'rgba(0, 0, 0, 0.5)',
          zIndex: 40,
          opacity: isOpen ? 1 : 0,
          pointerEvents: isOpen ? 'auto' : 'none',
          transition: 'opacity 0.3s ease-in-out',
        })}
        onClick={onClose}
      />

      {/* Drawer */}
      <div
        ref={drawerRef}
        data-component="mobile-drawer"
        className={css({
          position: 'fixed',
          top: 0,
          left: 0,
          bottom: 0,
          width: '90%',
          maxWidth: '400px',
          bg: isDark ? 'gray.800' : 'white',
          zIndex: 50,
          overflow: 'auto',
          boxShadow: '2xl',
          transition: touchStart === null ? 'transform 0.3s ease-in-out' : 'none',
        })}
        style={{
          transform: getTransform(),
        }}
        onTouchStart={handleTouchStart}
        onTouchMove={handleTouchMove}
        onTouchEnd={handleTouchEnd}
      >
        {/* Close button */}
        <div
          className={css({
            position: 'sticky',
            top: 0,
            right: 0,
            zIndex: 10,
            p: 4,
            display: 'flex',
            justifyContent: 'flex-end',
            bg: isDark ? 'gray.800' : 'white',
            borderBottom: '1px solid',
            borderColor: isDark ? 'gray.700' : 'gray.200',
          })}
        >
          <button
            data-action="close-mobile-drawer"
            onClick={onClose}
            className={css({
              width: '40px',
              height: '40px',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              rounded: 'full',
              bg: isDark ? 'gray.700' : 'gray.200',
              color: isDark ? 'gray.300' : 'gray.700',
              fontSize: 'xl',
              cursor: 'pointer',
              transition: 'all 0.2s',
              _hover: {
                bg: isDark ? 'gray.600' : 'gray.300',
                transform: 'scale(1.05)',
              },
              _active: {
                transform: 'scale(0.95)',
              },
            })}
            aria-label="Close settings"
          >

          </button>
        </div>

        {/* Content */}
        <div className={css({ p: 4 })}>{children}</div>
      </div>
    </>
  )
}